1 | /**
|
2 | * The interface that AWS Lambda will invoke your handler with.
|
3 | * There are more specialized types for many cases where AWS services
|
4 | * invoke your lambda, but you can directly use this type for when you are invoking
|
5 | * your lambda directly.
|
6 | *
|
7 | * See the {@link http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html AWS documentation}
|
8 | * for more information about the runtime behavior, and the
|
9 | * {@link https://aws.amazon.com/blogs/compute/node-js-8-10-runtime-now-available-in-aws-lambda/ AWS Blog post}
|
10 | * introducing the async handler behavior in the 8.10 runtime.
|
11 | *
|
12 | * @example <caption>Defining a custom handler type</caption>
|
13 | * import { Handler } from 'aws-lambda'
|
14 | *
|
15 | * interface NameEvent {
|
16 | * fullName: string
|
17 | * }
|
18 | * interface NameResult {
|
19 | * firstName: string
|
20 | * middleNames: string
|
21 | * lastName: string
|
22 | * }
|
23 | * type PersonHandler = Handler<NameEvent, NameResult>
|
24 | *
|
25 | * export const handler: PersonHandler = async (event) => {
|
26 | * const names = event.fullName.split(' ')
|
27 | * const firstName = names.shift()
|
28 | * const lastName = names.pop()
|
29 | * return { firstName, middleNames: names, lastName }
|
30 | * }
|
31 | *
|
32 | * @example <caption>Logs the contents of the event object and returns the location of the logs</caption>
|
33 | * import { Handler } from 'aws-lambda'
|
34 | *
|
35 | * export const handler: Handler = async (event, context) => {
|
36 | * console.log("EVENT: \n" + JSON.stringify(event, null, 2))
|
37 | * return context.logStreamName
|
38 | * }
|
39 | *
|
40 | * @example <caption>AWS SDK with Async Function and Promises</caption>
|
41 | * import { Handler } from 'aws-lambda'
|
42 | * import AWS from 'aws-sdk'
|
43 | *
|
44 | * const s3 = new AWS.S3()
|
45 | *
|
46 | * export const handler: Handler = async (event) => {
|
47 | * const response = await s3.listBuckets().promise()
|
48 | * return response?.Buckets.map((bucket) => bucket.Name)
|
49 | * }
|
50 | *
|
51 | * @example <caption>HTTP Request with Callback</caption>
|
52 | * import { Handler } from 'aws-lambda'
|
53 | * import https from 'https'
|
54 | *
|
55 | * let url = "https://docs.aws.amazon.com/lambda/latest/dg/welcome.html"
|
56 | *
|
57 | * export const handler: Handler<void, number> = (event, context, callback) => {
|
58 | * https.get(url, (res) => {
|
59 | * callback(null, res.statusCode)
|
60 | * }).on('error', (e) => {
|
61 | * callback(Error(e))
|
62 | * })
|
63 | * }
|
64 | *
|
65 | * @param event
|
66 | * Parsed JSON data in the lambda request payload. For an AWS service triggered
|
67 | * lambda this should be in the format of a type ending in Event, for example the
|
68 | * S3Handler receives an event of type S3Event.
|
69 | * @param context
|
70 | * Runtime contextual information of the current invocation, for example the caller
|
71 | * identity, available memory and time remaining, legacy completion callbacks, and
|
72 | * a mutable property controlling when the lambda execution completes.
|
73 | * @param callback
|
74 | * NodeJS-style completion callback that the AWS Lambda runtime will provide that can
|
75 | * be used to provide the lambda result payload value, or any execution error. Can
|
76 | * instead return a promise that resolves with the result payload value or rejects
|
77 | * with the execution error.
|
78 | * @return
|
79 | * A promise that resolves with the lambda result payload value, or rejects with the
|
80 | * execution error. Note that if you implement your handler as an async function,
|
81 | * you will automatically return a promise that will resolve with a returned value,
|
82 | * or reject with a thrown value.
|
83 | */
|
84 | export type Handler<TEvent = any, TResult = any> = (
|
85 | event: TEvent,
|
86 | context: Context,
|
87 | callback: Callback<TResult>,
|
88 | // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
89 | ) => void | Promise<TResult>;
|
90 |
|
91 | /**
|
92 | * {@link Handler} context parameter.
|
93 | * See {@link https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html AWS documentation}.
|
94 | */
|
95 | export interface Context {
|
96 | callbackWaitsForEmptyEventLoop: boolean;
|
97 | functionName: string;
|
98 | functionVersion: string;
|
99 | invokedFunctionArn: string;
|
100 | memoryLimitInMB: string;
|
101 | awsRequestId: string;
|
102 | logGroupName: string;
|
103 | logStreamName: string;
|
104 | identity?: CognitoIdentity | undefined;
|
105 | clientContext?: ClientContext | undefined;
|
106 |
|
107 | getRemainingTimeInMillis(): number;
|
108 |
|
109 | // Functions for compatibility with earlier Node.js Runtime v0.10.42
|
110 | // No longer documented, so they are deprecated, but they still work
|
111 | // as of the 12.x runtime, so they are not removed from the types.
|
112 |
|
113 | /** @deprecated Use handler callback or promise result */
|
114 | done(error?: Error, result?: any): void;
|
115 | /** @deprecated Use handler callback with first argument or reject a promise result */
|
116 | fail(error: Error | string): void;
|
117 | /** @deprecated Use handler callback with second argument or resolve a promise result */
|
118 | succeed(messageOrObject: any): void;
|
119 | // Unclear what behavior this is supposed to have, I couldn't find any still extant reference,
|
120 | // and it behaves like the above, ignoring the object parameter.
|
121 | /** @deprecated Use handler callback or promise result */
|
122 | succeed(message: string, object: any): void;
|
123 | }
|
124 |
|
125 | export interface CognitoIdentity {
|
126 | cognitoIdentityId: string;
|
127 | cognitoIdentityPoolId: string;
|
128 | }
|
129 |
|
130 | export interface ClientContext {
|
131 | client: ClientContextClient;
|
132 | Custom?: any;
|
133 | env: ClientContextEnv;
|
134 | }
|
135 |
|
136 | export interface ClientContextClient {
|
137 | installationId: string;
|
138 | appTitle: string;
|
139 | appVersionName: string;
|
140 | appVersionCode: string;
|
141 | appPackageName: string;
|
142 | }
|
143 |
|
144 | export interface ClientContextEnv {
|
145 | platformVersion: string;
|
146 | platform: string;
|
147 | make: string;
|
148 | model: string;
|
149 | locale: string;
|
150 | }
|
151 |
|
152 | /**
|
153 | * NodeJS-style callback parameter for the {@link Handler} type.
|
154 | * Can be used instead of returning a promise, see the
|
155 | * {@link https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html AWS documentation}
|
156 | * for the handler programming model.
|
157 | *
|
158 | * @param error
|
159 | * Parameter to use to provide the error payload for a failed lambda execution.
|
160 | * See {@link https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-mode-exceptions.html AWS documentation}
|
161 | * for error handling.
|
162 | * If an Error instance is passed, the error payload uses the `name` property as the `errorType`,
|
163 | * the `message` property as the `errorMessage`, and parses the `stack` property string into
|
164 | * the `trace` array.
|
165 | * For other values, the `errorType` is `typeof value`, the `errorMessage` is `String(value)`, and
|
166 | * `trace` is an empty array.
|
167 | *
|
168 | * @param result
|
169 | * Parameter to use to provide the result payload for a successful lambda execution.
|
170 | * Pass `null` or `undefined` for the `error` parameter to use this parameter.
|
171 | */
|
172 | export type Callback<TResult = any> = (error?: Error | string | null, result?: TResult) => void;
|