or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.mdrequest-cookies.mdresponse-cookies.mdserialization.md
tile.json

index.mddocs/

Edge Runtime Cookies

Edge 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).

Package Information

  • Package Name: @edge-runtime/cookies
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @edge-runtime/cookies

Core Imports

import { 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");

Basic Usage

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
});

Architecture

Edge Runtime Cookies is built around several key components:

  • RequestCookies Class: Handles cookies from incoming requests (Cookie header)
  • ResponseCookies Class: Manages cookies for outgoing responses (Set-Cookie header)
  • Type System: Full TypeScript support with W3C-compliant cookie interfaces
  • Serialization Utilities: Functions for parsing and stringifying cookie values
  • Edge Runtime Compatibility: Works seamlessly in Vercel Edge Runtime and similar environments

Capabilities

Request Cookie Management

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;
}

Request Cookies

Response Cookie Management

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;
}

Response Cookies

Cookie Serialization

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[];

Cookie Serialization

Core Types

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';
};