微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Node AWS SDK v3:Lambda 函数中`event` 和`context` 参数的类型?

如何解决Node AWS SDK v3:Lambda 函数中`event` 和`context` 参数的类型?

我正在切换到新的 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.
}

解决方法

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。