Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>