节点 AWS SDK v3:Lambda 函数中“事件”和“上下文”参数的类型?

bsc*_*dam 10 node.js typescript aws-lambda aws-sdk-js aws-sdk-js-v3

我将改用新的 Node AWS SDK (v3),以利用其模块化和 Typescript 支持。我需要做的第一件事是编写 Lambda 函数,但我找不到支持处理函数签名的类型。中的类型@aws/client-lambda似乎都与管理Lambda 的客户端相关。

Node SDK 是否有用于在某处编写Lambda 的官方类型?尤其:

  • 参数的类型在哪里context
  • 为了event论证,是否有可能来自其他 AWS 服务及其相应类型的事件列表?
interface Event {
  // This could be anything -- a custom structure or something 
  // created by another AWS service, so it makes sense that
  // there isn't a discoverable type for this. There should be
  // corresponding types for each service that can send events
  // to Lambda functions though. Where are these?
}

interface Context {
  // This is provided by Lambda, but I can't find types for it anywhere.
  // Since it's always the same, there should be a type defined somewhere,
  // but where?
}

exports.handler = ( event: Event, context: Context )=>{
  // While `event` could anything so it makes sense to not have a single type available,
  // `context` is always the same thing and should have a type somewhere.
}
Run Code Online (Sandbox Code Playgroud)

小智 10

使用aws-lambda types,它具有大多数事件的类型。

处理程序示例:

import { SQSHandler, SNSHandler, APIGatewayProxyHandler } from 'aws-lambda';

export const sqsHandler: SQSHandler = async (event, context) => {
  
}

export const snsHandler: SNSHandler = async (event, context) => {
}

export const apiV2Handler: APIGatewayProxyHandler = async (event, context) => {
  return {
    body: 'Response body',
    statusCode: 200
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 这个答案是否意味着新的 v3 aws-sdk 实际上并不提供这些类型,而正确的解决方案是依赖这个独立的库? (2认同)