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