Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cookies): make RequestCookies and ResponseCookies more spec compliant #177

Merged

Conversation

balazsorban44
Copy link
Member

@balazsorban44 balazsorban44 commented Oct 20, 2022

Changes based on the conversation in this Slack thread.

Spec: https://wicg.github.io/cookie-store

Notable breaking changes:

  • ResponseCookies#get has been renamed to ResponseCookies#getValue
  • ResponseCookies#getWithOptions has been renamed to ResponseCookies#get

@changeset-bot
Copy link

changeset-bot bot commented Oct 20, 2022

🦋 Changeset detected

Latest commit: b471e97

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@edge-runtime/cookies Major

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

@vercel
Copy link

vercel bot commented Oct 20, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
edge-runtime ✅ Ready (Inspect) Visit Preview Oct 21, 2022 at 3:08PM (UTC)

@codecov-commenter
Copy link

Codecov Report

Merging #177 (542f476) into main (94ad6e1) will decrease coverage by 2.94%.
The diff coverage is 84.31%.

❗ Current head 542f476 differs from pull request most recent head b16a7f7. Consider uploading reports for the commit b16a7f7 to get more accurate results

@@            Coverage Diff             @@
##             main     #177      +/-   ##
==========================================
- Coverage   87.91%   84.96%   -2.95%     
==========================================
  Files          14       21       +7     
  Lines        1704     1803      +99     
  Branches      213      181      -32     
==========================================
+ Hits         1498     1532      +34     
- Misses        192      251      +59     
- Partials       14       20       +6     
Impacted Files Coverage Δ
packages/runtime/src/edge-runtime.ts 94.11% <66.66%> (-2.92%) ⬇️
packages/cookies/src/response-cookies.ts 71.42% <71.42%> (ø)
packages/runtime/src/server/body-streams.ts 38.13% <72.41%> (-24.51%) ⬇️
packages/jest-environment/src/index.ts 97.95% <75.00%> (-2.05%) ⬇️
packages/cookies/src/request-cookies.ts 77.35% <77.35%> (ø)
packages/runtime/src/server/create-handler.ts 86.02% <79.16%> (-8.28%) ⬇️
packages/cookies/src/serialize.ts 94.73% <94.73%> (ø)
packages/user-agent/src/index.ts 97.72% <97.72%> (ø)
jest.setup.js 100.00% <100.00%> (ø)
packages/cookies/src/cached.ts 100.00% <100.00%> (ø)
... and 14 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

* Uses {@link ResponseCookies.get} to return only the cookie value.
*/
getValue(...args: [key: string] | [options: Cookie]): string | undefined {
return this.get(...args)?.value
Copy link
Member Author

@balazsorban44 balazsorban44 Oct 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arguably, this method should be unnecessary:
.getValue("cookie") is the same as
.get("cookie")?.value

Breaking spec to save 2 characters might not be worth it.

@@ -1,3 +1,3 @@
export type { Options } from './serialize'
export type { Cookie, CookieListItem } from './serialize'
Copy link
Member

@Kikobeats Kikobeats Oct 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CookieOptions looks more natural to me, although I can see this is a thing defined by the spec interface

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's essentially the entire parsed cookie, with name and value. Only wording, so I am OK with changing it, but in my mind Cookie made more sense.

}
getAll(...args: [name: string] | [Cookie] | [undefined]) {
const all = Array.from(this.#parsed())
if (!args.length) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a strict comparison could be desirable

args.length === 0

Copy link
Member Author

@balazsorban44 balazsorban44 Oct 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of TypeScript, this would show:

This condition will always return 'false' since the types '1' and '0' have no overlap.ts(2367)

Since we technically don't allow more than one argument, but the user might/will try. !args.length should be as safe, since args is always going to be an array.

Copy link
Member

@Kikobeats Kikobeats left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super great PR! just added some suggestion to help maintain this code for the future.

@Kikobeats Kikobeats merged commit 2c20094 into vercel:main Oct 21, 2022
@balazsorban44 balazsorban44 deleted the feat/response-cookies-to-cookiestore branch October 22, 2022 10:52
import { cached } from './cached'

/**
* A class for manipulating {@link Request} cookies.
*/
export class RequestCookies {
private readonly headers: Headers
readonly #headers: Headers
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the future is here and now

@Kikobeats Kikobeats mentioned this pull request Oct 24, 2022
ijjk pushed a commit to vercel/next.js that referenced this pull request Oct 27, 2022
…and `ResponseCookies` (#41526)

Ref: [Slack
thread](https://vercel.slack.com/archives/C035J346QQL/p1666056382299069?thread_ts=1666041444.633059&cid=C035J346QQL),
[docs update](vercel/front#17090)

Spec: https://wicg.github.io/cookie-store/

BREAKING CHANGE:

Ref: vercel/edge-runtime#177,
vercel/edge-runtime#181

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
jridgewell pushed a commit to jridgewell/edge-runtime that referenced this pull request Jun 23, 2023
…compliant (vercel#177)

* feat(cookies): make `RequestCookies` and `ResponseCookies` more spec compliant

* add changeset

* mention breaking changes

* test `get()?.value`

* address review
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants