@schematichq/schematic-js 0.0.10
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/README.md +60 -0
- package/dist/schematic.browser.js +2 -0
- package/dist/schematic.cjs.js +214 -0
- package/dist/schematic.d.ts +49 -0
- package/dist/schematic.esm.js +191 -0
- package/package.json +48 -0
package/README.md
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# schematic-js
|
|
2
|
+
|
|
3
|
+
## Install
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
yarn install
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Build
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
yarn build
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage example
|
|
16
|
+
|
|
17
|
+
```javascript
|
|
18
|
+
const schematic = new Schematic("your-api-key");
|
|
19
|
+
|
|
20
|
+
// Send an identify event
|
|
21
|
+
const userId = "my-user-id";
|
|
22
|
+
const keys = {
|
|
23
|
+
id: userId,
|
|
24
|
+
};
|
|
25
|
+
const traits = {
|
|
26
|
+
anykey: "anyval",
|
|
27
|
+
};
|
|
28
|
+
const company = {
|
|
29
|
+
name: "My Company",
|
|
30
|
+
keys: {
|
|
31
|
+
id: "my-company-id",
|
|
32
|
+
},
|
|
33
|
+
traits: {
|
|
34
|
+
location: "Atlanta, GA",
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
schematic.identify({ keys, traits, company });
|
|
38
|
+
|
|
39
|
+
// Send a track event
|
|
40
|
+
const event = "query";
|
|
41
|
+
const traits = {
|
|
42
|
+
};
|
|
43
|
+
const company = {
|
|
44
|
+
};
|
|
45
|
+
const user = {
|
|
46
|
+
},
|
|
47
|
+
schematic.track({
|
|
48
|
+
event: "query",
|
|
49
|
+
traits: {
|
|
50
|
+
feature: "feat_cns2asuKAG2",
|
|
51
|
+
},
|
|
52
|
+
company: {
|
|
53
|
+
id: "my-company-id",
|
|
54
|
+
},
|
|
55
|
+
name: "My User",
|
|
56
|
+
user: {
|
|
57
|
+
id: "my-user-id",
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
```
|
|
@@ -0,0 +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;})();
|
|
2
|
+
/* @preserve */
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
Schematic: () => Schematic
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(src_exports);
|
|
26
|
+
|
|
27
|
+
// node_modules/uuid/dist/esm-browser/rng.js
|
|
28
|
+
var getRandomValues;
|
|
29
|
+
var rnds8 = new Uint8Array(16);
|
|
30
|
+
function rng() {
|
|
31
|
+
if (!getRandomValues) {
|
|
32
|
+
getRandomValues = typeof crypto !== "undefined" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto);
|
|
33
|
+
if (!getRandomValues) {
|
|
34
|
+
throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return getRandomValues(rnds8);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// node_modules/uuid/dist/esm-browser/stringify.js
|
|
41
|
+
var byteToHex = [];
|
|
42
|
+
for (let i = 0; i < 256; ++i) {
|
|
43
|
+
byteToHex.push((i + 256).toString(16).slice(1));
|
|
44
|
+
}
|
|
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();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// node_modules/uuid/dist/esm-browser/native.js
|
|
50
|
+
var randomUUID = typeof crypto !== "undefined" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
|
|
51
|
+
var native_default = {
|
|
52
|
+
randomUUID
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// node_modules/uuid/dist/esm-browser/v4.js
|
|
56
|
+
function v4(options, buf, offset) {
|
|
57
|
+
if (native_default.randomUUID && !buf && !options) {
|
|
58
|
+
return native_default.randomUUID();
|
|
59
|
+
}
|
|
60
|
+
options = options || {};
|
|
61
|
+
const rnds = options.random || (options.rng || rng)();
|
|
62
|
+
rnds[6] = rnds[6] & 15 | 64;
|
|
63
|
+
rnds[8] = rnds[8] & 63 | 128;
|
|
64
|
+
if (buf) {
|
|
65
|
+
offset = offset || 0;
|
|
66
|
+
for (let i = 0; i < 16; ++i) {
|
|
67
|
+
buf[offset + i] = rnds[i];
|
|
68
|
+
}
|
|
69
|
+
return buf;
|
|
70
|
+
}
|
|
71
|
+
return unsafeStringify(rnds);
|
|
72
|
+
}
|
|
73
|
+
var v4_default = v4;
|
|
74
|
+
|
|
75
|
+
// src/index.ts
|
|
76
|
+
var anonymousIdKey = "schematicId";
|
|
77
|
+
var Schematic = class {
|
|
78
|
+
apiKey;
|
|
79
|
+
eventQueue;
|
|
80
|
+
storage;
|
|
81
|
+
constructor(apiKey, storage) {
|
|
82
|
+
this.apiKey = apiKey;
|
|
83
|
+
this.eventQueue = [];
|
|
84
|
+
if (storage) {
|
|
85
|
+
this.storage = storage;
|
|
86
|
+
} else if (typeof localStorage !== "undefined") {
|
|
87
|
+
this.storage = localStorage;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
sendEvent(event) {
|
|
91
|
+
const captureUrl = "https://c.schematichq.com/e";
|
|
92
|
+
const payload = JSON.stringify(event);
|
|
93
|
+
fetch(captureUrl, {
|
|
94
|
+
method: "POST",
|
|
95
|
+
headers: {
|
|
96
|
+
"Content-Type": "application/json;charset=UTF-8"
|
|
97
|
+
},
|
|
98
|
+
body: payload
|
|
99
|
+
}).then((response) => {
|
|
100
|
+
if (!response.ok) {
|
|
101
|
+
throw new Error(
|
|
102
|
+
`Network response was not ok: ${response.statusText}`
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
}).catch((error) => {
|
|
106
|
+
console.error("There was a problem with the fetch operation:", error);
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
flushEventQueue() {
|
|
110
|
+
while (this.eventQueue.length > 0) {
|
|
111
|
+
const event = this.eventQueue.shift();
|
|
112
|
+
if (event) {
|
|
113
|
+
this.sendEvent(event);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
storeEvent(event) {
|
|
118
|
+
this.eventQueue.push(event);
|
|
119
|
+
}
|
|
120
|
+
handleEvent(eventType, eventBody) {
|
|
121
|
+
const event = {
|
|
122
|
+
api_key: this.apiKey,
|
|
123
|
+
body: eventBody,
|
|
124
|
+
sent_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
125
|
+
tracker_event_id: v4_default(),
|
|
126
|
+
tracker_user_id: this.getAnonymousId(),
|
|
127
|
+
type: eventType
|
|
128
|
+
};
|
|
129
|
+
if (typeof document !== "undefined" && document.hidden) {
|
|
130
|
+
this.storeEvent(event);
|
|
131
|
+
} else {
|
|
132
|
+
this.sendEvent(event);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
getAnonymousId() {
|
|
136
|
+
if (!this.storage) {
|
|
137
|
+
return v4_default();
|
|
138
|
+
}
|
|
139
|
+
const storedAnonymousId = this.storage.getItem(anonymousIdKey);
|
|
140
|
+
if (typeof storedAnonymousId !== "undefined") {
|
|
141
|
+
return storedAnonymousId;
|
|
142
|
+
}
|
|
143
|
+
const generatedAnonymousId = v4_default();
|
|
144
|
+
this.storage.setItem(anonymousIdKey, generatedAnonymousId);
|
|
145
|
+
return generatedAnonymousId;
|
|
146
|
+
}
|
|
147
|
+
async checkFlag(key, context) {
|
|
148
|
+
if (!context.company && !context.user) {
|
|
149
|
+
return Promise.resolve(false);
|
|
150
|
+
}
|
|
151
|
+
const requestUrl = `https://api.schematichq.com/flags/${key}/check`;
|
|
152
|
+
const requestBody = JSON.stringify(context);
|
|
153
|
+
return fetch(requestUrl, {
|
|
154
|
+
method: "POST",
|
|
155
|
+
headers: {
|
|
156
|
+
"X-Schematic-Api-Key": this.apiKey,
|
|
157
|
+
"Content-Type": "application/json;charset=UTF-8"
|
|
158
|
+
},
|
|
159
|
+
body: requestBody
|
|
160
|
+
}).then((response) => {
|
|
161
|
+
if (!response.ok) {
|
|
162
|
+
throw new Error("Network response was not ok");
|
|
163
|
+
}
|
|
164
|
+
return response.json();
|
|
165
|
+
}).then((data) => {
|
|
166
|
+
return data.data.value;
|
|
167
|
+
}).catch((error) => {
|
|
168
|
+
console.error("There was a problem with the fetch operation:", error);
|
|
169
|
+
return false;
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
async checkFlags(context) {
|
|
173
|
+
if (!context.company && !context.user) {
|
|
174
|
+
return Promise.resolve({});
|
|
175
|
+
}
|
|
176
|
+
const requestUrl = "https://api.schematichq.com/flags/check";
|
|
177
|
+
const requestBody = JSON.stringify(context);
|
|
178
|
+
return fetch(requestUrl, {
|
|
179
|
+
method: "POST",
|
|
180
|
+
headers: {
|
|
181
|
+
"X-Schematic-Api-Key": this.apiKey,
|
|
182
|
+
"Content-Type": "application/json;charset=UTF-8"
|
|
183
|
+
},
|
|
184
|
+
body: requestBody
|
|
185
|
+
}).then((response) => {
|
|
186
|
+
if (!response.ok) {
|
|
187
|
+
throw new Error("Network response was not ok");
|
|
188
|
+
}
|
|
189
|
+
return response.json();
|
|
190
|
+
}).then((data) => {
|
|
191
|
+
return data.data.flags.reduce((acc, flag) => {
|
|
192
|
+
acc[flag.flag] = flag.value;
|
|
193
|
+
return acc;
|
|
194
|
+
});
|
|
195
|
+
}).catch((error) => {
|
|
196
|
+
console.error("There was a problem with the fetch operation:", error);
|
|
197
|
+
return false;
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
identify(body) {
|
|
201
|
+
this.handleEvent("identify", body);
|
|
202
|
+
}
|
|
203
|
+
track(body) {
|
|
204
|
+
this.handleEvent("track", body);
|
|
205
|
+
}
|
|
206
|
+
initialize() {
|
|
207
|
+
if (typeof window !== "undefined") {
|
|
208
|
+
window.addEventListener("beforeunload", () => {
|
|
209
|
+
this.flushEventQueue();
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
/* @preserve */
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
declare type EventBodyCompany = {
|
|
2
|
+
keys: Record<string, string>;
|
|
3
|
+
name?: string;
|
|
4
|
+
traits: Record<string, any>;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
declare type EventBodyIdentify = {
|
|
8
|
+
company?: EventBodyCompany;
|
|
9
|
+
keys: Record<string, string>;
|
|
10
|
+
name?: string;
|
|
11
|
+
traits: Record<string, any>;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
declare type EventBodyTrack = {
|
|
15
|
+
event: string;
|
|
16
|
+
traits: Record<string, any>;
|
|
17
|
+
company?: Record<string, string>;
|
|
18
|
+
user?: Record<string, string>;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
declare type FlagCheckContext = {
|
|
22
|
+
company?: Record<string, string>;
|
|
23
|
+
user?: Record<string, string>;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export declare class Schematic {
|
|
27
|
+
private apiKey;
|
|
28
|
+
private eventQueue;
|
|
29
|
+
private storage;
|
|
30
|
+
constructor(apiKey: string, storage?: StoragePersister);
|
|
31
|
+
private sendEvent;
|
|
32
|
+
private flushEventQueue;
|
|
33
|
+
private storeEvent;
|
|
34
|
+
private handleEvent;
|
|
35
|
+
private getAnonymousId;
|
|
36
|
+
checkFlag(key: string, context: FlagCheckContext): Promise<boolean>;
|
|
37
|
+
checkFlags(context: FlagCheckContext): Promise<Record<string, boolean>>;
|
|
38
|
+
identify(body: EventBodyIdentify): void;
|
|
39
|
+
track(body: EventBodyTrack): void;
|
|
40
|
+
initialize(): void;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
declare type StoragePersister = {
|
|
44
|
+
setItem(key: string, value: any): void;
|
|
45
|
+
getItem(key: string): any;
|
|
46
|
+
removeItem(key: string): void;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export { }
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
// node_modules/uuid/dist/esm-browser/rng.js
|
|
2
|
+
var getRandomValues;
|
|
3
|
+
var rnds8 = new Uint8Array(16);
|
|
4
|
+
function rng() {
|
|
5
|
+
if (!getRandomValues) {
|
|
6
|
+
getRandomValues = typeof crypto !== "undefined" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto);
|
|
7
|
+
if (!getRandomValues) {
|
|
8
|
+
throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
return getRandomValues(rnds8);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// node_modules/uuid/dist/esm-browser/stringify.js
|
|
15
|
+
var byteToHex = [];
|
|
16
|
+
for (let i = 0; i < 256; ++i) {
|
|
17
|
+
byteToHex.push((i + 256).toString(16).slice(1));
|
|
18
|
+
}
|
|
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();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// node_modules/uuid/dist/esm-browser/native.js
|
|
24
|
+
var randomUUID = typeof crypto !== "undefined" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
|
|
25
|
+
var native_default = {
|
|
26
|
+
randomUUID
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// node_modules/uuid/dist/esm-browser/v4.js
|
|
30
|
+
function v4(options, buf, offset) {
|
|
31
|
+
if (native_default.randomUUID && !buf && !options) {
|
|
32
|
+
return native_default.randomUUID();
|
|
33
|
+
}
|
|
34
|
+
options = options || {};
|
|
35
|
+
const rnds = options.random || (options.rng || rng)();
|
|
36
|
+
rnds[6] = rnds[6] & 15 | 64;
|
|
37
|
+
rnds[8] = rnds[8] & 63 | 128;
|
|
38
|
+
if (buf) {
|
|
39
|
+
offset = offset || 0;
|
|
40
|
+
for (let i = 0; i < 16; ++i) {
|
|
41
|
+
buf[offset + i] = rnds[i];
|
|
42
|
+
}
|
|
43
|
+
return buf;
|
|
44
|
+
}
|
|
45
|
+
return unsafeStringify(rnds);
|
|
46
|
+
}
|
|
47
|
+
var v4_default = v4;
|
|
48
|
+
|
|
49
|
+
// src/index.ts
|
|
50
|
+
var anonymousIdKey = "schematicId";
|
|
51
|
+
var Schematic = class {
|
|
52
|
+
apiKey;
|
|
53
|
+
eventQueue;
|
|
54
|
+
storage;
|
|
55
|
+
constructor(apiKey, storage) {
|
|
56
|
+
this.apiKey = apiKey;
|
|
57
|
+
this.eventQueue = [];
|
|
58
|
+
if (storage) {
|
|
59
|
+
this.storage = storage;
|
|
60
|
+
} else if (typeof localStorage !== "undefined") {
|
|
61
|
+
this.storage = localStorage;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
sendEvent(event) {
|
|
65
|
+
const captureUrl = "https://c.schematichq.com/e";
|
|
66
|
+
const payload = JSON.stringify(event);
|
|
67
|
+
fetch(captureUrl, {
|
|
68
|
+
method: "POST",
|
|
69
|
+
headers: {
|
|
70
|
+
"Content-Type": "application/json;charset=UTF-8"
|
|
71
|
+
},
|
|
72
|
+
body: payload
|
|
73
|
+
}).then((response) => {
|
|
74
|
+
if (!response.ok) {
|
|
75
|
+
throw new Error(
|
|
76
|
+
`Network response was not ok: ${response.statusText}`
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
}).catch((error) => {
|
|
80
|
+
console.error("There was a problem with the fetch operation:", error);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
flushEventQueue() {
|
|
84
|
+
while (this.eventQueue.length > 0) {
|
|
85
|
+
const event = this.eventQueue.shift();
|
|
86
|
+
if (event) {
|
|
87
|
+
this.sendEvent(event);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
storeEvent(event) {
|
|
92
|
+
this.eventQueue.push(event);
|
|
93
|
+
}
|
|
94
|
+
handleEvent(eventType, eventBody) {
|
|
95
|
+
const event = {
|
|
96
|
+
api_key: this.apiKey,
|
|
97
|
+
body: eventBody,
|
|
98
|
+
sent_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
99
|
+
tracker_event_id: v4_default(),
|
|
100
|
+
tracker_user_id: this.getAnonymousId(),
|
|
101
|
+
type: eventType
|
|
102
|
+
};
|
|
103
|
+
if (typeof document !== "undefined" && document.hidden) {
|
|
104
|
+
this.storeEvent(event);
|
|
105
|
+
} else {
|
|
106
|
+
this.sendEvent(event);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
getAnonymousId() {
|
|
110
|
+
if (!this.storage) {
|
|
111
|
+
return v4_default();
|
|
112
|
+
}
|
|
113
|
+
const storedAnonymousId = this.storage.getItem(anonymousIdKey);
|
|
114
|
+
if (typeof storedAnonymousId !== "undefined") {
|
|
115
|
+
return storedAnonymousId;
|
|
116
|
+
}
|
|
117
|
+
const generatedAnonymousId = v4_default();
|
|
118
|
+
this.storage.setItem(anonymousIdKey, generatedAnonymousId);
|
|
119
|
+
return generatedAnonymousId;
|
|
120
|
+
}
|
|
121
|
+
async checkFlag(key, context) {
|
|
122
|
+
if (!context.company && !context.user) {
|
|
123
|
+
return Promise.resolve(false);
|
|
124
|
+
}
|
|
125
|
+
const requestUrl = `https://api.schematichq.com/flags/${key}/check`;
|
|
126
|
+
const requestBody = JSON.stringify(context);
|
|
127
|
+
return fetch(requestUrl, {
|
|
128
|
+
method: "POST",
|
|
129
|
+
headers: {
|
|
130
|
+
"X-Schematic-Api-Key": this.apiKey,
|
|
131
|
+
"Content-Type": "application/json;charset=UTF-8"
|
|
132
|
+
},
|
|
133
|
+
body: requestBody
|
|
134
|
+
}).then((response) => {
|
|
135
|
+
if (!response.ok) {
|
|
136
|
+
throw new Error("Network response was not ok");
|
|
137
|
+
}
|
|
138
|
+
return response.json();
|
|
139
|
+
}).then((data) => {
|
|
140
|
+
return data.data.value;
|
|
141
|
+
}).catch((error) => {
|
|
142
|
+
console.error("There was a problem with the fetch operation:", error);
|
|
143
|
+
return false;
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
async checkFlags(context) {
|
|
147
|
+
if (!context.company && !context.user) {
|
|
148
|
+
return Promise.resolve({});
|
|
149
|
+
}
|
|
150
|
+
const requestUrl = "https://api.schematichq.com/flags/check";
|
|
151
|
+
const requestBody = JSON.stringify(context);
|
|
152
|
+
return fetch(requestUrl, {
|
|
153
|
+
method: "POST",
|
|
154
|
+
headers: {
|
|
155
|
+
"X-Schematic-Api-Key": this.apiKey,
|
|
156
|
+
"Content-Type": "application/json;charset=UTF-8"
|
|
157
|
+
},
|
|
158
|
+
body: requestBody
|
|
159
|
+
}).then((response) => {
|
|
160
|
+
if (!response.ok) {
|
|
161
|
+
throw new Error("Network response was not ok");
|
|
162
|
+
}
|
|
163
|
+
return response.json();
|
|
164
|
+
}).then((data) => {
|
|
165
|
+
return data.data.flags.reduce((acc, flag) => {
|
|
166
|
+
acc[flag.flag] = flag.value;
|
|
167
|
+
return acc;
|
|
168
|
+
});
|
|
169
|
+
}).catch((error) => {
|
|
170
|
+
console.error("There was a problem with the fetch operation:", error);
|
|
171
|
+
return false;
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
identify(body) {
|
|
175
|
+
this.handleEvent("identify", body);
|
|
176
|
+
}
|
|
177
|
+
track(body) {
|
|
178
|
+
this.handleEvent("track", body);
|
|
179
|
+
}
|
|
180
|
+
initialize() {
|
|
181
|
+
if (typeof window !== "undefined") {
|
|
182
|
+
window.addEventListener("beforeunload", () => {
|
|
183
|
+
this.flushEventQueue();
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
export {
|
|
189
|
+
Schematic
|
|
190
|
+
};
|
|
191
|
+
/* @preserve */
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@schematichq/schematic-js",
|
|
3
|
+
"main": "dist/schematic.cjs.js",
|
|
4
|
+
"module": "dist/schematic.esm.js",
|
|
5
|
+
"author": "Ben Papillon <ben@schematichq.com>",
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"@types/uuid": "^9.0.2",
|
|
8
|
+
"uuid": "^9.0.0"
|
|
9
|
+
},
|
|
10
|
+
"devDependencies": {
|
|
11
|
+
"@microsoft/api-extractor": "^7.38.3",
|
|
12
|
+
"@types/jest": "^29.5.11",
|
|
13
|
+
"@typescript-eslint/eslint-plugin": "^6.13.2",
|
|
14
|
+
"@typescript-eslint/parser": "^6.13.2",
|
|
15
|
+
"esbuild": "^0.19.9",
|
|
16
|
+
"esbuild-jest": "^0.5.0",
|
|
17
|
+
"eslint": "^8.55.0",
|
|
18
|
+
"jest": "^29.7.0",
|
|
19
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
20
|
+
"jest-esbuild": "^0.3.0",
|
|
21
|
+
"jest-fetch-mock": "^3.0.3",
|
|
22
|
+
"ts-jest": "^29.1.1",
|
|
23
|
+
"typescript": "^5.0.2"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist/schematic.cjs.js",
|
|
27
|
+
"dist/schematic.esm.js",
|
|
28
|
+
"dist/schematic.browser.js",
|
|
29
|
+
"dist/schematic.d.ts"
|
|
30
|
+
],
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+ssh://git@github.com/SchematicHQ/schematic-js.git"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "npx tsc && yarn clean && yarn build:browser && yarn build:cjs && yarn build:esm && yarn build:types",
|
|
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 --platform=neutral",
|
|
40
|
+
"build:esm": "npx esbuild src/index.ts --bundle --format=esm --outfile=dist/schematic.esm.js --platform=neutral",
|
|
41
|
+
"build:types": "npx tsc && npx api-extractor run",
|
|
42
|
+
"clean": "rm -rf dist",
|
|
43
|
+
"lint": "eslint src --ext ts,tsx --report-unused-disable-directives --fix",
|
|
44
|
+
"test": "jest --config jest.config.js"
|
|
45
|
+
},
|
|
46
|
+
"types": "dist/schematic.d.ts",
|
|
47
|
+
"version": "0.0.10"
|
|
48
|
+
}
|