@schematichq/schematic-react 0.1.4 → 0.1.5

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.
@@ -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
- eventUrl = "c.schematichq.com";
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,6 +98,9 @@ var Schematic = class {
97
98
  if (options?.eventUrl !== void 0) {
98
99
  this.eventUrl = options.eventUrl;
99
100
  }
101
+ if (options?.webSocketUrl !== void 0) {
102
+ this.webSocketUrl = options.webSocketUrl;
103
+ }
100
104
  if (typeof window !== "undefined") {
101
105
  window.addEventListener("beforeunload", () => {
102
106
  this.flushEventQueue();
@@ -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.getUrl(this.apiUrl, `flags/${key}/check`);
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.getUrl(this.apiUrl, "flags/check");
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();
@@ -235,7 +229,7 @@ var Schematic = class {
235
229
  }
236
230
  };
237
231
  sendEvent = (event) => {
238
- const captureUrl = this.getUrl(this.eventUrl, "e");
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.getUrl(this.apiUrl, "flags/bootstrap", "ws");
258
+ const wsUrl = `${this.webSocketUrl}/flags/bootstrap`;
265
259
  const webSocket = new WebSocket(wsUrl);
266
260
  this.conn = webSocket;
267
261
  webSocket.onopen = () => {
@@ -345,7 +339,9 @@ var SchematicContext = (0, import_react.createContext)({
345
339
  var SchematicProvider = ({
346
340
  apiUrl,
347
341
  children,
348
- publishableKey
342
+ eventUrl,
343
+ publishableKey,
344
+ webSocketUrl
349
345
  }) => {
350
346
  const [client, setClient] = (0, import_react.useState)();
351
347
  const [flagValues, setFlagValues] = (0, import_react.useState)({});
@@ -355,12 +351,14 @@ var SchematicProvider = ({
355
351
  }
356
352
  const client2 = new Schematic(publishableKey, {
357
353
  apiUrl,
354
+ eventUrl,
358
355
  flagListener: setFlagValues,
359
- useWebSocket: true
356
+ useWebSocket: true,
357
+ webSocketUrl
360
358
  });
361
359
  setClient(client2);
362
360
  return client2.cleanup;
363
- }, [publishableKey]);
361
+ }, [apiUrl, eventUrl, publishableKey, webSocketUrl]);
364
362
  const contextValue = {
365
363
  client,
366
364
  flagValues
@@ -48,6 +48,8 @@ declare interface SchematicProviderProps {
48
48
  children: ReactNode;
49
49
  publishableKey?: string;
50
50
  apiUrl?: string;
51
+ eventUrl?: string;
52
+ webSocketUrl?: string;
51
53
  }
52
54
 
53
55
  export { Traits }
@@ -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
- eventUrl = "c.schematichq.com";
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,6 +68,9 @@ var Schematic = class {
67
68
  if (options?.eventUrl !== void 0) {
68
69
  this.eventUrl = options.eventUrl;
69
70
  }
71
+ if (options?.webSocketUrl !== void 0) {
72
+ this.webSocketUrl = options.webSocketUrl;
73
+ }
70
74
  if (typeof window !== "undefined") {
71
75
  window.addEventListener("beforeunload", () => {
72
76
  this.flushEventQueue();
@@ -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.getUrl(this.apiUrl, `flags/${key}/check`);
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.getUrl(this.apiUrl, "flags/check");
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();
@@ -205,7 +199,7 @@ var Schematic = class {
205
199
  }
206
200
  };
207
201
  sendEvent = (event) => {
208
- const captureUrl = this.getUrl(this.eventUrl, "e");
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.getUrl(this.apiUrl, "flags/bootstrap", "ws");
228
+ const wsUrl = `${this.webSocketUrl}/flags/bootstrap`;
235
229
  const webSocket = new WebSocket(wsUrl);
236
230
  this.conn = webSocket;
237
231
  webSocket.onopen = () => {
@@ -320,7 +314,9 @@ var SchematicContext = createContext({
320
314
  var SchematicProvider = ({
321
315
  apiUrl,
322
316
  children,
323
- publishableKey
317
+ eventUrl,
318
+ publishableKey,
319
+ webSocketUrl
324
320
  }) => {
325
321
  const [client, setClient] = useState();
326
322
  const [flagValues, setFlagValues] = useState({});
@@ -330,12 +326,14 @@ var SchematicProvider = ({
330
326
  }
331
327
  const client2 = new Schematic(publishableKey, {
332
328
  apiUrl,
329
+ eventUrl,
333
330
  flagListener: setFlagValues,
334
- useWebSocket: true
331
+ useWebSocket: true,
332
+ webSocketUrl
335
333
  });
336
334
  setClient(client2);
337
335
  return client2.cleanup;
338
- }, [publishableKey]);
336
+ }, [apiUrl, eventUrl, publishableKey, webSocketUrl]);
339
337
  const contextValue = {
340
338
  client,
341
339
  flagValues
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.4",
45
+ "version": "0.1.5",
46
46
  "dependencies": {
47
- "@schematichq/schematic-js": "^0.1.4"
47
+ "@schematichq/schematic-js": "^0.1.5"
48
48
  },
49
49
  "peerDependencies": {
50
50
  "react": ">=18"