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

fix: deprecate parse, serialize exports for more useful functions #14

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/utils/__snapshots__/helpers.spec.ts.snap
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"`;
36 changes: 36 additions & 0 deletions src/utils/helpers.spec.ts
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();
});
});
});
48 changes: 47 additions & 1 deletion src/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,50 @@
export { parse, serialize } from "cookie";
import type { CookieSerializeOptions } from "cookie";
import { parse as cookieParse, serialize as cookieSerialize } from "cookie";

/**
* @deprecated Since v0.4.0: Please use {@link parseCookieHeader}. `parse` will
* not be available for import starting v1.0.0 of `@supabase/ssr`.
*/
export const parse = cookieParse;

/**
* @deprecated Since v0.4.0: Please use {@link serializeCookieHeader}.
* `serialize` will not be available for import starting v1.0.0 of
* `@supabase/ssr`.
*/
export const serialize = cookieSerialize;

/**
* Parses the `Cookie` HTTP header into an array of cookie name-value objects.
*
* @param header The `Cookie` HTTP header. Decodes cookie names and values from
* URI encoding first.
*/
export function parseCookieHeader(
header: string,
): { name: string; value: string }[] {
const parsed = cookieParse(header);

return Object.keys(parsed ?? {}).map((name) => ({
name,
value: parsed[name],
}));
}

/**
* Converts the arguments to a valid `Set-Cookie` header. Non US-ASCII chars
* and other forbidden cookie chars will be URI encoded.
*
* @param name Name of cookie.
* @param value Value of cookie.
*/
export function serializeCookieHeader(
name: string,
value: string,
options: CookieSerializeOptions,
): string {
return cookieSerialize(name, value, options);
}

export function isBrowser() {
return (
Expand Down