summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Matthew Phillips <matthew@skypack.dev> 2022-10-12 08:31:55 -0400
committerGravatar GitHub <noreply@github.com> 2022-10-12 08:31:55 -0400
commit1e27992437aa0371b8550acb3e3f79e62721a506 (patch)
tree884fb5500c70446904569383c6a2d1708784f76a
parentbaf88ee9e5e692a94981d7a696fbdcb4cd8ab2a6 (diff)
downloadastro-1e27992437aa0371b8550acb3e3f79e62721a506.tar.gz
astro-1e27992437aa0371b8550acb3e3f79e62721a506.tar.zst
astro-1e27992437aa0371b8550acb3e3f79e62721a506.zip
Update the Astro.cookies.set types to receive boolean and numbers (#5047)
Diffstat (limited to '')
-rw-r--r--.changeset/witty-sheep-wave.md7
-rw-r--r--packages/astro/src/core/cookies/cookies.ts2
-rw-r--r--packages/astro/test/units/cookies/set.test.js10
-rw-r--r--packages/integrations/deno/test/basics.test.ts17
-rw-r--r--packages/integrations/deno/test/fixtures/basics/src/pages/admin.astro8
-rw-r--r--packages/integrations/deno/test/fixtures/basics/src/pages/login.astro10
6 files changed, 53 insertions, 1 deletions
diff --git a/.changeset/witty-sheep-wave.md b/.changeset/witty-sheep-wave.md
new file mode 100644
index 000000000..cac7841e4
--- /dev/null
+++ b/.changeset/witty-sheep-wave.md
@@ -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.
diff --git a/packages/astro/src/core/cookies/cookies.ts b/packages/astro/src/core/cookies/cookies.ts
index 3345024d7..6a4adf788 100644
--- a/packages/astro/src/core/cookies/cookies.ts
+++ b/packages/astro/src/core/cookies/cookies.ts
@@ -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;
}
diff --git a/packages/astro/test/units/cookies/set.test.js b/packages/astro/test/units/cookies/set.test.js
index a70f2f457..0913bcc7d 100644
--- a/packages/astro/test/units/cookies/set.test.js
+++ b/packages/astro/test/units/cookies/set.test.js
@@ -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);
diff --git a/packages/integrations/deno/test/basics.test.ts b/packages/integrations/deno/test/basics.test.ts
index 9c55397c2..d1f8907cb 100644
--- a/packages/integrations/deno/test/basics.test.ts
+++ b/packages/integrations/deno/test/basics.test.ts
@@ -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,
+});
diff --git a/packages/integrations/deno/test/fixtures/basics/src/pages/admin.astro b/packages/integrations/deno/test/fixtures/basics/src/pages/admin.astro
new file mode 100644
index 000000000..8aa627394
--- /dev/null
+++ b/packages/integrations/deno/test/fixtures/basics/src/pages/admin.astro
@@ -0,0 +1,8 @@
+---
+Astro.cookies.set('logged-in', false, {
+ maxAge: 60 * 60 * 24 * 900,
+ path: '/'
+});
+
+return Astro.redirect('/login');
+---
diff --git a/packages/integrations/deno/test/fixtures/basics/src/pages/login.astro b/packages/integrations/deno/test/fixtures/basics/src/pages/login.astro
new file mode 100644
index 000000000..e06d49b85
--- /dev/null
+++ b/packages/integrations/deno/test/fixtures/basics/src/pages/login.astro
@@ -0,0 +1,10 @@
+---
+---
+<html>
+ <head>
+ <title>Testing</title>
+ </head>
+ <body>
+ <h1>Testing</h1>
+ </body>
+</html>