skip to Main Content

The documentation for the AWS Cognito Pre Token Generation function has node example code, but no typescript example code.

Are there typescript types I can import to strongly type the three parameters passed to the handler function?

import { TypeA, TypeB, TypeC } from '???'; // Are these defined by AWS somewhere?
exports.handler = (event: TypeA, context: TypeB, callback: TypeC) => {
  event.response = {
    // the good stuff goes here
  }
  callback(null, event)
}

2

Answers


  1. Try this:

    import { Callback, Context, PreTokenGenerationTriggerEvent } from "aws-lambda";
    
    exports.handler = (event: PreTokenGenerationTriggerEvent, context: Context, callback: Callback) => {
      event.response = {
        // the good stuff goes here
      }
      callback(null, event)
    }
    
    Login or Signup to reply.
  2. @types/aws-lambda contains type definitions for AWS Lambda. Specifically, events of type PreTokenGenerationTriggerEvent from pre-token-generation.d.ts

    Also, you can use an async function instead of a callback.

    import { Context, PreTokenGenerationTriggerEvent } from "aws-lambda";
    
    export const handler = async (
      event: PreTokenGenerationTriggerEvent,
      context: Context
    ): Promise<PreTokenGenerationTriggerEvent> => {
      console.log(`Event: ${JSON.stringify(event, null, 2)}`);
    
      // good stuff goes here, like
      event.response.claimsOverrideDetails = {
        claimsToAddOrOverride: {
          'custom:claim': 'custom value'
        }
      };
      return event;
    };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search