UNPKG

7.2 kBTypeScriptView Raw
1import {
2 APIGatewayEventDefaultAuthorizerContext,
3 APIGatewayEventRequestContextWithAuthorizer,
4} from "../common/api-gateway";
5import { Callback, Handler } from "../handler";
6
7/**
8 * Works with Lambda Proxy Integration for Rest API or HTTP API integration Payload Format version 1.0
9 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
10 */
11export type APIGatewayProxyHandler = Handler<APIGatewayProxyEvent, APIGatewayProxyResult>;
12/**
13 * Works with Lambda Proxy Integration for Rest API or HTTP API integration Payload Format version 1.0
14 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
15 */
16export type APIGatewayProxyCallback = Callback<APIGatewayProxyResult>;
17
18/**
19 * Works with HTTP API integration Payload Format version 2.0
20 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
21 */
22export type APIGatewayProxyHandlerV2<T = never> = Handler<APIGatewayProxyEventV2, APIGatewayProxyResultV2<T>>;
23/**
24 * Works with HTTP API integration Payload Format version 2.0
25 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
26 */
27export type APIGatewayProxyCallbackV2 = Callback<APIGatewayProxyResultV2>;
28
29/**
30 * Works with Lambda Proxy Integration for Rest API or HTTP API integration Payload Format version 1.0
31 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
32 */
33export type APIGatewayProxyEvent = APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>;
34
35export type APIGatewayProxyWithLambdaAuthorizerHandler<TAuthorizerContext> =
36 Handler<APIGatewayProxyWithLambdaAuthorizerEvent<TAuthorizerContext>, APIGatewayProxyResult>;
37
38export type APIGatewayProxyWithCognitoAuthorizerHandler =
39 Handler<APIGatewayProxyWithCognitoAuthorizerEvent, APIGatewayProxyResult>;
40
41export type APIGatewayProxyWithLambdaAuthorizerEvent<TAuthorizerContext> =
42 APIGatewayProxyEventBase<APIGatewayEventLambdaAuthorizerContext<TAuthorizerContext>>;
43
44export type APIGatewayProxyWithLambdaAuthorizerEventRequestContext<TAuthorizerContext> =
45 APIGatewayEventRequestContextWithAuthorizer<APIGatewayEventLambdaAuthorizerContext<TAuthorizerContext>>;
46
47// API Gateway proxy integration mangles the context from a custom authorizer,
48// converting all number or boolean properties to string, and adding some extra properties.
49export type APIGatewayEventLambdaAuthorizerContext<TAuthorizerContext> = {
50 [P in keyof TAuthorizerContext]: TAuthorizerContext[P] extends null ? null : string;
51} & {
52 principalId: string;
53 integrationLatency: number;
54};
55
56export type APIGatewayProxyWithCognitoAuthorizerEvent = APIGatewayProxyEventBase<APIGatewayProxyCognitoAuthorizer>;
57
58// All claims are coerced into strings.
59export interface APIGatewayProxyCognitoAuthorizer {
60 claims: {
61 [name: string]: string;
62 };
63}
64
65export interface APIGatewayProxyEventHeaders {
66 [name: string]: string | undefined;
67}
68
69export interface APIGatewayProxyEventMultiValueHeaders {
70 [name: string]: string[] | undefined;
71}
72
73export interface APIGatewayProxyEventPathParameters {
74 [name: string]: string | undefined;
75}
76
77export interface APIGatewayProxyEventQueryStringParameters {
78 [name: string]: string | undefined;
79}
80
81export interface APIGatewayProxyEventMultiValueQueryStringParameters {
82 [name: string]: string[] | undefined;
83}
84
85export interface APIGatewayProxyEventStageVariables {
86 [name: string]: string | undefined;
87}
88
89export interface APIGatewayProxyEventBase<TAuthorizerContext> {
90 body: string | null;
91 headers: APIGatewayProxyEventHeaders;
92 multiValueHeaders: APIGatewayProxyEventMultiValueHeaders;
93 httpMethod: string;
94 isBase64Encoded: boolean;
95 path: string;
96 pathParameters: APIGatewayProxyEventPathParameters | null;
97 queryStringParameters: APIGatewayProxyEventQueryStringParameters | null;
98 multiValueQueryStringParameters: APIGatewayProxyEventMultiValueQueryStringParameters | null;
99 stageVariables: APIGatewayProxyEventStageVariables | null;
100 requestContext: APIGatewayEventRequestContextWithAuthorizer<TAuthorizerContext>;
101 resource: string;
102}
103
104/**
105 * Works with Lambda Proxy Integration for Rest API or HTTP API integration Payload Format version 1.0
106 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
107 */
108export interface APIGatewayProxyResult {
109 statusCode: number;
110 headers?: {
111 [header: string]: boolean | number | string;
112 } | undefined;
113 multiValueHeaders?: {
114 [header: string]: Array<boolean | number | string>;
115 } | undefined;
116 body: string;
117 isBase64Encoded?: boolean | undefined;
118}
119
120/**
121 * Works with HTTP API integration Payload Format version 2.0
122 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
123 */
124export interface APIGatewayProxyEventV2 {
125 version: string;
126 routeKey: string;
127 rawPath: string;
128 rawQueryString: string;
129 cookies?: string[] | undefined;
130 headers: APIGatewayProxyEventHeaders;
131 queryStringParameters?: APIGatewayProxyEventQueryStringParameters | undefined;
132 requestContext: {
133 accountId: string;
134 apiId: string;
135 authorizer?: {
136 jwt: {
137 claims: { [name: string]: string | number | boolean | string[] };
138 scopes: string[];
139 };
140 } | undefined;
141 domainName: string;
142 domainPrefix: string;
143 http: {
144 method: string;
145 path: string;
146 protocol: string;
147 sourceIp: string;
148 userAgent: string;
149 };
150 requestId: string;
151 routeKey: string;
152 stage: string;
153 time: string;
154 timeEpoch: number;
155 };
156 body?: string | undefined;
157 pathParameters?: APIGatewayProxyEventPathParameters | undefined;
158 isBase64Encoded: boolean;
159 stageVariables?: APIGatewayProxyEventStageVariables | undefined;
160}
161
162/**
163 * Works with HTTP API integration Payload Format version 2.0
164 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
165 */
166export type APIGatewayProxyResultV2<T = never> = APIGatewayProxyStructuredResultV2 | string | T;
167
168/**
169 * Interface for structured response with `statusCode` and`headers`
170 * Works with HTTP API integration Payload Format version 2.0
171 * @see - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
172 */
173export interface APIGatewayProxyStructuredResultV2 {
174 statusCode?: number | undefined;
175 headers?: {
176 [header: string]: boolean | number | string;
177 } | undefined;
178 body?: string | undefined;
179 isBase64Encoded?: boolean | undefined;
180 cookies?: string[] | undefined;
181}
182
183// Legacy names
184export type ProxyHandler = APIGatewayProxyHandler;
185export type ProxyCallback = APIGatewayProxyCallback;
186export type APIGatewayEvent = APIGatewayProxyEvent;
187export type ProxyResult = APIGatewayProxyResult;