@schematichq/schematic-js 1.0.3 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -77,6 +77,9 @@ schematic.identify({
77
77
  });
78
78
 
79
79
  await schematic.checkFlag("some-flag-key");
80
+
81
+ // Close the connection when you're done with the Schematic client
82
+ schematic.cleanup();
80
83
  ```
81
84
 
82
85
  ## License
@@ -1,3 +1,3 @@
1
- "use strict";(()=>{var z=Object.create;var C=Object.defineProperty;var Y=Object.getOwnPropertyDescriptor;var Z=Object.getOwnPropertyNames;var ee=Object.getPrototypeOf,te=Object.prototype.hasOwnProperty;var re=(s,t)=>()=>(t||s((t={exports:{}}).exports,t),t.exports);var ne=(s,t,n,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of Z(t))!te.call(s,i)&&i!==n&&C(s,i,{get:()=>t[i],enumerable:!(o=Y(t,i))||o.enumerable});return s};var ie=(s,t,n)=>(n=s!=null?z(ee(s)):{},ne(t||!s||!s.__esModule?C(n,"default",{value:s,enumerable:!0}):n,s));var V=re(j=>{(function(s){var t=function(n){var o=typeof globalThis<"u"&&globalThis||typeof s<"u"&&s||typeof o<"u"&&o,i={searchParams:"URLSearchParams"in o,iterable:"Symbol"in o&&"iterator"in Symbol,blob:"FileReader"in o&&"Blob"in o&&function(){try{return new Blob,!0}catch{return!1}}(),formData:"FormData"in o,arrayBuffer:"ArrayBuffer"in o};function l(e){return e&&DataView.prototype.isPrototypeOf(e)}if(i.arrayBuffer)var d=["[object Int8Array]","[object Uint8Array]","[object Uint8ClampedArray]","[object Int16Array]","[object Uint16Array]","[object Int32Array]","[object Uint32Array]","[object Float32Array]","[object Float64Array]"],p=ArrayBuffer.isView||function(e){return e&&d.indexOf(Object.prototype.toString.call(e))>-1};function g(e){if(typeof e!="string"&&(e=String(e)),/[^a-z0-9\-#$%&'*+.^_`|~!]/i.test(e)||e==="")throw new TypeError('Invalid character in header field name: "'+e+'"');return e.toLowerCase()}function S(e){return typeof e!="string"&&(e=String(e)),e}function b(e){var r={next:function(){var a=e.shift();return{done:a===void 0,value:a}}};return i.iterable&&(r[Symbol.iterator]=function(){return r}),r}function f(e){this.map={},e instanceof f?e.forEach(function(r,a){this.append(a,r)},this):Array.isArray(e)?e.forEach(function(r){this.append(r[0],r[1])},this):e&&Object.getOwnPropertyNames(e).forEach(function(r){this.append(r,e[r])},this)}f.prototype.append=function(e,r){e=g(e),r=S(r);var a=this.map[e];this.map[e]=a?a+", "+r:r},f.prototype.delete=function(e){delete this.map[g(e)]},f.prototype.get=function(e){return e=g(e),this.has(e)?this.map[e]:null},f.prototype.has=function(e){return this.map.hasOwnProperty(g(e))},f.prototype.set=function(e,r){this.map[g(e)]=S(r)},f.prototype.forEach=function(e,r){for(var a in this.map)this.map.hasOwnProperty(a)&&e.call(r,this.map[a],a,this)},f.prototype.keys=function(){var e=[];return this.forEach(function(r,a){e.push(a)}),b(e)},f.prototype.values=function(){var e=[];return this.forEach(function(r){e.push(r)}),b(e)},f.prototype.entries=function(){var e=[];return this.forEach(function(r,a){e.push([a,r])}),b(e)},i.iterable&&(f.prototype[Symbol.iterator]=f.prototype.entries);function B(e){if(e.bodyUsed)return Promise.reject(new TypeError("Already read"));e.bodyUsed=!0}function F(e){return new Promise(function(r,a){e.onload=function(){r(e.result)},e.onerror=function(){a(e.error)}})}function q(e){var r=new FileReader,a=F(r);return r.readAsArrayBuffer(e),a}function M(e){var r=new FileReader,a=F(r);return r.readAsText(e),a}function W(e){for(var r=new Uint8Array(e),a=new Array(r.length),h=0;h<r.length;h++)a[h]=String.fromCharCode(r[h]);return a.join("")}function R(e){if(e.slice)return e.slice(0);var r=new Uint8Array(e.byteLength);return r.set(new Uint8Array(e)),r.buffer}function I(){return this.bodyUsed=!1,this._initBody=function(e){this.bodyUsed=this.bodyUsed,this._bodyInit=e,e?typeof e=="string"?this._bodyText=e:i.blob&&Blob.prototype.isPrototypeOf(e)?this._bodyBlob=e:i.formData&&FormData.prototype.isPrototypeOf(e)?this._bodyFormData=e:i.searchParams&&URLSearchParams.prototype.isPrototypeOf(e)?this._bodyText=e.toString():i.arrayBuffer&&i.blob&&l(e)?(this._bodyArrayBuffer=R(e.buffer),this._bodyInit=new Blob([this._bodyArrayBuffer])):i.arrayBuffer&&(ArrayBuffer.prototype.isPrototypeOf(e)||p(e))?this._bodyArrayBuffer=R(e):this._bodyText=e=Object.prototype.toString.call(e):this._bodyText="",this.headers.get("content-type")||(typeof e=="string"?this.headers.set("content-type","text/plain;charset=UTF-8"):this._bodyBlob&&this._bodyBlob.type?this.headers.set("content-type",this._bodyBlob.type):i.searchParams&&URLSearchParams.prototype.isPrototypeOf(e)&&this.headers.set("content-type","application/x-www-form-urlencoded;charset=UTF-8"))},i.blob&&(this.blob=function(){var e=B(this);if(e)return e;if(this._bodyBlob)return Promise.resolve(this._bodyBlob);if(this._bodyArrayBuffer)return Promise.resolve(new Blob([this._bodyArrayBuffer]));if(this._bodyFormData)throw new Error("could not read FormData body as blob");return Promise.resolve(new Blob([this._bodyText]))},this.arrayBuffer=function(){if(this._bodyArrayBuffer){var e=B(this);return e||(ArrayBuffer.isView(this._bodyArrayBuffer)?Promise.resolve(this._bodyArrayBuffer.buffer.slice(this._bodyArrayBuffer.byteOffset,this._bodyArrayBuffer.byteOffset+this._bodyArrayBuffer.byteLength)):Promise.resolve(this._bodyArrayBuffer))}else return this.blob().then(q)}),this.text=function(){var e=B(this);if(e)return e;if(this._bodyBlob)return M(this._bodyBlob);if(this._bodyArrayBuffer)return Promise.resolve(W(this._bodyArrayBuffer));if(this._bodyFormData)throw new Error("could not read FormData body as text");return Promise.resolve(this._bodyText)},i.formData&&(this.formData=function(){return this.text().then(J)}),this.json=function(){return this.text().then(JSON.parse)},this}var K=["DELETE","GET","HEAD","OPTIONS","POST","PUT"];function $(e){var r=e.toUpperCase();return K.indexOf(r)>-1?r:e}function w(e,r){if(!(this instanceof w))throw new TypeError('Please use the "new" operator, this DOM object constructor cannot be called as a function.');r=r||{};var a=r.body;if(e instanceof w){if(e.bodyUsed)throw new TypeError("Already read");this.url=e.url,this.credentials=e.credentials,r.headers||(this.headers=new f(e.headers)),this.method=e.method,this.mode=e.mode,this.signal=e.signal,!a&&e._bodyInit!=null&&(a=e._bodyInit,e.bodyUsed=!0)}else this.url=String(e);if(this.credentials=r.credentials||this.credentials||"same-origin",(r.headers||!this.headers)&&(this.headers=new f(r.headers)),this.method=$(r.method||this.method||"GET"),this.mode=r.mode||this.mode||null,this.signal=r.signal||this.signal,this.referrer=null,(this.method==="GET"||this.method==="HEAD")&&a)throw new TypeError("Body not allowed for GET or HEAD requests");if(this._initBody(a),(this.method==="GET"||this.method==="HEAD")&&(r.cache==="no-store"||r.cache==="no-cache")){var h=/([?&])_=[^&]*/;if(h.test(this.url))this.url=this.url.replace(h,"$1_="+new Date().getTime());else{var u=/\?/;this.url+=(u.test(this.url)?"&":"?")+"_="+new Date().getTime()}}}w.prototype.clone=function(){return new w(this,{body:this._bodyInit})};function J(e){var r=new FormData;return e.trim().split("&").forEach(function(a){if(a){var h=a.split("="),u=h.shift().replace(/\+/g," "),c=h.join("=").replace(/\+/g," ");r.append(decodeURIComponent(u),decodeURIComponent(c))}}),r}function Q(e){var r=new f,a=e.replace(/\r?\n[\t ]+/g," ");return a.split("\r").map(function(h){return h.indexOf(`
2
- `)===0?h.substr(1,h.length):h}).forEach(function(h){var u=h.split(":"),c=u.shift().trim();if(c){var T=u.join(":").trim();r.append(c,T)}}),r}I.call(w.prototype);function v(e,r){if(!(this instanceof v))throw new TypeError('Please use the "new" operator, this DOM object constructor cannot be called as a function.');r||(r={}),this.type="default",this.status=r.status===void 0?200:r.status,this.ok=this.status>=200&&this.status<300,this.statusText=r.statusText===void 0?"":""+r.statusText,this.headers=new f(r.headers),this.url=r.url||"",this._initBody(e)}I.call(v.prototype),v.prototype.clone=function(){return new v(this._bodyInit,{status:this.status,statusText:this.statusText,headers:new f(this.headers),url:this.url})},v.error=function(){var e=new v(null,{status:0,statusText:""});return e.type="error",e};var X=[301,302,303,307,308];v.redirect=function(e,r){if(X.indexOf(r)===-1)throw new RangeError("Invalid status code");return new v(null,{status:r,headers:{location:e}})},n.DOMException=o.DOMException;try{new n.DOMException}catch{n.DOMException=function(r,a){this.message=r,this.name=a;var h=Error(r);this.stack=h.stack},n.DOMException.prototype=Object.create(Error.prototype),n.DOMException.prototype.constructor=n.DOMException}function O(e,r){return new Promise(function(a,h){var u=new w(e,r);if(u.signal&&u.signal.aborted)return h(new n.DOMException("Aborted","AbortError"));var c=new XMLHttpRequest;function T(){c.abort()}c.onload=function(){var m={status:c.status,statusText:c.statusText,headers:Q(c.getAllResponseHeaders()||"")};m.url="responseURL"in c?c.responseURL:m.headers.get("X-Request-URL");var P="response"in c?c.response:c.responseText;setTimeout(function(){a(new v(P,m))},0)},c.onerror=function(){setTimeout(function(){h(new TypeError("Network request failed"))},0)},c.ontimeout=function(){setTimeout(function(){h(new TypeError("Network request failed"))},0)},c.onabort=function(){setTimeout(function(){h(new n.DOMException("Aborted","AbortError"))},0)};function G(m){try{return m===""&&o.location.href?o.location.href:m}catch{return m}}c.open(u.method,G(u.url),!0),u.credentials==="include"?c.withCredentials=!0:u.credentials==="omit"&&(c.withCredentials=!1),"responseType"in c&&(i.blob?c.responseType="blob":i.arrayBuffer&&u.headers.get("Content-Type")&&u.headers.get("Content-Type").indexOf("application/octet-stream")!==-1&&(c.responseType="arraybuffer")),r&&typeof r.headers=="object"&&!(r.headers instanceof f)?Object.getOwnPropertyNames(r.headers).forEach(function(m){c.setRequestHeader(m,S(r.headers[m]))}):u.headers.forEach(function(m,P){c.setRequestHeader(P,m)}),u.signal&&(u.signal.addEventListener("abort",T),c.onreadystatechange=function(){c.readyState===4&&u.signal.removeEventListener("abort",T)}),c.send(typeof u._bodyInit>"u"?null:u._bodyInit)})}return O.polyfill=!0,o.fetch||(o.fetch=O,o.Headers=f,o.Request=w,o.Response=v),n.Headers=f,n.Request=w,n.Response=v,n.fetch=O,n}({})})(typeof self<"u"?self:j)});var y=[];for(U=0;U<256;++U)y.push((U+256).toString(16).slice(1));var U;function D(s,t=0){return(y[s[t+0]]+y[s[t+1]]+y[s[t+2]]+y[s[t+3]]+"-"+y[s[t+4]]+y[s[t+5]]+"-"+y[s[t+6]]+y[s[t+7]]+"-"+y[s[t+8]]+y[s[t+9]]+"-"+y[s[t+10]]+y[s[t+11]]+y[s[t+12]]+y[s[t+13]]+y[s[t+14]]+y[s[t+15]]).toLowerCase()}var k,oe=new Uint8Array(16);function _(){if(!k&&(k=typeof crypto<"u"&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!k))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return k(oe)}var se=typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto),L={randomUUID:se};function ae(s,t,n){if(L.randomUUID&&!t&&!s)return L.randomUUID();s=s||{};var o=s.random||(s.rng||_)();if(o[6]=o[6]&15|64,o[8]=o[8]&63|128,t){n=n||0;for(var i=0;i<16;++i)t[n+i]=o[i];return t}return D(o)}var x=ae;var we=ie(V());function E(s){let t=Object.keys(s).reduce((n,o)=>{let l=Object.keys(s[o]||{}).sort().reduce((d,p)=>(d[p]=s[o][p],d),{});return n[o]=l,n},{});return JSON.stringify(t)}var H="schematicId";var A=class{additionalHeaders={};apiKey;apiUrl="https://api.schematichq.com";conn=null;context={};eventQueue;eventUrl="https://c.schematichq.com";flagListener;flagValueListeners={};isPending=!0;isPendingListeners=new Set;storage;useWebSocket=!1;values={};webSocketUrl="wss://api.schematichq.com";constructor(t,n){this.apiKey=t,this.eventQueue=[],this.useWebSocket=n?.useWebSocket??!1,this.flagListener=n?.flagListener,n?.additionalHeaders&&(this.additionalHeaders=n.additionalHeaders),n?.storage?this.storage=n.storage:typeof localStorage<"u"&&(this.storage=localStorage),n?.apiUrl!==void 0&&(this.apiUrl=n.apiUrl),n?.eventUrl!==void 0&&(this.eventUrl=n.eventUrl),n?.webSocketUrl!==void 0&&(this.webSocketUrl=n.webSocketUrl),typeof window<"u"&&window?.addEventListener&&window.addEventListener("beforeunload",()=>{this.flushEventQueue()})}async checkFlag(t){let{fallback:n=!1,key:o}=t,i=t.context||this.context,l=E(i);if(!this.useWebSocket){let d=`${this.apiUrl}/flags/${o}/check`;return fetch(d,{method:"POST",headers:{...this.additionalHeaders??{},"Content-Type":"application/json;charset=UTF-8","X-Schematic-Api-Key":this.apiKey},body:JSON.stringify(i)}).then(p=>{if(!p.ok)throw new Error("Network response was not ok");return p.json()}).then(p=>p.data.value).catch(p=>(console.error("There was a problem with the fetch operation:",p),n))}try{let d=this.values[l];if(this.conn&&typeof d<"u"&&typeof d[o]<"u")return d[o];try{await this.setContext(i)}catch(g){return console.error("WebSocket connection failed, falling back to REST:",g),this.fallbackToRest(o,i,n)}let p=this.values[l]??{};return typeof p[o]>"u"?n:p[o]}catch(d){return console.error("Unexpected error in checkFlag:",d),n}}async fallbackToRest(t,n,o){try{let i=`${this.apiUrl}/flags/${t}/check`,l=await fetch(i,{method:"POST",headers:{...this.additionalHeaders??{},"Content-Type":"application/json;charset=UTF-8","X-Schematic-Api-Key":this.apiKey},body:JSON.stringify(n)});if(!l.ok)throw new Error("Network response was not ok");return(await l.json()).data.value}catch(i){return console.error("REST API call failed, using fallback value:",i),o}}checkFlags=async t=>{t=t||this.context;let n=`${this.apiUrl}/flags/check`,o=JSON.stringify(t);return fetch(n,{method:"POST",headers:{...this.additionalHeaders??{},"Content-Type":"application/json;charset=UTF-8","X-Schematic-Api-Key":this.apiKey},body:o}).then(i=>{if(!i.ok)throw new Error("Network response was not ok");return i.json()}).then(i=>(i?.data?.flags??[]).reduce((l,d)=>(l[d.flag]=d.value,l),{})).catch(i=>(console.error("There was a problem with the fetch operation:",i),!1))};identify=t=>{try{this.setContext({company:t.company?.keys,user:t.keys})}catch(n){console.error("Error setting context:",n)}return this.handleEvent("identify",t)};setContext=async t=>{if(!this.useWebSocket)return this.context=t,Promise.resolve();try{this.setIsPending(!0),this.conn||(this.conn=this.wsConnect());let n=await this.conn;await this.wsSendMessage(n,t)}catch(n){throw console.error("Failed to establish WebSocket connection:",n),n}};track=t=>{let{company:n,user:o,event:i,traits:l}=t;return this.handleEvent("track",{company:n??this.context.company,event:i,traits:l??{},user:o??this.context.user})};flushEventQueue=()=>{for(;this.eventQueue.length>0;){let t=this.eventQueue.shift();t&&this.sendEvent(t)}};getAnonymousId=()=>{if(!this.storage)return x();let t=this.storage.getItem(H);if(typeof t<"u")return t;let n=x();return this.storage.setItem(H,n),n};handleEvent=(t,n)=>{let o={api_key:this.apiKey,body:n,sent_at:new Date().toISOString(),tracker_event_id:x(),tracker_user_id:this.getAnonymousId(),type:t};return document?.hidden?this.storeEvent(o):this.sendEvent(o)};sendEvent=async t=>{let n=`${this.eventUrl}/e`,o=JSON.stringify(t);try{await fetch(n,{method:"POST",headers:{...this.additionalHeaders??{},"Content-Type":"application/json;charset=UTF-8"},body:o})}catch(i){console.error("Error sending Schematic event: ",i)}return Promise.resolve()};storeEvent=t=>(this.eventQueue.push(t),Promise.resolve());cleanup=async()=>{if(this.conn)try{(await this.conn).close()}catch(t){console.error("Error during cleanup:",t)}finally{this.conn=null}};wsConnect=()=>new Promise((t,n)=>{let o=`${this.webSocketUrl}/flags/bootstrap`,i=new WebSocket(o);i.onopen=()=>{t(i)},i.onerror=l=>{n(l)},i.onclose=()=>{this.conn=null}});wsSendMessage=(t,n)=>new Promise((o,i)=>{if(E(n)==E(this.context))return o(this.setIsPending(!1));this.context=n;let l=()=>{let d=!1,p=g=>{let S=JSON.parse(g.data);E(n)in this.values||(this.values[E(n)]={}),(S.flags??[]).forEach(b=>{this.values[E(n)][b.flag]=b.value,this.notifyFlagValueListeners(b.flag,b.value)}),this.flagListener&&this.flagListener(this.getFlagValues()),this.setIsPending(!1),d||(d=!0,o())};t.addEventListener("message",p),t.send(JSON.stringify({apiKey:this.apiKey,data:n}))};t.readyState===WebSocket.OPEN?l():t.readyState===WebSocket.CONNECTING?t.addEventListener("open",l):i("WebSocket is not open or connecting")});getIsPending=()=>this.isPending;addIsPendingListener=t=>(this.isPendingListeners.add(t),()=>{this.isPendingListeners.delete(t)});setIsPending=t=>{this.isPending=t,this.isPendingListeners.forEach(n=>N(n,t))};getFlagValue=t=>this.getFlagValues()[t];getFlagValues=()=>{let t=E(this.context);return this.values[t]??{}};addFlagValueListener=(t,n)=>(t in this.flagValueListeners||(this.flagValueListeners[t]=new Set),this.flagValueListeners[t].add(n),()=>{this.flagValueListeners[t].delete(n)});notifyFlagValueListeners=(t,n)=>{(this.flagValueListeners?.[t]??[]).forEach(i=>N(i,n))}},N=(s,t)=>{s.length>0?s(t):s()};window.Schematic=A;})();
1
+ "use strict";(()=>{var te=Object.create;var H=Object.defineProperty;var re=Object.getOwnPropertyDescriptor;var ne=Object.getOwnPropertyNames;var ae=Object.getPrototypeOf,se=Object.prototype.hasOwnProperty;var oe=(r,t)=>()=>(t||r((t={exports:{}}).exports,t),t.exports);var ie=(r,t,n,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of ne(t))!se.call(r,s)&&s!==n&&H(r,s,{get:()=>t[s],enumerable:!(o=re(t,s))||o.enumerable});return r};var le=(r,t,n)=>(n=r!=null?te(ae(r)):{},ie(t||!r||!r.__esModule?H(n,"default",{value:r,enumerable:!0}):n,r));var W=oe(q=>{(function(r){var t=function(n){var o=typeof globalThis<"u"&&globalThis||typeof r<"u"&&r||typeof global<"u"&&global||{},s={searchParams:"URLSearchParams"in o,iterable:"Symbol"in o&&"iterator"in Symbol,blob:"FileReader"in o&&"Blob"in o&&function(){try{return new Blob,!0}catch{return!1}}(),formData:"FormData"in o,arrayBuffer:"ArrayBuffer"in o};function h(e){return e&&DataView.prototype.isPrototypeOf(e)}if(s.arrayBuffer)var d=["[object Int8Array]","[object Uint8Array]","[object Uint8ClampedArray]","[object Int16Array]","[object Uint16Array]","[object Int32Array]","[object Uint32Array]","[object Float32Array]","[object Float64Array]"],p=ArrayBuffer.isView||function(e){return e&&d.indexOf(Object.prototype.toString.call(e))>-1};function m(e){if(typeof e!="string"&&(e=String(e)),/[^a-z0-9\-#$%&'*+.^_`|~!]/i.test(e)||e==="")throw new TypeError('Invalid character in header field name: "'+e+'"');return e.toLowerCase()}function E(e){return typeof e!="string"&&(e=String(e)),e}function b(e){var a={next:function(){var i=e.shift();return{done:i===void 0,value:i}}};return s.iterable&&(a[Symbol.iterator]=function(){return a}),a}function c(e){this.map={},e instanceof c?e.forEach(function(a,i){this.append(i,a)},this):Array.isArray(e)?e.forEach(function(a){if(a.length!=2)throw new TypeError("Headers constructor: expected name/value pair to be length 2, found"+a.length);this.append(a[0],a[1])},this):e&&Object.getOwnPropertyNames(e).forEach(function(a){this.append(a,e[a])},this)}c.prototype.append=function(e,a){e=m(e),a=E(a);var i=this.map[e];this.map[e]=i?i+", "+a:a},c.prototype.delete=function(e){delete this.map[m(e)]},c.prototype.get=function(e){return e=m(e),this.has(e)?this.map[e]:null},c.prototype.has=function(e){return this.map.hasOwnProperty(m(e))},c.prototype.set=function(e,a){this.map[m(e)]=E(a)},c.prototype.forEach=function(e,a){for(var i in this.map)this.map.hasOwnProperty(i)&&e.call(a,this.map[i],i,this)},c.prototype.keys=function(){var e=[];return this.forEach(function(a,i){e.push(i)}),b(e)},c.prototype.values=function(){var e=[];return this.forEach(function(a){e.push(a)}),b(e)},c.prototype.entries=function(){var e=[];return this.forEach(function(a,i){e.push([i,a])}),b(e)},s.iterable&&(c.prototype[Symbol.iterator]=c.prototype.entries);function R(e){if(!e._noBody){if(e.bodyUsed)return Promise.reject(new TypeError("Already read"));e.bodyUsed=!0}}function x(e){return new Promise(function(a,i){e.onload=function(){a(e.result)},e.onerror=function(){i(e.error)}})}function N(e){var a=new FileReader,i=x(a);return a.readAsArrayBuffer(e),i}function $(e){var a=new FileReader,i=x(a),u=/charset=([A-Za-z0-9_-]+)/.exec(e.type),f=u?u[1]:"utf-8";return a.readAsText(e,f),i}function Q(e){for(var a=new Uint8Array(e),i=new Array(a.length),u=0;u<a.length;u++)i[u]=String.fromCharCode(a[u]);return i.join("")}function B(e){if(e.slice)return e.slice(0);var a=new Uint8Array(e.byteLength);return a.set(new Uint8Array(e)),a.buffer}function J(){return this.bodyUsed=!1,this._initBody=function(e){this.bodyUsed=this.bodyUsed,this._bodyInit=e,e?typeof e=="string"?this._bodyText=e:s.blob&&Blob.prototype.isPrototypeOf(e)?this._bodyBlob=e:s.formData&&FormData.prototype.isPrototypeOf(e)?this._bodyFormData=e:s.searchParams&&URLSearchParams.prototype.isPrototypeOf(e)?this._bodyText=e.toString():s.arrayBuffer&&s.blob&&h(e)?(this._bodyArrayBuffer=B(e.buffer),this._bodyInit=new Blob([this._bodyArrayBuffer])):s.arrayBuffer&&(ArrayBuffer.prototype.isPrototypeOf(e)||p(e))?this._bodyArrayBuffer=B(e):this._bodyText=e=Object.prototype.toString.call(e):(this._noBody=!0,this._bodyText=""),this.headers.get("content-type")||(typeof e=="string"?this.headers.set("content-type","text/plain;charset=UTF-8"):this._bodyBlob&&this._bodyBlob.type?this.headers.set("content-type",this._bodyBlob.type):s.searchParams&&URLSearchParams.prototype.isPrototypeOf(e)&&this.headers.set("content-type","application/x-www-form-urlencoded;charset=UTF-8"))},s.blob&&(this.blob=function(){var e=R(this);if(e)return e;if(this._bodyBlob)return Promise.resolve(this._bodyBlob);if(this._bodyArrayBuffer)return Promise.resolve(new Blob([this._bodyArrayBuffer]));if(this._bodyFormData)throw new Error("could not read FormData body as blob");return Promise.resolve(new Blob([this._bodyText]))}),this.arrayBuffer=function(){if(this._bodyArrayBuffer){var e=R(this);return e||(ArrayBuffer.isView(this._bodyArrayBuffer)?Promise.resolve(this._bodyArrayBuffer.buffer.slice(this._bodyArrayBuffer.byteOffset,this._bodyArrayBuffer.byteOffset+this._bodyArrayBuffer.byteLength)):Promise.resolve(this._bodyArrayBuffer))}else{if(s.blob)return this.blob().then(N);throw new Error("could not read as ArrayBuffer")}},this.text=function(){var e=R(this);if(e)return e;if(this._bodyBlob)return $(this._bodyBlob);if(this._bodyArrayBuffer)return Promise.resolve(Q(this._bodyArrayBuffer));if(this._bodyFormData)throw new Error("could not read FormData body as text");return Promise.resolve(this._bodyText)},s.formData&&(this.formData=function(){return this.text().then(j)}),this.json=function(){return this.text().then(JSON.parse)},this}var z=["CONNECT","DELETE","GET","HEAD","OPTIONS","PATCH","POST","PUT","TRACE"];function Y(e){var a=e.toUpperCase();return z.indexOf(a)>-1?a:e}function F(e,a){if(!(this instanceof F))throw new TypeError('Please use the "new" operator, this DOM object constructor cannot be called as a function.');a=a||{};var i=a.body;if(e instanceof F){if(e.bodyUsed)throw new TypeError("Already read");this.url=e.url,this.credentials=e.credentials,a.headers||(this.headers=new c(e.headers)),this.method=e.method,this.mode=e.mode,this.signal=e.signal,!i&&e._bodyInit!=null&&(i=e._bodyInit,e.bodyUsed=!0)}else this.url=String(e);if(this.credentials=a.credentials||this.credentials||"same-origin",(a.headers||!this.headers)&&(this.headers=new c(a.headers)),this.method=Y(a.method||this.method||"GET"),this.mode=a.mode||this.mode||null,this.signal=a.signal||this.signal||function(){if("AbortController"in o){var l=new AbortController;return l.signal}}(),this.referrer=null,(this.method==="GET"||this.method==="HEAD")&&i)throw new TypeError("Body not allowed for GET or HEAD requests");if(this._initBody(i),(this.method==="GET"||this.method==="HEAD")&&(a.cache==="no-store"||a.cache==="no-cache")){var u=/([?&])_=[^&]*/;if(u.test(this.url))this.url=this.url.replace(u,"$1_="+new Date().getTime());else{var f=/\?/;this.url+=(f.test(this.url)?"&":"?")+"_="+new Date().getTime()}}}F.prototype.clone=function(){return new F(this,{body:this._bodyInit})};function j(e){var a=new FormData;return e.trim().split("&").forEach(function(i){if(i){var u=i.split("="),f=u.shift().replace(/\+/g," "),l=u.join("=").replace(/\+/g," ");a.append(decodeURIComponent(f),decodeURIComponent(l))}}),a}function Z(e){var a=new c,i=e.replace(/\r?\n[\t ]+/g," ");return i.split("\r").map(function(u){return u.indexOf(`
2
+ `)===0?u.substr(1,u.length):u}).forEach(function(u){var f=u.split(":"),l=f.shift().trim();if(l){var T=f.join(":").trim();try{a.append(l,T)}catch(A){console.warn("Response "+A.message)}}}),a}J.call(F.prototype);function k(e,a){if(!(this instanceof k))throw new TypeError('Please use the "new" operator, this DOM object constructor cannot be called as a function.');if(a||(a={}),this.type="default",this.status=a.status===void 0?200:a.status,this.status<200||this.status>599)throw new RangeError("Failed to construct 'Response': The status provided (0) is outside the range [200, 599].");this.ok=this.status>=200&&this.status<300,this.statusText=a.statusText===void 0?"":""+a.statusText,this.headers=new c(a.headers),this.url=a.url||"",this._initBody(e)}J.call(k.prototype),k.prototype.clone=function(){return new k(this._bodyInit,{status:this.status,statusText:this.statusText,headers:new c(this.headers),url:this.url})},k.error=function(){var e=new k(null,{status:200,statusText:""});return e.ok=!1,e.status=0,e.type="error",e};var ee=[301,302,303,307,308];k.redirect=function(e,a){if(ee.indexOf(a)===-1)throw new RangeError("Invalid status code");return new k(null,{status:a,headers:{location:e}})},n.DOMException=o.DOMException;try{new n.DOMException}catch{n.DOMException=function(a,i){this.message=a,this.name=i;var u=Error(a);this.stack=u.stack},n.DOMException.prototype=Object.create(Error.prototype),n.DOMException.prototype.constructor=n.DOMException}function O(e,a){return new Promise(function(i,u){var f=new F(e,a);if(f.signal&&f.signal.aborted)return u(new n.DOMException("Aborted","AbortError"));var l=new XMLHttpRequest;function T(){l.abort()}l.onload=function(){var y={statusText:l.statusText,headers:Z(l.getAllResponseHeaders()||"")};f.url.indexOf("file://")===0&&(l.status<200||l.status>599)?y.status=200:y.status=l.status,y.url="responseURL"in l?l.responseURL:y.headers.get("X-Request-URL");var C="response"in l?l.response:l.responseText;setTimeout(function(){i(new k(C,y))},0)},l.onerror=function(){setTimeout(function(){u(new TypeError("Network request failed"))},0)},l.ontimeout=function(){setTimeout(function(){u(new TypeError("Network request timed out"))},0)},l.onabort=function(){setTimeout(function(){u(new n.DOMException("Aborted","AbortError"))},0)};function A(y){try{return y===""&&o.location.href?o.location.href:y}catch{return y}}if(l.open(f.method,A(f.url),!0),f.credentials==="include"?l.withCredentials=!0:f.credentials==="omit"&&(l.withCredentials=!1),"responseType"in l&&(s.blob?l.responseType="blob":s.arrayBuffer&&(l.responseType="arraybuffer")),a&&typeof a.headers=="object"&&!(a.headers instanceof c||o.Headers&&a.headers instanceof o.Headers)){var V=[];Object.getOwnPropertyNames(a.headers).forEach(function(y){V.push(m(y)),l.setRequestHeader(y,E(a.headers[y]))}),f.headers.forEach(function(y,C){V.indexOf(C)===-1&&l.setRequestHeader(C,y)})}else f.headers.forEach(function(y,C){l.setRequestHeader(C,y)});f.signal&&(f.signal.addEventListener("abort",T),l.onreadystatechange=function(){l.readyState===4&&f.signal.removeEventListener("abort",T)}),l.send(typeof f._bodyInit>"u"?null:f._bodyInit)})}return O.polyfill=!0,o.fetch||(o.fetch=O,o.Headers=c,o.Request=F,o.Response=k),n.Headers=c,n.Request=F,n.Response=k,n.fetch=O,n}({})})(typeof self<"u"?self:q)});var g=[];for(let r=0;r<256;++r)g.push((r+256).toString(16).slice(1));function M(r,t=0){return(g[r[t+0]]+g[r[t+1]]+g[r[t+2]]+g[r[t+3]]+"-"+g[r[t+4]]+g[r[t+5]]+"-"+g[r[t+6]]+g[r[t+7]]+"-"+g[r[t+8]]+g[r[t+9]]+"-"+g[r[t+10]]+g[r[t+11]]+g[r[t+12]]+g[r[t+13]]+g[r[t+14]]+g[r[t+15]]).toLowerCase()}var U,ce=new Uint8Array(16);function L(){if(!U){if(typeof crypto>"u"||!crypto.getRandomValues)throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");U=crypto.getRandomValues.bind(crypto)}return U(ce)}var ue=typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto),P={randomUUID:ue};function fe(r,t,n){if(P.randomUUID&&!t&&!r)return P.randomUUID();r=r||{};let o=r.random??r.rng?.()??L();if(o.length<16)throw new Error("Random bytes length must be >= 16");if(o[6]=o[6]&15|64,o[8]=o[8]&63|128,t){if(n=n||0,n<0||n+16>t.length)throw new RangeError(`UUID byte range ${n}:${n+15} is out of buffer bounds`);for(let s=0;s<16;++s)t[n+s]=o[s];return t}return M(o)}var w=fe;var ze=le(W());function S(r){return he(r,!1)}function he(r,t){return r==null?r:{companyId:r.company_id==null?void 0:r.company_id,error:r.error==null?void 0:r.error,featureAllocation:r.feature_allocation==null?void 0:r.feature_allocation,featureUsage:r.feature_usage==null?void 0:r.feature_usage,featureUsagePeriod:r.feature_usage_period==null?void 0:r.feature_usage_period,featureUsageResetAt:r.feature_usage_reset_at==null?void 0:new Date(r.feature_usage_reset_at),flag:r.flag,flagId:r.flag_id==null?void 0:r.flag_id,reason:r.reason,ruleId:r.rule_id==null?void 0:r.rule_id,ruleType:r.rule_type==null?void 0:r.rule_type,userId:r.user_id==null?void 0:r.user_id,value:r.value}}function _(r){return ye(r,!1)}function ye(r,t){return r==null?r:{data:S(r.data),params:r.params}}function K(r){return ge(r,!1)}function ge(r,t){return r==null?r:{flags:r.flags.map(S)}}function I(r){return me(r,!1)}function me(r,t){return r==null?r:{data:K(r.data),params:r.params}}var G=r=>{let{companyId:t,error:n,featureAllocation:o,featureUsage:s,featureUsagePeriod:h,featureUsageResetAt:d,flag:p,flagId:m,reason:E,ruleId:b,ruleType:c,userId:R,value:x}=S(r);return{featureUsageExceeded:!x&&(c=="company_override_usage_exceeded"||c=="plan_entitlement_usage_exceeded"),companyId:t??void 0,error:n??void 0,featureAllocation:o??void 0,featureUsage:s??void 0,featureUsagePeriod:h??void 0,featureUsageResetAt:d??void 0,flag:p,flagId:m??void 0,reason:E,ruleId:b??void 0,ruleType:c??void 0,userId:R??void 0,value:x}};function v(r){let t=Object.keys(r).reduce((n,o)=>{let h=Object.keys(r[o]||{}).sort().reduce((d,p)=>(d[p]=r[o][p],d),{});return n[o]=h,n},{});return JSON.stringify(t)}var X="schematicId";var D=class{additionalHeaders={};apiKey;apiUrl="https://api.schematichq.com";conn=null;context={};eventQueue;eventUrl="https://c.schematichq.com";flagCheckListeners={};flagValueListeners={};isPending=!0;isPendingListeners=new Set;storage;useWebSocket=!1;checks={};webSocketUrl="wss://api.schematichq.com";constructor(t,n){this.apiKey=t,this.eventQueue=[],this.useWebSocket=n?.useWebSocket??!1,n?.additionalHeaders&&(this.additionalHeaders=n.additionalHeaders),n?.storage?this.storage=n.storage:typeof localStorage<"u"&&(this.storage=localStorage),n?.apiUrl!==void 0&&(this.apiUrl=n.apiUrl),n?.eventUrl!==void 0&&(this.eventUrl=n.eventUrl),n?.webSocketUrl!==void 0&&(this.webSocketUrl=n.webSocketUrl),typeof window<"u"&&window?.addEventListener&&window.addEventListener("beforeunload",()=>{this.flushEventQueue()})}async checkFlag(t){let{fallback:n=!1,key:o}=t,s=t.context||this.context,h=v(s);if(!this.useWebSocket){let d=`${this.apiUrl}/flags/${o}/check`;return fetch(d,{method:"POST",headers:{...this.additionalHeaders??{},"Content-Type":"application/json;charset=UTF-8","X-Schematic-Api-Key":this.apiKey},body:JSON.stringify(s)}).then(p=>{if(!p.ok)throw new Error("Network response was not ok");return p.json()}).then(p=>_(p).data.value).catch(p=>(console.error("There was a problem with the fetch operation:",p),n))}try{let d=this.checks[h];if(this.conn&&typeof d<"u"&&typeof d[o]<"u")return d[o].value;try{await this.setContext(s)}catch(m){return console.error("WebSocket connection failed, falling back to REST:",m),this.fallbackToRest(o,s,n)}return(this.checks[h]??{})[o]?.value??n}catch(d){return console.error("Unexpected error in checkFlag:",d),n}}async fallbackToRest(t,n,o){try{let s=`${this.apiUrl}/flags/${t}/check`,h=await fetch(s,{method:"POST",headers:{...this.additionalHeaders??{},"Content-Type":"application/json;charset=UTF-8","X-Schematic-Api-Key":this.apiKey},body:JSON.stringify(n)});if(!h.ok)throw new Error("Network response was not ok");return _(await h.json())?.data?.value??!1}catch(s){return console.error("REST API call failed, using fallback value:",s),o}}checkFlags=async t=>{t=t||this.context;let n=`${this.apiUrl}/flags/check`,o=JSON.stringify(t);return fetch(n,{method:"POST",headers:{...this.additionalHeaders??{},"Content-Type":"application/json;charset=UTF-8","X-Schematic-Api-Key":this.apiKey},body:o}).then(s=>{if(!s.ok)throw new Error("Network response was not ok");return s.json()}).then(s=>(I(s)?.data?.flags??[]).reduce((d,p)=>(d[p.flag]=p.value,d),{})).catch(s=>(console.error("There was a problem with the fetch operation:",s),{}))};identify=t=>{try{this.setContext({company:t.company?.keys,user:t.keys})}catch(n){console.error("Error setting context:",n)}return this.handleEvent("identify",t)};setContext=async t=>{if(!this.useWebSocket)return this.context=t,Promise.resolve();try{this.setIsPending(!0),this.conn||(this.conn=this.wsConnect());let n=await this.conn;await this.wsSendMessage(n,t)}catch(n){throw console.error("Failed to establish WebSocket connection:",n),n}};track=t=>{let{company:n,user:o,event:s,traits:h}=t;return this.handleEvent("track",{company:n??this.context.company,event:s,traits:h??{},user:o??this.context.user})};flushEventQueue=()=>{for(;this.eventQueue.length>0;){let t=this.eventQueue.shift();t&&this.sendEvent(t)}};getAnonymousId=()=>{if(!this.storage)return w();let t=this.storage.getItem(X);if(typeof t<"u")return t;let n=w();return this.storage.setItem(X,n),n};handleEvent=(t,n)=>{let o={api_key:this.apiKey,body:n,sent_at:new Date().toISOString(),tracker_event_id:w(),tracker_user_id:this.getAnonymousId(),type:t};return document?.hidden?this.storeEvent(o):this.sendEvent(o)};sendEvent=async t=>{let n=`${this.eventUrl}/e`,o=JSON.stringify(t);try{await fetch(n,{method:"POST",headers:{...this.additionalHeaders??{},"Content-Type":"application/json;charset=UTF-8"},body:o})}catch(s){console.error("Error sending Schematic event: ",s)}return Promise.resolve()};storeEvent=t=>(this.eventQueue.push(t),Promise.resolve());cleanup=async()=>{if(this.conn)try{(await this.conn).close()}catch(t){console.error("Error during cleanup:",t)}finally{this.conn=null}};wsConnect=()=>new Promise((t,n)=>{let o=`${this.webSocketUrl}/flags/bootstrap`,s=new WebSocket(o);s.onopen=()=>{t(s)},s.onerror=h=>{n(h)},s.onclose=()=>{this.conn=null}});wsSendMessage=(t,n)=>new Promise((o,s)=>{if(v(n)==v(this.context))return o(this.setIsPending(!1));this.context=n;let h=()=>{let d=!1,p=m=>{let E=JSON.parse(m.data);v(n)in this.checks||(this.checks[v(n)]={}),(E.flags??[]).forEach(b=>{let c=G(b);this.checks[v(n)][c.flag]=c,this.notifyFlagCheckListeners(b.flag,c),this.notifyFlagValueListeners(b.flag,c.value)}),this.setIsPending(!1),d||(d=!0,o())};t.addEventListener("message",p),t.send(JSON.stringify({apiKey:this.apiKey,data:n}))};t.readyState===WebSocket.OPEN?h():t.readyState===WebSocket.CONNECTING?t.addEventListener("open",h):s("WebSocket is not open or connecting")});getIsPending=()=>this.isPending;addIsPendingListener=t=>(this.isPendingListeners.add(t),()=>{this.isPendingListeners.delete(t)});setIsPending=t=>{this.isPending=t,this.isPendingListeners.forEach(n=>ke(n,t))};getFlagCheck=t=>{let n=v(this.context);return(this.checks[n]??{})[t]};getFlagValue=t=>this.getFlagCheck(t)?.value;addFlagValueListener=(t,n)=>(t in this.flagValueListeners||(this.flagValueListeners[t]=new Set),this.flagValueListeners[t].add(n),()=>{this.flagValueListeners[t].delete(n)});addFlagCheckListener=(t,n)=>(t in this.flagCheckListeners||(this.flagCheckListeners[t]=new Set),this.flagCheckListeners[t].add(n),()=>{this.flagCheckListeners[t].delete(n)});notifyFlagCheckListeners=(t,n)=>{(this.flagCheckListeners?.[t]??[]).forEach(s=>be(s,n))};notifyFlagValueListeners=(t,n)=>{(this.flagValueListeners?.[t]??[]).forEach(s=>Fe(s,n))}},ke=(r,t)=>{r.length>0?r(t):r()},be=(r,t)=>{r.length>0?r(t):r()},Fe=(r,t)=>{r.length>0?r(t):r()};window.Schematic=D;})();
3
3
  /* @preserve */