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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ the constructor takes some advanced options that affect behavior.
const seam = new SeamHttp({
apiKey: 'your-api-key',
endpoint: 'https://example.com',
timeout: 30000,
axiosOptions: {},
axiosRetryOptions: {},
})
Expand All @@ -438,6 +439,7 @@ these options may be passed in as the last argument.
```ts
const seam = SeamHttp.fromApiKey('some-api-key', {
endpoint: 'https://example.com',
timeout: 30000,
axiosOptions: {},
axiosRetryOptions: {},
})
Expand All @@ -451,6 +453,22 @@ This option corresponds to the Axios `baseURL` setting.

Either pass the `endpoint` option, or set the `SEAM_ENDPOINT` environment variable.

#### Setting the request timeout

Requests time out after 30 seconds by default.
Pass the `timeout` option, in milliseconds, to override this:

```ts
const seam = new SeamHttp({
apiKey: 'your-api-key',
timeout: 60000,
})
```

Set `timeout` to `0` to disable the timeout entirely.
A request that times out rejects with an Axios `ECONNABORTED` error,
and is retried according to the retry options.

#### Configuring the Axios Client

The Axios client and retry behavior may be configured with custom initiation options
Expand Down
4 changes: 4 additions & 0 deletions src/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { errorInterceptor } from './error-interceptor.js'

export type Client = AxiosInstance

export const defaultTimeout = 30_000

export interface ClientOptions {
timeout?: number
axiosOptions?: AxiosRequestConfig
axiosRetryOptions?: AxiosRetryConfig
}
Expand All @@ -17,6 +20,7 @@ export const createClient = (options: ClientOptions): AxiosInstance => {
const client = axios.create({
paramsSerializer: serializeUrlSearchParams,
adapter: 'fetch',
timeout: options.timeout ?? defaultTimeout,
...options.axiosOptions,
})

Expand Down
3 changes: 2 additions & 1 deletion src/lib/parse-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { seamApiLtsVersion } from 'lib/lts-version.js'
import version from 'lib/version.js'

import { getAuthHeaders } from './auth.js'
import type { Client, ClientOptions } from './client.js'
import { type Client, type ClientOptions, defaultTimeout } from './client.js'
import {
isSeamHttpOptionsWithClient,
isSeamHttpOptionsWithClientSessionToken,
Expand Down Expand Up @@ -40,6 +40,7 @@ export const parseOptions = (

return {
...options,
timeout: options.timeout ?? defaultTimeout,
axiosOptions: {
baseURL: options.endpoint ?? getEndpointFromEnv() ?? defaultEndpoint,
withCredentials: isSeamHttpOptionsWithClientSessionToken(options),
Expand Down
45 changes: 45 additions & 0 deletions test/seam/connect/timeout.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import test from 'ava'
import { AxiosError } from 'axios'
import { getTestServer } from 'fixtures/seam/connect/api.js'

import { SeamHttp } from '@seamapi/http/connect'

test('SeamHttp: times out requests after 30s by default', async (t) => {
const { seed, endpoint } = await getTestServer(t)
const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { endpoint })
t.is(seam.client.defaults.timeout, 30_000)
})

test('SeamHttp: timeout option overrides the default', async (t) => {
const { seed, endpoint } = await getTestServer(t)
const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, {
endpoint,
timeout: 60_000,
})
t.is(seam.client.defaults.timeout, 60_000)
})

test('SeamHttp: axiosOptions timeout overrides the timeout option', async (t) => {
const { seed, endpoint } = await getTestServer(t)
const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, {
endpoint,
timeout: 60_000,
axiosOptions: { timeout: 1_000 },
})
t.is(seam.client.defaults.timeout, 1_000)
})

test('SeamHttp: timeout option aborts slow requests', async (t) => {
const { seed, endpoint } = await getTestServer(t)
const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, {
endpoint,
timeout: 1,
axiosRetryOptions: { retries: 0 },
})

const err = await t.throwsAsync(async () => await seam.devices.list(), {
instanceOf: AxiosError,
})

t.is(err?.code, AxiosError.ECONNABORTED)
})