-
Notifications
You must be signed in to change notification settings - Fork 84
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): align RequestCookies
and ResponseCookies
#181
Merged
Schniz
merged 10 commits into
vercel:main
from
balazsorban44:feat/align-request-response-cookie-interfaces
Oct 23, 2022
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
098b0ea
extract types into its own module
balazsorban44 ff0a651
change constructor to only take `Headers`
balazsorban44 c91d41e
remove non-spec `getValue` method
balazsorban44 a3397e5
fix tests
balazsorban44 d5c18ed
add changeset
balazsorban44 a417854
Apply suggestions from code review
balazsorban44 07df6bb
make `.clear()` work in `ResposneCookies`
balazsorban44 bf950d2
Merge branch 'feat/align-request-response-cookie-interfaces' of githu…
balazsorban44 d7ec963
lowercase in `parseSameSite`
balazsorban44 e1d71d0
remove .clear from ResponseCookies
balazsorban44 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@edge-runtime/cookies': major | ||
--- | ||
|
||
Align `RequestCookies` and `ResponseCookies` APIs as much as possible with [CookieStore](https://developer.mozilla.org/en-US/docs/Web/API/CookieStore) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export type { Cookie, CookieListItem } from './serialize' | ||
export { ResponseCookies } from './response-cookies' | ||
export type { CookieListItem, RequestCookie, ResponseCookie } from './types' | ||
export { RequestCookies } from './request-cookies' | ||
export { ResponseCookies } from './response-cookies' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,23 @@ | ||
import type { ResponseCookie } from './types' | ||
import { cached } from './cached' | ||
import { type Cookie, parseSetCookieString, serialize } from './serialize' | ||
|
||
export type CookieBag = Map<string, Cookie> | ||
import { parseSetCookieString, serialize } from './serialize' | ||
|
||
/** | ||
* A class for manipulating {@link Response} cookies (`Set-Cookie` header). | ||
* Loose implementation of the experimental [Cookie Store API](https://wicg.github.io/cookie-store/#dictdef-cookie) | ||
* The main difference is `ResponseCookies` methods do not return a Promise. | ||
*/ | ||
export class ResponseCookies { | ||
readonly #headers: Headers | ||
|
||
constructor(response: Response) { | ||
this.#headers = response.headers | ||
constructor(responseHeaders: Headers) { | ||
this.#headers = responseHeaders | ||
Comment on lines
+13
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as RequestCookies |
||
} | ||
|
||
#cache = cached(() => { | ||
// @ts-expect-error See https://github.com/whatwg/fetch/issues/973 | ||
const headers = this.#headers.getAll('set-cookie') | ||
const map = new Map<string, Cookie>() | ||
const map = new Map<string, ResponseCookie>() | ||
|
||
for (const header of headers) { | ||
const parsed = parseSetCookieString(header) | ||
|
@@ -37,14 +37,18 @@ export class ResponseCookies { | |
/** | ||
* {@link https://wicg.github.io/cookie-store/#CookieStore-get CookieStore#get} without the Promise. | ||
*/ | ||
get(...args: [key: string] | [options: Cookie]): Cookie | undefined { | ||
get( | ||
...args: [key: string] | [options: ResponseCookie] | ||
): ResponseCookie | undefined { | ||
const key = typeof args[0] === 'string' ? args[0] : args[0].name | ||
return this.#parsed().get(key) | ||
} | ||
/** | ||
* {@link https://wicg.github.io/cookie-store/#CookieStore-getAll CookieStore#getAll} without the Promise. | ||
*/ | ||
getAll(...args: [key: string] | [options: Cookie] | [undefined]): Cookie[] { | ||
getAll( | ||
...args: [key: string] | [options: ResponseCookie] | [] | ||
): ResponseCookie[] { | ||
const all = Array.from(this.#parsed().values()) | ||
if (!args.length) { | ||
return all | ||
|
@@ -59,8 +63,8 @@ export class ResponseCookies { | |
*/ | ||
set( | ||
...args: | ||
| [key: string, value: string, cookie?: Partial<Cookie>] | ||
| [options: Cookie] | ||
| [key: string, value: string, cookie?: Partial<ResponseCookie>] | ||
| [options: ResponseCookie] | ||
): this { | ||
const [name, value, cookie] = | ||
args.length === 1 ? [args[0].name, args[0].value, args[0]] : args | ||
|
@@ -74,46 +78,27 @@ export class ResponseCookies { | |
/** | ||
* {@link https://wicg.github.io/cookie-store/#CookieStore-delete CookieStore#delete} without the Promise. | ||
*/ | ||
delete(...args: [key: string] | [options: Cookie]): this { | ||
delete(...args: [key: string] | [options: ResponseCookie]): this { | ||
const name = typeof args[0] === 'string' ? args[0] : args[0].name | ||
return this.set({ name, value: '', expires: new Date(0) }) | ||
} | ||
|
||
// Non-spec | ||
|
||
/** | ||
* Uses {@link ResponseCookies.get} to return only the cookie value. | ||
*/ | ||
getValue(...args: [key: string] | [options: Cookie]): string | undefined { | ||
return this.get(...args)?.value | ||
} | ||
|
||
/** | ||
* Uses {@link ResponseCookies.delete} to invalidate all cookies matching the given name. | ||
* If no name is provided, all cookies are invalidated. | ||
*/ | ||
clear(...args: [key: string] | [options: Cookie] | [undefined]): this { | ||
const key = typeof args[0] === 'string' ? args[0] : args[0]?.name | ||
this.getAll(key).forEach((c) => this.delete(c)) | ||
return this | ||
} | ||
|
||
[Symbol.for('edge-runtime.inspect.custom')]() { | ||
return `ResponseCookies ${JSON.stringify( | ||
Object.fromEntries(this.#parsed()) | ||
)}` | ||
} | ||
} | ||
|
||
function replace(bag: CookieBag, headers: Headers) { | ||
function replace(bag: Map<string, ResponseCookie>, headers: Headers) { | ||
headers.delete('set-cookie') | ||
for (const [, value] of bag) { | ||
const serialized = serialize(value) | ||
headers.append('set-cookie', serialized) | ||
} | ||
} | ||
|
||
function normalizeCookie(cookie: Cookie = { name: '', value: '' }) { | ||
function normalizeCookie(cookie: ResponseCookie = { name: '', value: '' }) { | ||
if (cookie.maxAge) { | ||
cookie.expires = new Date(Date.now() + cookie.maxAge * 1000) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { CookieSerializeOptions } from 'cookie' | ||
|
||
/** | ||
* {@link https://wicg.github.io/cookie-store/#dictdef-cookielistitem CookieListItem} | ||
* as specified by W3C. | ||
*/ | ||
export interface CookieListItem | ||
extends Pick< | ||
CookieSerializeOptions, | ||
'domain' | 'path' | 'expires' | 'secure' | 'sameSite' | ||
> { | ||
/** A string with the name of a cookie. */ | ||
name: string | ||
/** A string containing the value of the cookie. */ | ||
value: string | ||
} | ||
|
||
/** | ||
* Superset of {@link CookieListItem} extending it with | ||
* the `httpOnly`, `maxAge` and `priority` properties. | ||
*/ | ||
export type ResponseCookie = CookieListItem & | ||
Pick<CookieSerializeOptions, 'httpOnly' | 'maxAge' | 'priority'> | ||
|
||
/** | ||
* Subset of {@link CookieListItem}, only containing `name` and `value` | ||
* since other cookie attributes aren't be available on a `Request`. | ||
*/ | ||
export type RequestCookie = Pick<CookieListItem, 'name' | 'value'> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used that type to hint that we need a request here and not a response
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new RequestCookies(request)
looks nicer thannew RequestCookies(request.headers)
, the latter feels kinda leaky to me. wdyt?