UNPKG

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