/servie-cookie-store

Cookie storage and manipulation for Servie

Primary LanguageTypeScriptOtherNOASSERTION

Servie Cookie Store

NPM version NPM downloads Build status Test coverage

Cookie storage and manipulation for Servie.

Installation

npm install servie-cookie-store --save

Usage

import { Cookie } from "servie-cookie-store";

Constructor

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

Methods

  • get(key) retrieves the cookie by key name, returning undefined when not found or invalid
  • set(res, key, data, options?) sets the cookie with optional cookie options
  • delete(res, key, options?) deletes the cookie by setting maxAge into past with optional cookie options
  • stringify(key, data, options?) creates a Set-Cookie header with optional cookie options
  • encode(value) stringifies a JavaScript value
  • decode(value) parses the cookie string into JavaScript

TypeScript

This project is written using TypeScript and publishes the definitions directly to NPM.

License

Apache 2.0