Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 146 additions & 17 deletions src/__tests__/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
});
Expand All @@ -217,7 +225,6 @@ describe("Client", () => {

client
.request({
auth: true,
method: "PUT",
path: "/context",
query: { a: 1 },
Expand Down Expand Up @@ -266,7 +273,6 @@ describe("Client", () => {

client
.request({
auth: true,
method: "PUT",
path: "/context",
query: { a: 1 },
Expand Down Expand Up @@ -310,7 +316,6 @@ describe("Client", () => {

client
.request({
auth: true,
method: "PUT",
path: "/context",
query: { a: 1 },
Expand Down Expand Up @@ -361,7 +366,6 @@ describe("Client", () => {

client
.request({
auth: true,
method: "PUT",
path: "/context",
query: { a: 1 },
Expand Down Expand Up @@ -402,7 +406,6 @@ describe("Client", () => {

client
.request({
auth: true,
method: "PUT",
path: "/context",
query: { a: 1 },
Expand All @@ -428,7 +431,6 @@ describe("Client", () => {

client
.request({
auth: true,
method: "PUT",
path: "/context",
query: { a: 1 },
Expand All @@ -455,7 +457,6 @@ describe("Client", () => {

client
.request({
auth: true,
method: "PUT",
path: "/context",
query: { a: 1 },
Expand Down Expand Up @@ -497,7 +498,6 @@ describe("Client", () => {

client
.request({
auth: true,
method: "PUT",
path: "/context",
query: { a: 1 },
Expand Down Expand Up @@ -532,7 +532,6 @@ describe("Client", () => {

client
.request({
auth: true,
method: "PUT",
path: "/context",
query: { a: 1 },
Expand Down Expand Up @@ -562,7 +561,6 @@ describe("Client", () => {

client
.request({
auth: true,
method: "PUT",
path: "/context",
query: { a: 1 },
Expand All @@ -587,7 +585,6 @@ describe("Client", () => {

client
.request({
auth: true,
method: "POST",
path: "/context",
query: { a: 1 },
Expand Down Expand Up @@ -625,7 +622,6 @@ describe("Client", () => {

client
.request({
auth: true,
method: "POST",
path: "/context",
query: { a: 1 },
Expand Down Expand Up @@ -663,7 +659,6 @@ describe("Client", () => {

client
.request({
auth: true,
method: "PUT",
path: "/context",
query: { a: 1, b: "ã=á" },
Expand Down Expand Up @@ -699,7 +694,6 @@ describe("Client", () => {

client
.request({
auth: true,
method: "PUT",
path: "/context",
query: {},
Expand Down Expand Up @@ -735,7 +729,6 @@ describe("Client", () => {

client
.request({
auth: true,
method: "PUT",
path: "/context",
})
Expand Down Expand Up @@ -769,7 +762,6 @@ describe("Client", () => {

client
.request({
auth: true,
method: "PUT",
path: "/context",
})
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -954,7 +1084,6 @@ describe("Client", () => {

client
.request({
auth: true,
method: "PUT",
path: "/context",
})
Expand Down
20 changes: 15 additions & 5 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type ClientRequestOptions = {
path: string;
method?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE" | "HEAD" | "OPTIONS";
body?: Record<string, unknown>;
/** @deprecated The API key is sent by default; set to false to suppress auth headers. */
auth?: boolean;
signal?: AbortSignal | ABsmartlyAbortSignal;
timeout?: number;
Expand Down Expand Up @@ -90,7 +91,7 @@ export default class Client {
}

getContext(options?: Partial<ClientRequestOptions>) {
return this.getUnauthed({
return this.get({
...options,
path: "/context",
query: {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -281,15 +288,13 @@ export default class Client {
post(options: ClientRequestOptions) {
return this.request({
...options,
auth: true,
method: "POST",
});
}

put(options: ClientRequestOptions) {
return this.request({
...options,
auth: true,
method: "PUT",
});
}
Expand All @@ -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);
}
}
Loading