From c829f3727740748e9356b12c957eb786056806a9 Mon Sep 17 00:00:00 2001 From: danielconde Date: Wed, 4 Sep 2019 21:43:58 +0100 Subject: [PATCH] configurable http methods for default cache behavior --- README.md | 1 + __tests__/__snapshots__/custom-url-origin.test.js.snap | 7 ++++++- __tests__/custom-url-origin.test.js | 1 + lib/getDefaultCacheBehavior.js | 6 ++++-- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cbddc42..6c0dd1e 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ distribution: enabled: true # optional defaults: # optional ttl: 15 + allowedHttpMethods: ['HEAD', 'GET'] lambda@edge: # added to cloudfront default cache behavior viewer-request: arn:aws:lambda:us-east-1:123:function:myFunc:version origins: diff --git a/__tests__/__snapshots__/custom-url-origin.test.js.snap b/__tests__/__snapshots__/custom-url-origin.test.js.snap index 21b3a5d..7adfafa 100644 --- a/__tests__/__snapshots__/custom-url-origin.test.js.snap +++ b/__tests__/__snapshots__/custom-url-origin.test.js.snap @@ -24,9 +24,14 @@ Object { }, "Items": Array [ "HEAD", + "DELETE", + "POST", "GET", + "OPTIONS", + "PUT", + "PATCH", ], - "Quantity": 2, + "Quantity": 7, }, "Compress": false, "DefaultTTL": 10, diff --git a/__tests__/custom-url-origin.test.js b/__tests__/custom-url-origin.test.js index a730ef1..a375b08 100644 --- a/__tests__/custom-url-origin.test.js +++ b/__tests__/custom-url-origin.test.js @@ -24,6 +24,7 @@ describe('Input origin as a custom url', () => { it('creates distribution with custom url origin and sets defaults', async () => { await component.default({ defaults: { + allowedHttpMethods: ['HEAD', 'DELETE', 'POST', 'GET', 'OPTIONS', 'PUT', 'PATCH'], ttl: 10, 'lambda@edge': { 'origin-request': 'arn:aws:lambda:us-east-1:123:function:originRequestFunction' diff --git a/lib/getDefaultCacheBehavior.js b/lib/getDefaultCacheBehavior.js index 0f1b5a9..4f16e2f 100644 --- a/lib/getDefaultCacheBehavior.js +++ b/lib/getDefaultCacheBehavior.js @@ -1,6 +1,8 @@ const addLambdaAtEdgeToCacheBehavior = require('./addLambdaAtEdgeToCacheBehavior') module.exports = (originId, defaults = {}) => { + const allowedMethods = defaults.allowedHttpMethods || ['HEAD', 'GET'] + const defaultCacheBehavior = { TargetOriginId: originId, ForwardedValues: { @@ -25,8 +27,8 @@ module.exports = (originId, defaults = {}) => { ViewerProtocolPolicy: 'redirect-to-https', MinTTL: 0, AllowedMethods: { - Quantity: 2, - Items: ['HEAD', 'GET'], + Quantity: allowedMethods.length, + Items: allowedMethods, CachedMethods: { Quantity: 2, Items: ['HEAD', 'GET']