Skip to main content
Module

x/oak/mod.ts>Cookies

A middleware framework for handling HTTP with Deno 🐿️ 🦕
Extremely Popular
Go to Latest
class Cookies
import { Cookies } from "https://deno.land/x/oak@v10.6.0/mod.ts";

An interface which allows setting and accessing cookies related to both the current request and response. Each Context has a property .cookies which is an instance of this class.

Because oak supports automatic encryption, most methods (except .delete) are asynchronous. This is because oak leverages the Web Crypto APIs, which are asynchronous by nature.

Example

import { Application } from "https://deno.land/x/oak/mod.ts";

const app = new Application();

app.use(async (ctx) => {
  for await (const cookie of ctx.cookies) {
    // iterating over each cookie
  }
  await ctx.cookie.set("myCookie", "a value"); // setting or updating a cookie
  const id = await ctx.cookie.get("my-id"); // getting a value of a cookie if present
  ctx.cookie.delete();
});

Constructors

new
Cookies(
request: Request,
response: Response,
options?: CookiesOptions,
)

Methods

delete(name: string, options?: CookiesSetDeleteOptions): boolean

Set a cookie to be deleted in the response. This is a "shortcut" to .set(name, null, options?).

entries(): AsyncIterableIterator<[string, string]>

Iterate over the request's cookies, yielding up a tuple containing the key and the value.

If there are keys set on the application, only keys and values that are properly signed will be returned.

forEach(callback: (
key: string,
value: string,
cookies: this,
) => void
, thisArg?: any
): Promise<void>
get(name: string, options?: CookiesGetOptions): Promise<string | undefined>

Get the value of a cookie from the request.

If the cookie is signed, and the signature is invalid, the cookie will be set to be deleted in the the response. If the signature uses an "old" key, the cookie will be re-signed with the current key and be added to the response to be updated.

keys(): AsyncIterableIterator<string>

Iterate over the request's cookies, yielding up the keys.

If there are keys set on the application, only the keys that are properly signed will be returned.

set(
name: string,
value: string | null,
): Promise<this>

Set a cookie in the response.

If there are keys set in the application, cookies will be automatically signed, unless overridden by the set options. Cookies can be deleted by setting the value to null.

values(): AsyncIterableIterator<string>

Iterate over the request's cookies, yielding up each value.

If there are keys set on the application, only the values that are properly signed will be returned.

[Symbol.asyncIterator](): AsyncIterableIterator<[string, string]>

Iterate over the request's cookies, yielding up a tuple containing the key and the value.

If there are keys set on the application, only keys and values that are properly signed will be returned.

[Symbol.for("nodejs.util.inspect.custom")](
depth: number,
options: any,
inspect: (value: unknown, options?: unknown) => string,
)