diff --git a/src/lib/seam-http-request.ts b/src/lib/seam-http-request.ts index 7f10e0cd..4b36050d 100644 --- a/src/lib/seam-http-request.ts +++ b/src/lib/seam-http-request.ts @@ -92,10 +92,7 @@ export class SeamHttpRequest< public get url(): URL { const { client } = this.#parent - const serializer = - typeof client.defaults.paramsSerializer === 'function' - ? client.defaults.paramsSerializer - : serializeUrlSearchParams + const serializer = getParamsSerializer(client.defaults.paramsSerializer) const origin = getUrlPrefix(client.defaults.baseURL ?? '') @@ -256,6 +253,16 @@ export class SeamHttpRequest< } } +const getParamsSerializer = ( + paramsSerializer: Client['defaults']['paramsSerializer'], +): ((params: Record) => string) => { + if (typeof paramsSerializer === 'function') return paramsSerializer + if (typeof paramsSerializer?.serialize === 'function') { + return paramsSerializer.serialize + } + return serializeUrlSearchParams +} + /** * Reads the response data at the response key, * throwing a {@link SeamHttpInvalidResponseError} for a success response diff --git a/test/seam/connect/seam-http-request.test.ts b/test/seam/connect/seam-http-request.test.ts index d61c3f86..8ed8ea48 100644 --- a/test/seam/connect/seam-http-request.test.ts +++ b/test/seam/connect/seam-http-request.test.ts @@ -205,6 +205,36 @@ test.serial( }, ) +test('SeamHttpRequest: url uses a custom function form paramsSerializer', async (t) => { + const { seed } = await getTestServer(t) + const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { + endpoint: 'https://example.com', + axiosOptions: { + paramsSerializer: () => 'custom=function', + }, + }) + + const { url } = seam.devices.get({ device_id: 'abc123' }) + + t.is(url.search, '?custom=function') +}) + +test('SeamHttpRequest: url uses a custom object form paramsSerializer', async (t) => { + const { seed } = await getTestServer(t) + const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { + endpoint: 'https://example.com', + axiosOptions: { + paramsSerializer: { + serialize: () => 'custom=object', + }, + }, + }) + + const { url } = seam.devices.get({ device_id: 'abc123' }) + + t.is(url.search, '?custom=object') +}) + test('SeamHttpRequest: sends the request at most once when awaited more than once', async (t) => { const { seed, endpoint } = await getTestServer(t) const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { endpoint })