summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.changeset/twenty-cheetahs-deny.md17
-rw-r--r--packages/astro/src/core/cookies/cookies.ts17
-rw-r--r--packages/astro/test/units/cookies/delete.test.js2
-rw-r--r--packages/astro/test/units/cookies/get.test.js14
4 files changed, 35 insertions, 15 deletions
diff --git a/.changeset/twenty-cheetahs-deny.md b/.changeset/twenty-cheetahs-deny.md
new file mode 100644
index 000000000..38a7298bf
--- /dev/null
+++ b/.changeset/twenty-cheetahs-deny.md
@@ -0,0 +1,17 @@
+---
+'astro': major
+---
+
+Astro.cookies.get(key) returns undefined if cookie doesn't exist
+
+With this change, Astro.cookies.get(key) no longer always returns a `AstroCookie` object. Instead it now returns `undefined` if the cookie does not exist.
+
+You should update your code if you assume that all calls to `get()` return a value. When using with `has()` you still need to assert the value, like so:
+
+```astro
+---
+if(Astro.cookies.has(id)) {
+ const id = Astro.cookies.get(id)!;
+}
+---
+```
diff --git a/packages/astro/src/core/cookies/cookies.ts b/packages/astro/src/core/cookies/cookies.ts
index 013357f32..0babd73f2 100644
--- a/packages/astro/src/core/cookies/cookies.ts
+++ b/packages/astro/src/core/cookies/cookies.ts
@@ -15,14 +15,14 @@ interface AstroCookieSetOptions {
type AstroCookieDeleteOptions = Pick<AstroCookieSetOptions, 'domain' | 'path'>;
interface AstroCookieInterface {
- value: string | undefined;
+ value: string;
json(): Record<string, any>;
number(): number;
boolean(): boolean;
}
interface AstroCookiesInterface {
- get(key: string): AstroCookieInterface;
+ get(key: string): AstroCookieInterface | undefined;
has(key: string): boolean;
set(
key: string,
@@ -37,7 +37,7 @@ const DELETED_VALUE = 'deleted';
const responseSentSymbol = Symbol.for('astro.responseSent');
class AstroCookie implements AstroCookieInterface {
- constructor(public value: string | undefined) {}
+ constructor(public value: string) {}
json() {
if (this.value === undefined) {
throw new Error(`Cannot convert undefined to an object.`);
@@ -97,20 +97,23 @@ class AstroCookies implements AstroCookiesInterface {
* @param key The cookie to get.
* @returns An object containing the cookie value as well as convenience methods for converting its value.
*/
- get(key: string): AstroCookie {
+ get(key: string): AstroCookie | undefined {
// Check for outgoing Set-Cookie values first
if (this.#outgoing?.has(key)) {
let [serializedValue, , isSetValue] = this.#outgoing.get(key)!;
if (isSetValue) {
return new AstroCookie(serializedValue);
} else {
- return new AstroCookie(undefined);
+ return undefined;
}
}
const values = this.#ensureParsed();
- const value = values[key];
- return new AstroCookie(value);
+ if(key in values) {
+ const value = values[key];
+ return new AstroCookie(value);
+ }
+
}
/**
diff --git a/packages/astro/test/units/cookies/delete.test.js b/packages/astro/test/units/cookies/delete.test.js
index 67fa1306b..f4c9fab53 100644
--- a/packages/astro/test/units/cookies/delete.test.js
+++ b/packages/astro/test/units/cookies/delete.test.js
@@ -30,7 +30,7 @@ describe('astro/src/core/cookies', () => {
expect(cookies.get('foo').value).to.equal('bar');
cookies.delete('foo');
- expect(cookies.get('foo').value).to.equal(undefined);
+ expect(cookies.get('foo')).to.equal(undefined);
});
it('calling cookies.has() after returns false', () => {
diff --git a/packages/astro/test/units/cookies/get.test.js b/packages/astro/test/units/cookies/get.test.js
index f044d715a..f79dd47be 100644
--- a/packages/astro/test/units/cookies/get.test.js
+++ b/packages/astro/test/units/cookies/get.test.js
@@ -16,6 +16,13 @@ describe('astro/src/core/cookies', () => {
expect(cookies.get('foo').value).to.equal('bar');
});
+ it('Returns undefined is the value doesn\'t exist', () => {
+ const req = new Request('http://example.com/');
+ let cookies = new AstroCookies(req);
+ let cookie = cookies.get('foo');
+ expect(cookie).to.equal(undefined);
+ });
+
describe('.json()', () => {
it('returns a JavaScript object', () => {
const req = new Request('http://example.com/', {
@@ -29,13 +36,6 @@ describe('astro/src/core/cookies', () => {
expect(json).to.be.an('object');
expect(json.key).to.equal('value');
});
-
- it('throws if the value is undefined', () => {
- const req = new Request('http://example.com/');
- let cookies = new AstroCookies(req);
- let cookie = cookies.get('foo');
- expect(() => cookie.json()).to.throw('Cannot convert undefined to an object.');
- });
});
describe('.number()', () => {