Update the Astro.cookies.set types to receive boolean and numbers (#5047)

This commit is contained in:
Matthew Phillips 2022-10-12 08:31:55 -04:00 committed by GitHub
parent baf88ee9e5
commit 1e27992437
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 53 additions and 1 deletions

View file

@ -0,0 +1,7 @@
---
'astro': patch
---
Update Astro.cookies.set types to allow booleans and numbers
Note that booleans and numbers were already allowed, they just were not allowed by the type definitions.

View file

@ -25,7 +25,7 @@ interface AstroCookieInterface {
interface AstroCookiesInterface {
get(key: string): AstroCookieInterface;
has(key: string): boolean;
set(key: string, value: string | Record<string, any>, options?: AstroCookieSetOptions): void;
set(key: string, value: string | number | boolean | Record<string, any>, options?: AstroCookieSetOptions): void;
delete(key: string, options?: AstroCookieDeleteOptions): void;
}

View file

@ -45,6 +45,16 @@ describe('astro/src/core/cookies', () => {
expect(headers[0]).to.equal('one=2');
});
it('Can pass a boolean', () => {
let req = new Request('http://example.com/');
let cookies = new AstroCookies(req);
cookies.set('admin', true);
expect(cookies.get('admin').boolean()).to.equal(true);
let headers = Array.from(cookies.headers());
expect(headers).to.have.a.lengthOf(1);
expect(headers[0]).to.equal('admin=true');
});
it('Can get the value after setting', () => {
let req = new Request('http://example.com/');
let cookies = new AstroCookies(req);

View file

@ -126,3 +126,20 @@ Deno.test({
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: 'Astro.cookies',
permissions: defaultTestPermissions,
async fn() {
await startApp(async (baseUrl: URL) => {
const url = new URL('/admin', baseUrl);
const resp = await fetch(url, { redirect: 'manual' });
assertEquals(resp.status, 302);
const headers = resp.headers;
assertEquals(headers.get('set-cookie'), 'logged-in=false; Max-Age=77760000; Path=/');
});
},
sanitizeResources: false,
sanitizeOps: false,
});

View file

@ -0,0 +1,8 @@
---
Astro.cookies.set('logged-in', false, {
maxAge: 60 * 60 * 24 * 900,
path: '/'
});
return Astro.redirect('/login');
---

View file

@ -0,0 +1,10 @@
---
---
<html>
<head>
<title>Testing</title>
</head>
<body>
<h1>Testing</h1>
</body>
</html>