UNPKG

9.17 kBTypeScriptView Raw
1import {
2 APIGatewayEventClientCertificate,
3 APIGatewayEventDefaultAuthorizerContext,
4 APIGatewayEventRequestContextWithAuthorizer,
5} from '../common/api-gateway';
6import { Callback, 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 adds JWT Authroizer to RequestContext
27 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
28 */
29export type APIGatewayProxyHandlerV2WithJWTAuthorizer<T = never> = Handler<
30 APIGatewayProxyEventV2WithJWTAuthorizer,
31 APIGatewayProxyResultV2<T>
32>;
33
34/**
35 * Works with HTTP API integration Payload Format version 2.0 adds Lambda Authroizer to RequestContext
36 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
37 */
38export type APIGatewayProxyHandlerV2WithLambdaAuthorizer<TAuthorizerContext, T = never> = Handler<
39 APIGatewayProxyEventV2WithLambdaAuthorizer<TAuthorizerContext>,
40 APIGatewayProxyResultV2<T>
41>;
42
43/**
44 * Works with HTTP API integration Payload Format version 2.0
45 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
46 */
47export type APIGatewayProxyCallbackV2 = Callback<APIGatewayProxyResultV2>;
48
49/**
50 * Works with Lambda Proxy Integration for Rest API or HTTP API integration Payload Format version 1.0
51 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
52 */
53export type APIGatewayProxyEvent = APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>;
54
55export type APIGatewayProxyWithLambdaAuthorizerHandler<TAuthorizerContext> =
56 Handler<APIGatewayProxyWithLambdaAuthorizerEvent<TAuthorizerContext>, APIGatewayProxyResult>;
57
58export type APIGatewayProxyWithCognitoAuthorizerHandler =
59 Handler<APIGatewayProxyWithCognitoAuthorizerEvent, APIGatewayProxyResult>;
60
61export type APIGatewayProxyWithLambdaAuthorizerEvent<TAuthorizerContext> =
62 APIGatewayProxyEventBase<APIGatewayEventLambdaAuthorizerContext<TAuthorizerContext>>;
63
64export type APIGatewayProxyWithLambdaAuthorizerEventRequestContext<TAuthorizerContext> =
65 APIGatewayEventRequestContextWithAuthorizer<APIGatewayEventLambdaAuthorizerContext<TAuthorizerContext>>;
66
67// API Gateway proxy integration mangles the context from a custom authorizer,
68// converting all number or boolean properties to string, and adding some extra properties.
69export type APIGatewayEventLambdaAuthorizerContext<TAuthorizerContext> = {
70 [P in keyof TAuthorizerContext]: TAuthorizerContext[P] extends null ? null : string;
71} & {
72 principalId: string;
73 integrationLatency: number;
74};
75
76export type APIGatewayProxyWithCognitoAuthorizerEvent = APIGatewayProxyEventBase<APIGatewayProxyCognitoAuthorizer>;
77
78// All claims are coerced into strings.
79export interface APIGatewayProxyCognitoAuthorizer {
80 claims: {
81 [name: string]: string;
82 };
83}
84
85export interface APIGatewayProxyEventHeaders {
86 [name: string]: string | undefined;
87}
88
89export interface APIGatewayProxyEventMultiValueHeaders {
90 [name: string]: string[] | undefined;
91}
92
93export interface APIGatewayProxyEventPathParameters {
94 [name: string]: string | undefined;
95}
96
97export interface APIGatewayProxyEventQueryStringParameters {
98 [name: string]: string | undefined;
99}
100
101export interface APIGatewayProxyEventMultiValueQueryStringParameters {
102 [name: string]: string[] | undefined;
103}
104
105export interface APIGatewayProxyEventStageVariables {
106 [name: string]: string | undefined;
107}
108
109export interface APIGatewayProxyEventBase<TAuthorizerContext> {
110 body: string | null;
111 headers: APIGatewayProxyEventHeaders;
112 multiValueHeaders: APIGatewayProxyEventMultiValueHeaders;
113 httpMethod: string;
114 isBase64Encoded: boolean;
115 path: string;
116 pathParameters: APIGatewayProxyEventPathParameters | null;
117 queryStringParameters: APIGatewayProxyEventQueryStringParameters | null;
118 multiValueQueryStringParameters: APIGatewayProxyEventMultiValueQueryStringParameters | null;
119 stageVariables: APIGatewayProxyEventStageVariables | null;
120 requestContext: APIGatewayEventRequestContextWithAuthorizer<TAuthorizerContext>;
121 resource: string;
122}
123
124/**
125 * Works with Lambda Proxy Integration for Rest API or HTTP API integration Payload Format version 1.0
126 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
127 */
128export interface APIGatewayProxyResult {
129 statusCode: number;
130 headers?: {
131 [header: string]: boolean | number | string;
132 } | undefined;
133 multiValueHeaders?: {
134 [header: string]: Array<boolean | number | string>;
135 } | undefined;
136 body: string;
137 isBase64Encoded?: boolean | undefined;
138}
139
140/**
141 * Works with HTTP API integration Payload Format version 2.0
142 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
143 */
144export interface APIGatewayEventRequestContextV2 {
145 accountId: string;
146 apiId: string;
147 authentication?: {
148 clientCert: APIGatewayEventClientCertificate;
149 };
150 domainName: string;
151 domainPrefix: string;
152 http: {
153 method: string;
154 path: string;
155 protocol: string;
156 sourceIp: string;
157 userAgent: string;
158 };
159 requestId: string;
160 routeKey: string;
161 stage: string;
162 time: string;
163 timeEpoch: number;
164}
165
166/**
167 * Proxy Event with adaptable requestContext for different authorizer scenarios
168 */
169export interface APIGatewayProxyEventV2WithRequestContext<TRequestContext> {
170 version: string;
171 routeKey: string;
172 rawPath: string;
173 rawQueryString: string;
174 cookies?: string[];
175 headers: APIGatewayProxyEventHeaders;
176 queryStringParameters?: APIGatewayProxyEventQueryStringParameters;
177 requestContext: TRequestContext;
178 body?: string;
179 pathParameters?: APIGatewayProxyEventPathParameters;
180 isBase64Encoded: boolean;
181 stageVariables?: APIGatewayProxyEventStageVariables;
182}
183
184/**
185 * Lambda Authorizer Payload
186 */
187export interface APIGatewayEventRequestContextLambdaAuthorizer<TAuthorizerContext> {
188 lambda: TAuthorizerContext;
189}
190
191/**
192 * JWT Authorizer Payload
193 */
194export interface APIGatewayEventRequestContextJWTAuthorizer {
195 principalId: string;
196 integrationLatency: number;
197 jwt: {
198 claims: { [name: string]: string | number | boolean | string[] };
199 scopes: string[];
200 };
201}
202
203export type APIGatewayProxyEventV2WithJWTAuthorizer = APIGatewayProxyEventV2WithRequestContext<
204 APIGatewayEventRequestContextV2WithAuthorizer<APIGatewayEventRequestContextJWTAuthorizer>
205>;
206
207export type APIGatewayProxyEventV2WithLambdaAuthorizer<TAuthorizerContext> = APIGatewayProxyEventV2WithRequestContext<
208 APIGatewayEventRequestContextV2WithAuthorizer<APIGatewayEventRequestContextLambdaAuthorizer<TAuthorizerContext>>
209>;
210
211export interface APIGatewayEventRequestContextV2WithAuthorizer<TAuthorizer> extends APIGatewayEventRequestContextV2 {
212 authorizer: TAuthorizer;
213}
214
215/**
216 * Default Proxy event with no Authorizer
217 */
218export type APIGatewayProxyEventV2 = APIGatewayProxyEventV2WithRequestContext<APIGatewayEventRequestContextV2>;
219
220/**
221 * Works with HTTP API integration Payload Format version 2.0
222 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
223 */
224export type APIGatewayProxyResultV2<T = never> = APIGatewayProxyStructuredResultV2 | string | T;
225
226/**
227 * Interface for structured response with `statusCode` and`headers`
228 * Works with HTTP API integration Payload Format version 2.0
229 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
230 */
231export interface APIGatewayProxyStructuredResultV2 {
232 statusCode?: number | undefined;
233 headers?: {
234 [header: string]: boolean | number | string;
235 } | undefined;
236 body?: string | undefined;
237 isBase64Encoded?: boolean | undefined;
238 cookies?: string[] | undefined;
239}
240
241// Legacy names
242export type ProxyHandler = APIGatewayProxyHandler;
243export type ProxyCallback = APIGatewayProxyCallback;
244export type APIGatewayEvent = APIGatewayProxyEvent;
245export type ProxyResult = APIGatewayProxyResult;