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 <matthew@skypack.dev>
This commit is contained in:
Alex Draper 2022-12-14 07:27:04 -08:00 committed by GitHub
parent dced4a8a26
commit 6b156dd3b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
'astro': minor
---
Allow setting domain when deleting cookies

View file

@ -11,9 +11,7 @@ interface AstroCookieSetOptions {
secure?: boolean;
}
interface AstroCookieDeleteOptions {
path?: string;
}
type AstroCookieDeleteOptions = Pick<AstroCookieSetOptions, 'domain' | 'path'>;
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;
}

View file

@ -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/);
});
});
});