UNPKG

9.7 kBTypeScriptView Raw
1/**
2 * CloudFront events
3 * http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-event-structure.html
4 * Bear in mind that the "example" event structure in the page above includes
5 * both an S3 and a Custom origin, which is not strictly allowed. Only one
6 * of these per event may be present.
7 */
8export interface CloudFrontHeaders {
9 [name: string]: Array<{
10 key?: string | undefined;
11 value: string;
12 }>;
13}
14
15export type CloudFrontOrigin =
16 | { s3: CloudFrontS3Origin; custom?: never | undefined }
17 | { custom: CloudFrontCustomOrigin; s3?: never | undefined };
18
19export interface CloudFrontCustomOrigin {
20 customHeaders: CloudFrontHeaders;
21 domainName: string;
22 keepaliveTimeout: number;
23 path: string;
24 port: number;
25 protocol: 'http' | 'https';
26 readTimeout: number;
27 sslProtocols: string[];
28}
29
30export interface CloudFrontS3Origin {
31 authMethod: 'origin-access-identity' | 'none';
32 customHeaders: CloudFrontHeaders;
33 domainName: string;
34 path: string;
35 region: string;
36}
37
38export interface CloudFrontResponse {
39 status: string;
40 statusDescription: string;
41 headers: CloudFrontHeaders;
42}
43
44export interface CloudFrontRequest {
45 body?:
46 | {
47 action: 'read-only' | 'replace';
48 data: string;
49 encoding: 'base64' | 'text';
50 readonly inputTruncated: boolean;
51 }
52 | undefined;
53 readonly clientIp: string;
54 readonly method: string;
55 uri: string;
56 querystring: string;
57 headers: CloudFrontHeaders;
58 origin?: CloudFrontOrigin | undefined;
59}
60
61export interface CloudFrontEvent {
62 config: {
63 readonly distributionDomainName: string;
64 readonly distributionId: string;
65 readonly eventType: 'origin-request' | 'origin-response' | 'viewer-request' | 'viewer-response';
66 readonly requestId: string;
67 };
68}
69
70/**
71 * Generated HTTP response in viewer request event or origin request event
72 *
73 * https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-generating-http-responses-in-requests.html#lambda-generating-http-responses-object
74 */
75export interface CloudFrontResultResponse {
76 status: string;
77 statusDescription?: string | undefined;
78 headers?: CloudFrontHeaders | undefined;
79 bodyEncoding?: 'text' | 'base64' | undefined;
80 body?: string | undefined;
81}
82
83/** @see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-event-structure.html#functions-event-structure-query-header-cookie */
84export interface CloudFrontFunctionsCookies {
85 [key: string]: {
86 value: string;
87 attributes?: string;
88 multiValue?: Array<{ value: string; attributes?: string }>;
89 };
90}
91
92/** @see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-event-structure.html#functions-event-structure-query-header-cookie */
93export interface CloudFrontFunctionsQuerystring {
94 [key: string]: { value: string; multiValue?: Array<{ value: string }> };
95}
96
97/** @see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-event-structure.html#functions-event-structure-query-header-cookie */
98export interface CloudFrontFunctionsHeaders {
99 [key: string]: { value: string; multiValue?: Array<{ value: string }> };
100}
101
102/**
103 * @see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-event-structure.html
104 *
105 * @usage
106 * ```ts
107 * export const handler = async (event: CloudFrontFunctionsEvent) => {
108 * var response = event.response;
109 * var headers = response.headers;
110 * // Set the cache-control header
111 * headers["cache-control"] = { value: "public,max-age=31536000,immutable" };
112 * // Return response to viewers
113 * return response
114 * };
115 * ```
116 */
117export interface CloudFrontFunctionsEvent {
118 /**
119 * ## Version field
120 * The version field contains a string that specifies the version of the
121 * CloudFront Functions event object. The current version is 1.0.
122 */
123 version: string;
124 /**
125 * ## Context object
126 * The `context` object contains contextual information about the event. It includes the following fields:
127 * - `distributionDomainName`
128 * - `distributionId`
129 * - `eventType`
130 * - `requestId`
131 */
132 context: {
133 /** The CloudFront domain name (for example, d111111abcdef8.cloudfront.net) of the distribution that’s associated with the event. */
134 distributionDomainName: string;
135 /** The ID of the distribution (for example, EDFDVBD6EXAMPLE) that’s associated with the event. */
136 distributionId: string;
137 /** The event type, either `viewer-request` or `viewer-response`. */
138 eventType: 'viewer-request' | 'viewer-response';
139 /** A string that uniquely identifies a CloudFront request (and its associated response). */
140 requestId: string;
141 };
142 /**
143 * ## Viewer object
144 * The `viewer` object contains an `ip` field whose value is the IP address of the viewer (client) that sent the request.
145 * If the viewer request came through an HTTP proxy or a load balancer, the value is the IP address of the proxy or load balancer.
146 */
147 viewer: {
148 ip: string;
149 };
150 /**
151 * ## Request object
152 * The `request` object contains a representation of a viewer-to-CloudFront HTTP request.
153 * In the `event` object that’s passed to your function, the `request` object represents the
154 * actual request that CloudFront received from the viewer.
155 *
156 * If your function code returns a `request` object to CloudFront, it must use this same structure.
157 *
158 * The `request` object contains the following fields:
159 * - `method`
160 * - `uri`
161 * - `querystring`
162 * - `headers`
163 * - `cookies`
164 */
165 request: {
166 /** The HTTP method of the request. If your function code returns a `request`, it cannot modify this field. This is the only read-only field in the `request` object. */
167 method: string;
168 /**
169 * The relative path of the requested object. If your function modifies the `uri value, note the following:
170 * - The new `uri` value must begin with a forward slash (`/`)`.
171 * - When a function changes the `uri` value, it changes the object that the viewer is requesting.
172 * - When a function changes the `uri` value, it doesn’t change the cache behavior for the request or the origin that an origin request is sent to.
173 */
174 uri: string;
175 /**
176 * An object that represents the query string in the request. If the request doesn’t include a query string,
177 * the `request` object still includes an empty `querystring` object.
178 *
179 * The `querystring` object contains one field for each query string parameter in the request.
180 * Query string parameter names are converted to lowercase.
181 */
182 querystring: CloudFrontFunctionsQuerystring;
183 /**
184 * An object that represents the HTTP headers in the request. If the request contains any `Cookie` headers,
185 * those headers are not part of the `headers` object. Cookies are represented separately in the `cookies` object.
186 *
187 * The `headers` object contains one field for each header in the request. Header names are converted to lowercase.
188 */
189 headers: CloudFrontFunctionsHeaders;
190 /**
191 * An object that represents the cookies in the request (`Cookie` headers).
192 *
193 * The `cookies` object contains one field for each cookie in the request. Cookie names are converted to lowercase.
194 */
195 cookies: CloudFrontFunctionsCookies;
196 };
197 /**
198 * ## Response object
199 *
200 * The `response` object contains a representation of a CloudFront-to-viewer HTTP response.
201 * In the `event` object that’s passed to your function, the `response` object represents CloudFront’s actual response to a viewer request.
202 *
203 * If your function code returns a `response` object, it must use this same structure.
204 *
205 * The `response` object contains the following fields:
206 */
207 response: {
208 /**
209 * The HTTP status code of the response. This value is an integer, not a string.
210 *
211 * If the function is associated with a _viewer response_ event type, your function code cannot change
212 * the `statusCode` that it received. If the function is associated with a _viewer request_ event type
213 * and [generates an HTTP response](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/writing-function-code.html#function-code-generate-response),
214 * your function code can set the `statusCode`.
215 */
216 statusCode: number;
217 /** The HTTP status description of the response. If your function code generates a response, this field is optional. */
218 statusDescription?: string;
219 /**
220 * An object that represents the HTTP headers in the response. If the response contains any `Set-Cookie` headers,
221 * those `headers` are not part of the headers object. Cookies are represented separately in the `cookies` object.
222 *
223 * The `headers` object contains one field for each header in the response. Header names are converted to lowercase.
224 */
225 headers: CloudFrontFunctionsHeaders;
226 /**
227 * An object that represents the cookies in the response (`Set-Cookie` headers).
228 * The `cookies` object contains one field for each cookie in the response. Cookie names are converted to lowercase.
229 */
230 cookies: CloudFrontFunctionsCookies;
231 };
232}