function serializeCookie
Since 0.2.0
serializeCookie(
name: string,
value: string,
): string

Serializes a cookie into a Set-Cookie header value.

The value is percent-encoded, so a payload containing ;, ,, or whitespace can neither inject attributes nor split the header.

Examples

Example 1

serializeCookie('sid', 'abc', { path: '/', httpOnly: true, sameSite: 'lax', maxAge: 3600 });
// → 'sid=abc; Max-Age=3600; Path=/; HttpOnly; SameSite=Lax'

Parameters

name: string

Cookie name; must match RFC 6265's token production

value: string

Cookie value; percent-encoded on the way out

optional
attrs: CookieAttributes

Attributes to emit; omitted fields emit no attribute

Return Type

string

The Set-Cookie header value

Throws

TypeError

If name is not a valid cookie name, or maxAge is not an integer

Usage

import { serializeCookie } from "common/src/index.ts";