UNPKG

@happykit/flags

Version:

Feature Flags for Next.js

2 lines (1 loc) 1.88 kB
function s(e,t){return Object.prototype.hasOwnProperty.call(e,t)}function l(e){return Object.entries(e).reduce((t,[n,r])=>(r!==null&&(t[n]=r),t),{})}function p(e,t){return e?Object.keys(t).every(r=>s(e,r)&&e[r]!==null)?e:{...e,...t,...l(e)}:t}function c(e,t){if(e){let n=e.split(";");for(let r of n){let i=r.split("=",2);if(i[0].trim()===t)return i[1]}}return null}function a(e,t){var n,r;if(e===t)return!0;if(e&&t&&(n=e.constructor)===t.constructor){if(n===Date)return e.getTime()===t.getTime();if(n===RegExp)return e.toString()===t.toString();if(n===Array){if((r=e.length)===t.length)for(;r--&&a(e[r],t[r]););return r===-1}if(!n||typeof e=="object"){r=0;for(n in e)if(s(e,n)&&++r&&!s(t,n)||!(n in t)||!a(e[n],t[n]))return!1;return Object.keys(t).length===r}}return e!==e&&t!==t}var o=class{constructor(){this.keys=[],this.values=[]}_getIndex(t){return this.keys.findIndex(n=>a(t,n))}set(t,n){let r=this._getIndex(t);r==-1?(this.keys=[t,...this.keys],this.values=[n,...this.values]):this.values[r]=n}get(t){let n=this._getIndex(t);return n===-1?null:this.values[n]}clear(){this.keys=[],this.values=[]}};function u(e){return`hkvk=${encodeURIComponent(e)}; Path=/; Max-Age=15552000; SameSite=Lax`}function h(e){if(!e)throw new Error("@happykit/flags: config missing");if(!e.envKey||e.envKey.length===0)throw new Error("@happykit/flags: envKey missing");let t={endpoint:"https://happykit.dev/api/flags",defaultFlags:{},serializeVisitorKeyCookie:u},n=e.envKey.match(/^flags_pub_(?:(development|preview|production)_)?([a-z0-9]+)$/);if(!n)throw new Error("@happykit/flags: invalid envKey");let r=n[2],i=n[1]||"production";if(!r)throw new Error("@happykit/flags: could not parse projectId from envKey");if(!i)throw new Error("@happykit/flags: could not parse environment from envKey");return Object.assign({},t,e,{projectId:r,environment:i})}export{s as a,p as b,c,a as d,o as e,h as f};