Skip to content

Commit 1e27992

Browse files
authored
Update the Astro.cookies.set types to receive boolean and numbers (#5047)
1 parent baf88ee commit 1e27992

6 files changed

Lines changed: 53 additions & 1 deletion

File tree

.changeset/witty-sheep-wave.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Update Astro.cookies.set types to allow booleans and numbers
6+
7+
Note that booleans and numbers were already allowed, they just were not allowed by the type definitions.

packages/astro/src/core/cookies/cookies.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ interface AstroCookieInterface {
2525
interface AstroCookiesInterface {
2626
get(key: string): AstroCookieInterface;
2727
has(key: string): boolean;
28-
set(key: string, value: string | Record<string, any>, options?: AstroCookieSetOptions): void;
28+
set(key: string, value: string | number | boolean | Record<string, any>, options?: AstroCookieSetOptions): void;
2929
delete(key: string, options?: AstroCookieDeleteOptions): void;
3030
}
3131

packages/astro/test/units/cookies/set.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ describe('astro/src/core/cookies', () => {
4545
expect(headers[0]).to.equal('one=2');
4646
});
4747

48+
it('Can pass a boolean', () => {
49+
let req = new Request('http://example.com/');
50+
let cookies = new AstroCookies(req);
51+
cookies.set('admin', true);
52+
expect(cookies.get('admin').boolean()).to.equal(true);
53+
let headers = Array.from(cookies.headers());
54+
expect(headers).to.have.a.lengthOf(1);
55+
expect(headers[0]).to.equal('admin=true');
56+
});
57+
4858
it('Can get the value after setting', () => {
4959
let req = new Request('http://example.com/');
5060
let cookies = new AstroCookies(req);

packages/integrations/deno/test/basics.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,20 @@ Deno.test({
126126
sanitizeResources: false,
127127
sanitizeOps: false,
128128
});
129+
130+
Deno.test({
131+
name: 'Astro.cookies',
132+
permissions: defaultTestPermissions,
133+
async fn() {
134+
await startApp(async (baseUrl: URL) => {
135+
const url = new URL('/admin', baseUrl);
136+
const resp = await fetch(url, { redirect: 'manual' });
137+
assertEquals(resp.status, 302);
138+
139+
const headers = resp.headers;
140+
assertEquals(headers.get('set-cookie'), 'logged-in=false; Max-Age=77760000; Path=/');
141+
});
142+
},
143+
sanitizeResources: false,
144+
sanitizeOps: false,
145+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
Astro.cookies.set('logged-in', false, {
3+
maxAge: 60 * 60 * 24 * 900,
4+
path: '/'
5+
});
6+
7+
return Astro.redirect('/login');
8+
---
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
---
3+
<html>
4+
<head>
5+
<title>Testing</title>
6+
</head>
7+
<body>
8+
<h1>Testing</h1>
9+
</body>
10+
</html>

0 commit comments

Comments
 (0)