@schematichq/schematic-js 0.0.10 → 0.0.12

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 d,g=new Uint8Array(16);function u(){if(!d&&(d=typeof crypto<"u"&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!d))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return d(g)}var o=[];for(let t=0;t<256;++t)o.push((t+256).toString(16).slice(1));function h(t,e=0){return(o[t[e+0]]+o[t[e+1]]+o[t[e+2]]+o[t[e+3]]+"-"+o[t[e+4]]+o[t[e+5]]+"-"+o[t[e+6]]+o[t[e+7]]+"-"+o[t[e+8]]+o[t[e+9]]+"-"+o[t[e+10]]+o[t[e+11]]+o[t[e+12]]+o[t[e+13]]+o[t[e+14]]+o[t[e+15]]).toLowerCase()}var l=typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto),y={randomUUID:l};function m(t,e,n){if(y.randomUUID&&!e&&!t)return y.randomUUID();t=t||{};let i=t.random||(t.rng||u)();if(i[6]=i[6]&15|64,i[8]=i[8]&63|128,e){n=n||0;for(let r=0;r<16;++r)e[n+r]=i[r];return e}return h(i)}var a=m;var v="schematicId";var c=class{apiKey;eventQueue;storage;constructor(e,n){this.apiKey=e,this.eventQueue=[],n?this.storage=n:typeof localStorage<"u"&&(this.storage=localStorage)}sendEvent(e){let n="https://c.schematichq.com/e",i=JSON.stringify(e);fetch(n,{method:"POST",headers:{"Content-Type":"application/json;charset=UTF-8"},body:i}).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)})}flushEventQueue(){for(;this.eventQueue.length>0;){let e=this.eventQueue.shift();e&&this.sendEvent(e)}}storeEvent(e){this.eventQueue.push(e)}handleEvent(e,n){let i={api_key:this.apiKey,body:n,sent_at:new Date().toISOString(),tracker_event_id:a(),tracker_user_id:this.getAnonymousId(),type:e};typeof document<"u"&&document.hidden?this.storeEvent(i):this.sendEvent(i)}getAnonymousId(){if(!this.storage)return a();let e=this.storage.getItem(v);if(typeof e<"u")return e;let n=a();return this.storage.setItem(v,n),n}async checkFlag(e,n){if(!n.company&&!n.user)return Promise.resolve(!1);let i=`https://api.schematichq.com/flags/${e}/check`,r=JSON.stringify(n);return fetch(i,{method:"POST",headers:{"X-Schematic-Api-Key":this.apiKey,"Content-Type":"application/json;charset=UTF-8"},body: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),!1))}async checkFlags(e){if(!e.company&&!e.user)return Promise.resolve({});let n="https://api.schematichq.com/flags/check",i=JSON.stringify(e);return fetch(n,{method:"POST",headers:{"X-Schematic-Api-Key":this.apiKey,"Content-Type":"application/json;charset=UTF-8"},body:i}).then(r=>{if(!r.ok)throw new Error("Network response was not ok");return r.json()}).then(r=>r.data.flags.reduce((s,p)=>(s[p.flag]=p.value,s))).catch(r=>(console.error("There was a problem with the fetch operation:",r),!1))}identify(e){this.handleEvent("identify",e)}track(e){this.handleEvent("track",e)}initialize(){typeof window<"u"&&window.addEventListener("beforeunload",()=>{this.flushEventQueue()})}};window.Schematic=c;})();
1
+ "use strict";(()=>{var d,v=new Uint8Array(16);function y(){if(!d&&(d=typeof crypto<"u"&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!d))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return d(v)}var o=[];for(let t=0;t<256;++t)o.push((t+256).toString(16).slice(1));function h(t,e=0){return o[t[e+0]]+o[t[e+1]]+o[t[e+2]]+o[t[e+3]]+"-"+o[t[e+4]]+o[t[e+5]]+"-"+o[t[e+6]]+o[t[e+7]]+"-"+o[t[e+8]]+o[t[e+9]]+"-"+o[t[e+10]]+o[t[e+11]]+o[t[e+12]]+o[t[e+13]]+o[t[e+14]]+o[t[e+15]]}var m=typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto),c={randomUUID:m};function g(t,e,r){if(c.randomUUID&&!e&&!t)return c.randomUUID();t=t||{};let i=t.random||(t.rng||y)();if(i[6]=i[6]&15|64,i[8]=i[8]&63|128,e){r=r||0;for(let n=0;n<16;++n)e[r+n]=i[n];return e}return h(i)}var a=g;var l="schematicId";var p=class{apiKey;eventQueue;storage;constructor(e,r){this.apiKey=e,this.eventQueue=[],r?this.storage=r:typeof localStorage<"u"&&(this.storage=localStorage),typeof window<"u"&&window.addEventListener("beforeunload",()=>{this.flushEventQueue()})}sendEvent(e){let r="https://c.schematichq.com/e",i=JSON.stringify(e);fetch(r,{method:"POST",headers:{"Content-Type":"application/json;charset=UTF-8"},body:i}).then(n=>{if(!n.ok)throw new Error(`Network response was not ok: ${n.statusText}`)}).catch(n=>{console.error("There was a problem with the fetch operation:",n)})}flushEventQueue(){for(;this.eventQueue.length>0;){let e=this.eventQueue.shift();e&&this.sendEvent(e)}}storeEvent(e){this.eventQueue.push(e)}handleEvent(e,r){let i={api_key:this.apiKey,body:r,sent_at:new Date().toISOString(),tracker_event_id:a(),tracker_user_id:this.getAnonymousId(),type:e};typeof document<"u"&&document.hidden?this.storeEvent(i):this.sendEvent(i)}getAnonymousId(){if(!this.storage)return a();let e=this.storage.getItem(l);if(typeof e<"u")return e;let r=a();return this.storage.setItem(l,r),r}async checkFlag(e,r){if(!r.company)return Promise.resolve(!1);let i=`https://api.schematichq.com/flags/${e}/check`,n=JSON.stringify(r);return fetch(i,{method:"POST",headers:{"X-Schematic-Api-Key":this.apiKey,"Content-Type":"application/json;charset=UTF-8"},body:n}).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),!1))}async checkFlags(e){if(!e.company)return Promise.resolve({});let r="https://api.schematichq.com/flags/check",i=JSON.stringify(e);return fetch(r,{method:"POST",headers:{"Content-Type":"application/json;charset=UTF-8","X-Schematic-Api-Key":this.apiKey},body:i}).then(n=>{if(!n.ok)throw new Error("Network response was not ok");return n.json()}).then(n=>n.data.flags.reduce((s,u)=>(s[u.flag]=u.value,s),{})).catch(n=>(console.error("There was a problem with the fetch operation:",n),!1))}identify(e){this.handleEvent("identify",e)}track(e){this.handleEvent("track",e)}};window.Schematic=p;})();
2
2
  /* @preserve */
