@schematichq/schematic-js 0.1.7 → 0.1.8
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/dist/schematic.browser.js +1 -1
- package/dist/schematic.cjs.js +6 -10
- package/dist/schematic.d.ts +1 -1
- package/dist/schematic.esm.js +6 -10
- package/package.json +1 -1
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";(()=>{var p,m=new Uint8Array(16);function u(){if(!p&&(p=typeof crypto<"u"&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!p))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return p(m)}var
|
|
1
|
+
"use strict";(()=>{var p,m=new Uint8Array(16);function u(){if(!p&&(p=typeof crypto<"u"&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!p))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return p(m)}var i=[];for(let n=0;n<256;++n)i.push((n+256).toString(16).slice(1));function v(n,e=0){return i[n[e+0]]+i[n[e+1]]+i[n[e+2]]+i[n[e+3]]+"-"+i[n[e+4]]+i[n[e+5]]+"-"+i[n[e+6]]+i[n[e+7]]+"-"+i[n[e+8]]+i[n[e+9]]+"-"+i[n[e+10]]+i[n[e+11]]+i[n[e+12]]+i[n[e+13]]+i[n[e+14]]+i[n[e+15]]}var f=typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto),y={randomUUID:f};function k(n,e,t){if(y.randomUUID&&!e&&!n)return y.randomUUID();n=n||{};let r=n.random||(n.rng||u)();if(r[6]=r[6]&15|64,r[8]=r[8]&63|128,e){t=t||0;for(let o=0;o<16;++o)e[t+o]=r[o];return e}return v(r)}var d=k;var g="schematicId";var l=class{apiKey;apiUrl="https://api.schematichq.com";webSocketUrl="wss://api.schematichq.com";eventUrl="https://c.schematichq.com";conn=null;context={};eventQueue;storage;useWebSocket=!1;values={};flagListener;constructor(e,t){this.apiKey=e,this.eventQueue=[],this.useWebSocket=t?.useWebSocket??!1,this.flagListener=t?.flagListener,t?.storage?this.storage=t.storage:typeof localStorage<"u"&&(this.storage=localStorage),t?.apiUrl!==void 0&&(this.apiUrl=t.apiUrl),t?.eventUrl!==void 0&&(this.eventUrl=t.eventUrl),t?.webSocketUrl!==void 0&&(this.webSocketUrl=t.webSocketUrl),window?.addEventListener&&window.addEventListener("beforeunload",()=>{this.flushEventQueue()})}async checkFlag(e){let{fallback:t=!1,key:r}=e,o=e.context||this.context;if(this.useWebSocket){let s=this.values[h(o)]??{};return typeof s[r]>"u"?t:s[r]}let a=`${this.apiUrl}/flags/${r}/check`;return fetch(a,{method:"POST",headers:{"X-Schematic-Api-Key":this.apiKey,"Content-Type":"application/json;charset=UTF-8"},body:JSON.stringify(o)}).then(s=>{if(!s.ok)throw new Error("Network response was not ok");return s.json()}).then(s=>s.data.value).catch(s=>(console.error("There was a problem with the fetch operation:",s),t))}checkFlags=async e=>{e=e||this.context;let t=`${this.apiUrl}/flags/check`,r=JSON.stringify(e);return fetch(t,{method:"POST",headers:{"Content-Type":"application/json;charset=UTF-8","X-Schematic-Api-Key":this.apiKey},body:r}).then(o=>{if(!o.ok)throw new Error("Network response was not ok");return o.json()}).then(o=>(o?.data?.flags??[]).reduce((a,s)=>(a[s.flag]=s.value,a),{})).catch(o=>(console.error("There was a problem with the fetch operation:",o),!1))};cleanup=()=>{this.conn&&this.conn.close()};identify=e=>{this.handleEvent("identify",e)};setContext=e=>this.useWebSocket?new Promise(t=>{this.wsConnect().then(()=>{this.wsSendMessage(e),t()})}):(this.context=e,Promise.resolve());track=e=>{this.handleEvent("track",e)};flushEventQueue=()=>{for(;this.eventQueue.length>0;){let e=this.eventQueue.shift();e&&this.sendEvent(e)}};getAnonymousId=()=>{if(!this.storage)return d();let e=this.storage.getItem(g);if(typeof e<"u")return e;let t=d();return this.storage.setItem(g,t),t};handleEvent=(e,t)=>{let r={api_key:this.apiKey,body:t,sent_at:new Date().toISOString(),tracker_event_id:d(),tracker_user_id:this.getAnonymousId(),type:e};document?.hidden?this.storeEvent(r):this.sendEvent(r)};sendEvent=e=>{let t=`${this.eventUrl}/e`,r=JSON.stringify(e);fetch(t,{method:"POST",headers:{"Content-Type":"application/json;charset=UTF-8"},body:r}).then(o=>{if(!o.ok)throw new Error(`Network response was not ok: ${o.statusText}`)}).catch(o=>{console.error("There was a problem with the fetch operation:",o)})};storeEvent=e=>{this.eventQueue.push(e)};wsConnect=()=>new Promise(e=>{this.conn&&e();let t=`${this.webSocketUrl}/flags/subscribe`,r=new WebSocket(t);this.conn=r,r.onopen=()=>{e()},r.onclose=()=>{this.conn=null}});wsSendMessage=e=>new Promise((t,r)=>{if(h(e)==h(this.context)&&t(),this.context=e,!this.conn){r("Not connected");return}if(this.conn.readyState===WebSocket.OPEN){let o=!1;this.conn.onmessage=a=>{(JSON.parse(a.data).flags??[]).forEach(c=>{this.values[h(e)][c.flag]=c.value}),this.flagListener&&this.flagListener(this.values[h(e)]),o||(o=!0,t())},this.conn.onerror=a=>{console.error("Schematic websocket error: ",a)},this.conn.send(JSON.stringify({apiKey:this.apiKey,data:e}))}else this.conn.readyState===WebSocket.CONNECTING?this.conn.onopen=()=>{this.wsSendMessage(e)}:r("Not connected")})};function h(n){let e=Object.keys(n).reduce((t,r)=>{let a=Object.keys(n[r]||{}).sort().reduce((s,c)=>(s[c]=n[r][c],s),{});return t[r]=a,t},{});return JSON.stringify(e)}window.Schematic=l;})();
|
|
2
2
|
/* @preserve */
|
package/dist/schematic.cjs.js
CHANGED
|
@@ -111,7 +111,7 @@ var Schematic = class {
|
|
|
111
111
|
});
|
|
112
112
|
}
|
|
113
113
|
}
|
|
114
|
-
|
|
114
|
+
async checkFlag(options) {
|
|
115
115
|
const { fallback = false, key } = options;
|
|
116
116
|
const context = options.context || this.context;
|
|
117
117
|
if (this.useWebSocket) {
|
|
@@ -137,7 +137,7 @@ var Schematic = class {
|
|
|
137
137
|
console.error("There was a problem with the fetch operation:", error);
|
|
138
138
|
return fallback;
|
|
139
139
|
});
|
|
140
|
-
}
|
|
140
|
+
}
|
|
141
141
|
// Make a REST API call to fetch all flag values for a given context
|
|
142
142
|
checkFlags = async (context) => {
|
|
143
143
|
context = context || this.context;
|
|
@@ -259,7 +259,7 @@ var Schematic = class {
|
|
|
259
259
|
if (this.conn) {
|
|
260
260
|
resolve();
|
|
261
261
|
}
|
|
262
|
-
const wsUrl = `${this.webSocketUrl}/flags/
|
|
262
|
+
const wsUrl = `${this.webSocketUrl}/flags/subscribe`;
|
|
263
263
|
const webSocket = new WebSocket(wsUrl);
|
|
264
264
|
this.conn = webSocket;
|
|
265
265
|
webSocket.onopen = () => {
|
|
@@ -285,13 +285,9 @@ var Schematic = class {
|
|
|
285
285
|
let resolved = false;
|
|
286
286
|
this.conn.onmessage = (event) => {
|
|
287
287
|
const message = JSON.parse(event.data);
|
|
288
|
-
|
|
289
|
-
(
|
|
290
|
-
|
|
291
|
-
return accum;
|
|
292
|
-
},
|
|
293
|
-
{}
|
|
294
|
-
);
|
|
288
|
+
(message.flags ?? []).forEach((flag) => {
|
|
289
|
+
this.values[contextString(context)][flag.flag] = flag.value;
|
|
290
|
+
});
|
|
295
291
|
if (this.flagListener) {
|
|
296
292
|
this.flagListener(this.values[contextString(context)]);
|
|
297
293
|
}
|
package/dist/schematic.d.ts
CHANGED
|
@@ -64,7 +64,7 @@ export declare class Schematic {
|
|
|
64
64
|
private values;
|
|
65
65
|
private flagListener?;
|
|
66
66
|
constructor(apiKey: string, options?: SchematicOptions);
|
|
67
|
-
checkFlag
|
|
67
|
+
checkFlag(options: CheckOptions): Promise<boolean>;
|
|
68
68
|
checkFlags: (context?: SchematicContext) => Promise<Record<string, boolean>>;
|
|
69
69
|
cleanup: () => void;
|
|
70
70
|
identify: (body: EventBodyIdentify) => void;
|
package/dist/schematic.esm.js
CHANGED
|
@@ -85,7 +85,7 @@ var Schematic = class {
|
|
|
85
85
|
});
|
|
86
86
|
}
|
|
87
87
|
}
|
|
88
|
-
|
|
88
|
+
async checkFlag(options) {
|
|
89
89
|
const { fallback = false, key } = options;
|
|
90
90
|
const context = options.context || this.context;
|
|
91
91
|
if (this.useWebSocket) {
|
|
@@ -111,7 +111,7 @@ var Schematic = class {
|
|
|
111
111
|
console.error("There was a problem with the fetch operation:", error);
|
|
112
112
|
return fallback;
|
|
113
113
|
});
|
|
114
|
-
}
|
|
114
|
+
}
|
|
115
115
|
// Make a REST API call to fetch all flag values for a given context
|
|
116
116
|
checkFlags = async (context) => {
|
|
117
117
|
context = context || this.context;
|
|
@@ -233,7 +233,7 @@ var Schematic = class {
|
|
|
233
233
|
if (this.conn) {
|
|
234
234
|
resolve();
|
|
235
235
|
}
|
|
236
|
-
const wsUrl = `${this.webSocketUrl}/flags/
|
|
236
|
+
const wsUrl = `${this.webSocketUrl}/flags/subscribe`;
|
|
237
237
|
const webSocket = new WebSocket(wsUrl);
|
|
238
238
|
this.conn = webSocket;
|
|
239
239
|
webSocket.onopen = () => {
|
|
@@ -259,13 +259,9 @@ var Schematic = class {
|
|
|
259
259
|
let resolved = false;
|
|
260
260
|
this.conn.onmessage = (event) => {
|
|
261
261
|
const message = JSON.parse(event.data);
|
|
262
|
-
|
|
263
|
-
(
|
|
264
|
-
|
|
265
|
-
return accum;
|
|
266
|
-
},
|
|
267
|
-
{}
|
|
268
|
-
);
|
|
262
|
+
(message.flags ?? []).forEach((flag) => {
|
|
263
|
+
this.values[contextString(context)][flag.flag] = flag.value;
|
|
264
|
+
});
|
|
269
265
|
if (this.flagListener) {
|
|
270
266
|
this.flagListener(this.values[contextString(context)]);
|
|
271
267
|
}
|
package/package.json
CHANGED