简体   繁体   中英

What is the Lambda handler Signature for AWS SDK V3 with TypeScirpt

I'm updating an existing project from V2 to V3 of the AWS SDK for JavaScript and also moving our usage from JavaScript to TypeScript.

I'm struggling to define strongly typed handlers for the Lamdas.

The examples I've found are similar to this. I'm guessing that they're using V2 of the SDK.

export const lambdaHandler = async (event: APIGatewayEvent, context: Context): Promise<APIGatewayProxyResult> => {
    return {
        statusCode: 200,
        body: JSON.stringify({
            message: 'hello world',
        }),
    };
};

I've had a look through the V3 source for classes similar to APIGatewayEvent , Context and APIGatewayProxyResult but nothing jumps out at me as filling those roles.

Can someone please tell me how to strongly type these signatures?

I've been looking at this and come up with the following:

import {HttpRequest as __HttpRequest,} from "@aws-sdk/protocol-http";
export const handler = async (
    eventIn: { Records: { body: string }[] },
    context: __HttpRequest
) => {}

Note that I only implemented body, but of course you could put messageId, receiptHandle etc at the same level.

Using this turns out the following

export type EventIn = {
  Records: Array<{
    messageId: string
    receiptHandle: string
    body: string
    attributes: {
      ApproximateReceiveCount: string
      SentTimestamp: string
      SenderId: string
      ApproximateFirstReceiveTimestamp: string
    }
    messageAttributes: {}
    md5OfBody: string
    eventSource: string
    eventSourceARN: string
    awsRegion: string
  }>
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM