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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@absmartly/javascript-sdk",
"version": "1.13.3",
"version": "1.13.4",
"description": "A/B Smartly Javascript SDK",
"homepage": "https://github.com/absmartly/javascript-sdk#README.md",
"bugs": "https://github.com/absmartly/javascript-sdk/issues",
Expand Down
33 changes: 33 additions & 0 deletions src/__tests__/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,39 @@ describe("Client", () => {
});
});

it("constructor() should accept custom agent string", (done) => {
fetch.mockResolvedValueOnce(responseMock(200, "OK", defaultMockResponse));

const customAgent = "custom-agent";
const client = new Client({ ...clientOptions, agent: customAgent });

client
.request({
auth: true,
method: "PUT",
path: "/context",
})
.then(() => {
expect(fetch).toHaveBeenCalledTimes(1);
expect(fetch).toHaveBeenCalledWith(`${endpoint}/context`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
"X-API-Key": apiKey,
"X-Agent": customAgent,
"X-Environment": "test",
"X-Application": "test_app",
"X-Application-Version": 1000000,
},
body: undefined,
keepalive: true,
signal: expect.any(Object),
});

done();
});
});

it("publish() should not have the keepalive flag if specified", (done) => {
fetch.mockResolvedValueOnce(responseMock(200, "OK", defaultMockResponse));

Expand Down
20 changes: 20 additions & 0 deletions src/__tests__/sdk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,26 @@ describe("SDK", () => {
done();
});

it("should pass custom agent to client", (done) => {
const customAgent = "my-custom-agent";
const options = {
...sdkOptions,
agent: customAgent,
};

const sdk = new SDK(options);

expect(sdk).toBeInstanceOf(SDK);
expect(Client).toHaveBeenCalledTimes(1);
expect(Client).toHaveBeenCalledWith(
expect.objectContaining({
agent: customAgent,
})
);

done();
});

it("should set default values for unspecified client options", (done) => {
const options = {
application: "application",
Expand Down
2 changes: 1 addition & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type ClientRequestOptions = {
};

export type ClientOptions = {
agent?: "javascript-client";
agent?: string;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just string? Should we list the possible options here? like "javascript-client" | "react-native-client" or something? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user can technically set it as whatever they want 🙂 (Although, apparently they've never tried to 😅)

apiKey: string;
application: string | { name: string; version: number };
endpoint: string;
Expand Down