diff --git a/src/__tests__/client.test.js b/src/__tests__/client.test.js index 7834ff4..80cf106 100644 --- a/src/__tests__/client.test.js +++ b/src/__tests__/client.test.js @@ -197,6 +197,14 @@ describe("Client", () => { expect(fetch).toHaveBeenCalledTimes(1); expect(fetch).toHaveBeenCalledWith(`${endpoint}/context?application=test_app&environment=test`, { method: "GET", + headers: { + "Content-Type": "application/json", + "X-API-Key": apiKey, + "X-Agent": "javascript-client", + "X-Environment": "test", + "X-Application": "test_app", + "X-Application-Version": 1000000, + }, keepalive: true, signal: expect.any(Object), }); @@ -217,7 +225,6 @@ describe("Client", () => { client .request({ - auth: true, method: "PUT", path: "/context", query: { a: 1 }, @@ -266,7 +273,6 @@ describe("Client", () => { client .request({ - auth: true, method: "PUT", path: "/context", query: { a: 1 }, @@ -310,7 +316,6 @@ describe("Client", () => { client .request({ - auth: true, method: "PUT", path: "/context", query: { a: 1 }, @@ -361,7 +366,6 @@ describe("Client", () => { client .request({ - auth: true, method: "PUT", path: "/context", query: { a: 1 }, @@ -402,7 +406,6 @@ describe("Client", () => { client .request({ - auth: true, method: "PUT", path: "/context", query: { a: 1 }, @@ -428,7 +431,6 @@ describe("Client", () => { client .request({ - auth: true, method: "PUT", path: "/context", query: { a: 1 }, @@ -455,7 +457,6 @@ describe("Client", () => { client .request({ - auth: true, method: "PUT", path: "/context", query: { a: 1 }, @@ -497,7 +498,6 @@ describe("Client", () => { client .request({ - auth: true, method: "PUT", path: "/context", query: { a: 1 }, @@ -532,7 +532,6 @@ describe("Client", () => { client .request({ - auth: true, method: "PUT", path: "/context", query: { a: 1 }, @@ -562,7 +561,6 @@ describe("Client", () => { client .request({ - auth: true, method: "PUT", path: "/context", query: { a: 1 }, @@ -587,7 +585,6 @@ describe("Client", () => { client .request({ - auth: true, method: "POST", path: "/context", query: { a: 1 }, @@ -625,7 +622,6 @@ describe("Client", () => { client .request({ - auth: true, method: "POST", path: "/context", query: { a: 1 }, @@ -663,7 +659,6 @@ describe("Client", () => { client .request({ - auth: true, method: "PUT", path: "/context", query: { a: 1, b: "ã=á" }, @@ -699,7 +694,6 @@ describe("Client", () => { client .request({ - auth: true, method: "PUT", path: "/context", query: {}, @@ -735,7 +729,6 @@ describe("Client", () => { client .request({ - auth: true, method: "PUT", path: "/context", }) @@ -769,7 +762,6 @@ describe("Client", () => { client .request({ - auth: true, method: "PUT", path: "/context", }) @@ -822,6 +814,144 @@ describe("Client", () => { }); }); + it("request() should still send headers when auth is explicitly true (deprecated)", (done) => { + fetch.mockResolvedValueOnce(responseMock(200, "OK", defaultMockResponse)); + const warnSpy = jest.spyOn(console, "warn").mockImplementation(); + + const client = new Client(clientOptions); + + client + .request({ + auth: true, + method: "PUT", + path: "/context", + }) + .then((response) => { + expect(fetch).toHaveBeenCalledTimes(1); + expect(fetch).toHaveBeenLastCalledWith(`${endpoint}/context`, { + method: "PUT", + headers: { + "Content-Type": "application/json", + "X-API-Key": apiKey, + "X-Agent": "javascript-client", + "X-Environment": "test", + "X-Application": "test_app", + "X-Application-Version": 1000000, + }, + body: undefined, + keepalive: true, + signal: expect.any(Object), + }); + + expect(warnSpy).toHaveBeenCalledWith( + "[ABsmartly] The `auth` option is deprecated. Auth headers are now sent by default. Remove `auth: true` from your request options." + ); + + expect(response).toEqual(defaultMockResponse); + + warnSpy.mockRestore(); + done(); + }); + }); + + it("get() should send a GET request with headers", (done) => { + fetch.mockResolvedValueOnce(responseMock(200, "OK", defaultMockResponse)); + + const client = new Client(clientOptions); + + client + .get({ + path: "/context", + query: { application: "test_app", environment: "test" }, + }) + .then((response) => { + expect(fetch).toHaveBeenCalledTimes(1); + expect(fetch).toHaveBeenLastCalledWith( + `${endpoint}/context?application=test_app&environment=test`, + { + method: "GET", + headers: { + "Content-Type": "application/json", + "X-API-Key": apiKey, + "X-Agent": "javascript-client", + "X-Environment": "test", + "X-Application": "test_app", + "X-Application-Version": 1000000, + }, + keepalive: true, + signal: expect.any(Object), + } + ); + + expect(response).toEqual(defaultMockResponse); + + done(); + }); + }); + + it("get() should not send headers when auth is false", (done) => { + fetch.mockResolvedValueOnce(responseMock(200, "OK", defaultMockResponse)); + + const client = new Client(Object.assign({}, clientOptions, { application: "website" })); + + client + .get({ + auth: false, + path: "/context", + query: { application: "website", environment: "test" }, + }) + .then((response) => { + expect(fetch).toHaveBeenCalledTimes(1); + expect(fetch).toHaveBeenLastCalledWith( + `${endpoint}/context?application=website&environment=test`, + { + method: "GET", + keepalive: true, + signal: expect.any(Object), + } + ); + + expect(response).toEqual(defaultMockResponse); + + done(); + }); + }); + + it("getUnauthed() should forward to get()", (done) => { + fetch.mockResolvedValueOnce(responseMock(200, "OK", defaultMockResponse)); + + const client = new Client(clientOptions); + + client + .getUnauthed({ + path: "/context", + query: { application: "test_app", environment: "test" }, + }) + .then((response) => { + expect(fetch).toHaveBeenCalledTimes(1); + expect(fetch).toHaveBeenLastCalledWith( + `${endpoint}/context?application=test_app&environment=test`, + { + method: "GET", + headers: { + "Content-Type": "application/json", + "X-API-Key": apiKey, + "X-Agent": "javascript-client", + "X-Environment": "test", + "X-Application": "test_app", + "X-Application-Version": 1000000, + }, + keepalive: true, + signal: expect.any(Object), + } + ); + + expect(response).toEqual(defaultMockResponse); + + done(); + }); + }); + it("publish() calls endpoint", (done) => { fetch.mockResolvedValueOnce(responseMock(200, "OK", defaultMockResponse)); @@ -954,7 +1084,6 @@ describe("Client", () => { client .request({ - auth: true, method: "PUT", path: "/context", }) diff --git a/src/client.ts b/src/client.ts index e7d4e12..56d91a2 100644 --- a/src/client.ts +++ b/src/client.ts @@ -21,6 +21,7 @@ export type ClientRequestOptions = { path: string; method?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS"; body?: Record; + /** @deprecated The API key is sent by default; set to false to suppress auth headers. */ auth?: boolean; signal?: AbortSignal | ABsmartlyAbortSignal; timeout?: number; @@ -90,7 +91,7 @@ export default class Client { } getContext(options?: Partial) { - return this.getUnauthed({ + return this.get({ ...options, path: "/context", query: { @@ -161,7 +162,13 @@ export default class Client { keepalive: this._opts.keepalive, }; - if (options.auth) { + if (options.auth === true) { + console.warn( + "[ABsmartly] The `auth` option is deprecated. Auth headers are now sent by default. Remove `auth: true` from your request options." + ); + } + + if (options.auth !== false) { opts.headers = { "Content-Type": "application/json", "X-API-Key": this._opts.apiKey, @@ -281,7 +288,6 @@ export default class Client { post(options: ClientRequestOptions) { return this.request({ ...options, - auth: true, method: "POST", }); } @@ -289,7 +295,6 @@ export default class Client { put(options: ClientRequestOptions) { return this.request({ ...options, - auth: true, method: "PUT", }); } @@ -302,10 +307,15 @@ export default class Client { return this._opts.application; } - getUnauthed(options: ClientRequestOptions) { + get(options: ClientRequestOptions) { return this.request({ ...options, method: "GET", }); } + + /** @deprecated Use get() instead. */ + getUnauthed(options: ClientRequestOptions) { + return this.get(options); + } }