Cookie helpers compatible with Edge Runtime
npx @tessl/cli install tessl/npm-edge-runtime--cookies@6.0.0Edge Runtime Cookies provides cookie helper utilities specifically designed for Edge Runtime compatibility. It offers comprehensive tools for managing HTTP cookies in request and response contexts, with full TypeScript support and W3C Cookie Store API compliance (without Promises).
npm install @edge-runtime/cookiesimport { RequestCookies, ResponseCookies, stringifyCookie, parseCookie, parseSetCookie, splitCookiesString } from "@edge-runtime/cookies";
import type { CookieListItem, RequestCookie, ResponseCookie } from "@edge-runtime/cookies";For CommonJS:
const { RequestCookies, ResponseCookies, stringifyCookie, parseCookie, parseSetCookie, splitCookiesString } = require("@edge-runtime/cookies");import { RequestCookies, ResponseCookies } from "@edge-runtime/cookies";
// Working with request cookies
const request = new Request("https://example.com", {
headers: { "Cookie": "session=abc123; theme=dark" }
});
const requestCookies = new RequestCookies(request.headers);
const sessionCookie = requestCookies.get("session");
console.log(sessionCookie?.value); // "abc123"
// Working with response cookies
const response = new Response("Hello");
const responseCookies = new ResponseCookies(response.headers);
responseCookies.set("user", "john", {
httpOnly: true,
secure: true,
maxAge: 86400
});Edge Runtime Cookies is built around several key components:
Provides tools for reading and manipulating cookies from incoming HTTP requests. The RequestCookies class offers a comprehensive API for accessing, modifying, and managing cookies sent by clients.
class RequestCookies {
constructor(requestHeaders: Headers);
get size(): number;
get(...args: [name: string] | [RequestCookie]): RequestCookie | undefined;
getAll(...args: [name: string] | [RequestCookie] | []): RequestCookie[];
has(name: string): boolean;
set(...args: [key: string, value: string] | [options: RequestCookie]): this;
delete(names: string | string[]): boolean | boolean[];
clear(): this;
toString(): string;
[Symbol.iterator](): Iterator<[string, RequestCookie]>;
[Symbol.for('edge-runtime.inspect.custom')](): string;
}Handles cookies for HTTP responses with full support for cookie attributes like HttpOnly, Secure, SameSite, and more. Based on the W3C Cookie Store API but without Promises for Edge Runtime compatibility.
class ResponseCookies {
constructor(responseHeaders: Headers);
get(...args: [key: string] | [options: ResponseCookie]): ResponseCookie | undefined;
getAll(...args: [key: string] | [options: ResponseCookie] | []): ResponseCookie[];
has(name: string): boolean;
set(...args: [key: string, value: string, cookie?: Partial<ResponseCookie>] | [options: ResponseCookie]): this;
delete(...args: [key: string] | [options: Omit<ResponseCookie, 'value' | 'expires'>]): this;
toString(): string;
[Symbol.for('edge-runtime.inspect.custom')](): string;
}Utility functions for parsing Cookie and Set-Cookie headers, and serializing cookie objects to strings. These functions handle URL encoding/decoding and proper attribute formatting.
function stringifyCookie(c: ResponseCookie | RequestCookie): string;
function parseCookie(cookie: string): Map<string, string>;
function parseSetCookie(setCookie: string): undefined | ResponseCookie;
function splitCookiesString(cookiesString: string): string[];interface CookieListItem {
name: string;
value: string;
expires?: number | Date;
domain?: string;
path?: string;
secure?: boolean;
sameSite?: 'strict' | 'lax' | 'none';
partitioned?: boolean;
}
type RequestCookie = Pick<CookieListItem, 'name' | 'value'>;
type ResponseCookie = CookieListItem & {
httpOnly?: boolean;
maxAge?: number;
priority?: 'low' | 'medium' | 'high';
};