I'm following the example in https://github.com/serverless/examples/tree/master/aws-node-typescript-nest
What I've done
1) Successfully deployed this to AWS and added a Cognito authorizer (I've verified all this works and I'm getting user data in requestContext).
2) Setup NestJS to handle routing.
What I need to do
Hand the event and context variables to either the NestJS controller or an injectable service so I can do further verification and personalization.
main.ts snippit where request are first routed. Notice event and context
async function bootstrapServer(): Promise<Server> {
if (!cachedServer) {
try {
const expressApp = require('express')();
const nestApp = await NestFactory.create(AppModule, expressApp);
nestApp.use(eventContext());
await nestApp.init();
cachedServer = createServer(expressApp, undefined, binaryMimeTypes);
}
catch (error) {
return Promise.reject(error);
}
}
return Promise.resolve(cachedServer);
}
export const handler: Handler = async (event: any, context: Context) => {
cachedServer = await bootstrapServer();
// LOOK HERE!!!!!!
// How do I pass event and context
return proxy(cachedServer, event, context, 'PROMISE').promise;
}
And here is the controller
controller snippit where I'd like to use event and context
import { AppService } from './app.service';
@Controller()
export class AppController {
// how do I access event and context in this controller class?
constructor(private readonly appService: AppService) {}
@Get('hello')
getHello(): string {
return this.appService.getHello();
}
}
Finally tracked this down.
aws-serverless-example gave me the hint I needed.
Part of aws-serverless-express is a middleware that will add the event information to the req body.
In app.ts (or whatever your launch file is)
import * as awsServerlessExpressMiddleware from "aws-serverless-express/middleware";
app.use(awsServerlessExpressMiddleware.eventContext());
app.get('/', (req, res) => {
res.json(req.apiGateway.event);
});
and in the controller/resolver
@Query(returns => String)
async eventCreate(@Context() context, @Info() info): Promise<string> {
let c = context;
return Promise.resolve("WTF");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With