From 6b156dd3b467884839a571c53114aadf26fa4b0b Mon Sep 17 00:00:00 2001 From: Alex Draper Date: Wed, 14 Dec 2022 07:27:04 -0800 Subject: [PATCH] Allow setting domain when deleting cookies (#5341) * Allow setting domain when deleting cookies * Add delete cookie with domain tests * Fix syntax for AstroCookieDeleteOptions type * Add changeset * Update to a minor change rather than a patch Co-authored-by: Matthew Phillips --- .changeset/small-ravens-cover.md | 5 +++++ packages/astro/src/core/cookies/cookies.ts | 7 ++++--- packages/astro/test/units/cookies/delete.test.js | 11 +++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 .changeset/small-ravens-cover.md diff --git a/.changeset/small-ravens-cover.md b/.changeset/small-ravens-cover.md new file mode 100644 index 000000000000..67b8e31f03e1 --- /dev/null +++ b/.changeset/small-ravens-cover.md @@ -0,0 +1,5 @@ +--- +'astro': minor +--- + +Allow setting domain when deleting cookies diff --git a/packages/astro/src/core/cookies/cookies.ts b/packages/astro/src/core/cookies/cookies.ts index f999db8a16b6..73996f51fb42 100644 --- a/packages/astro/src/core/cookies/cookies.ts +++ b/packages/astro/src/core/cookies/cookies.ts @@ -11,9 +11,7 @@ interface AstroCookieSetOptions { secure?: boolean; } -interface AstroCookieDeleteOptions { - path?: string; -} +type AstroCookieDeleteOptions = Pick; interface AstroCookieInterface { value: string | undefined; @@ -75,6 +73,9 @@ class AstroCookies implements AstroCookiesInterface { expires: DELETED_EXPIRATION, }; + if (options?.domain) { + serializeOptions.domain = options.domain; + } if (options?.path) { serializeOptions.path = options.path; } diff --git a/packages/astro/test/units/cookies/delete.test.js b/packages/astro/test/units/cookies/delete.test.js index 571a3e40d736..67fa1306bc23 100644 --- a/packages/astro/test/units/cookies/delete.test.js +++ b/packages/astro/test/units/cookies/delete.test.js @@ -56,5 +56,16 @@ describe('astro/src/core/cookies', () => { expect(headers).to.have.a.lengthOf(1); expect(headers[0]).to.match(/Path=\/subpath\//); }); + + it('can provide a domain', () => { + let req = new Request('http://example.com/'); + let cookies = new AstroCookies(req); + cookies.delete('foo', { + domain: '.example.com', + }); + let headers = Array.from(cookies.headers()); + expect(headers).to.have.a.lengthOf(1); + expect(headers[0]).to.match(/Domain=\.example\.com/); + }); }); });