UNPKG

@minisss/cookie

Version:

A Document Browser Cookie And Next.js Cookie Manager Lib

2 lines (1 loc) 3.24 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).MSCookie=t()}(this,function(){"use strict";const e=/^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;function t(t,i,n={}){const r=n||{};if(!e.test(t))throw new TypeError("argument name is invalid");const o=encodeURIComponent("string"==typeof i?i:JSON.stringify(i));if(o&&!e.test(o))throw new TypeError("argument val is invalid");let s=`${t}=${o}`;if(r.domain){if(!e.test(r.domain))throw new TypeError("option domain is invalid");s+=`; Domain=${r.domain}`}if(r.path){if(!e.test(r.path))throw new TypeError("option path is invalid");s+=`; Path=${r.path}`}if(r.expires){if(!function(e){return"[object Date]"===Object.prototype.toString.call(e)||e instanceof Date}(r.expires))throw new TypeError("option expires is invalid");s+=`; Expires=${r.expires.toUTCString()}`}if("number"==typeof r.maxAge){if(isNaN(r.maxAge)||!isFinite(r.maxAge))throw new TypeError("option maxAge is invalid");s+=`; Max-Age=${Math.floor(r.maxAge)}`}if(r.httpOnly&&(s+="; HttpOnly"),r.secure&&(s+="; Secure"),r.sameSite){if("Strict"!==r.sameSite&&"Lax"!==r.sameSite&&"None"!==r.sameSite)throw new TypeError("option sameSite invalid, sameSite must be Strict | Lax |None");s+=`; SameSite=${r.sameSite}`}if(r.partitioned&&(s+="; Partitioned"),r.priority){if("High"!==r.priority&&"Medium"!==r.priority&&"Low"!==r.priority)throw new TypeError("option priority invalid, priority muse be High | Medium | Low");s+=`; Priority=${r.priority}`}return s}function i(e){try{if(!e||"string"!=typeof e)return{};const t=function(e){try{return e.includes("%")?decodeURIComponent(e):e}catch(t){return e}}(e).split(";").filter(Boolean);return t.map(e=>e.split("=")).reduce((e,[t,...i])=>{const n=t.trim(),r=i.map(e=>e.trim()).join("=");try{return Object.assign(Object.assign({},e),{[n]:JSON.parse(r)})}catch(t){return Object.assign(Object.assign({},e),{[n]:r})}},{})}catch(e){return{}}}function n(e,t={}){try{return"undefined"==typeof window?t.ctx?i(t.ctx.req.headers.cookie||"")[e]:"":i(document.cookie)[e]}catch(t){return i("")[e]}}function r(e,i,n={}){try{const r=t(String(e),i,n);if("undefined"==typeof window)return!!n.ctx&&(n.ctx.res.setHeader("'Set-Cookie'",r),!0);if(n.httpOnly)throw new Error("Can not set a httpOnly cookie in the browser.");return document.cookie=r,!0}catch(e){return!1}}function o(e,t={}){return r(e,"",Object.assign(Object.assign({},t),{maxAge:-1}))}function s(e,t={}){try{return"undefined"==typeof window?!!t.ctx&&!!i(t.ctx.req.headers.cookie||"")[e]:!!i(document.cookie)[e]}catch(e){return!1}}var a="1.0.5";class c{constructor(e={}){this.VERSION=a,this.config={},this.config=e}get(e,t={}){return n(e,Object.assign(Object.assign({},this.config),t))}set(e,t,i={}){return r(e,t,Object.assign(Object.assign({},this.config),i))}del(e,t={}){return o(e,Object.assign(Object.assign(Object.assign({},this.config),t),{maxAge:-1}))}has(e,t={}){return s(e,Object.assign(Object.assign({},this.config),t))}serialize(e,i,n={}){return t(e,i,Object.assign(Object.assign({},this.config),n))}parse(e){return i(e)}}return{VERSION:a,create:(e={})=>new c(e),get:n,set:r,del:o,has:s,serialize:t,parse:i}});