Class RestApi<PATHS, OPS>

The RestApi construct sets up an AWS API Gateway REST API using OpenAPI specification. This construct facilitates the creation of a REST API with various configurations, including custom domain, CORS support, and integration with Lambda functions. It allows auto-generating routes based on the provided OpenAPI definition and provides methods to dynamically add custom routes and manage Lambda function integrations.

Example

const api = new RestApi(this, 'MyRestApi', {
apiName: 'MyAPI',
stageName: 'dev',
definitionFileName: 'openapi.yaml',
authentication: myCognitoAuth,
singleTableDatastore: myDynamoDBTable,
autoGenerateRoutes: true,
});

// Add a custom REST resource
api.addRestResource('/items', 'get');

// Get the Lambda function for a specific operation
const lambdaFunction = api.getFunctionForOperation('getItems');

Type Parameters

  • PATHS

    The type definition for the API paths.

  • OPS

    The type definition for the API operations.

Hierarchy

Constructors

  • Creates an instance of RestApi.

    Type Parameters

    • PATHS

    • OPS

    Parameters

    • scope: Construct

      The scope in which this construct is defined.

    • id: string

      The scoped construct ID.

    • props: RestApiProps<OPS>

      The properties of the RestApi construct.

    Returns RestApi<PATHS, OPS>

Properties

_functions: {
    [operationId: string]: LambdaFunction;
} = {}

A collection of Lambda functions used as integrations for the API operations.

Type declaration

api: SpecRestApi

The AWS API Gateway REST API instance.

apiDomainName?: string
apiFQDN?: string
apiHostName?: string
apiSpec: OpenAPI3

The OpenAPI specification for the REST API.

hostedZone?: IHostedZone
node: Node

The tree node.

props: RestApiProps<OPS>

The properties of the RestApi construct.

Methods

  • Parameters

    • operation: OperationObject
    • method: string
    • description: string
    • additionalLambdaOptions: LambdaOptions = {}

    Returns undefined | LambdaFunction

  • Type Parameters

    • P extends string | number | symbol

    Parameters

    • path: P
    • method: keyof PATHS[P]

    Returns undefined | LambdaFunction

  • Parameters

    • spec: {
          [key: string]: any;
      }
      • [key: string]: any

    Returns {
        [key: string]: any;
    }

    • [key: string]: any
  • return the generated Lambda function for the specified API operation

    Parameters

    • operationId: keyof OPS

    Returns LambdaFunction

  • Visitor method to modify the given functions

    Parameters

    • operationIds: (keyof OPS)[]

      the list of functions to visit

    • op: ((fn) => void)

      the function to call for every function

    Returns void

  • AWS does not properly apply the 'security' option to every single path-method. According to documentation, global security gets applied, if there is no security set for the particular path-method. If there is any, even empty, it will get precedence. THIS DOES NOT HAPPEN ON AWS. IT JUST ALWAYS USES THE GLOBAL SETTING!

    'security applies [...] schemes globally to all API operations, unless overridden on the operation level'

    Parameters

    • spec: OpenAPI3

    Returns void

  • Parameters

    • method: string

    Returns boolean

  • Returns a string representation of this construct.

    Returns string

  • Checks if x is a construct.

    Use this method instead of instanceof to properly detect Construct instances, even when the construct library is symlinked.

    Explanation: in JavaScript, multiple copies of the constructs library on disk are seen as independent, completely different libraries. As a consequence, the class Construct in each copy of the constructs library is seen as a different class, and an instance of one class will not test as instanceof the other class. npm install will not create installations like this, but users may manually symlink construct libraries together or use a monorepo tool: in those cases, multiple copies of the constructs library can be accidentally installed, and instanceof will behave unpredictably. It is safest to avoid using instanceof, and using this type-testing method instead.

    Parameters

    • x: any

      Any object

    Returns x is Construct

    true if x is an object created from a class which extends Construct.

Generated using TypeDoc