Skip to content

Commit

Permalink
Make Astro.cookies.get(key) return undefined (#7888)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp authored and ematipico committed Aug 3, 2023
1 parent 61806a6 commit 59d6e56
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
17 changes: 17 additions & 0 deletions .changeset/twenty-cheetahs-deny.md
Original file line number Diff line number Diff line change
@@ -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)!;
}
---
```
17 changes: 10 additions & 7 deletions packages/astro/src/core/cookies/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.`);
Expand Down Expand Up @@ -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);
}

}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/test/units/cookies/delete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
14 changes: 7 additions & 7 deletions packages/astro/test/units/cookies/get.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/', {
Expand All @@ -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()', () => {
Expand Down

0 comments on commit 59d6e56

Please sign in to comment.