Skip to content
Merged
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
4 changes: 4 additions & 0 deletions src/lib/api/CommercetoolsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3205,6 +3205,10 @@ export class CommercetoolsApi {
url,
headers,
params,
timeoutMs: options.timeoutMs,
aggregateTimeoutMs: options.aggregateTimeoutMs,
retry: options.retry,
abortController: options.abortController,
}
}

Expand Down
21 changes: 21 additions & 0 deletions src/test/api/CommercetoolsApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4618,6 +4618,27 @@ describe('CommercetoolsApi', () => {
url: 'https://api.europe-west1.gcp.commercetools.com/test-project-key/test',
})
})

it('should pass through timeoutMs, aggregateTimeoutMs, retry, and abortController', async () => {
const api = new CommercetoolsApi(defaultConfig)
const abortController = new AbortController()

const result = await api.getRequestOptions({
path: '/test',
method: 'GET',
headers: {},
accessToken: 'mock-access-token',
timeoutMs: 25_000,
aggregateTimeoutMs: 25_000,
retry: { maxRetries: 5, delayMs: 2000 },
abortController,
})

expect(result.timeoutMs).toBe(25_000)
expect(result.aggregateTimeoutMs).toBe(25_000)
expect(result.retry).toEqual({ maxRetries: 5, delayMs: 2000 })
expect(result.abortController).toBe(abortController)
})
})

describe('applyStore', () => {
Expand Down
67 changes: 67 additions & 0 deletions src/test/api/per-request-timeout.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import nock from 'nock'
import { CommercetoolsApi, CommercetoolsApiConfig, Region } from '../../lib/index.js'
import { CommercetoolsGrantResponse } from '../../lib/auth/types.js'

const AUTH_URL = 'https://auth.europe-west1.gcp.commercetools.com'
const API_URL = 'https://api.europe-west1.gcp.commercetools.com'

const config: CommercetoolsApiConfig = {
projectKey: 'test-project',
clientId: 'test-client-id',
clientSecret: 'test-client-secret',
region: Region.EUROPE_GCP,
clientScopes: ['manage_orders'],
timeoutMs: 1_000,
}

const grantResponse: CommercetoolsGrantResponse = {
access_token: 'test-access-token',
refresh_token: 'test-refresh-token',
scope: 'manage_orders:test-project',
expires_in: 172800,
}

function mockAuth(): void {
nock(AUTH_URL, { encodedQueryParams: true }).persist().post('/oauth/token').reply(200, grantResponse)
}

describe('per-request timeout override', () => {
beforeEach(() => {
nock.cleanAll()
nock.enableNetConnect('127.0.0.1')
mockAuth()
})

afterAll(() => {
nock.cleanAll()
nock.disableNetConnect()
})

it('should timeout at the instance-level default (1s) when no per-request override is given', async () => {
nock(API_URL).get('/test-project/orders').query(true).delay(2_000).reply(200, { results: [], total: 0 })

const api = new CommercetoolsApi(config)

await expect(api.queryOrders({ params: { limit: 1 }, retry: { maxRetries: 0, delayMs: 0 } })).rejects.toThrow(
/timeout/i,
)
}, 10_000)

it('should NOT timeout at the instance default when a per-request timeoutMs override exceeds the delay', async () => {
nock(API_URL)
.get('/test-project/orders')
.query(true)
.delay(2_000)
.reply(200, { results: [{ id: 'order-1' }], total: 1 })

const api = new CommercetoolsApi(config)

const result = await api.queryOrders({
params: { limit: 1 },
timeoutMs: 5_000,
aggregateTimeoutMs: 5_000,
})

expect(result.results).toEqual([{ id: 'order-1' }])
}, 10_000)
})