Class RestApi

The RestApi construct sets up an OpenAPI-based REST API for a serverless project using projen. This construct extends the projen Component to include dependencies and development dependencies required for OpenAPI and AWS Lambda, and provides methods to generate TypeScript types from the OpenAPI definition file. It also generates sample handler files for the API endpoints.

Example

const restApi = new RestApi(app, {
apiName: 'MyRestApi',
definitionFile: 'src/openapi/schema.yaml',
});

Hierarchy

  • Component
    • RestApi

Constructors

  • Creates an instance of RestApi.

    Parameters

    • app: AwsCdkTypeScriptApp

      The AWS CDK TypeScript app.

    • options: RestApiOptions

      The options for configuring the RestApi.

    Returns RestApi

Properties

node: Node

The tree node.

The options for configuring the RestApi.

project: Project

Methods

  • Adds a sample handler file for a REST API resource.

    Parameters

    • files: {
          [fileName: string]: (() => string);
      }

      The collection of file generators.

      • [fileName: string]: (() => string)
          • (): string
          • Returns string

    • apiSpec: OpenAPI3

      The OpenAPI specification object.

    • path: string

      The path of the API resource.

    • method: string

      The HTTP method of the API resource.

    Returns void

  • Creates the construct file content for the generated REST API.

    Parameters

    • file: FileBase

      The file object to create content for.

    Returns string

    The content of the construct file.

  • Creates the entry file content for a REST API handler.

    Parameters

    • method: string

      The HTTP method of the API resource.

    • operationId: string

      The operation ID of the API resource.

    Returns string

    The content of the entry file.

  • Generates sample handler files for the REST API based on the OpenAPI specification.

    Returns {
        [fileName: string]: (() => string);
    }

    The collection of file generators for the sample handler files.

    • [fileName: string]: (() => string)
        • (): string
        • Returns string

  • Called after synthesis. Order is not guaranteed.

    Returns void

  • Called before synthesis.

    Returns void

  • Synthesizes files to the project output directory.

    Returns void

  • Returns a string representation of this construct.

    Returns string

  • Validates a path reference in the OpenAPI specification.

    Parameters

    • apiSpec: OpenAPI3

      The OpenAPI specification object.

    • path: string

      The path reference to validate.

    • visited: string[] = []

      The list of visited paths to detect cycles.

    Returns boolean

    True if the path reference is valid, otherwise false.

    Throws

    If the path reference is invalid or cyclical.

  • Test whether the given construct is a component.

    Parameters

    • x: any

    Returns x is Component

  • 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