feat: add cookies.setSerialized to create a cookie from a string - #13681
feat: add cookies.setSerialized to create a cookie from a string#13681while1618 wants to merge 17 commits into
cookies.setSerialized to create a cookie from a string#13681Conversation
|
cookies.setFromString to create a cookie from a string
🦋 Changeset detectedLatest commit: 449c33d The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
I edited the PR description. If you say "fixes" or "closes" followed by the issue number then GitHub will link the issue and PR |
|
The thrown errors for validation make me a bit wary of whether want to add this - feels a bit safer to parse the cookie from a string, then set it - that way you're forced to handle this yourself and TypeScript can help you. |
|
@dummdidumm it may be my lack of typescript expertise speaking here, but how would typescript help? |
|
Hey @benmccann, your suggestions make sense to me. Should I just commit them, or should we wait for another maintainer? |
|
I don't think there's much harm in adding my suggestions, but we'll need agreement for a larger group of maintainers before deciding whether to accept the PR |
|
Okay, I'll commit them. And what is your approach for PRs to be merged? Are we just waiting for enough maintainers to see this or you guys have some email/tag if opinion of others is required? |
Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
cookies.setFromString to create a cookie from a stringcookies.setSerialized to create a cookie from a string
|
Hi @Rich-Harris, this PR has been opened for a while now, can you please take a look, and let me know if you agree with this approach, or you want me to close the PR? Thanks |
|
To be honest I don't think this is the right API, since SvelteKit insists on having a So instead of this... const response = await fetch('...');
// oops, errors, no `Path=/` in the header
cookies.setSerialized(response.headers.get('set-cookie'));...something like this? const response = await fetch('...');
const { name, value, ...options } = cookies.parse(response.headers.get('set-cookie'));
cookies.set(name, value, { ...options, path: '/' });Here, Thoughts? |
|
Thanks for the response @Rich-Harris. So, if I understand you correctly, your suggestion is to just expose That sounds fine with me, but the one concern I have is this method Also, if |
It probably needs to return an array. So it would look more like: const response = await fetch('...');
const new_cookies = cookies.parse(response.headers.get('set-cookie'));
new_cookies.map(({ name, value, options }) => {
cookies.set(name, value, { ...options, path: '/', secure: options.secure ?? false, httpOnly: options.httpOnly ?? false });
});
It's probably better that the user handles this themselves as shown in the example above. We'll want to keep the defaults secure. |
| * | ||
| * If you do not specify name, value and path, it will throw an error. | ||
| * @param cookie the serialized cookie | ||
| * @since 2.21.0 |
There was a problem hiding this comment.
We'll need to update this to the next minor version
| const { cookies, new_cookies } = cookies_setup(); | ||
| const cookie_string = 'a=b; Path=/;'; | ||
| cookies.setSerialized(cookie_string); | ||
| const opts = new_cookies['a']?.options; |
Ah right. I think we probably want to discourage So usage would be more like this: const response = await fetch('...');
for (const str of response.headers.getSetCookie()) {
const { name, value, ...options } = cookies.parse(str);
cookies.set(name, value, { ...options, path: '/' });
} |
closes #13680 closes #13681 closes #8564 Adds a `cookies.parse` method for dealing with cookie headers from external sources: ```js const response = await fetch('...'); for (const str of response.headers.getSetCookie()) { const { name, value, ...options } = cookies.parse(str); cookies.set(name, value, { ...options, path: '/' }); } ``` Design decisions that might warrant discussion: - invalid values are ignored. If you do `SameSite=Nope` instead of SameSite=None`, nothing will happen. Maybe it should throw instead? Or maybe it should just apply the value even if it's gibberish, to make it future-proof? - same for invalid properties — it only recognises `Expires`, `Max-Age`, `Secure`, `HttpOnly`, `Partitioned`, `Priority`, `SameSite`, `Domain` and `Path` --- ### Please don't delete this checklist! Before submitting the PR, please make sure you do the following: - [x] It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs - [x] This message body should clearly illustrate what problems it solves. - [x] Ideally, include a test that fails without this PR but passes with it. ### Tests - [x] Run the tests with `pnpm test` and lint the project with `pnpm lint` and `pnpm check` ### Changesets - [x] If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running `pnpm changeset` and following the prompts. Changesets that add features should be `minor` and those that fix bugs should be `patch`. Please prefix changeset messages with `feat:`, `fix:`, or `chore:`. ### Edits - [x] Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed. --------- Co-authored-by: vercel[bot] <35613825+vercel[bot]@users.noreply.github.com> Co-authored-by: Vercel <vercel[bot]@users.noreply.github.com>
|
closing in favour of #16203 — thanks |
closes #13680
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm testand lint the project withpnpm lintandpnpm checkChangesets
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.Edits