Skip to content

Releases: Bluzzi/cookie-muncher

V0.4.0

25 Apr 20:55
Compare
Choose a tag to compare

🚀 What's New

  • The documentation has been greatly improved, with lots of references to RFC 6265 specifications and relevant details
  • The function domCookie.setCookie() now checks if there are already more than 50 cookies and if the cookie is longer than 4096 bytes (see the limitations of RFC 6265)
  • The domCookie.removeCookie() function now takes an options argument to be able to remove cookies on specific path or domain
  • An error will now be thrown if navigator.cookieEnabled is false when using the domCookie functions
  • The serialization of a cookie now encodes (with encodeURIComponent()) its value as well as the path and domain parameters if they are specified

V0.3.0

24 Apr 18:31
Compare
Choose a tag to compare

📦 Changes

Separate CookieOptions into two distinct types (DomCookieOptions and HttpCookieOptions) because the HttpOnly parameter is not available on DOM side.

V0.2.0

24 Apr 11:29
Compare
Choose a tag to compare

🚀 What's New

  • domCookie for manage cookies from browser DOM
    • domCookie.set(cookie: Cookie, options?: CookieOptions): void
    • domCookie.get(name: string): Cookie | null
    • domCookie.getAll(): Cookie[]
    • domCookie.remove(name: string): void

📦 Changes

  • serializeCookie(...) function is now httpCookie.serialize(...)
  • parseCookies(...) function is now httpCookie.parse(...)
  • Path default value is now / for CookieOptions

V0.1.0

23 Apr 05:11
Compare
Choose a tag to compare

📦 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" }]