From 8eef7a8374d3537871a1c4e053c8c95d6ff73a42 Mon Sep 17 00:00:00 2001 From: Simon Garner Date: Fri, 3 May 2019 05:29:11 +1200 Subject: [PATCH] [aws-lambda] Keys should be optional inside CloudFrontHeaders (#35121) * [aws-lambda] Keys should be optional inside CloudFrontHeaders According to the AWS documentation, it is optional to specify a `key` for each header value, but the type declaration is currently requiring this to be specified unnecessarily. https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-event-structure.html#lambda-event-structure-response > - `key` (optional) is the case-sensitive name of the header as it appears in an HTTP request; for example, `accept` or `host`. > - If you do not include the header key portion of the key-value pair, Lambda@Edge will automatically insert a header key using the header name that you provide. Regardless of how you've formatted the header name, the header key that is inserted automatically will be formatted with initial capitalization for each part, separated by hyphens (-). > > For example, you can add a header like the following, without a header key: `'content-type': [{ value: 'text/html;charset=UTF-8' }]`. In this example, Lambda@Edge creates the following header key: `Content-Type`. By making this property optional, one is able to write: ```ts import { CloudFrontHeaders } from 'aws-lambda'; const headers: CloudFrontHeaders = { 'content-type': [{ value: 'text/plain '}], }; ``` ... instead of having to repeat the header name like: ```ts import { CloudFrontHeaders } from 'aws-lambda'; const headers: CloudFrontHeaders = { 'content-type': [{ key: 'content-type', value: 'text/plain '}], }; ``` * Update test Setting a header without specifying the key should not raise a TypeScript compiler error. * Revert "Update test" This reverts commit 088653e362e6f0a152f54e9b5694bb778b072354. * Add test for AWSLambda.CloudFrontHeaders with and without explicit keys --- types/aws-lambda/aws-lambda-tests.ts | 5 +++++ types/aws-lambda/index.d.ts | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/types/aws-lambda/aws-lambda-tests.ts b/types/aws-lambda/aws-lambda-tests.ts index edc2015616..0639988ef7 100644 --- a/types/aws-lambda/aws-lambda-tests.ts +++ b/types/aws-lambda/aws-lambda-tests.ts @@ -1019,6 +1019,11 @@ const cloudFrontRequestHandler: AWSLambda.CloudFrontRequestHandler = (event: AWS const cloudFrontResponseHandler: AWSLambda.CloudFrontResponseHandler = (event: AWSLambda.CloudFrontResponseEvent, context: AWSLambda.Context, cb: AWSLambda.CloudFrontResponseCallback) => { }; +const cloudFrontHeaders: AWSLambda.CloudFrontHeaders = { + 'content-type': [{ value: 'text/plain' }], + 'x-foo-bar': [{ key: 'X-Foo-Bar', value: 'example' }] +}; + const customAuthorizerHandler: AWSLambda.CustomAuthorizerHandler = (event: AWSLambda.CustomAuthorizerEvent, context: AWSLambda.Context, cb: AWSLambda.CustomAuthorizerCallback) => { }; interface CustomEvent { eventString: string; eventBool: boolean; } diff --git a/types/aws-lambda/index.d.ts b/types/aws-lambda/index.d.ts index fc9b0e4532..91e5de7d3e 100644 --- a/types/aws-lambda/index.d.ts +++ b/types/aws-lambda/index.d.ts @@ -734,7 +734,7 @@ export type CodePipelineCloudWatchEvent = */ export interface CloudFrontHeaders { [name: string]: Array<{ - key: string; + key?: string; value: string; }>; }