UNPKG

6.68 kBTypeScriptView Raw
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 tme {@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 */
84export type Handler<TEvent = any, TResult = any> = (
85 event: TEvent,
86 context: Context,
87 callback: Callback<TResult>,
88) => void | Promise<TResult>;
89
90/**
91 * {@link Handler} context parameter.
92 * See {@link https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html AWS documentation}.
93 */
94export interface Context {
95 callbackWaitsForEmptyEventLoop: boolean;
96 functionName: string;
97 functionVersion: string;
98 invokedFunctionArn: string;
99 memoryLimitInMB: string;
100 awsRequestId: string;
101 logGroupName: string;
102 logStreamName: string;
103 identity?: CognitoIdentity | undefined;
104 clientContext?: ClientContext | undefined;
105
106 getRemainingTimeInMillis(): number;
107
108 // Functions for compatibility with earlier Node.js Runtime v0.10.42
109 // No longer documented, so they are deprecated, but they still work
110 // as of the 12.x runtime, so they are not removed from the types.
111
112 /** @deprecated Use handler callback or promise result */
113 done(error?: Error, result?: any): void;
114 /** @deprecated Use handler callback with first argument or reject a promise result */
115 fail(error: Error | string): void;
116 /** @deprecated Use handler callback with second argument or resolve a promise result */
117 succeed(messageOrObject: any): void;
118 // Unclear what behavior this is supposed to have, I couldn't find any still extant reference,
119 // and it behaves like the above, ignoring the object parameter.
120 /** @deprecated Use handler callback or promise result */
121 succeed(message: string, object: any): void;
122}
123
124export interface CognitoIdentity {
125 cognitoIdentityId: string;
126 cognitoIdentityPoolId: string;
127}
128
129export interface ClientContext {
130 client: ClientContextClient;
131 Custom?: any;
132 env: ClientContextEnv;
133}
134
135export interface ClientContextClient {
136 installationId: string;
137 appTitle: string;
138 appVersionName: string;
139 appVersionCode: string;
140 appPackageName: string;
141}
142
143export interface ClientContextEnv {
144 platformVersion: string;
145 platform: string;
146 make: string;
147 model: string;
148 locale: string;
149}
150
151/**
152 * NodeJS-style callback parameter for the {@link Handler} type.
153 * Can be used instead of returning a promise, see the
154 * {@link https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html AWS documentation}
155 * for the handler programming model.
156 *
157 * @param error
158 * Parameter to use to provide the error payload for a failed lambda execution.
159 * See {@link https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-mode-exceptions.html AWS documentation}
160 * for error handling.
161 * If an Error instance is passed, the error payload uses the `name` property as the `errorType`,
162 * the `message` property as the `errorMessage`, and parses the `stack` property string into
163 * the `trace` array.
164 * For other values, the `errorType` is `typeof value`, the `errorMessage` is `String(value)`, and
165 * `trace` is an empty array.
166 *
167 * @param result
168 * Parameter to use to provide the result payload for a successful lambda execution.
169 * Pass `null` or `undefined` for the `error` parameter to use this parameter.
170 */
171export type Callback<TResult = any> = (error?: Error | string | null, result?: TResult) => void;