@schematichq/schematic-js 1.2.0 → 1.2.1
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.
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use strict";(()=>{var
|
|
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 b(e,a){if(!(this instanceof b))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(b.prototype),b.prototype.clone=function(){return new b(this._bodyInit,{status:this.status,statusText:this.statusText,headers:new c(this.headers),url:this.url})},b.error=function(){var e=new b(null,{status:200,statusText:""});return e.ok=!1,e.status=0,e.type="error",e};var ee=[301,302,303,307,308];b.redirect=function(e,a){if(ee.indexOf(a)===-1)throw new RangeError("Invalid status code");return new b(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 b(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=b),n.Headers=c,n.Request=F,n.Response=b,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:k,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:k??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(k=>{let c=G(k);this.checks[v(n)][c.flag]=c,this.notifyFlagCheckListeners(k.flag,c),this.notifyFlagValueListeners(k.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=>be(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=>ke(s,n))};notifyFlagValueListeners=(t,n)=>{(this.flagValueListeners?.[t]??[]).forEach(s=>Fe(s,n))}},be=(r,t)=>{r.length>0?r(t):r()},ke=(r,t)=>{r.length>0?r(t):r()},Fe=(r,t)=>{r.length>0?r(t):r()};window.Schematic=D;})();
|
|
1
|
+
"use strict";(()=>{var re=Object.create;var M=Object.defineProperty;var ne=Object.getOwnPropertyDescriptor;var se=Object.getOwnPropertyNames;var ae=Object.getPrototypeOf,oe=Object.prototype.hasOwnProperty;var ie=(r,t)=>()=>(t||r((t={exports:{}}).exports,t),t.exports);var le=(r,t,n,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of se(t))!oe.call(r,a)&&a!==n&&M(r,a,{get:()=>t[a],enumerable:!(o=ne(t,a))||o.enumerable});return r};var ce=(r,t,n)=>(n=r!=null?re(ae(r)):{},le(t||!r||!r.__esModule?M(n,"default",{value:r,enumerable:!0}):n,r));var W=ie(K=>{(function(r){var t=function(n){var o=typeof globalThis<"u"&&globalThis||typeof r<"u"&&r||typeof global<"u"&&global||{},a={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(a.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 k(e){var s={next:function(){var i=e.shift();return{done:i===void 0,value:i}}};return a.iterable&&(s[Symbol.iterator]=function(){return s}),s}function c(e){this.map={},e instanceof c?e.forEach(function(s,i){this.append(i,s)},this):Array.isArray(e)?e.forEach(function(s){if(s.length!=2)throw new TypeError("Headers constructor: expected name/value pair to be length 2, found"+s.length);this.append(s[0],s[1])},this):e&&Object.getOwnPropertyNames(e).forEach(function(s){this.append(s,e[s])},this)}c.prototype.append=function(e,s){e=m(e),s=E(s);var i=this.map[e];this.map[e]=i?i+", "+s:s},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,s){this.map[m(e)]=E(s)},c.prototype.forEach=function(e,s){for(var i in this.map)this.map.hasOwnProperty(i)&&e.call(s,this.map[i],i,this)},c.prototype.keys=function(){var e=[];return this.forEach(function(s,i){e.push(i)}),k(e)},c.prototype.values=function(){var e=[];return this.forEach(function(s){e.push(s)}),k(e)},c.prototype.entries=function(){var e=[];return this.forEach(function(s,i){e.push([i,s])}),k(e)},a.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(s,i){e.onload=function(){s(e.result)},e.onerror=function(){i(e.error)}})}function B(e){var s=new FileReader,i=x(s);return s.readAsArrayBuffer(e),i}function Q(e){var s=new FileReader,i=x(s),u=/charset=([A-Za-z0-9_-]+)/.exec(e.type),f=u?u[1]:"utf-8";return s.readAsText(e,f),i}function j(e){for(var s=new Uint8Array(e),i=new Array(s.length),u=0;u<s.length;u++)i[u]=String.fromCharCode(s[u]);return i.join("")}function J(e){if(e.slice)return e.slice(0);var s=new Uint8Array(e.byteLength);return s.set(new Uint8Array(e)),s.buffer}function V(){return this.bodyUsed=!1,this._initBody=function(e){this.bodyUsed=this.bodyUsed,this._bodyInit=e,e?typeof e=="string"?this._bodyText=e:a.blob&&Blob.prototype.isPrototypeOf(e)?this._bodyBlob=e:a.formData&&FormData.prototype.isPrototypeOf(e)?this._bodyFormData=e:a.searchParams&&URLSearchParams.prototype.isPrototypeOf(e)?this._bodyText=e.toString():a.arrayBuffer&&a.blob&&h(e)?(this._bodyArrayBuffer=J(e.buffer),this._bodyInit=new Blob([this._bodyArrayBuffer])):a.arrayBuffer&&(ArrayBuffer.prototype.isPrototypeOf(e)||p(e))?this._bodyArrayBuffer=J(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):a.searchParams&&URLSearchParams.prototype.isPrototypeOf(e)&&this.headers.set("content-type","application/x-www-form-urlencoded;charset=UTF-8"))},a.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(a.blob)return this.blob().then(B);throw new Error("could not read as ArrayBuffer")}},this.text=function(){var e=R(this);if(e)return e;if(this._bodyBlob)return Q(this._bodyBlob);if(this._bodyArrayBuffer)return Promise.resolve(j(this._bodyArrayBuffer));if(this._bodyFormData)throw new Error("could not read FormData body as text");return Promise.resolve(this._bodyText)},a.formData&&(this.formData=function(){return this.text().then(Z)}),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 s=e.toUpperCase();return z.indexOf(s)>-1?s:e}function F(e,s){if(!(this instanceof F))throw new TypeError('Please use the "new" operator, this DOM object constructor cannot be called as a function.');s=s||{};var i=s.body;if(e instanceof F){if(e.bodyUsed)throw new TypeError("Already read");this.url=e.url,this.credentials=e.credentials,s.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=s.credentials||this.credentials||"same-origin",(s.headers||!this.headers)&&(this.headers=new c(s.headers)),this.method=Y(s.method||this.method||"GET"),this.mode=s.mode||this.mode||null,this.signal=s.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")&&(s.cache==="no-store"||s.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 Z(e){var s=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," ");s.append(decodeURIComponent(f),decodeURIComponent(l))}}),s}function ee(e){var s=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{s.append(l,T)}catch(A){console.warn("Response "+A.message)}}}),s}V.call(F.prototype);function b(e,s){if(!(this instanceof b))throw new TypeError('Please use the "new" operator, this DOM object constructor cannot be called as a function.');if(s||(s={}),this.type="default",this.status=s.status===void 0?200:s.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=s.statusText===void 0?"":""+s.statusText,this.headers=new c(s.headers),this.url=s.url||"",this._initBody(e)}V.call(b.prototype),b.prototype.clone=function(){return new b(this._bodyInit,{status:this.status,statusText:this.statusText,headers:new c(this.headers),url:this.url})},b.error=function(){var e=new b(null,{status:200,statusText:""});return e.ok=!1,e.status=0,e.type="error",e};var te=[301,302,303,307,308];b.redirect=function(e,s){if(te.indexOf(s)===-1)throw new RangeError("Invalid status code");return new b(null,{status:s,headers:{location:e}})},n.DOMException=o.DOMException;try{new n.DOMException}catch{n.DOMException=function(s,i){this.message=s,this.name=i;var u=Error(s);this.stack=u.stack},n.DOMException.prototype=Object.create(Error.prototype),n.DOMException.prototype.constructor=n.DOMException}function O(e,s){return new Promise(function(i,u){var f=new F(e,s);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:ee(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 b(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&&(a.blob?l.responseType="blob":a.arrayBuffer&&(l.responseType="arraybuffer")),s&&typeof s.headers=="object"&&!(s.headers instanceof c||o.Headers&&s.headers instanceof o.Headers)){var H=[];Object.getOwnPropertyNames(s.headers).forEach(function(y){H.push(m(y)),l.setRequestHeader(y,E(s.headers[y]))}),f.headers.forEach(function(y,C){H.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=b),n.Headers=c,n.Request=F,n.Response=b,n.fetch=O,n}({})})(typeof self<"u"?self:K)});var g=[];for(let r=0;r<256;++r)g.push((r+256).toString(16).slice(1));function q(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,ue=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(ue)}var fe=typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto),P={randomUUID:fe};function de(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 a=0;a<16;++a)t[n+a]=o[a];return t}return q(o)}var w=de;var Ye=ce(W());function S(r){return pe(r,!1)}function pe(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 ge(r,!1)}function ge(r,t){return r==null?r:{data:S(r.data),params:r.params}}function $(r){return me(r,!1)}function me(r,t){return r==null?r:{flags:r.flags.map(S)}}function I(r){return be(r,!1)}function be(r,t){return r==null?r:{data:$(r.data),params:r.params}}var X=r=>{let{companyId:t,error:n,featureAllocation:o,featureUsage:a,featureUsagePeriod:h,featureUsageResetAt:d,flag:p,flagId:m,reason:E,ruleId:k,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:a??void 0,featureUsagePeriod:h??void 0,featureUsageResetAt:d??void 0,flag:p,flagId:m??void 0,reason:E,ruleId:k??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 N="1.2.1";var G="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,this.additionalHeaders={"X-Schematic-Client-Version":`schematic-js@${N}`,...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,a=t.context||this.context,h=v(a);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(a)}).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(a)}catch(m){return console.error("WebSocket connection failed, falling back to REST:",m),this.fallbackToRest(o,a,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 a=`${this.apiUrl}/flags/${t}/check`,h=await fetch(a,{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(a){return console.error("REST API call failed, using fallback value:",a),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(a=>{if(!a.ok)throw new Error("Network response was not ok");return a.json()}).then(a=>(I(a)?.data?.flags??[]).reduce((d,p)=>(d[p.flag]=p.value,d),{})).catch(a=>(console.error("There was a problem with the fetch operation:",a),{}))};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:a,traits:h}=t;return this.handleEvent("track",{company:n??this.context.company,event:a,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(G);if(typeof t<"u")return t;let n=w();return this.storage.setItem(G,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(a){console.error("Error sending Schematic event: ",a)}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?apiKey=${this.apiKey}`,a=new WebSocket(o);a.onopen=()=>{t(a)},a.onerror=h=>{n(h)},a.onclose=()=>{this.conn=null}});wsSendMessage=(t,n)=>new Promise((o,a)=>{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(k=>{let c=X(k);this.checks[v(n)][c.flag]=c,this.notifyFlagCheckListeners(k.flag,c),this.notifyFlagValueListeners(k.flag,c.value)}),this.setIsPending(!1),d||(d=!0,o())};t.addEventListener("message",p),t.send(JSON.stringify({apiKey:this.apiKey,clientVersion:`schematic-js@${N}`,data:n}))};t.readyState===WebSocket.OPEN?h():t.readyState===WebSocket.CONNECTING?t.addEventListener("open",h):a("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(a=>Fe(a,n))};notifyFlagValueListeners=(t,n)=>{(this.flagValueListeners?.[t]??[]).forEach(a=>ve(a,n))}},ke=(r,t)=>{r.length>0?r(t):r()},Fe=(r,t)=>{r.length>0?r(t):r()},ve=(r,t)=>{r.length>0?r(t):r()};window.Schematic=D;})();
|
|
3
3
|
/* @preserve */
|
package/dist/schematic.cjs.js
CHANGED
|
@@ -770,6 +770,9 @@ function contextString(context) {
|
|
|
770
770
|
return JSON.stringify(sortedContext);
|
|
771
771
|
}
|
|
772
772
|
|
|
773
|
+
// src/version.ts
|
|
774
|
+
var version = "1.2.1";
|
|
775
|
+
|
|
773
776
|
// src/index.ts
|
|
774
777
|
var anonymousIdKey = "schematicId";
|
|
775
778
|
var Schematic = class {
|
|
@@ -792,9 +795,10 @@ var Schematic = class {
|
|
|
792
795
|
this.apiKey = apiKey;
|
|
793
796
|
this.eventQueue = [];
|
|
794
797
|
this.useWebSocket = options?.useWebSocket ?? false;
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
+
this.additionalHeaders = {
|
|
799
|
+
"X-Schematic-Client-Version": `schematic-js@${version}`,
|
|
800
|
+
...options?.additionalHeaders ?? {}
|
|
801
|
+
};
|
|
798
802
|
if (options?.storage) {
|
|
799
803
|
this.storage = options.storage;
|
|
800
804
|
} else if (typeof localStorage !== "undefined") {
|
|
@@ -1063,7 +1067,7 @@ var Schematic = class {
|
|
|
1063
1067
|
// Open a websocket connection
|
|
1064
1068
|
wsConnect = () => {
|
|
1065
1069
|
return new Promise((resolve, reject) => {
|
|
1066
|
-
const wsUrl = `${this.webSocketUrl}/flags/bootstrap`;
|
|
1070
|
+
const wsUrl = `${this.webSocketUrl}/flags/bootstrap?apiKey=${this.apiKey}`;
|
|
1067
1071
|
const webSocket = new WebSocket(wsUrl);
|
|
1068
1072
|
webSocket.onopen = () => {
|
|
1069
1073
|
resolve(webSocket);
|
|
@@ -1107,6 +1111,7 @@ var Schematic = class {
|
|
|
1107
1111
|
socket.send(
|
|
1108
1112
|
JSON.stringify({
|
|
1109
1113
|
apiKey: this.apiKey,
|
|
1114
|
+
clientVersion: `schematic-js@${version}`,
|
|
1110
1115
|
data: context
|
|
1111
1116
|
})
|
|
1112
1117
|
);
|
package/dist/schematic.esm.js
CHANGED
|
@@ -752,6 +752,9 @@ function contextString(context) {
|
|
|
752
752
|
return JSON.stringify(sortedContext);
|
|
753
753
|
}
|
|
754
754
|
|
|
755
|
+
// src/version.ts
|
|
756
|
+
var version = "1.2.1";
|
|
757
|
+
|
|
755
758
|
// src/index.ts
|
|
756
759
|
var anonymousIdKey = "schematicId";
|
|
757
760
|
var Schematic = class {
|
|
@@ -774,9 +777,10 @@ var Schematic = class {
|
|
|
774
777
|
this.apiKey = apiKey;
|
|
775
778
|
this.eventQueue = [];
|
|
776
779
|
this.useWebSocket = options?.useWebSocket ?? false;
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
+
this.additionalHeaders = {
|
|
781
|
+
"X-Schematic-Client-Version": `schematic-js@${version}`,
|
|
782
|
+
...options?.additionalHeaders ?? {}
|
|
783
|
+
};
|
|
780
784
|
if (options?.storage) {
|
|
781
785
|
this.storage = options.storage;
|
|
782
786
|
} else if (typeof localStorage !== "undefined") {
|
|
@@ -1045,7 +1049,7 @@ var Schematic = class {
|
|
|
1045
1049
|
// Open a websocket connection
|
|
1046
1050
|
wsConnect = () => {
|
|
1047
1051
|
return new Promise((resolve, reject) => {
|
|
1048
|
-
const wsUrl = `${this.webSocketUrl}/flags/bootstrap`;
|
|
1052
|
+
const wsUrl = `${this.webSocketUrl}/flags/bootstrap?apiKey=${this.apiKey}`;
|
|
1049
1053
|
const webSocket = new WebSocket(wsUrl);
|
|
1050
1054
|
webSocket.onopen = () => {
|
|
1051
1055
|
resolve(webSocket);
|
|
@@ -1089,6 +1093,7 @@ var Schematic = class {
|
|
|
1089
1093
|
socket.send(
|
|
1090
1094
|
JSON.stringify({
|
|
1091
1095
|
apiKey: this.apiKey,
|
|
1096
|
+
clientVersion: `schematic-js@${version}`,
|
|
1092
1097
|
data: context
|
|
1093
1098
|
})
|
|
1094
1099
|
);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@schematichq/schematic-js",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"main": "dist/schematic.cjs.js",
|
|
5
5
|
"module": "dist/schematic.esm.js",
|
|
6
6
|
"types": "dist/schematic.d.ts",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"url": "git+ssh://git@github.com/SchematicHQ/schematic-js.git"
|
|
18
18
|
},
|
|
19
19
|
"scripts": {
|
|
20
|
-
"build": "npx tsc && yarn clean && yarn build:browser && yarn build:cjs && yarn build:esm && yarn build:types",
|
|
20
|
+
"build": "./version.sh && npx tsc && yarn clean && yarn build:browser && yarn build:cjs && yarn build:esm && yarn build:types",
|
|
21
21
|
"build:browser": "npx esbuild src/browser.ts --bundle --minify --outfile=dist/schematic.browser.js --platform=browser",
|
|
22
22
|
"build:cjs": "npx esbuild src/index.ts --bundle --format=cjs --outfile=dist/schematic.cjs.js",
|
|
23
23
|
"build:esm": "npx esbuild src/index.ts --bundle --format=esm --outfile=dist/schematic.esm.js",
|