0.0.8 • Published 2 years ago

@almamedia-open-source/cdk-openapix v0.0.8

Weekly downloads
-
License
UNLICENSED
Repository
github
Last release
2 years ago

CDK OpenApiX

experimental

🚧 Work-in-Progress

🚨 Do not use this: Things may break at any given time & this project may be destroyed at any given time!

npm i -D @almamedia-open-source/cdk-openapix
const fn = new lambda.Function(this, "fn", {
  handler: "index.handler",
  runtime: lambda.Runtime.NODEJS_14_X,
  code: lambda.Code.fromInline('export function handler() { return { statusCode: 200, body: JSON.stringify("hello")} }'),
});

const pkName = 'item';

const table = new dynamodb.Table(this, 'table', {
  partitionKey: {
    type: dynamodb.AttributeType.STRING,
    name: pkName,
  }
});

const role = new iam.Role(this, "role", {
  assumedBy: new iam.ServicePrincipal('apigateway.amazonaws.com'),
});

table.grantReadData(role);

const apiDefinition = new OpenApiXDefinition(this, {
  upload: false, // by default add as inline Body, set to true to use as BodyS3Location
  source: './schema.yaml',
  integrations: {

    // Mock Integration
    '/mock': { 'GET': new OpenApiXMock(this) },

    // AWS Lambda Proxy integration
    '/message': { 'GET': new OpenApiXLambda(this, fn) },

    // HTTP Proxy integration
    '/ext': { 'GET': new OpenApiXHttp(this, "https://example.com") },

    // Direct integration to AWS Service
    '/item': { 'GET': new OpenApiXService(this, {
          service: 'dynamodb',
          action: 'GetItem',
          options: {
            credentialsRole: role,
            requestTemplates: {
              'application/json': JSON.stringify({
                "TableName": table.tableName,
                "Key": {
                  [pkName]: {
                    "S": "$input.params('item')"
                  }
                }
              }),
            },
          },
      }),
    },

  },

  injectPaths: { "info.title": "FancyPantsAPI" },
  rejectPaths: ['info.version'],

  // TODO add validators...
})


new apigateway.SpecRestApi(this, 'api', {
  apiDefinition,
});

// Also supports new OpenApiX()