@schematichq/schematic-react 0.1.4 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/schematic-react.cjs.js +42 -30
- package/dist/schematic-react.d.ts +20 -5
- package/dist/schematic-react.esm.js +42 -30
- package/package.json +2 -2
@@ -72,8 +72,9 @@ var v4_default = v4;
|
|
72
72
|
var anonymousIdKey = "schematicId";
|
73
73
|
var Schematic = class {
|
74
74
|
apiKey;
|
75
|
-
apiUrl = "api.schematichq.com";
|
76
|
-
|
75
|
+
apiUrl = "https://api.schematichq.com";
|
76
|
+
webSocketUrl = "wss://api.schematichq.com";
|
77
|
+
eventUrl = "https://c.schematichq.com";
|
77
78
|
conn = null;
|
78
79
|
context = {};
|
79
80
|
eventQueue;
|
@@ -97,7 +98,10 @@ var Schematic = class {
|
|
97
98
|
if (options?.eventUrl !== void 0) {
|
98
99
|
this.eventUrl = options.eventUrl;
|
99
100
|
}
|
100
|
-
if (
|
101
|
+
if (options?.webSocketUrl !== void 0) {
|
102
|
+
this.webSocketUrl = options.webSocketUrl;
|
103
|
+
}
|
104
|
+
if (window?.addEventListener) {
|
101
105
|
window.addEventListener("beforeunload", () => {
|
102
106
|
this.flushEventQueue();
|
103
107
|
});
|
@@ -110,7 +114,7 @@ var Schematic = class {
|
|
110
114
|
const contextVals = this.values[contextString(context)] ?? {};
|
111
115
|
return typeof contextVals[key] === "undefined" ? fallback : contextVals[key];
|
112
116
|
}
|
113
|
-
const requestUrl = this.
|
117
|
+
const requestUrl = `${this.apiUrl}/flags/${key}/check`;
|
114
118
|
return fetch(requestUrl, {
|
115
119
|
method: "POST",
|
116
120
|
headers: {
|
@@ -133,7 +137,7 @@ var Schematic = class {
|
|
133
137
|
// Make a REST API call to fetch all flag values for a given context
|
134
138
|
checkFlags = async (context) => {
|
135
139
|
context = context || this.context;
|
136
|
-
const requestUrl = this.
|
140
|
+
const requestUrl = `${this.apiUrl}/flags/check`;
|
137
141
|
const requestBody = JSON.stringify(context);
|
138
142
|
return fetch(requestUrl, {
|
139
143
|
method: "POST",
|
@@ -189,16 +193,6 @@ var Schematic = class {
|
|
189
193
|
track = (body) => {
|
190
194
|
this.handleEvent("track", body);
|
191
195
|
};
|
192
|
-
getUrl = (domain, path, urlType) => {
|
193
|
-
let scheme = "http";
|
194
|
-
if (urlType === "ws") {
|
195
|
-
scheme = "ws";
|
196
|
-
}
|
197
|
-
if (typeof window === "undefined" || window.location.protocol === "https:") {
|
198
|
-
return `${scheme}s://${domain}/${path}`;
|
199
|
-
}
|
200
|
-
return `${scheme}://${domain}/${path}`;
|
201
|
-
};
|
202
196
|
flushEventQueue = () => {
|
203
197
|
while (this.eventQueue.length > 0) {
|
204
198
|
const event = this.eventQueue.shift();
|
@@ -228,14 +222,14 @@ var Schematic = class {
|
|
228
222
|
tracker_user_id: this.getAnonymousId(),
|
229
223
|
type: eventType
|
230
224
|
};
|
231
|
-
if (
|
225
|
+
if (document?.hidden) {
|
232
226
|
this.storeEvent(event);
|
233
227
|
} else {
|
234
228
|
this.sendEvent(event);
|
235
229
|
}
|
236
230
|
};
|
237
231
|
sendEvent = (event) => {
|
238
|
-
const captureUrl = this.
|
232
|
+
const captureUrl = `${this.eventUrl}/e`;
|
239
233
|
const payload = JSON.stringify(event);
|
240
234
|
fetch(captureUrl, {
|
241
235
|
method: "POST",
|
@@ -261,7 +255,7 @@ var Schematic = class {
|
|
261
255
|
if (this.conn) {
|
262
256
|
resolve();
|
263
257
|
}
|
264
|
-
const wsUrl = this.
|
258
|
+
const wsUrl = `${this.webSocketUrl}/flags/bootstrap`;
|
265
259
|
const webSocket = new WebSocket(wsUrl);
|
266
260
|
this.conn = webSocket;
|
267
261
|
webSocket.onopen = () => {
|
@@ -343,24 +337,29 @@ var SchematicContext = (0, import_react.createContext)({
|
|
343
337
|
flagValues: {}
|
344
338
|
});
|
345
339
|
var SchematicProvider = ({
|
346
|
-
apiUrl,
|
347
340
|
children,
|
341
|
+
client: providedClient,
|
342
|
+
clientOpts,
|
348
343
|
publishableKey
|
349
344
|
}) => {
|
350
345
|
const [client, setClient] = (0, import_react.useState)();
|
351
346
|
const [flagValues, setFlagValues] = (0, import_react.useState)({});
|
352
347
|
(0, import_react.useEffect)(() => {
|
348
|
+
if (providedClient) {
|
349
|
+
setClient(providedClient);
|
350
|
+
return providedClient.cleanup;
|
351
|
+
}
|
353
352
|
if (publishableKey === void 0) {
|
354
353
|
return;
|
355
354
|
}
|
356
|
-
const
|
357
|
-
|
355
|
+
const newClient = new Schematic(publishableKey, {
|
356
|
+
...clientOpts,
|
358
357
|
flagListener: setFlagValues,
|
359
358
|
useWebSocket: true
|
360
359
|
});
|
361
|
-
setClient(
|
362
|
-
return
|
363
|
-
}, [publishableKey]);
|
360
|
+
setClient(newClient);
|
361
|
+
return newClient.cleanup;
|
362
|
+
}, [clientOpts, providedClient, publishableKey]);
|
364
363
|
const contextValue = {
|
365
364
|
client,
|
366
365
|
flagValues
|
@@ -368,23 +367,36 @@ var SchematicProvider = ({
|
|
368
367
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SchematicContext.Provider, { value: contextValue, children });
|
369
368
|
};
|
370
369
|
var useSchematic = () => (0, import_react.useContext)(SchematicContext);
|
371
|
-
var
|
372
|
-
const
|
370
|
+
var useSchematicClient = (opts) => {
|
371
|
+
const schematic = useSchematic();
|
372
|
+
const { client } = opts ?? {};
|
373
|
+
if (client) {
|
374
|
+
return client;
|
375
|
+
}
|
376
|
+
return schematic.client;
|
377
|
+
};
|
378
|
+
var useSchematicContext = (opts) => {
|
379
|
+
const client = useSchematicClient(opts);
|
373
380
|
const { setContext } = client ?? {};
|
374
381
|
return { setContext };
|
375
382
|
};
|
376
|
-
var useSchematicEvents = () => {
|
377
|
-
const
|
383
|
+
var useSchematicEvents = (opts) => {
|
384
|
+
const client = useSchematicClient(opts);
|
378
385
|
const { track, identify } = client ?? {};
|
379
386
|
return { track, identify };
|
380
387
|
};
|
381
|
-
var useSchematicFlag = (key,
|
388
|
+
var useSchematicFlag = (key, opts) => {
|
382
389
|
const { flagValues } = useSchematic();
|
390
|
+
const { client } = opts ?? {};
|
391
|
+
const { fallback = false } = opts ?? {};
|
383
392
|
const [value, setValue] = (0, import_react.useState)(fallback ?? false);
|
384
393
|
const flagValue = flagValues[key];
|
385
394
|
(0, import_react.useEffect)(() => {
|
386
|
-
typeof flagValue === "undefined" ? setValue(fallback
|
395
|
+
typeof flagValue === "undefined" ? setValue(fallback) : setValue(flagValue);
|
387
396
|
}, [key, fallback, flagValue]);
|
397
|
+
if (client) {
|
398
|
+
return client.checkFlag({ key, fallback });
|
399
|
+
}
|
388
400
|
return value;
|
389
401
|
};
|
390
402
|
/*! Bundled license information:
|
@@ -9,8 +9,10 @@ import { FlagCheckWithKeyResponseBody } from '@schematichq/schematic-js';
|
|
9
9
|
import { Keys } from '@schematichq/schematic-js';
|
10
10
|
import { default as React_2 } from 'react';
|
11
11
|
import { ReactNode } from 'react';
|
12
|
+
import { Schematic } from '@schematichq/schematic-js';
|
12
13
|
import { SchematicContext } from '@schematichq/schematic-js';
|
13
14
|
import * as SchematicJS from '@schematichq/schematic-js';
|
15
|
+
import { SchematicOptions } from '@schematichq/schematic-js';
|
14
16
|
import { Traits } from '@schematichq/schematic-js';
|
15
17
|
|
16
18
|
export { Event_2 as Event }
|
@@ -31,6 +33,8 @@ export { FlagCheckWithKeyResponseBody }
|
|
31
33
|
|
32
34
|
export { Keys }
|
33
35
|
|
36
|
+
export { Schematic }
|
37
|
+
|
34
38
|
export { SchematicContext }
|
35
39
|
|
36
40
|
declare interface SchematicContextProps {
|
@@ -42,27 +46,38 @@ export declare interface SchematicFlags {
|
|
42
46
|
[key: string]: boolean;
|
43
47
|
}
|
44
48
|
|
49
|
+
export declare interface SchematicHookOpts {
|
50
|
+
client?: SchematicJS.Schematic;
|
51
|
+
}
|
52
|
+
|
53
|
+
export { SchematicOptions }
|
54
|
+
|
45
55
|
export declare const SchematicProvider: React_2.FC<SchematicProviderProps>;
|
46
56
|
|
47
|
-
declare interface SchematicProviderProps {
|
57
|
+
export declare interface SchematicProviderProps {
|
48
58
|
children: ReactNode;
|
59
|
+
client?: SchematicJS.Schematic;
|
60
|
+
clientOpts?: SchematicJS.SchematicOptions;
|
49
61
|
publishableKey?: string;
|
50
|
-
apiUrl?: string;
|
51
62
|
}
|
52
63
|
|
53
64
|
export { Traits }
|
54
65
|
|
55
66
|
export declare const useSchematic: () => SchematicContextProps;
|
56
67
|
|
57
|
-
export declare const useSchematicContext: () => {
|
68
|
+
export declare const useSchematicContext: (opts?: SchematicHookOpts) => {
|
58
69
|
setContext: ((context: SchematicJS.SchematicContext) => Promise<void>) | undefined;
|
59
70
|
};
|
60
71
|
|
61
|
-
export declare const useSchematicEvents: () => {
|
72
|
+
export declare const useSchematicEvents: (opts?: SchematicHookOpts) => {
|
62
73
|
track: ((body: SchematicJS.EventBodyTrack) => void) | undefined;
|
63
74
|
identify: ((body: SchematicJS.EventBodyIdentify) => void) | undefined;
|
64
75
|
};
|
65
76
|
|
66
|
-
export declare const useSchematicFlag: (key: string,
|
77
|
+
export declare const useSchematicFlag: (key: string, opts?: UseSchematicFlagOpts) => boolean | Promise<boolean>;
|
78
|
+
|
79
|
+
export declare type UseSchematicFlagOpts = SchematicHookOpts & {
|
80
|
+
fallback?: boolean;
|
81
|
+
};
|
67
82
|
|
68
83
|
export { }
|
@@ -42,8 +42,9 @@ var v4_default = v4;
|
|
42
42
|
var anonymousIdKey = "schematicId";
|
43
43
|
var Schematic = class {
|
44
44
|
apiKey;
|
45
|
-
apiUrl = "api.schematichq.com";
|
46
|
-
|
45
|
+
apiUrl = "https://api.schematichq.com";
|
46
|
+
webSocketUrl = "wss://api.schematichq.com";
|
47
|
+
eventUrl = "https://c.schematichq.com";
|
47
48
|
conn = null;
|
48
49
|
context = {};
|
49
50
|
eventQueue;
|
@@ -67,7 +68,10 @@ var Schematic = class {
|
|
67
68
|
if (options?.eventUrl !== void 0) {
|
68
69
|
this.eventUrl = options.eventUrl;
|
69
70
|
}
|
70
|
-
if (
|
71
|
+
if (options?.webSocketUrl !== void 0) {
|
72
|
+
this.webSocketUrl = options.webSocketUrl;
|
73
|
+
}
|
74
|
+
if (window?.addEventListener) {
|
71
75
|
window.addEventListener("beforeunload", () => {
|
72
76
|
this.flushEventQueue();
|
73
77
|
});
|
@@ -80,7 +84,7 @@ var Schematic = class {
|
|
80
84
|
const contextVals = this.values[contextString(context)] ?? {};
|
81
85
|
return typeof contextVals[key] === "undefined" ? fallback : contextVals[key];
|
82
86
|
}
|
83
|
-
const requestUrl = this.
|
87
|
+
const requestUrl = `${this.apiUrl}/flags/${key}/check`;
|
84
88
|
return fetch(requestUrl, {
|
85
89
|
method: "POST",
|
86
90
|
headers: {
|
@@ -103,7 +107,7 @@ var Schematic = class {
|
|
103
107
|
// Make a REST API call to fetch all flag values for a given context
|
104
108
|
checkFlags = async (context) => {
|
105
109
|
context = context || this.context;
|
106
|
-
const requestUrl = this.
|
110
|
+
const requestUrl = `${this.apiUrl}/flags/check`;
|
107
111
|
const requestBody = JSON.stringify(context);
|
108
112
|
return fetch(requestUrl, {
|
109
113
|
method: "POST",
|
@@ -159,16 +163,6 @@ var Schematic = class {
|
|
159
163
|
track = (body) => {
|
160
164
|
this.handleEvent("track", body);
|
161
165
|
};
|
162
|
-
getUrl = (domain, path, urlType) => {
|
163
|
-
let scheme = "http";
|
164
|
-
if (urlType === "ws") {
|
165
|
-
scheme = "ws";
|
166
|
-
}
|
167
|
-
if (typeof window === "undefined" || window.location.protocol === "https:") {
|
168
|
-
return `${scheme}s://${domain}/${path}`;
|
169
|
-
}
|
170
|
-
return `${scheme}://${domain}/${path}`;
|
171
|
-
};
|
172
166
|
flushEventQueue = () => {
|
173
167
|
while (this.eventQueue.length > 0) {
|
174
168
|
const event = this.eventQueue.shift();
|
@@ -198,14 +192,14 @@ var Schematic = class {
|
|
198
192
|
tracker_user_id: this.getAnonymousId(),
|
199
193
|
type: eventType
|
200
194
|
};
|
201
|
-
if (
|
195
|
+
if (document?.hidden) {
|
202
196
|
this.storeEvent(event);
|
203
197
|
} else {
|
204
198
|
this.sendEvent(event);
|
205
199
|
}
|
206
200
|
};
|
207
201
|
sendEvent = (event) => {
|
208
|
-
const captureUrl = this.
|
202
|
+
const captureUrl = `${this.eventUrl}/e`;
|
209
203
|
const payload = JSON.stringify(event);
|
210
204
|
fetch(captureUrl, {
|
211
205
|
method: "POST",
|
@@ -231,7 +225,7 @@ var Schematic = class {
|
|
231
225
|
if (this.conn) {
|
232
226
|
resolve();
|
233
227
|
}
|
234
|
-
const wsUrl = this.
|
228
|
+
const wsUrl = `${this.webSocketUrl}/flags/bootstrap`;
|
235
229
|
const webSocket = new WebSocket(wsUrl);
|
236
230
|
this.conn = webSocket;
|
237
231
|
webSocket.onopen = () => {
|
@@ -318,24 +312,29 @@ var SchematicContext = createContext({
|
|
318
312
|
flagValues: {}
|
319
313
|
});
|
320
314
|
var SchematicProvider = ({
|
321
|
-
apiUrl,
|
322
315
|
children,
|
316
|
+
client: providedClient,
|
317
|
+
clientOpts,
|
323
318
|
publishableKey
|
324
319
|
}) => {
|
325
320
|
const [client, setClient] = useState();
|
326
321
|
const [flagValues, setFlagValues] = useState({});
|
327
322
|
useEffect(() => {
|
323
|
+
if (providedClient) {
|
324
|
+
setClient(providedClient);
|
325
|
+
return providedClient.cleanup;
|
326
|
+
}
|
328
327
|
if (publishableKey === void 0) {
|
329
328
|
return;
|
330
329
|
}
|
331
|
-
const
|
332
|
-
|
330
|
+
const newClient = new Schematic(publishableKey, {
|
331
|
+
...clientOpts,
|
333
332
|
flagListener: setFlagValues,
|
334
333
|
useWebSocket: true
|
335
334
|
});
|
336
|
-
setClient(
|
337
|
-
return
|
338
|
-
}, [publishableKey]);
|
335
|
+
setClient(newClient);
|
336
|
+
return newClient.cleanup;
|
337
|
+
}, [clientOpts, providedClient, publishableKey]);
|
339
338
|
const contextValue = {
|
340
339
|
client,
|
341
340
|
flagValues
|
@@ -343,23 +342,36 @@ var SchematicProvider = ({
|
|
343
342
|
return /* @__PURE__ */ jsx(SchematicContext.Provider, { value: contextValue, children });
|
344
343
|
};
|
345
344
|
var useSchematic = () => useContext(SchematicContext);
|
346
|
-
var
|
347
|
-
const
|
345
|
+
var useSchematicClient = (opts) => {
|
346
|
+
const schematic = useSchematic();
|
347
|
+
const { client } = opts ?? {};
|
348
|
+
if (client) {
|
349
|
+
return client;
|
350
|
+
}
|
351
|
+
return schematic.client;
|
352
|
+
};
|
353
|
+
var useSchematicContext = (opts) => {
|
354
|
+
const client = useSchematicClient(opts);
|
348
355
|
const { setContext } = client ?? {};
|
349
356
|
return { setContext };
|
350
357
|
};
|
351
|
-
var useSchematicEvents = () => {
|
352
|
-
const
|
358
|
+
var useSchematicEvents = (opts) => {
|
359
|
+
const client = useSchematicClient(opts);
|
353
360
|
const { track, identify } = client ?? {};
|
354
361
|
return { track, identify };
|
355
362
|
};
|
356
|
-
var useSchematicFlag = (key,
|
363
|
+
var useSchematicFlag = (key, opts) => {
|
357
364
|
const { flagValues } = useSchematic();
|
365
|
+
const { client } = opts ?? {};
|
366
|
+
const { fallback = false } = opts ?? {};
|
358
367
|
const [value, setValue] = useState(fallback ?? false);
|
359
368
|
const flagValue = flagValues[key];
|
360
369
|
useEffect(() => {
|
361
|
-
typeof flagValue === "undefined" ? setValue(fallback
|
370
|
+
typeof flagValue === "undefined" ? setValue(fallback) : setValue(flagValue);
|
362
371
|
}, [key, fallback, flagValue]);
|
372
|
+
if (client) {
|
373
|
+
return client.checkFlag({ key, fallback });
|
374
|
+
}
|
363
375
|
return value;
|
364
376
|
};
|
365
377
|
export {
|
package/package.json
CHANGED
@@ -42,9 +42,9 @@
|
|
42
42
|
"test": "jest --config jest.config.js"
|
43
43
|
},
|
44
44
|
"types": "dist/schematic-react.d.ts",
|
45
|
-
"version": "0.1.
|
45
|
+
"version": "0.1.6",
|
46
46
|
"dependencies": {
|
47
|
-
"@schematichq/schematic-js": "^0.1.
|
47
|
+
"@schematichq/schematic-js": "^0.1.7"
|
48
48
|
},
|
49
49
|
"peerDependencies": {
|
50
50
|
"react": ">=18"
|