Skip to content

Commit

Permalink
feat: add cookies.getAll (#9287)
Browse files Browse the repository at this point in the history
* implement `cookies.getAll`

* check domain and path

* oops
  • Loading branch information
msonnberger authored Mar 2, 2023
1 parent 2d8c63a commit 2b647fd
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/nervous-beans-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': minor
---

feat: add `cookies.getAll`
19 changes: 19 additions & 0 deletions packages/kit/src/runtime/server/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,25 @@ export function get_cookies(request, url, trailing_slash) {
}
},

/**
* @param {import('cookie').CookieParseOptions} opts
*/
getAll(opts) {
const decoder = opts?.decode || decodeURIComponent;
const cookies = parse(header, { decode: decoder });

for (const c of Object.values(new_cookies)) {
if (
domain_matches(url.hostname, c.options.domain) &&
path_matches(url.pathname, c.options.path)
) {
cookies[c.name] = c.value;
}
}

return Object.entries(cookies).map(([name, value]) => ({ name, value }));
},

/**
* @param {string} name
* @param {string} value
Expand Down
14 changes: 14 additions & 0 deletions packages/kit/src/runtime/server/cookie.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,18 @@ test('warns if cookie exceeds 4,129 bytes', () => {
globalThis.__SVELTEKIT_DEV__ = false;
});

test('get all cookies from header and set calls', () => {
const { cookies } = cookies_setup();
assert.equal(cookies.getAll(), [{ name: 'a', value: 'b' }]);

cookies.set('a', 'foo');
cookies.set('a', 'bar');
cookies.set('b', 'baz');

assert.equal(cookies.getAll(), [
{ name: 'a', value: 'bar' },
{ name: 'b', value: 'baz' }
]);
});

test.run();
8 changes: 7 additions & 1 deletion packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,13 @@ export interface Cookies {
get(name: string, opts?: import('cookie').CookieParseOptions): string | undefined;

/**
* Sets a cookie. This will add a `set-cookie` header to the response, but also make the cookie available via `cookies.get` during the current request.
* Gets all cookies that were previously set with `cookies.set`, or from the request headers.
* @param opts the options, passed directily to `cookie.parse`. See documentation [here](https://github.com/jshttp/cookie#cookieparsestr-options)
*/
getAll(opts?: import('cookie').CookieParseOptions): Array<{ name: string; value: string }>;

/**
* Sets a cookie. This will add a `set-cookie` header to the response, but also make the cookie available via `cookies.get` or `cookies.getAll` during the current request.
*
* The `httpOnly` and `secure` options are `true` by default (except on http://localhost, where `secure` is `false`), and must be explicitly disabled if you want cookies to be readable by client-side JavaScript and/or transmitted over HTTP. The `sameSite` option defaults to `lax`.
*
Expand Down

0 comments on commit 2b647fd

Please sign in to comment.