0.13.0 • Published 2 years ago

@spec-validator/aws-api-gw-adapter v0.13.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 years ago

@spec-validator/aws-api-gw-adapter

To power lambda serving AWS API GW you need the following:

import { createAwsLambdaHandler, createUnboundEnvStore } from '@spec-validator/aws-api-gw-adapter'

import {
  segmentField as $, stringField,
} from '@spec-validator/validator/fields'

import { _ } from '@spec-validator/rest-api-server'

import { expectType } from '@spec-validator/test-utils/expectType'

// NOTE: this is a typesafe way of bridging environment variables from CDK
// to the ones within lambda
export const createEnvStore = createUnboundEnvStore(
  'ENTRYPOINT_REGION',
)

const envs = createEnvStore()

export const handle = createAwsLambdaHandler({
  routes: [
    _.GET($._('/items')).spec({
      response: {
        body: [{
          title: stringField(),
          description: stringField(),
        }],
      },
    }).handler(async () => ({
      body: [
        {
          title: 'Item 1',
          description: `Region is ${envs.ENTRYPOINT_REGION}`,
        },
      ],
    })),
  ],
})

Handler's type should be as follows:

import { AwsReq } from '@spec-validator/aws-api-gw-adapter/awsLambdaHandler'
import { Route } from '@spec-validator/rest-api-server'
import { WildCardResponse } from '@spec-validator/rest-api-server/handler'

expectType<typeof handle, ((event: AwsReq) => Promise<WildCardResponse>) & {
  config: Partial<{
      routes: Route[];
  }>;
}>(true)

CDK part for the lambda above may look as follows:

import * as lambda from '@aws-cdk/aws-lambda'
import * as nLambda from '@aws-cdk/aws-lambda-nodejs'
import * as logs from '@aws-cdk/aws-logs'

import { createEnvStore } from './src'

const lambdaStack = (region: string) => {
  const stack = new cdk.Stack(app, `${name}-RestApi`, {
    env,
  })

  ...

  const entryPoint = new nLambda.NodejsFunction(stack, `${name}-EntryPoint`, {
    entry: `${__dirname}/src/index.ts`,
    runtime: lambda.Runtime.NODEJS_14_X,
    functionName: `${name}-RestApi-EntryPoint`,
    tracing: lambda.Tracing.DISABLED,
    logRetention: logs.RetentionDays.ONE_MONTH,
    bundling: {
      tsconfig: `${__dirname}/tsconfig.json`,
      minify: true,
      sourceMap: true,
      target: 'es2020',
    },
  })

  // NOTE: here we bind setter of the environment variable to
  // addEnvironment method of a lambda function
  const store = createEnvStore((key, value) => entryPoint.addEnvironment(key, value))

  // Only known keys can be set
  store.ENTRYPOINT_REGION = region

  ...
}
0.10.0

2 years ago

0.9.0

2 years ago

0.7.2

2 years ago

0.7.1

2 years ago

0.13.0

2 years ago

0.6.1

3 years ago

0.5.0

3 years ago

0.4.8

3 years ago

0.4.7

3 years ago

0.4.5

3 years ago

0.4.6

3 years ago

0.4.4

3 years ago

0.4.3

3 years ago

0.4.2

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.3.0

3 years ago

0.3.2

3 years ago

0.3.1

3 years ago

0.2.19

3 years ago