@@ -43,7 +43,7 @@ for (let i = 0; i < 256; ++i) {
43
43
  byteToHex.push((i + 256).toString(16).slice(1));
44
44
  }
45
45
  function unsafeStringify(arr, offset = 0) {
46
- return (byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + "-" + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + "-" + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + "-" + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + "-" + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]).toLowerCase();
46
+ return byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + "-" + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + "-" + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + "-" + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + "-" + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]];
47
47
  }
48
48
 
49
49
  // node_modules/uuid/dist/esm-browser/native.js
@@ -86,6 +86,11 @@ var Schematic = class {
86
86
  } else if (typeof localStorage !== "undefined") {
87
87
  this.storage = localStorage;
88
88
  }
89
+ if (typeof window !== "undefined") {
90
+ window.addEventListener("beforeunload", () => {
91
+ this.flushEventQueue();
92
+ });
93
+ }
89
94
  }
90
95
  sendEvent(event) {
91
96
  const captureUrl = "https://c.schematichq.com/e";
@@ -145,7 +150,7 @@ var Schematic = class {
145
150
  return generatedAnonymousId;
146
151
  }
147
152
  async checkFlag(key, context) {
148
- if (!context.company && !context.user) {
153
+ if (!context.company) {
149
154
  return Promise.resolve(false);
150
155
  }
151
156
  const requestUrl = `https://api.schematichq.com/flags/${key}/check`;
@@ -170,7 +175,7 @@ var Schematic = class {
170
175
  });
171
176
  }
172
177
  async checkFlags(context) {
173
- if (!context.company && !context.user) {
178
+ if (!context.company) {
174
179
  return Promise.resolve({});
175
180
  }
176
181
  const requestUrl = "https://api.schematichq.com/flags/check";
@@ -178,8 +183,8 @@ var Schematic = class {
178
183
  return fetch(requestUrl, {
179
184
  method: "POST",
180
185
  headers: {
181
- "X-Schematic-Api-Key": this.apiKey,
182
- "Content-Type": "application/json;charset=UTF-8"
186
+ "Content-Type": "application/json;charset=UTF-8",
187
+ "X-Schematic-Api-Key": this.apiKey
183
188
  },
184
189
  body: requestBody
185
190
  }).then((response) => {
@@ -188,10 +193,10 @@ var Schematic = class {
188
193
  }
189
194
  return response.json();
190
195
  }).then((data) => {
191
- return data.data.flags.reduce((acc, flag) => {
192
- acc[flag.flag] = flag.value;
193
- return acc;
194
- });
196
+ return data.data.flags.reduce((accum, flag) => {
197
+ accum[flag.flag] = flag.value;
198
+ return accum;
199
+ }, {});
195
200
  }).catch((error) => {
196
201
  console.error("There was a problem with the fetch operation:", error);
197
202
  return false;
@@ -203,12 +208,5 @@ var Schematic = class {
203
208
  track(body) {
204
209
  this.handleEvent("track", body);
205
210
  }
206
- initialize() {
207
- if (typeof window !== "undefined") {
208
- window.addEventListener("beforeunload", () => {
209
- this.flushEventQueue();
210
- });
211
- }
212
- }
213
211
  };
214
212
  /* @preserve */
@@ -1,28 +1,57 @@
1
- declare type EventBodyCompany = {
2
- keys: Record<string, string>;
1
+ declare type Event_2 = {
2
+ api_key: string;
3
+ body: EventBody;
4
+ sent_at: string;
5
+ tracker_event_id: string;
6
+ tracker_user_id: string;
7
+ type: EventType;
8
+ };
9
+ export { Event_2 as Event }
10
+
11
+ export declare type EventBody = EventBodyIdentify | EventBodyTrack;
12
+
13
+ export declare type EventBodyCompany = {
14
+ keys: Keys;
3
15
  name?: string;
4
- traits: Record<string, any>;
16
+ traits: Traits;
5
17
  };
6
18
 
7
- declare type EventBodyIdentify = {
19
+ export declare type EventBodyIdentify = {
8
20
  company?: EventBodyCompany;
9
- keys: Record<string, string>;
21
+ keys: Keys;
10
22
  name?: string;
11
- traits: Record<string, any>;
23
+ traits: Traits;
12
24
  };
13
25
 
14
- declare type EventBodyTrack = {
26
+ export declare type EventBodyTrack = {
27
+ company?: Keys;
15
28
  event: string;
16
- traits: Record<string, any>;
17
- company?: Record<string, string>;
18
- user?: Record<string, string>;
29
+ traits: Traits;
30
+ user?: Keys;
31
+ };
32
+
33
+ export declare type EventType = "identify" | "track";
34
+
35
+ export declare type FlagCheckContext = {
36
+ company?: Keys;
37
+ user?: Keys;
38
+ };
39
+
40
+ export declare type FlagCheckResponseBody = {
41
+ company_id?: string;
42
+ error?: string;
43
+ reason: string;
44
+ rule_id?: string;
45
+ user_id?: string;
46
+ value: boolean;
19
47
  };
20
48
 
21
- declare type FlagCheckContext = {
22
- company?: Record<string, string>;
23
- user?: Record<string, string>;
49
+ export declare type FlagCheckWithKeyResponseBody = FlagCheckResponseBody & {
50
+ flag: string;
24
51
  };
25
52
 
53
+ export declare type Keys = Record<string, string>;
54
+
26
55
  export declare class Schematic {
27
56
  private apiKey;
28
57
  private eventQueue;
@@ -37,7 +66,6 @@ export declare class Schematic {
37
66
  checkFlags(context: FlagCheckContext): Promise<Record<string, boolean>>;
38
67
  identify(body: EventBodyIdentify): void;
39
68
  track(body: EventBodyTrack): void;
40
- initialize(): void;
41
69
  }
42
70
 
43
71
  declare type StoragePersister = {
@@ -46,4 +74,6 @@ declare type StoragePersister = {
46
74
  removeItem(key: string): void;
47
75
  };
48
76
 
77
+ export declare type Traits = Record<string, any>;
78
+
49
79
  export { }
@@ -17,7 +17,7 @@ for (let i = 0; i < 256; ++i) {
17
17
  byteToHex.push((i + 256).toString(16).slice(1));
18
18
  }
19
19
  function unsafeStringify(arr, offset = 0) {
20
- return (byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + "-" + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + "-" + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + "-" + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + "-" + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]).toLowerCase();
20
+ return byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + "-" + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + "-" + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + "-" + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + "-" + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]];
21
21
  }
22
22
 
23
23
  // node_modules/uuid/dist/esm-browser/native.js
@@ -60,6 +60,11 @@ var Schematic = class {
60
60
  } else if (typeof localStorage !== "undefined") {
61
61
  this.storage = localStorage;
62
62
  }
63
+ if (typeof window !== "undefined") {
64
+ window.addEventListener("beforeunload", () => {
65
+ this.flushEventQueue();
66
+ });
67
+ }
63
68
  }
64
69
  sendEvent(event) {
65
70
  const captureUrl = "https://c.schematichq.com/e";
@@ -119,7 +124,7 @@ var Schematic = class {
119
124
  return generatedAnonymousId;
120
125
  }
121
126
  async checkFlag(key, context) {
122
- if (!context.company && !context.user) {
127
+ if (!context.company) {
123
128
  return Promise.resolve(false);
124
129
  }
125
130
  const requestUrl = `https://api.schematichq.com/flags/${key}/check`;
@@ -144,7 +149,7 @@ var Schematic = class {
144
149
  });
145
150
  }
146
151
  async checkFlags(context) {
147
- if (!context.company && !context.user) {
152
+ if (!context.company) {
148
153
  return Promise.resolve({});
149
154
  }
150
155
  const requestUrl = "https://api.schematichq.com/flags/check";
@@ -152,8 +157,8 @@ var Schematic = class {
152
157
  return fetch(requestUrl, {
153
158
  method: "POST",
154
159
  headers: {
155
- "X-Schematic-Api-Key": this.apiKey,
156
- "Content-Type": "application/json;charset=UTF-8"
160
+ "Content-Type": "application/json;charset=UTF-8",
161
+ "X-Schematic-Api-Key": this.apiKey
157
162
  },
158
163
  body: requestBody
159
164
  }).then((response) => {
@@ -162,10 +167,10 @@ var Schematic = class {
162
167
  }
163
168
  return response.json();
164
169
  }).then((data) => {
165
- return data.data.flags.reduce((acc, flag) => {
166
- acc[flag.flag] = flag.value;
167
- return acc;
168
- });
170
+ return data.data.flags.reduce((accum, flag) => {
171
+ accum[flag.flag] = flag.value;
172
+ return accum;
173
+ }, {});
169
174
  }).catch((error) => {
170
175
  console.error("There was a problem with the fetch operation:", error);
171
176
  return false;
@@ -177,13 +182,6 @@ var Schematic = class {
177
182
  track(body) {
178
183
  this.handleEvent("track", body);
179
184
  }
180
- initialize() {
181
- if (typeof window !== "undefined") {
182
- window.addEventListener("beforeunload", () => {
183
- this.flushEventQueue();
184
- });
185
- }
186
- }
187
185
  };
188
186
  export {
189
187
  Schematic
package/package.json CHANGED
@@ -44,5 +44,5 @@
44
44
  "test": "jest --config jest.config.js"
45
45
  },
46
46
  "types": "dist/schematic.d.ts",
47
- "version": "0.0.10"
47
+ "version": "0.0.12"
48
48
  }