Skip to content
Open
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
15 changes: 11 additions & 4 deletions src/lib/seam-http-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? '')

Expand Down Expand Up @@ -256,6 +253,16 @@ export class SeamHttpRequest<
}
}

const getParamsSerializer = (
paramsSerializer: Client['defaults']['paramsSerializer'],
): ((params: Record<string, unknown>) => 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
Expand Down
30 changes: 30 additions & 0 deletions test/seam/connect/seam-http-request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
Loading