Skip to content

Commit

Permalink
Add option for additional headers (#111)
Browse files Browse the repository at this point in the history
* Add option for additional headers

* bump version
  • Loading branch information
bpapillon authored Oct 23, 2024
1 parent 1279461 commit 1e726a9
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 3 deletions.
2 changes: 1 addition & 1 deletion js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@schematichq/schematic-js",
"version": "1.0.0",
"version": "1.0.1",
"main": "dist/schematic.cjs.js",
"module": "dist/schematic.esm.js",
"types": "dist/schematic.d.ts",
Expand Down
92 changes: 92 additions & 0 deletions js/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,47 @@ describe("Schematic", () => {
});
expect(flagValue).toBe(true);
});

it("should include additional headers", async () => {
const schematicWithHeaders = new Schematic("API_KEY", {
additionalHeaders: { "X-Additional-Header": "foo" },
});
const context = {
company: { companyId: "456" },
user: { userId: "123" },
};
const expectedResponse = {
data: {
companyId: "comp_YRucCyZ3us4",
flag: "FLAG_KEY",
reason: "Matched rule rule_iuBRNdJEjYh",
ruleId: "rule_iuBRNdJEjYh",
userId: "user_6oRr9UTncXf",
value: true,
},
};
mockFetch.mockResolvedValueOnce({
ok: true,
json: jest.fn().mockResolvedValueOnce(expectedResponse),
});

const flagValue = await schematicWithHeaders.checkFlag({
key: "FLAG_KEY",
context,
});

expect(mockFetch).toHaveBeenCalledTimes(1);
expect(mockFetch).toHaveBeenCalledWith(expect.any(String), {
method: "POST",
headers: {
"X-Schematic-Api-Key": "API_KEY",
"Content-Type": "application/json;charset=UTF-8",
"X-Additional-Header": "foo",
},
body: expect.any(String),
});
expect(flagValue).toBe(true);
});
});

describe("checkFlags", () => {
Expand Down Expand Up @@ -257,5 +298,56 @@ describe("Schematic", () => {
FLAG_KEY2: false,
});
});

it("should include additional headers", async () => {
const schematicWithHeaders = new Schematic("API_KEY", {
additionalHeaders: { "X-Additional-Header": "foo" },
});
const context = {
company: { companyId: "456" },
user: { userId: "123" },
};
const expectedResponse = {
data: {
flags: [
{
companyId: "comp_YRucCyZ3us4",
flag: "FLAG_KEY1",
reason: "Matched rule rule_iuBRNdJEjYh",
ruleId: "rule_iuBRNdJEjYh",
userId: "user_6oRr9UTncXf",
value: true,
},
{
companyId: "comp_YRucCyZ3us4",
flag: "FLAG_KEY2",
reason: "No rules matched",
ruleId: null,
userId: "user_6oRr9UTncXf",
value: false,
},
],
},
};
mockFetch.mockResolvedValueOnce({
ok: true,
json: jest.fn().mockResolvedValueOnce(expectedResponse),
});
const flagValues = await schematicWithHeaders.checkFlags(context);
expect(mockFetch).toHaveBeenCalledTimes(1);
expect(mockFetch).toHaveBeenCalledWith(expect.any(String), {
method: "POST",
headers: {
"X-Schematic-Api-Key": "API_KEY",
"Content-Type": "application/json;charset=UTF-8",
"X-Additional-Header": "foo",
},
body: expect.any(String),
});
expect(flagValues).toEqual({
FLAG_KEY1: true,
FLAG_KEY2: false,
});
});
});
});
10 changes: 9 additions & 1 deletion js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const anonymousIdKey = "schematicId";

/* @preserve */
export class Schematic {
private additionalHeaders: Record<string, string> = {};
private apiKey: string;
private apiUrl = "https://api.schematichq.com";
private conn: Promise<WebSocket> | null = null;
Expand All @@ -43,6 +44,10 @@ export class Schematic {
this.useWebSocket = options?.useWebSocket ?? false;
this.flagListener = options?.flagListener;

if (options?.additionalHeaders) {
this.additionalHeaders = options.additionalHeaders;
}

if (options?.storage) {
this.storage = options.storage;
} else if (typeof localStorage !== "undefined") {
Expand Down Expand Up @@ -86,8 +91,9 @@ export class Schematic {
return fetch(requestUrl, {
method: "POST",
headers: {
"X-Schematic-Api-Key": this.apiKey,
...(this.additionalHeaders ?? {}),
"Content-Type": "application/json;charset=UTF-8",
"X-Schematic-Api-Key": this.apiKey,
},
body: JSON.stringify(context),
})
Expand Down Expand Up @@ -117,6 +123,7 @@ export class Schematic {
return fetch(requestUrl, {
method: "POST",
headers: {
...(this.additionalHeaders ?? {}),
"Content-Type": "application/json;charset=UTF-8",
"X-Schematic-Api-Key": this.apiKey,
},
Expand Down Expand Up @@ -247,6 +254,7 @@ export class Schematic {
await fetch(captureUrl, {
method: "POST",
headers: {
...(this.additionalHeaders ?? {}),
"Content-Type": "application/json;charset=UTF-8",
},
body: payload,
Expand Down
3 changes: 2 additions & 1 deletion js/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ export type StoragePersister = {
};

export type SchematicOptions = {
additionalHeaders?: Record<string, string>;
apiUrl?: string;
webSocketUrl?: string;
eventUrl?: string;
flagListener?: (values: Record<string, boolean>) => void;
storage?: StoragePersister;
useWebSocket?: boolean;
webSocketUrl?: string;
};

export type CheckOptions = {
Expand Down

0 comments on commit 1e726a9

Please sign in to comment.