@schematichq/schematic-js 0.0.11 → 0.0.13

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]]}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 g=typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto),c={randomUUID:g};function m(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=m;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){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){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 */
@@ -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,9 +150,6 @@ var Schematic = class {
145
150
  return generatedAnonymousId;
146
151
  }
147
152
  async checkFlag(key, context) {
148
- if (!context.company && !context.user) {
149
- return Promise.resolve(false);
150
- }
151
153
  const requestUrl = `https://api.schematichq.com/flags/${key}/check`;
152
154
  const requestBody = JSON.stringify(context);
153
155
  return fetch(requestUrl, {
@@ -170,16 +172,13 @@ var Schematic = class {
170
172
  });
171
173
  }
172
174
  async checkFlags(context) {
173
- if (!context.company && !context.user) {
174
- return Promise.resolve({});
175
- }
176
175
  const requestUrl = "https://api.schematichq.com/flags/check";
177
176
  const requestBody = JSON.stringify(context);
178
177
  return fetch(requestUrl, {
179
178
  method: "POST",
180
179
  headers: {
181
- "X-Schematic-Api-Key": this.apiKey,
182
- "Content-Type": "application/json;charset=UTF-8"
180
+ "Content-Type": "application/json;charset=UTF-8",
181
+ "X-Schematic-Api-Key": this.apiKey
183
182
  },
184
183
  body: requestBody
185
184
  }).then((response) => {
@@ -188,10 +187,10 @@ var Schematic = class {
188
187
  }
189
188
  return response.json();
190
189
  }).then((data) => {
191
- return data.data.flags.reduce((acc, flag) => {
192
- acc[flag.flag] = flag.value;
193
- return acc;
194
- });
190
+ return data.data.flags.reduce((accum, flag) => {
191
+ accum[flag.flag] = flag.value;
192
+ return accum;
193
+ }, {});
195
194
  }).catch((error) => {
196
195
  console.error("There was a problem with the fetch operation:", error);
197
196
  return false;
@@ -203,12 +202,5 @@ var Schematic = class {
203
202
  track(body) {
204
203
  this.handleEvent("track", body);
205
204
  }
206
- initialize() {
207
- if (typeof window !== "undefined") {
208
- window.addEventListener("beforeunload", () => {
209
- this.flushEventQueue();
210
- });
211
- }
212
- }
213
205
  };
214
206
  /* @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 { }
@@ -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,9 +124,6 @@ var Schematic = class {
119
124
  return generatedAnonymousId;
120
125
  }
121
126
  async checkFlag(key, context) {
122
- if (!context.company && !context.user) {
123
- return Promise.resolve(false);
124
- }
125
127
  const requestUrl = `https://api.schematichq.com/flags/${key}/check`;
126
128
  const requestBody = JSON.stringify(context);
127
129
  return fetch(requestUrl, {
@@ -144,16 +146,13 @@ var Schematic = class {
144
146
  });
145
147
  }
146
148
  async checkFlags(context) {
147
- if (!context.company && !context.user) {
148
- return Promise.resolve({});
149
- }
150
149
  const requestUrl = "https://api.schematichq.com/flags/check";
151
150
  const requestBody = JSON.stringify(context);
152
151
  return fetch(requestUrl, {
153
152
  method: "POST",
154
153
  headers: {
155
- "X-Schematic-Api-Key": this.apiKey,
156
- "Content-Type": "application/json;charset=UTF-8"
154
+ "Content-Type": "application/json;charset=UTF-8",
155
+ "X-Schematic-Api-Key": this.apiKey
157
156
  },
158
157
  body: requestBody
159
158
  }).then((response) => {
@@ -162,10 +161,10 @@ var Schematic = class {
162
161
  }
163
162
  return response.json();
164
163
  }).then((data) => {
165
- return data.data.flags.reduce((acc, flag) => {
166
- acc[flag.flag] = flag.value;
167
- return acc;
168
- });
164
+ return data.data.flags.reduce((accum, flag) => {
165
+ accum[flag.flag] = flag.value;
166
+ return accum;
167
+ }, {});
169
168
  }).catch((error) => {
170
169
  console.error("There was a problem with the fetch operation:", error);
171
170
  return false;
@@ -177,13 +176,6 @@ var Schematic = class {
177
176
  track(body) {
178
177
  this.handleEvent("track", body);
179
178
  }
180
- initialize() {
181
- if (typeof window !== "undefined") {
182
- window.addEventListener("beforeunload", () => {
183
- this.flushEventQueue();
184
- });
185
- }
186
- }
187
179
  };
188
180
  export {
189
181
  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.11"
47
+ "version": "0.0.13"
48
48
  }