Skip to content

Commit 59d6e56

Browse files
matthewpematipico
authored andcommitted
Make Astro.cookies.get(key) return undefined (#7888)
1 parent 61806a6 commit 59d6e56

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

.changeset/twenty-cheetahs-deny.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
'astro': major
3+
---
4+
5+
Astro.cookies.get(key) returns undefined if cookie doesn't exist
6+
7+
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.
8+
9+
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:
10+
11+
```astro
12+
---
13+
if(Astro.cookies.has(id)) {
14+
const id = Astro.cookies.get(id)!;
15+
}
16+
---
17+
```

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ interface AstroCookieSetOptions {
1515
type AstroCookieDeleteOptions = Pick<AstroCookieSetOptions, 'domain' | 'path'>;
1616

1717
interface AstroCookieInterface {
18-
value: string | undefined;
18+
value: string;
1919
json(): Record<string, any>;
2020
number(): number;
2121
boolean(): boolean;
2222
}
2323

2424
interface AstroCookiesInterface {
25-
get(key: string): AstroCookieInterface;
25+
get(key: string): AstroCookieInterface | undefined;
2626
has(key: string): boolean;
2727
set(
2828
key: string,
@@ -37,7 +37,7 @@ const DELETED_VALUE = 'deleted';
3737
const responseSentSymbol = Symbol.for('astro.responseSent');
3838

3939
class AstroCookie implements AstroCookieInterface {
40-
constructor(public value: string | undefined) {}
40+
constructor(public value: string) {}
4141
json() {
4242
if (this.value === undefined) {
4343
throw new Error(`Cannot convert undefined to an object.`);
@@ -97,20 +97,23 @@ class AstroCookies implements AstroCookiesInterface {
9797
* @param key The cookie to get.
9898
* @returns An object containing the cookie value as well as convenience methods for converting its value.
9999
*/
100-
get(key: string): AstroCookie {
100+
get(key: string): AstroCookie | undefined {
101101
// Check for outgoing Set-Cookie values first
102102
if (this.#outgoing?.has(key)) {
103103
let [serializedValue, , isSetValue] = this.#outgoing.get(key)!;
104104
if (isSetValue) {
105105
return new AstroCookie(serializedValue);
106106
} else {
107-
return new AstroCookie(undefined);
107+
return undefined;
108108
}
109109
}
110110

111111
const values = this.#ensureParsed();
112-
const value = values[key];
113-
return new AstroCookie(value);
112+
if(key in values) {
113+
const value = values[key];
114+
return new AstroCookie(value);
115+
}
116+
114117
}
115118

116119
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('astro/src/core/cookies', () => {
3030
expect(cookies.get('foo').value).to.equal('bar');
3131

3232
cookies.delete('foo');
33-
expect(cookies.get('foo').value).to.equal(undefined);
33+
expect(cookies.get('foo')).to.equal(undefined);
3434
});
3535

3636
it('calling cookies.has() after returns false', () => {

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ describe('astro/src/core/cookies', () => {
1616
expect(cookies.get('foo').value).to.equal('bar');
1717
});
1818

19+
it('Returns undefined is the value doesn\'t exist', () => {
20+
const req = new Request('http://example.com/');
21+
let cookies = new AstroCookies(req);
22+
let cookie = cookies.get('foo');
23+
expect(cookie).to.equal(undefined);
24+
});
25+
1926
describe('.json()', () => {
2027
it('returns a JavaScript object', () => {
2128
const req = new Request('http://example.com/', {
@@ -29,13 +36,6 @@ describe('astro/src/core/cookies', () => {
2936
expect(json).to.be.an('object');
3037
expect(json.key).to.equal('value');
3138
});
32-
33-
it('throws if the value is undefined', () => {
34-
const req = new Request('http://example.com/');
35-
let cookies = new AstroCookies(req);
36-
let cookie = cookies.get('foo');
37-
expect(() => cookie.json()).to.throw('Cannot convert undefined to an object.');
38-
});
3939
});
4040

4141
describe('.number()', () => {

0 commit comments

Comments
 (0)