UNPKG

12 kBTypeScriptView Raw
1import {
2 APIGatewayEventClientCertificate,
3 APIGatewayEventDefaultAuthorizerContext,
4 APIGatewayEventRequestContextWithAuthorizer,
5} from "../common/api-gateway";
6import { Callback, CognitoIdentity, Handler } from "../handler";
7
8/**
9 * Works with Lambda Proxy Integration for Rest API or HTTP API integration Payload Format version 1.0
10 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
11 */
12export type APIGatewayProxyHandler = Handler<APIGatewayProxyEvent, APIGatewayProxyResult>;
13/**
14 * Works with Lambda Proxy Integration for Rest API or HTTP API integration Payload Format version 1.0
15 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
16 */
17export type APIGatewayProxyCallback = Callback<APIGatewayProxyResult>;
18
19/**
20 * Works with HTTP API integration Payload Format version 2.0
21 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
22 */
23export type APIGatewayProxyHandlerV2<T = never> = Handler<APIGatewayProxyEventV2, APIGatewayProxyResultV2<T>>;
24
25/**
26 * Works with HTTP API integration Payload Format version 2.0
27 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api-integration-requests.html
28 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api-mapping-template-reference.html
29 */
30export type APIGatewayProxyWebsocketHandlerV2<T = never> = Handler<
31 APIGatewayProxyWebsocketEventV2,
32 APIGatewayProxyResultV2<T>
33>;
34
35/**
36 * Works with HTTP API integration Payload Format version 2.0 adds JWT Authroizer to RequestContext
37 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
38 */
39export type APIGatewayProxyHandlerV2WithJWTAuthorizer<T = never> = Handler<
40 APIGatewayProxyEventV2WithJWTAuthorizer,
41 APIGatewayProxyResultV2<T>
42>;
43
44/**
45 * Works with HTTP API integration Payload Format version 2.0 adds Lambda Authroizer to RequestContext
46 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
47 */
48export type APIGatewayProxyHandlerV2WithLambdaAuthorizer<TAuthorizerContext, T = never> = Handler<
49 APIGatewayProxyEventV2WithLambdaAuthorizer<TAuthorizerContext>,
50 APIGatewayProxyResultV2<T>
51>;
52
53/**
54 * Works with HTTP API integration Payload Format version 2.0 adds IAM Authroizer to RequestContext
55 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
56 */
57export type APIGatewayProxyHandlerV2WithIAMAuthorizer<T = never> = Handler<
58 APIGatewayProxyEventV2WithIAMAuthorizer,
59 APIGatewayProxyResultV2<T>
60>;
61
62/**
63 * Works with HTTP API integration Payload Format version 2.0
64 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
65 */
66export type APIGatewayProxyCallbackV2 = Callback<APIGatewayProxyResultV2>;
67
68/**
69 * Works with Lambda Proxy Integration for Rest API or HTTP API integration Payload Format version 1.0
70 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
71 */
72export type APIGatewayProxyEvent = APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>;
73
74export type APIGatewayProxyWithLambdaAuthorizerHandler<TAuthorizerContext> = Handler<
75 APIGatewayProxyWithLambdaAuthorizerEvent<TAuthorizerContext>,
76 APIGatewayProxyResult
77>;
78
79export type APIGatewayProxyWithCognitoAuthorizerHandler = Handler<
80 APIGatewayProxyWithCognitoAuthorizerEvent,
81 APIGatewayProxyResult
82>;
83
84export type APIGatewayProxyWithLambdaAuthorizerEvent<TAuthorizerContext> = APIGatewayProxyEventBase<
85 APIGatewayEventLambdaAuthorizerContext<TAuthorizerContext>
86>;
87
88export type APIGatewayProxyWithLambdaAuthorizerEventRequestContext<TAuthorizerContext> =
89 APIGatewayEventRequestContextWithAuthorizer<APIGatewayEventLambdaAuthorizerContext<TAuthorizerContext>>;
90
91// API Gateway proxy integration mangles the context from a custom authorizer,
92// converting all number or boolean properties to string, and adding some extra properties.
93export type APIGatewayEventLambdaAuthorizerContext<TAuthorizerContext> =
94 & {
95 [P in keyof TAuthorizerContext]: TAuthorizerContext[P] extends null ? null : string;
96 }
97 & {
98 principalId: string;
99 integrationLatency: number;
100 };
101
102export type APIGatewayProxyWithCognitoAuthorizerEvent = APIGatewayProxyEventBase<APIGatewayProxyCognitoAuthorizer>;
103
104// All claims are coerced into strings.
105export interface APIGatewayProxyCognitoAuthorizer {
106 claims: {
107 [name: string]: string;
108 };
109}
110
111export interface APIGatewayProxyEventHeaders {
112 [name: string]: string | undefined;
113}
114
115export interface APIGatewayProxyEventMultiValueHeaders {
116 [name: string]: string[] | undefined;
117}
118
119export interface APIGatewayProxyEventPathParameters {
120 [name: string]: string | undefined;
121}
122
123export interface APIGatewayProxyEventQueryStringParameters {
124 [name: string]: string | undefined;
125}
126
127export interface APIGatewayProxyEventMultiValueQueryStringParameters {
128 [name: string]: string[] | undefined;
129}
130
131export interface APIGatewayProxyEventStageVariables {
132 [name: string]: string | undefined;
133}
134
135export interface APIGatewayProxyEventBase<TAuthorizerContext> {
136 body: string | null;
137 headers: APIGatewayProxyEventHeaders;
138 multiValueHeaders: APIGatewayProxyEventMultiValueHeaders;
139 httpMethod: string;
140 isBase64Encoded: boolean;
141 path: string;
142 pathParameters: APIGatewayProxyEventPathParameters | null;
143 queryStringParameters: APIGatewayProxyEventQueryStringParameters | null;
144 multiValueQueryStringParameters: APIGatewayProxyEventMultiValueQueryStringParameters | null;
145 stageVariables: APIGatewayProxyEventStageVariables | null;
146 requestContext: APIGatewayEventRequestContextWithAuthorizer<TAuthorizerContext>;
147 resource: string;
148}
149
150/**
151 * Works with Lambda Proxy Integration for Rest API or HTTP API integration Payload Format version 1.0
152 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
153 */
154export interface APIGatewayProxyResult {
155 statusCode: number;
156 headers?:
157 | {
158 [header: string]: boolean | number | string;
159 }
160 | undefined;
161 multiValueHeaders?:
162 | {
163 [header: string]: Array<boolean | number | string>;
164 }
165 | undefined;
166 body: string;
167 isBase64Encoded?: boolean | undefined;
168}
169
170/**
171 * Works with HTTP API integration Payload Format version 2.0
172 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
173 */
174export interface APIGatewayEventRequestContextV2 {
175 accountId: string;
176 apiId: string;
177 authentication?: {
178 clientCert: APIGatewayEventClientCertificate;
179 };
180 domainName: string;
181 domainPrefix: string;
182 http: {
183 method: string;
184 path: string;
185 protocol: string;
186 sourceIp: string;
187 userAgent: string;
188 };
189 requestId: string;
190 routeKey: string;
191 stage: string;
192 time: string;
193 timeEpoch: number;
194}
195/**
196 * Works with Websocket API integration Payload Format version 2.0
197 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api-integration-requests.html
198 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api-mapping-template-reference.html
199 */
200export interface APIGatewayEventWebsocketRequestContextV2 {
201 routeKey: string;
202 messageId: string;
203 eventType: "CONNECT" | "MESSAGE" | "DISCONNECT";
204 extendedRequestId: string;
205 requestTime: string;
206 messageDirection: "IN";
207 stage: string;
208 connectedAt: number;
209 requestTimeEpoch: number;
210 requestId: string;
211 domainName: string;
212 connectionId: string;
213 apiId: string;
214}
215
216/**
217 * Proxy Event with adaptable requestContext for different authorizer scenarios
218 */
219export interface APIGatewayProxyEventV2WithRequestContext<TRequestContext> {
220 version: string;
221 routeKey: string;
222 rawPath: string;
223 rawQueryString: string;
224 cookies?: string[];
225 headers: APIGatewayProxyEventHeaders;
226 queryStringParameters?: APIGatewayProxyEventQueryStringParameters;
227 requestContext: TRequestContext;
228 body?: string;
229 pathParameters?: APIGatewayProxyEventPathParameters;
230 isBase64Encoded: boolean;
231 stageVariables?: APIGatewayProxyEventStageVariables;
232}
233
234/**
235 * Proxy Websocket Event with adaptable requestContext for different authorizer scenarios
236 */
237export interface APIGatewayProxyWebsocketEventV2WithRequestContext<TRequestContext> {
238 requestContext: TRequestContext;
239 body?: string;
240 isBase64Encoded: boolean;
241 stageVariables?: APIGatewayProxyEventStageVariables;
242}
243
244/**
245 * Lambda Authorizer Payload
246 */
247export interface APIGatewayEventRequestContextLambdaAuthorizer<TAuthorizerContext> {
248 lambda: TAuthorizerContext;
249}
250
251/**
252 * JWT Authorizer Payload
253 */
254export interface APIGatewayEventRequestContextJWTAuthorizer {
255 principalId: string;
256 integrationLatency: number;
257 jwt: {
258 claims: { [name: string]: string | number | boolean | string[] };
259 scopes: string[];
260 };
261}
262
263/**
264 * IAM Authorizer Payload
265 */
266export interface APIGatewayEventRequestContextIAMAuthorizer {
267 iam: {
268 accessKey: string;
269 accountId: string;
270 callerId: string;
271 cognitoIdentity: null;
272 principalOrgId: string;
273 userArn: string;
274 userId: string;
275 };
276}
277
278export type APIGatewayProxyEventV2WithJWTAuthorizer = APIGatewayProxyEventV2WithRequestContext<
279 APIGatewayEventRequestContextV2WithAuthorizer<APIGatewayEventRequestContextJWTAuthorizer>
280>;
281
282export type APIGatewayProxyEventV2WithLambdaAuthorizer<TAuthorizerContext> = APIGatewayProxyEventV2WithRequestContext<
283 APIGatewayEventRequestContextV2WithAuthorizer<APIGatewayEventRequestContextLambdaAuthorizer<TAuthorizerContext>>
284>;
285
286/**
287 * Event type when invoking Lambda function URLs with IAM authorizer
288 */
289export type APIGatewayProxyEventV2WithIAMAuthorizer = APIGatewayProxyEventV2WithRequestContext<
290 APIGatewayEventRequestContextV2WithAuthorizer<APIGatewayEventRequestContextIAMAuthorizer>
291>;
292
293export interface APIGatewayEventRequestContextV2WithAuthorizer<TAuthorizer> extends APIGatewayEventRequestContextV2 {
294 authorizer: TAuthorizer;
295}
296
297/**
298 * Default Proxy event with no Authorizer
299 */
300export type APIGatewayProxyEventV2 = APIGatewayProxyEventV2WithRequestContext<APIGatewayEventRequestContextV2>;
301
302/**
303 * Default Websocket Proxy event with no Authorizer
304 */
305export type APIGatewayProxyWebsocketEventV2 = APIGatewayProxyWebsocketEventV2WithRequestContext<
306 APIGatewayEventWebsocketRequestContextV2
307>;
308
309/**
310 * Works with HTTP API integration Payload Format version 2.0
311 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
312 */
313export type APIGatewayProxyResultV2<T = never> = APIGatewayProxyStructuredResultV2 | string | T;
314
315/**
316 * Interface for structured response with `statusCode` and`headers`
317 * Works with HTTP API integration Payload Format version 2.0
318 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
319 */
320export interface APIGatewayProxyStructuredResultV2 {
321 statusCode?: number | undefined;
322 headers?:
323 | {
324 [header: string]: boolean | number | string;
325 }
326 | undefined;
327 body?: string | undefined;
328 isBase64Encoded?: boolean | undefined;
329 cookies?: string[] | undefined;
330}
331
332// Legacy names
333export type ProxyHandler = APIGatewayProxyHandler;
334export type ProxyCallback = APIGatewayProxyCallback;
335export type APIGatewayEvent = APIGatewayProxyEvent;
336export type ProxyResult = APIGatewayProxyResult;