Skip to content

Commit

Permalink
fix: format url query params as array rather than comma-separated list
Browse files Browse the repository at this point in the history
  • Loading branch information
connorlindsey committed Feb 26, 2024
1 parent 4b8e172 commit 7d63ef3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/common/fetchClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,12 @@ export default class FetchClient {
const url = new URL(this.config.baseURL + path);

if (params) {
Object.entries(params).forEach(([key, value]) =>
url.searchParams.append(key, value),
);
Object.entries(params).forEach(([key, value]) => {
// Send array values as URL-encoded arrays rather than the default comma-separated list
// e.g. key=[1,2,3] instead of key=1,2,3
const paramValue = Array.isArray(value) ? JSON.stringify(value) : value;
url.searchParams.append(key, paramValue);
});
}

return url;
Expand Down
50 changes: 50 additions & 0 deletions test/resources/workflows.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { afterAll, afterEach, beforeAll, describe, test, expect } from "vitest";
import { setupServer } from "msw/node";
import { HttpResponse, http } from "msw";
import { Knock } from "../../src/knock";
import { ListSchedulesProps } from "../../src/resources/workflows/interfaces";

const restHandlers = [
http.get("http://api.knock.test/v1/schedules", () => {
return HttpResponse.json({
entries: [],
page_info: {
__typename: "PageInfo",
after: null,
before: null,
page_size: 50,
total_count: 0,
},
});
}),
];

const server = setupServer(...restHandlers);

const knock = new Knock("sk_test_12345", {
host: "http://api.knock.test",
});

beforeAll(() => server.listen({ onUnhandledRequest: "error" }));

afterAll(() => server.close());

afterEach(() => server.resetHandlers());

describe("schedules", () => {
test("it can list schedules", async () => {
const params: ListSchedulesProps = {
recipients: ["1", "2", "3"],
};
const { url } = await knock.get("/v1/schedules", {
...params,
workflow: "test-workflow",
});

// Formats recipients list as a URL-encoded array
// 'http://api.knock.test/v1/schedules?recipients=["1","2","3"]&workflow=test-workflow'
expect(url).toBe(
"http://api.knock.test/v1/schedules?recipients=%5B%221%22%2C%222%22%2C%223%22%5D&workflow=test-workflow",
);
});
});

0 comments on commit 7d63ef3

Please sign in to comment.