-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…#14) `parse`, `serialize` from `cookie` were exported as-is. That's not super useful for importing outside of the library, so they're deprecated and in their stead adding `parseCookieHeader`, `serializeCookieHeader` which works with the `getAll`, `setAll` cookie methods more ergonomically. For example, instead of: ```typescript getAll() { const parsed = parse(request.headers.get('Cookie') ?? '') return Object.keys(parsed).map((name) => ({ name, value: parsed[name] })) } ``` You can just use: ```typescript getAll() { return parseCookieHeader(req.headers.get('Cookie') ?? '') } ```
- Loading branch information
Showing
3 changed files
with
107 additions
and
1 deletion.
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,24 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`helpers > parseCookieHeader > should parse a Cookie header 1`] = `[]`; | ||
|
||
exports[`helpers > parseCookieHeader > should parse a Cookie header 2`] = ` | ||
[ | ||
{ | ||
"name": "a", | ||
"value": "b", | ||
}, | ||
{ | ||
"name": "c", | ||
"value": " hello ", | ||
}, | ||
{ | ||
"name": "e", | ||
"value": "f", | ||
}, | ||
] | ||
`; | ||
|
||
exports[`helpers > serializeCookieHeader > should serialize a cookie to a Set-Cookie header 1`] = `"a=; Max-Age=123; Path=/; HttpOnly; Secure"`; | ||
|
||
exports[`helpers > serializeCookieHeader > should serialize a cookie to a Set-Cookie header 2`] = `"b=%20weird%20value; Max-Age=345"`; |
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,36 @@ | ||
import { describe, it, expect } from "vitest"; | ||
|
||
import { | ||
serialize, | ||
parse, | ||
// intentionally importing ^ to make sure they're exported, remove in v1.0.0 | ||
parseCookieHeader, | ||
serializeCookieHeader, | ||
} from "./helpers"; | ||
|
||
describe("helpers", () => { | ||
describe("parseCookieHeader", () => { | ||
it("should parse a Cookie header", () => { | ||
expect(parseCookieHeader("")).toMatchSnapshot(); | ||
expect( | ||
parseCookieHeader(`a=b;c=${encodeURIComponent(" hello ")};e=f`), | ||
).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe("serializeCookieHeader", () => { | ||
it("should serialize a cookie to a Set-Cookie header", () => { | ||
expect( | ||
serializeCookieHeader("a", "", { | ||
path: "/", | ||
maxAge: 123, | ||
httpOnly: true, | ||
secure: true, | ||
}), | ||
).toMatchSnapshot(); | ||
expect( | ||
serializeCookieHeader("b", " weird value", { maxAge: 345 }), | ||
).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); |
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