Azion `Cookies` library

The Azion Cookies library provides utility functions to get and set cookies in an HTTP request and response. This library is useful for handling cookies in web applications, ensuring ease of use and consistency.

Go to Azion Libraries Overview

Usage

getCookie

Retrieves the value of a specific cookie from the HTTP request.

Parameters:

ParameterTypeDescription
reqRequestThe HTTP request object.
keystring (optional)The name of the cookie to retrieve. If not provided, it returns all cookies as an object.
prefixOptionsCookiePrefix (optional)The prefix for the cookie ('host' or 'secure').

Returns:

TypeDescription
string | undefined | Record<string, string>The cookie value, or an object of all cookies if no key is provided.

Example:

import { setCookie } from 'azion/cookies';
import type { CookieOptions } from 'azion/cookies';
const options: CookieOptions = { maxAge: 3600 };
const res = setCookie(response, 'my-cookie', 'cookie-value', options);

setCookie

Sets a cookie in the HTTP response.

Parameters:

ParameterTypeDescription
resResponseThe HTTP response object.
namestringThe name of the cookie.
valuestringThe value of the cookie.
optionsCookieOptions (optional)Additional options for setting the cookie.

The CookieOption type used for the options parameter is an object with the following properties:

OptionTypeDescription
maxAgenumberOptional. The maximum age of the cookie, in seconds.
expiresDateOptional. The expiration date of the cookie.
pathstringOptional. The path on the server for which the cookie will be sent.
domainstringOptional. The domain for which the cookie is valid.
securebooleanOptional. If true, the cookie will only be transmitted over secure HTTPS.
httpOnlybooleanOptional. If true, the cookie is inaccessible to JavaScript’s Document.cookie API.
sameSite'Strict' | 'Lax' | 'None'Optional. Controls how the cookie is sent with cross-site requests.

Example:

import { setCookie } from 'azion/cookies';
import type { CookieOptions } from 'azion/cookies';
const options: CookieOptions = { maxAge: 3600 };
const res = setCookie(response, 'my-cookie', 'cookie-value', options);

Contributors