@schematichq/schematic-js 0.1.0 → 0.1.2
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,2 +1,2 @@
|
|
|
1
|
-
"use strict";(()=>{var
|
|
1
|
+
"use strict";(()=>{var p,f=new Uint8Array(16);function y(){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(f)}var i=[];for(let t=0;t<256;++t)i.push((t+256).toString(16).slice(1));function m(t,e=0){return i[t[e+0]]+i[t[e+1]]+i[t[e+2]]+i[t[e+3]]+"-"+i[t[e+4]]+i[t[e+5]]+"-"+i[t[e+6]]+i[t[e+7]]+"-"+i[t[e+8]]+i[t[e+9]]+"-"+i[t[e+10]]+i[t[e+11]]+i[t[e+12]]+i[t[e+13]]+i[t[e+14]]+i[t[e+15]]}var k=typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto),l={randomUUID:k};function S(t,e,n){if(l.randomUUID&&!e&&!t)return l.randomUUID();t=t||{};let o=t.random||(t.rng||y)();if(o[6]=o[6]&15|64,o[8]=o[8]&63|128,e){n=n||0;for(let r=0;r<16;++r)e[n+r]=o[r];return e}return m(o)}var d=S;var v="schematicId";var u=class{apiKey;conn=null;context={};eventQueue;storage;useWebSocket=!1;values={};flagListener;constructor(e,n){this.apiKey=e,this.eventQueue=[],this.useWebSocket=n?.useWebSocket??!1,this.flagListener=n?.flagListener,n?.storage?this.storage=n.storage:typeof localStorage<"u"&&(this.storage=localStorage),typeof window<"u"&&window.addEventListener("beforeunload",()=>{this.flushEventQueue()})}checkFlag=async e=>{let{fallback:n=!1,key:o}=e,r=e.context||this.context;if(this.useWebSocket){let s=this.values[h(r)]??{};return typeof s[o]>"u"?n:s[o]}let a=`https://api.schematichq.com/flags/${o}/check`;return fetch(a,{method:"POST",headers:{"X-Schematic-Api-Key":this.apiKey,"Content-Type":"application/json;charset=UTF-8"},body:JSON.stringify(r)}).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),n))};checkFlags=async e=>{e=e||this.context;let n="https://api.schematichq.com/flags/check",o=JSON.stringify(e);return fetch(n,{method:"POST",headers:{"Content-Type":"application/json;charset=UTF-8","X-Schematic-Api-Key":this.apiKey},body:o}).then(r=>{if(!r.ok)throw new Error("Network response was not ok");return r.json()}).then(r=>(r?.data?.flags??[]).reduce((a,s)=>(a[s.flag]=s.value,a),{})).catch(r=>(console.error("There was a problem with the fetch operation:",r),!1))};cleanup=()=>{this.conn&&this.conn.close()};identify=e=>{this.handleEvent("identify",e)};setContext=e=>this.useWebSocket?new Promise(n=>{this.wsConnect().then(()=>{this.wsSendMessage(e),n()})}):(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(v);if(typeof e<"u")return e;let n=d();return this.storage.setItem(v,n),n};handleEvent=(e,n)=>{let o={api_key:this.apiKey,body:n,sent_at:new Date().toISOString(),tracker_event_id:d(),tracker_user_id:this.getAnonymousId(),type:e};typeof document<"u"&&document.hidden?this.storeEvent(o):this.sendEvent(o)};sendEvent=e=>{let n="https://c.schematichq.com/e",o=JSON.stringify(e);fetch(n,{method:"POST",headers:{"Content-Type":"application/json;charset=UTF-8"},body:o}).then(r=>{if(!r.ok)throw new Error(`Network response was not ok: ${r.statusText}`)}).catch(r=>{console.error("There was a problem with the fetch operation:",r)})};storeEvent=e=>{this.eventQueue.push(e)};wsConnect=()=>new Promise(e=>{this.conn&&e();let n="wss://api.schematichq.com/flags/bootstrap",o=new WebSocket(n);this.conn=o,o.onopen=()=>{e()},o.onclose=()=>{this.conn=null}});wsSendMessage=e=>new Promise((n,o)=>{if(h(e)==h(this.context)&&n(),this.context=e,!this.conn){o("Not connected");return}if(this.conn.readyState===WebSocket.OPEN){let r=!1;this.conn.onmessage=a=>{let s=JSON.parse(a.data);this.values[h(e)]=(s.flags??[]).reduce((c,g)=>(c[g.flag]=g.value,c),{}),this.flagListener&&this.flagListener(this.values[h(e)]),r||(r=!0,n())},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)}:o("Not connected")})};function h(t){let e=Object.keys(t).reduce((n,o)=>{let a=Object.keys(t[o]||{}).sort().reduce((s,c)=>(s[c]=t[o][c],s),{});return n[o]=a,n},{});return JSON.stringify(e)}window.Schematic=u;})();
|
|
2
2
|
/* @preserve */
|
package/dist/schematic.cjs.js
CHANGED
|
@@ -82,10 +82,12 @@ var Schematic = class {
|
|
|
82
82
|
storage;
|
|
83
83
|
useWebSocket = false;
|
|
84
84
|
values = {};
|
|
85
|
+
flagListener;
|
|
85
86
|
constructor(apiKey, options) {
|
|
86
87
|
this.apiKey = apiKey;
|
|
87
88
|
this.eventQueue = [];
|
|
88
89
|
this.useWebSocket = options?.useWebSocket ?? false;
|
|
90
|
+
this.flagListener = options?.flagListener;
|
|
89
91
|
if (options?.storage) {
|
|
90
92
|
this.storage = options.storage;
|
|
91
93
|
} else if (typeof localStorage !== "undefined") {
|
|
@@ -278,6 +280,9 @@ var Schematic = class {
|
|
|
278
280
|
},
|
|
279
281
|
{}
|
|
280
282
|
);
|
|
283
|
+
if (this.flagListener) {
|
|
284
|
+
this.flagListener(this.values[contextString(context)]);
|
|
285
|
+
}
|
|
281
286
|
if (!resolved) {
|
|
282
287
|
resolved = true;
|
|
283
288
|
resolve();
|
package/dist/schematic.d.ts
CHANGED
|
@@ -59,6 +59,7 @@ export declare class Schematic {
|
|
|
59
59
|
private storage;
|
|
60
60
|
private useWebSocket;
|
|
61
61
|
private values;
|
|
62
|
+
private flagListener?;
|
|
62
63
|
constructor(apiKey: string, options?: SchematicOptions);
|
|
63
64
|
checkFlag: (options: CheckOptions) => Promise<boolean>;
|
|
64
65
|
checkFlags: (context?: SchematicContext) => Promise<Record<string, boolean>>;
|
|
@@ -81,6 +82,7 @@ export declare type SchematicContext = {
|
|
|
81
82
|
};
|
|
82
83
|
|
|
83
84
|
declare type SchematicOptions = {
|
|
85
|
+
flagListener?: (values: Record<string, boolean>) => void;
|
|
84
86
|
storage?: StoragePersister;
|
|
85
87
|
useWebSocket?: boolean;
|
|
86
88
|
};
|
package/dist/schematic.esm.js
CHANGED
|
@@ -56,10 +56,12 @@ var Schematic = class {
|
|
|
56
56
|
storage;
|
|
57
57
|
useWebSocket = false;
|
|
58
58
|
values = {};
|
|
59
|
+
flagListener;
|
|
59
60
|
constructor(apiKey, options) {
|
|
60
61
|
this.apiKey = apiKey;
|
|
61
62
|
this.eventQueue = [];
|
|
62
63
|
this.useWebSocket = options?.useWebSocket ?? false;
|
|
64
|
+
this.flagListener = options?.flagListener;
|
|
63
65
|
if (options?.storage) {
|
|
64
66
|
this.storage = options.storage;
|
|
65
67
|
} else if (typeof localStorage !== "undefined") {
|
|
@@ -252,6 +254,9 @@ var Schematic = class {
|
|
|
252
254
|
},
|
|
253
255
|
{}
|
|
254
256
|
);
|
|
257
|
+
if (this.flagListener) {
|
|
258
|
+
this.flagListener(this.values[contextString(context)]);
|
|
259
|
+
}
|
|
255
260
|
if (!resolved) {
|
|
256
261
|
resolved = true;
|
|
257
262
|
resolve();
|
package/package.json
CHANGED
|
@@ -36,13 +36,13 @@
|
|
|
36
36
|
"scripts": {
|
|
37
37
|
"build": "npx tsc && yarn clean && yarn build:browser && yarn build:cjs && yarn build:esm && yarn build:types",
|
|
38
38
|
"build:browser": "npx esbuild src/browser.ts --bundle --minify --outfile=dist/schematic.browser.js --platform=browser",
|
|
39
|
-
"build:cjs": "npx esbuild src/index.ts --bundle --format=cjs --outfile=dist/schematic.cjs.js
|
|
40
|
-
"build:esm": "npx esbuild src/index.ts --bundle --format=esm --outfile=dist/schematic.esm.js
|
|
39
|
+
"build:cjs": "npx esbuild src/index.ts --bundle --format=cjs --outfile=dist/schematic.cjs.js",
|
|
40
|
+
"build:esm": "npx esbuild src/index.ts --bundle --format=esm --outfile=dist/schematic.esm.js",
|
|
41
41
|
"build:types": "npx tsc && npx api-extractor run",
|
|
42
42
|
"clean": "rm -rf dist",
|
|
43
43
|
"lint": "eslint src --ext ts,tsx --report-unused-disable-directives --fix",
|
|
44
44
|
"test": "jest --config jest.config.js"
|
|
45
45
|
},
|
|
46
46
|
"types": "dist/schematic.d.ts",
|
|
47
|
-
"version": "0.1.
|
|
47
|
+
"version": "0.1.2"
|
|
48
48
|
}
|