I'm trying to write a trio of Cognito AuthChallenge lambdas for custom auth flow. I wanted to use serverless-offline to develop and test the lambdas locally with nodejs (also in jest tests in cicd pipeline).
For example, here is a simplified handler code with VerifyAuthChallengeResponseTriggerEvent
type from aws-lambda package:
import {
VerifyAuthChallengeResponseTriggerEvent,
VerifyAuthChallengeResponseTriggerHandler
} from "aws-lambda/trigger/cognito-user-pool-trigger/verify-auth-challenge-response";
export const verifyChallengeHandler: VerifyAuthChallengeResponseTriggerHandler =
async (event: VerifyAuthChallengeResponseTriggerEvent):
Promise<VerifyAuthChallengeResponseTriggerEvent> => {
event.response.answerCorrect = event.request.privateChallengeParameters["code"] == event.request.challengeAnswer;
return event;
};
I found everywhere only examples of using API Gateway with http event, so when I tried it locally with following config under the functions
in serverless config, it also worked:
events: [
{
http: {
method: "post",
path: "auth-verify",
cors: {
origin: '*',
headers:[
"Content-Type",
"X-Amz-Date",
"X-Amz-Security-Token",
"Authorization",
"X-Api-Key",
"X-Requested-With",
"Accept",
],
allowCredentials: true,
},
},
},
],
However, I'd have to rewrite the handler code, so that it works with different event type. That is not great as it would be different code for testing from the code to be deployed.
import { ValidatedEventAPIGatewayProxyEvent } from '@libs/apiGateway';
import { middyfy } from '@libs/lambda';
import schema from './schema';
const verifyChallengeHandler: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async(event) => {
...
Is there a way to configure serverless and what command to call that I can invoke the lambdas locally and also in jest tests with correct events and without changing the code? I'm also fine with creating a custom mock of events (but how to specify them?).
Ie. to use config under functions
as something like this:
events: [
{
cognitoUserPool: {
pool: <some_pool_name>,
trigger: 'VerifyAuthChallengeResponse' as const,
existing: false,
},
},