From 2b6da22999c23319c1e034796e3c59822652ef5f Mon Sep 17 00:00:00 2001 From: Ben Papillon Date: Sat, 27 Jan 2024 16:33:10 -0500 Subject: [PATCH] Improve URL override options --- js/package.json | 2 +- js/src/index.ts | 32 ++++++++++++-------------------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/js/package.json b/js/package.json index 387e9cd3..e6ad2dc0 100644 --- a/js/package.json +++ b/js/package.json @@ -44,5 +44,5 @@ "test": "jest --config jest.config.js" }, "types": "dist/schematic.d.ts", - "version": "0.1.4" + "version": "0.1.5" } diff --git a/js/src/index.ts b/js/src/index.ts index 22136028..132cda15 100644 --- a/js/src/index.ts +++ b/js/src/index.ts @@ -65,6 +65,7 @@ type StoragePersister = { type SchematicOptions = { apiUrl?: string; + webSocketUrl?: string; eventUrl?: string; flagListener?: (values: Record) => void; storage?: StoragePersister; @@ -80,8 +81,9 @@ export type CheckOptions = { /* @preserve */ export class Schematic { private apiKey: string; - private apiUrl = "api.schematichq.com"; - private eventUrl = "c.schematichq.com"; + private apiUrl = "https://api.schematichq.com"; + private webSocketUrl = "wss://api.schematichq.com"; + private eventUrl = "https://c.schematichq.com"; private conn: WebSocket | null = null; private context: SchematicContext = {}; private eventQueue: Event[]; @@ -110,6 +112,10 @@ export class Schematic { this.eventUrl = options.eventUrl; } + if (options?.webSocketUrl !== undefined) { + this.webSocketUrl = options.webSocketUrl; + } + if (typeof window !== "undefined") { window.addEventListener("beforeunload", () => { this.flushEventQueue(); @@ -128,7 +134,7 @@ export class Schematic { : contextVals[key]; } - const requestUrl = this.getUrl(this.apiUrl, `flags/${key}/check`); + const requestUrl = `${this.apiUrl}/flags/${key}/check`; return fetch(requestUrl, { method: "POST", headers: { @@ -158,9 +164,8 @@ export class Schematic { ): Promise> => { context = context || this.context; - const requestUrl = this.getUrl(this.apiUrl, "flags/check"); + const requestUrl = `${this.apiUrl}/flags/check`; const requestBody = JSON.stringify(context); - return fetch(requestUrl, { method: "POST", headers: { @@ -227,19 +232,6 @@ export class Schematic { this.handleEvent("track", body); }; - private getUrl = (domain: string, path: string, urlType?: string): string => { - let scheme = "http"; - if (urlType === "ws") { - scheme = "ws"; - } - - if (typeof window === "undefined" || window.location.protocol === "https:") { - return `${scheme}s://${domain}/${path}`; - } - - return `${scheme}://${domain}/${path}`; - } - private flushEventQueue = (): void => { while (this.eventQueue.length > 0) { const event = this.eventQueue.shift(); @@ -282,7 +274,7 @@ export class Schematic { }; private sendEvent = (event: Event): void => { - const captureUrl = this.getUrl(this.eventUrl, "e"); + const captureUrl = `${this.eventUrl}/e`; const payload = JSON.stringify(event); fetch(captureUrl, { @@ -314,7 +306,7 @@ export class Schematic { resolve(); } - const wsUrl = this.getUrl(this.apiUrl, "flags/bootstrap", "ws"); + const wsUrl = `${this.webSocketUrl}/flags/bootstrap`; const webSocket = new WebSocket(wsUrl); this.conn = webSocket;