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
32 changes: 32 additions & 0 deletions packages/core/src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,36 @@ describe('AmbossClient', () => {
);
assert.equal(new Probe({ serviceApiKey: 'amb_live_test' }).check(), 'amb_live_test');
});

it('applies timeoutMs as an abort signal on requests', async () => {
let receivedSignal: AbortSignal | undefined;
const fetchImpl: typeof fetch = async (_input, init) => {
receivedSignal = init?.signal ?? undefined;
return new Response(JSON.stringify({ data: { ok: true } }), {
headers: { 'content-type': 'application/json' },
});
};
class Probe extends AmbossClient {
run(): Promise<unknown> {
return this.gqlRequest('{ ok }', undefined, 'Probe');
}
}
await new Probe({ apiKey: 'sk_test', fetch: fetchImpl, timeoutMs: 5000 }).run();
assert.ok(receivedSignal instanceof AbortSignal);
});

it('rejects a request that exceeds timeoutMs', async () => {
const hangingFetch: typeof fetch = (_input, init) =>
new Promise((_resolve, reject) => {
init?.signal?.addEventListener('abort', () => reject(new Error('aborted')));
});
class Probe extends AmbossClient {
run(): Promise<unknown> {
return this.gqlRequest('{ ok }', undefined, 'Probe');
}
}
await assert.rejects(() =>
new Probe({ apiKey: 'sk_test', fetch: hangingFetch, timeoutMs: 10 }).run(),
);
});
});
5 changes: 4 additions & 1 deletion packages/core/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ export class AmbossClient {

constructor(config: ClientConfig = {}) {
this.config = AmbossClient.resolveConfig(config);
const { fetch: fetchImpl, timeoutMs } = this.config;
this.graphqlClient = new GraphQLClient(this.config.baseUrl, {
fetch: this.config.fetch,
// Apply timeoutMs as a per-request abort signal, honoring a caller-supplied signal if present.
fetch: (input, init) =>
fetchImpl(input, { ...init, signal: init?.signal ?? AbortSignal.timeout(timeoutMs) }),
headers: this.buildHeaders(),
});
}
Expand Down
Loading