简体   繁体   English

AWS SDK V3 with TypeScirpt 的 Lambda 处理程序签名是什么

[英]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.我正在将现有项目从 AWS SDK 的 V2 更新到 JavaScript 的 V3,并将我们的使用从 JavaScript 移动到 TypeScript。

I'm struggling to define strongly typed handlers for the Lamdas.我正在努力为 Lamda 定义强类型处理程序。

The examples I've found are similar to this.我发现的例子与此类似。 I'm guessing that they're using V2 of the SDK.我猜他们正在使用 SDK 的 V2。

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.我已经查看了类似于APIGatewayEventContextAPIGatewayProxyResult的类的V3 源代码,但在填补这些角色时我没有想到什么。

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.请注意,我只实现了正文,但当然您可以将 messageId、receiptHandle 等放在同一级别。

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
  }>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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