hardyscc/aws-nestjs-starter

aws-serverless-express NPM package will be deprecated in favor of @vendia/serverless-express

tktwong opened this issue · 0 comments

proposed changes:

  1. adopt @vendia/serverless-express
npm uninstall @types/aws-serverless-express
npm uninstall aws-serverless-express
npm i --save @vendia/serverless-express
  1. index.ts
import { ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { ExpressAdapter } from '@nestjs/platform-express';
import serverlessExpress from '@vendia/serverless-express';
import { APIGatewayProxyHandler, Handler } from 'aws-lambda';
import express from 'express';
import { AppModule } from './app.module';

let cachedServer: Handler;

const bootstrapServer = async (): Promise<Handler> => {
	const expressApp = express();
	const app = await NestFactory.create(AppModule, new ExpressAdapter(expressApp));
	app.useGlobalPipes(new ValidationPipe({ forbidUnknownValues: true }));
	app.enableCors();
	await app.init();
	return serverlessExpress({
		app: expressApp
	});
};

export const handler: APIGatewayProxyHandler = async (event, context, callback) => {
	if (!cachedServer) {
		cachedServer = await bootstrapServer();
	}
	return cachedServer(event, context, callback);
};