Cookie storage and manipulation for Servie.
npm install servie-cookie-store --save
import { Cookie } from "servie-cookie-store";
The Cookie
constructor accepts the Servie Request
and Response
objects. It optionally accepts a keys object for signing the cookie payloads. This object MUST have encode(data)
and decode(data)
methods, compatible with keysign and keycrypt. You SHOULD sign cookies when you want to avoid client-side tampering of requests.
const keys = new Keysign(["SEKRIT2", "SEKRIT1"]);
function handler(req) {
const cookie = new Cookie(req, keys);
const session = cookie.get("session");
const res = new Response();
cookie.set(res, "session", Object.assign({}, session, { demo: true }));
}
get(key)
retrieves the cookie bykey
name, returningundefined
when not found or invalidset(res, key, data, options?)
sets the cookie with optional cookie optionsdelete(res, key, options?)
deletes the cookie by settingmaxAge
into past with optional cookie optionsstringify(key, data, options?)
creates aSet-Cookie
header with optional cookie optionsencode(value)
stringifies a JavaScript valuedecode(value)
parses the cookie string into JavaScript
This project is written using TypeScript and publishes the definitions directly to NPM.
Apache 2.0