V0.1.0
📦 Features
serializeCookie
function to format a string representing a cookie, useful for setting cookies in a HTTP Set-Cookie headerparseCookies
function to parse cookies from a string separated with semicolonCookieMaxAge
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" }]