Skip to content

Commit

Permalink
Merge pull request #19 from SchematicHQ/url-overrides
Browse files Browse the repository at this point in the history
Improve URL override options
  • Loading branch information
bpapillon authored Jan 27, 2024
2 parents cf4179e + 2b6da22 commit 064cda5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
2 changes: 1 addition & 1 deletion js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@
"test": "jest --config jest.config.js"
},
"types": "dist/schematic.d.ts",
"version": "0.1.4"
"version": "0.1.5"
}
32 changes: 12 additions & 20 deletions js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type StoragePersister = {

type SchematicOptions = {
apiUrl?: string;
webSocketUrl?: string;
eventUrl?: string;
flagListener?: (values: Record<string, boolean>) => void;
storage?: StoragePersister;
Expand All @@ -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[];
Expand Down Expand Up @@ -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();
Expand All @@ -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: {
Expand Down Expand Up @@ -158,9 +164,8 @@ export class Schematic {
): Promise<Record<string, boolean>> => {
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: {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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, {
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 064cda5

Please sign in to comment.