198 lines
4.1 KiB
TypeScript
198 lines
4.1 KiB
TypeScript
export type CookieSameSite = "Lax" | "Strict" | "None";
|
|
|
|
export type CookieSetOptions = {
|
|
days?: number;
|
|
expires?: Date;
|
|
maxAgeSeconds?: number;
|
|
|
|
path?: string;
|
|
domain?: string;
|
|
|
|
secure?: boolean;
|
|
sameSite?: CookieSameSite;
|
|
};
|
|
|
|
export type CookieRemoveOptions = Pick<
|
|
CookieSetOptions,
|
|
"path" | "domain" | "secure" | "sameSite"
|
|
>;
|
|
|
|
export type CookieStoreErrorContext<TKey extends string> = {
|
|
operation: "get" | "set" | "remove";
|
|
|
|
key: TKey;
|
|
error: unknown;
|
|
};
|
|
|
|
export type CreateCookieStoreOptions<TKey extends string> = {
|
|
defaults?: CookieSetOptions;
|
|
|
|
onError?: (context: CookieStoreErrorContext<TKey>) => void;
|
|
};
|
|
|
|
export type CookieStore<TKey extends string = string> = {
|
|
get: (key: TKey) => string | null;
|
|
|
|
set: (key: TKey, value: string, options?: CookieSetOptions) => void;
|
|
|
|
remove: (key: TKey, options?: CookieRemoveOptions) => void;
|
|
};
|
|
|
|
const isBrowser = (): boolean => {
|
|
return typeof document !== "undefined";
|
|
};
|
|
|
|
const appendCookieOption = (
|
|
parts: string[],
|
|
name: string,
|
|
value?: string,
|
|
): void => {
|
|
if (value === undefined) {
|
|
parts.push(name);
|
|
return;
|
|
}
|
|
|
|
parts.push(`${name}=${value}`);
|
|
};
|
|
|
|
const buildCookieString = (
|
|
key: string,
|
|
value: string,
|
|
options: CookieSetOptions,
|
|
remove: boolean,
|
|
): string => {
|
|
const parts = [`${encodeURIComponent(key)}=${encodeURIComponent(value)}`];
|
|
|
|
if (remove) {
|
|
appendCookieOption(parts, "Expires", new Date(0).toUTCString());
|
|
|
|
appendCookieOption(parts, "Max-Age", "0");
|
|
} else if (options.maxAgeSeconds !== undefined) {
|
|
appendCookieOption(parts, "Max-Age", String(options.maxAgeSeconds));
|
|
} else if (options.expires !== undefined) {
|
|
appendCookieOption(parts, "Expires", options.expires.toUTCString());
|
|
} else if (options.days !== undefined && Number.isFinite(options.days)) {
|
|
const expires = new Date();
|
|
|
|
expires.setDate(expires.getDate() + options.days);
|
|
|
|
appendCookieOption(parts, "Expires", expires.toUTCString());
|
|
}
|
|
|
|
if (options.path) {
|
|
appendCookieOption(parts, "Path", options.path);
|
|
}
|
|
|
|
if (options.domain) {
|
|
appendCookieOption(parts, "Domain", options.domain);
|
|
}
|
|
|
|
if (options.secure) {
|
|
appendCookieOption(parts, "Secure");
|
|
}
|
|
|
|
if (options.sameSite) {
|
|
appendCookieOption(parts, "SameSite", options.sameSite);
|
|
}
|
|
|
|
return parts.join("; ");
|
|
};
|
|
|
|
export const createCookieStore = <TKey extends string = string>(
|
|
options: CreateCookieStoreOptions<TKey> = {},
|
|
): CookieStore<TKey> => {
|
|
const defaultOptions = options.defaults ?? {};
|
|
|
|
const reportError = (
|
|
operation: CookieStoreErrorContext<TKey>["operation"],
|
|
key: TKey,
|
|
error: unknown,
|
|
): void => {
|
|
options.onError?.({
|
|
operation,
|
|
key,
|
|
error,
|
|
});
|
|
};
|
|
|
|
const get = (key: TKey): string | null => {
|
|
if (!isBrowser()) {
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
const encodedName = `${encodeURIComponent(key)}=`;
|
|
|
|
const cookies = document.cookie ? document.cookie.split(";") : [];
|
|
|
|
for (const rawCookie of cookies) {
|
|
const cookie = rawCookie.trim();
|
|
|
|
if (!cookie.startsWith(encodedName)) {
|
|
continue;
|
|
}
|
|
|
|
const encodedValue = cookie.slice(encodedName.length);
|
|
|
|
return decodeURIComponent(encodedValue);
|
|
}
|
|
|
|
return null;
|
|
} catch (error: unknown) {
|
|
reportError("get", key, error);
|
|
|
|
return null;
|
|
}
|
|
};
|
|
|
|
const set = (
|
|
key: TKey,
|
|
value: string,
|
|
setOptions: CookieSetOptions = {},
|
|
): void => {
|
|
if (!isBrowser()) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
document.cookie = buildCookieString(
|
|
key,
|
|
value,
|
|
{
|
|
...defaultOptions,
|
|
...setOptions,
|
|
},
|
|
false,
|
|
);
|
|
} catch (error: unknown) {
|
|
reportError("set", key, error);
|
|
}
|
|
};
|
|
|
|
const remove = (key: TKey, removeOptions: CookieRemoveOptions = {}): void => {
|
|
if (!isBrowser()) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
document.cookie = buildCookieString(
|
|
key,
|
|
"",
|
|
{
|
|
...defaultOptions,
|
|
...removeOptions,
|
|
},
|
|
true,
|
|
);
|
|
} catch (error: unknown) {
|
|
reportError("remove", key, error);
|
|
}
|
|
};
|
|
|
|
return Object.freeze({
|
|
get,
|
|
set,
|
|
remove,
|
|
});
|
|
};
|