diff --git a/src/common/fetchClient.ts b/src/common/fetchClient.ts index 32aa775..f6a2479 100644 --- a/src/common/fetchClient.ts +++ b/src/common/fetchClient.ts @@ -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; diff --git a/test/resources/workflows.test.ts b/test/resources/workflows.test.ts new file mode 100644 index 0000000..1bf36a3 --- /dev/null +++ b/test/resources/workflows.test.ts @@ -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", + ); + }); +});