cookie-muncher
Advanced tools
Comparing version
@@ -25,2 +25,7 @@ type Cookie = { | ||
/** | ||
* Parse cookies from a string separated with semicolon. | ||
*/ | ||
declare function parseCookies(cookies: string): Cookie[]; | ||
/** | ||
* Format a string representing a cookie, useful to be able to set a cookie in a HTTP Set-Cookie header. | ||
@@ -44,7 +49,22 @@ */ | ||
/** | ||
* Parse cookies from a string separated with semicolon. | ||
*/ | ||
declare function parseCookies(cookies: string): Cookie[]; | ||
declare const httpCookie: { | ||
parse: typeof parseCookies; | ||
serialize: typeof serializeCookie; | ||
}; | ||
export { Cookie, CookieMaxAge, CookieOptions, parseCookies, serializeCookie }; | ||
declare function getAllCookies(): Cookie[]; | ||
declare function getCookie(name: string): Cookie | null; | ||
declare function setCookie(cookie: Cookie, options?: CookieOptions): void; | ||
declare function removeCookie(name: string): void; | ||
declare const domCookie: { | ||
getAll: typeof getAllCookies; | ||
get: typeof getCookie; | ||
set: typeof setCookie; | ||
remove: typeof removeCookie; | ||
}; | ||
export { Cookie, CookieMaxAge, CookieOptions, domCookie, httpCookie }; |
@@ -1,1 +0,1 @@ | ||
"use strict";var s=Object.defineProperty;var h=Object.getOwnPropertyDescriptor;var i=Object.getOwnPropertyNames;var c=Object.prototype.hasOwnProperty;var l=(e,r)=>{for(var n in r)s(e,n,{get:r[n],enumerable:!0})},O=(e,r,n,p)=>{if(r&&typeof r=="object"||typeof r=="function")for(let m of i(r))!c.call(e,m)&&m!==n&&s(e,m,{get:()=>r[m],enumerable:!(p=h(r,m))||p.enumerable});return e};var S=e=>O(s({},"__esModule",{value:!0}),e);var d={};l(d,{CookieMaxAge:()=>u,parseCookies:()=>$,serializeCookie:()=>y});module.exports=S(d);function y(e,r={}){let n=[`${e.name}=${e.value}`];return r.maxAge&&n.push(`Max-Age=${r.maxAge}`),r.expires&&n.push(`Expires=${r.expires.toUTCString()}`),r.domain&&n.push(`Domain=${r.domain}`),r.path&&n.push(`Path=${r.path}`),r.secure&&n.push("Secure"),r.httpOnly&&n.push("HttpOnly"),r.sameSite&&n.push(`SameSite=${r.sameSite}`),n.join("; ")}var u=(t=>(t[t.Now=-1]="Now",t[t.OneHour=3600]="OneHour",t[t.SixHours=21600]="SixHours",t[t.TwlveHours=43200]="TwlveHours",t[t.OneDay=86400]="OneDay",t[t.OneWeek=604800]="OneWeek",t[t.TwoWeeks=1209600]="TwoWeeks",t[t.OneMonth=2592e3]="OneMonth",t[t.ThreeMonths=7776e3]="ThreeMonths",t[t.SixMonths=15552e3]="SixMonths",t[t.OneYear=31536e3]="OneYear",t))(u||{});function $(e){return e?e.split(";").map(r=>{let[n,p]=r.split("="),m=decodeURIComponent(n.trim()),f=decodeURIComponent(p.trim());return{name:m,value:f}}):[]}0&&(module.exports={CookieMaxAge,parseCookies,serializeCookie}); | ||
"use strict";var f=Object.defineProperty;var a=Object.getOwnPropertyDescriptor;var h=Object.getOwnPropertyNames;var d=Object.prototype.hasOwnProperty;var y=(r,o)=>{for(var t in o)f(r,t,{get:o[t],enumerable:!0})},O=(r,o,t,s)=>{if(o&&typeof o=="object"||typeof o=="function")for(let i of h(o))!d.call(r,i)&&i!==t&&f(r,i,{get:()=>o[i],enumerable:!(s=a(o,i))||s.enumerable});return r};var v=r=>O(f({},"__esModule",{value:!0}),r);var $={};y($,{CookieMaxAge:()=>n,domCookie:()=>S,httpCookie:()=>x});module.exports=v($);function m(r){return r?r.split(";").map(o=>{let[t,s]=o.split("="),i=decodeURIComponent(t.trim()),C=decodeURIComponent(s.trim());return{name:i,value:C}}):[]}function p(r,o={}){let t=[`${r.name}=${r.value}`];return o.maxAge&&t.push(`Max-Age=${o.maxAge}`),o.expires&&t.push(`Expires=${o.expires.toUTCString()}`),o.domain&&t.push(`Domain=${o.domain}`),t.push(`Path=${o.path||"/"}`),o.secure&&t.push("Secure"),o.httpOnly&&t.push("HttpOnly"),o.sameSite&&t.push(`SameSite=${o.sameSite}`),t.join("; ")}var n=(e=>(e[e.Now=-1]="Now",e[e.OneHour=3600]="OneHour",e[e.SixHours=21600]="SixHours",e[e.TwlveHours=43200]="TwlveHours",e[e.OneDay=86400]="OneDay",e[e.OneWeek=604800]="OneWeek",e[e.TwoWeeks=1209600]="TwoWeeks",e[e.OneMonth=2592e3]="OneMonth",e[e.ThreeMonths=7776e3]="ThreeMonths",e[e.SixMonths=15552e3]="SixMonths",e[e.OneYear=31536e3]="OneYear",e))(n||{});var x={parse:m,serialize:p};function u(){return m(document.cookie)}function k(r){return m(document.cookie).find(o=>o.name===r)||null}function c(r,o={}){document.cookie=p(r,o)}function l(r){document.cookie=p({name:r,value:""},{maxAge:-1})}var S={getAll:u,get:k,set:c,remove:l};0&&(module.exports={CookieMaxAge,domCookie,httpCookie}); |
{ | ||
"name": "cookie-muncher", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"description": "Effortless cookie management", | ||
@@ -30,2 +30,3 @@ "author": "Bluzzi", | ||
"eslint": "^8.39.0", | ||
"happy-dom": "^9.9.2", | ||
"tsup": "^6.7.0", | ||
@@ -32,0 +33,0 @@ "typescript": "^5.0.4", |
@@ -27,12 +27,16 @@ # Cookie Muncher | ||
## Usage | ||
### `serializeCookie(cookie: Cookie, options?: CookieOptions): string` | ||
Serialize a cookie into a string. | ||
This package has two separate modules: | ||
- `httpCookie`: serialize and parse cookie from HTTP headers (`Cookie`, `Set-Cookie`) | ||
- `domCookie`: manage cookies from browser DOM (set, remove, get) | ||
### `httpCookie.serialize(cookie: Cookie, options?: CookieOptions): string` | ||
Serialize a cookie into a HTTP `Set-Cookie` header string. | ||
```ts | ||
import type { Cookie, CookieOptions } from "cookie-muncher"; | ||
import { serializeCookie, CookieMaxAge } from "cookie-muncher"; | ||
import { httpCookie, CookieMaxAge } from "cookie-muncher"; | ||
const cookie: Cookie = { | ||
name: "myCookie", | ||
value: "myValue", | ||
name: "foo", | ||
value: "bar", | ||
}; | ||
@@ -42,3 +46,2 @@ | ||
maxAge: CookieMaxAge.TwoWeeks, | ||
path: "/", | ||
secure: true, | ||
@@ -48,23 +51,48 @@ sameSite: "Strict", | ||
const serializedCookie = serializeCookie(cookie, options); | ||
console.log(httpCookie.serialize(cookie, options)); | ||
// Output: "foo=bar; Max-Age=3600; Path=/; Secure; SameSite=Strict" | ||
``` | ||
console.log(serializedCookie); | ||
// Output: "myCookie=myValue; Max-Age=3600; Path=/; Secure; SameSite=Strict" | ||
### `httpCookie.parse(cookies: string): Cookie[]` | ||
Parse a HTTP `Cookie` header string of cookies into an array of cookie objects. | ||
```ts | ||
import { httpCookie } from "cookie-muncher"; | ||
const cookies = "foo=bar; bar=foo"; | ||
console.log(httpCookie.parse(cookies)); | ||
// Output: [{ name: "foo", value: "bar" }, { name: "bar", value: "foo" }] | ||
``` | ||
### `parseCookies(cookies: string): Cookie[]` | ||
Parse a string of cookies into an array of cookie objects. | ||
### `domCookie.set(cookie: Cookie, options?: CookieOptions): void` | ||
### `domCookie.get(name: string): Cookie | null` | ||
### `domCookie.getAll(): Cookie[]` | ||
### `domCookie.remove(name: string): void` | ||
Create, update, get and remove cookies from the browser (only work on DOM context). | ||
```ts | ||
import { parseCookies } from "cookie-muncher"; | ||
import { domCookie } from "cookie-muncher"; | ||
``` | ||
const cookies = "myCookie1=myValue1; myCookie2=myValue2"; | ||
```ts | ||
domCookie.set({ name: "foo", value: "bar" }); | ||
domCookie.set({ name: "bar", value: "foo" }, { path: "/bar" }); | ||
``` | ||
const parsedCookies = parseCookies(cookies); | ||
```ts | ||
console.log(domCookie.get("foo")); | ||
// Ouput: { name: "foo", value: "bar" } | ||
``` | ||
console.log(parsedCookies); | ||
// Output: [{ name: "myCookie1", value: "myValue1" }, { name: "myCookie2", value: "myValue2" }] | ||
```ts | ||
console.log(domCookie.getAll()); | ||
// Output: [{ name: "foo", value: "bar" }, { name: "bar", value: "foo" }] | ||
``` | ||
```ts | ||
domCookie.remove("foo"); | ||
``` | ||
## License | ||
This package is MIT licensed. |
@@ -1,4 +0,3 @@ | ||
export * from "#/types/cookie"; | ||
export * from "#/serializer/serializer"; | ||
export * from "#/serializer/serializer.enum"; | ||
export * from "#/parser/parser"; | ||
export * from "#/http"; | ||
export * from "#/dom"; | ||
export * from "#/typing/cookie"; |
import { defineConfig } from "vitest/config"; | ||
export default defineConfig({ | ||
test: { | ||
environment: "happy-dom" | ||
}, | ||
resolve: { | ||
@@ -5,0 +8,0 @@ alias: { |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
19721
22.1%32
60%309
20.23%95
41.79%8
14.29%1
Infinity%