Skip to content

V0.1.0

Compare
Choose a tag to compare
@Bluzzi Bluzzi released this 23 Apr 05:11
· 84 commits to main since this release

📦 Features

  • serializeCookie function to format a string representing a cookie, useful for setting cookies in a HTTP Set-Cookie header
  • parseCookies function to parse cookies from a string separated with semicolon
  • CookieMaxAge enum with the most common expiration times

🛠️ Installation

Install the package using one of the following package managers:

  • npm: npm install cookie-muncher
  • yarn: yarn add cookie-muncher
  • pnpm: pnpm add cookie-muncher

🚀 Example Usage

import type { Cookie } from "cookie-muncher";
import { serializeCookie, parseCookies, CookieMaxAge } from "cookie-muncher";

// Serialize:
const cookie: Cookie = {
  name: "myCookie",
  value: "myValue",
};

const serializedCookie = serializeCookie(cookie, { maxAge: CookieMaxAge.OneMonth });

console.log(serializedCookie); // "myCookie=myValue; Max-Age=3600"

// Parse:
const cookieString = "myCookie=myValue; myOtherCookie=myOtherValue";
const parsedCookies = parseCookies(cookieString);

console.log(parsedCookies); // [{ name: "myCookie", value: "myValue" }, { name: "myOtherCookie", value: "myOtherValue" }]