Skip to content

Commit

Permalink
Update the Astro.cookies.set types to receive boolean and numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp committed Oct 10, 2022
1 parent 73c96f4 commit d8efa67
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .changeset/witty-sheep-wave.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion packages/astro/src/core/cookies/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
10 changes: 10 additions & 0 deletions packages/astro/test/units/cookies/set.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions packages/integrations/deno/test/basics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
Astro.cookies.set('logged-in', false, {
maxAge: 60 * 60 * 24 * 900,
path: '/'
});
return Astro.redirect('/login');
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
---
<html>
<head>
<title>Testing</title>
</head>
<body>
<h1>Testing</h1>
</body>
</html>

0 comments on commit d8efa67

Please sign in to comment.