UNPKG

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