Skip to content

Commit 0b5f881

Browse files
authored
fix: deprecate parse, serialize exports for more useful functions (#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') ?? '') } ```
1 parent d01c628 commit 0b5f881

File tree

3 files changed

+107
-1
lines changed

3 files changed

+107
-1
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`helpers > parseCookieHeader > should parse a Cookie header 1`] = `[]`;
4+
5+
exports[`helpers > parseCookieHeader > should parse a Cookie header 2`] = `
6+
[
7+
{
8+
"name": "a",
9+
"value": "b",
10+
},
11+
{
12+
"name": "c",
13+
"value": " hello ",
14+
},
15+
{
16+
"name": "e",
17+
"value": "f",
18+
},
19+
]
20+
`;
21+
22+
exports[`helpers > serializeCookieHeader > should serialize a cookie to a Set-Cookie header 1`] = `"a=; Max-Age=123; Path=/; HttpOnly; Secure"`;
23+
24+
exports[`helpers > serializeCookieHeader > should serialize a cookie to a Set-Cookie header 2`] = `"b=%20weird%20value; Max-Age=345"`;

src/utils/helpers.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { describe, it, expect } from "vitest";
2+
3+
import {
4+
serialize,
5+
parse,
6+
// intentionally importing ^ to make sure they're exported, remove in v1.0.0
7+
parseCookieHeader,
8+
serializeCookieHeader,
9+
} from "./helpers";
10+
11+
describe("helpers", () => {
12+
describe("parseCookieHeader", () => {
13+
it("should parse a Cookie header", () => {
14+
expect(parseCookieHeader("")).toMatchSnapshot();
15+
expect(
16+
parseCookieHeader(`a=b;c=${encodeURIComponent(" hello ")};e=f`),
17+
).toMatchSnapshot();
18+
});
19+
});
20+
21+
describe("serializeCookieHeader", () => {
22+
it("should serialize a cookie to a Set-Cookie header", () => {
23+
expect(
24+
serializeCookieHeader("a", "", {
25+
path: "/",
26+
maxAge: 123,
27+
httpOnly: true,
28+
secure: true,
29+
}),
30+
).toMatchSnapshot();
31+
expect(
32+
serializeCookieHeader("b", " weird value", { maxAge: 345 }),
33+
).toMatchSnapshot();
34+
});
35+
});
36+
});

src/utils/helpers.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,50 @@
1-
export { parse, serialize } from "cookie";
1+
import type { CookieSerializeOptions } from "cookie";
2+
import { parse as cookieParse, serialize as cookieSerialize } from "cookie";
3+
4+
/**
5+
* @deprecated Since v0.4.0: Please use {@link parseCookieHeader}. `parse` will
6+
* not be available for import starting v1.0.0 of `@supabase/ssr`.
7+
*/
8+
export const parse = cookieParse;
9+
10+
/**
11+
* @deprecated Since v0.4.0: Please use {@link serializeCookieHeader}.
12+
* `serialize` will not be available for import starting v1.0.0 of
13+
* `@supabase/ssr`.
14+
*/
15+
export const serialize = cookieSerialize;
16+
17+
/**
18+
* Parses the `Cookie` HTTP header into an array of cookie name-value objects.
19+
*
20+
* @param header The `Cookie` HTTP header. Decodes cookie names and values from
21+
* URI encoding first.
22+
*/
23+
export function parseCookieHeader(
24+
header: string,
25+
): { name: string; value: string }[] {
26+
const parsed = cookieParse(header);
27+
28+
return Object.keys(parsed ?? {}).map((name) => ({
29+
name,
30+
value: parsed[name],
31+
}));
32+
}
33+
34+
/**
35+
* Converts the arguments to a valid `Set-Cookie` header. Non US-ASCII chars
36+
* and other forbidden cookie chars will be URI encoded.
37+
*
38+
* @param name Name of cookie.
39+
* @param value Value of cookie.
40+
*/
41+
export function serializeCookieHeader(
42+
name: string,
43+
value: string,
44+
options: CookieSerializeOptions,
45+
): string {
46+
return cookieSerialize(name, value, options);
47+
}
248

349
export function isBrowser() {
450
return (

0 commit comments

Comments
 (0)