Skip to content

Commit d763165

Browse files
chore: make some internal functions async
1 parent 16b7822 commit d763165

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

src/client.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ export class Isaacus {
178178
* Create a new client instance re-using the same options given to the current client with optional overriding.
179179
*/
180180
withOptions(options: Partial<ClientOptions>): this {
181-
return new (this.constructor as any as new (props: ClientOptions) => typeof this)({
181+
const client = new (this.constructor as any as new (props: ClientOptions) => typeof this)({
182182
...this._options,
183183
baseURL: this.baseURL,
184184
maxRetries: this.maxRetries,
@@ -190,6 +190,7 @@ export class Isaacus {
190190
apiKey: this.apiKey,
191191
...options,
192192
});
193+
return client;
193194
}
194195

195196
/**
@@ -207,7 +208,7 @@ export class Isaacus {
207208
return;
208209
}
209210

210-
protected authHeaders(opts: FinalRequestOptions): NullableHeaders | undefined {
211+
protected async authHeaders(opts: FinalRequestOptions): Promise<NullableHeaders | undefined> {
211212
return buildHeaders([{ Authorization: `Bearer ${this.apiKey}` }]);
212213
}
213214

@@ -339,7 +340,9 @@ export class Isaacus {
339340

340341
await this.prepareOptions(options);
341342

342-
const { req, url, timeout } = this.buildRequest(options, { retryCount: maxRetries - retriesRemaining });
343+
const { req, url, timeout } = await this.buildRequest(options, {
344+
retryCount: maxRetries - retriesRemaining,
345+
});
343346

344347
await this.prepareRequest(req, { url, options });
345348

@@ -417,7 +420,7 @@ export class Isaacus {
417420
} with status ${response.status} in ${headersTime - startTime}ms`;
418421

419422
if (!response.ok) {
420-
const shouldRetry = this.shouldRetry(response);
423+
const shouldRetry = await this.shouldRetry(response);
421424
if (retriesRemaining && shouldRetry) {
422425
const retryMessage = `retrying, ${retriesRemaining} attempts remaining`;
423426

@@ -516,7 +519,7 @@ export class Isaacus {
516519
}
517520
}
518521

519-
private shouldRetry(response: Response): boolean {
522+
private async shouldRetry(response: Response): Promise<boolean> {
520523
// Note this is not a standard header.
521524
const shouldRetryHeader = response.headers.get('x-should-retry');
522525

@@ -593,18 +596,18 @@ export class Isaacus {
593596
return sleepSeconds * jitter * 1000;
594597
}
595598

596-
buildRequest(
599+
async buildRequest(
597600
inputOptions: FinalRequestOptions,
598601
{ retryCount = 0 }: { retryCount?: number } = {},
599-
): { req: FinalizedRequestInit; url: string; timeout: number } {
602+
): Promise<{ req: FinalizedRequestInit; url: string; timeout: number }> {
600603
const options = { ...inputOptions };
601604
const { method, path, query, defaultBaseURL } = options;
602605

603606
const url = this.buildURL(path!, query as Record<string, unknown>, defaultBaseURL);
604607
if ('timeout' in options) validatePositiveInteger('timeout', options.timeout);
605608
options.timeout = options.timeout ?? this.timeout;
606609
const { bodyHeaders, body } = this.buildBody({ options });
607-
const reqHeaders = this.buildHeaders({ options: inputOptions, method, bodyHeaders, retryCount });
610+
const reqHeaders = await this.buildHeaders({ options: inputOptions, method, bodyHeaders, retryCount });
608611

609612
const req: FinalizedRequestInit = {
610613
method,
@@ -620,7 +623,7 @@ export class Isaacus {
620623
return { req, url, timeout: options.timeout };
621624
}
622625

623-
private buildHeaders({
626+
private async buildHeaders({
624627
options,
625628
method,
626629
bodyHeaders,
@@ -630,7 +633,7 @@ export class Isaacus {
630633
method: HTTPMethod;
631634
bodyHeaders: HeadersLike;
632635
retryCount: number;
633-
}): Headers {
636+
}): Promise<Headers> {
634637
let idempotencyHeaders: HeadersLike = {};
635638
if (this.idempotencyHeader && method !== 'get') {
636639
if (!options.idempotencyKey) options.idempotencyKey = this.defaultIdempotencyKey();
@@ -646,7 +649,7 @@ export class Isaacus {
646649
...(options.timeout ? { 'X-Stainless-Timeout': String(Math.trunc(options.timeout / 1000)) } : {}),
647650
...getPlatformHeaders(),
648651
},
649-
this.authHeaders(options),
652+
await this.authHeaders(options),
650653
this._options.defaultHeaders,
651654
bodyHeaders,
652655
options.headers,

tests/index.test.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,22 @@ describe('instantiate client', () => {
2626
apiKey: 'My API Key',
2727
});
2828

29-
test('they are used in the request', () => {
30-
const { req } = client.buildRequest({ path: '/foo', method: 'post' });
29+
test('they are used in the request', async () => {
30+
const { req } = await client.buildRequest({ path: '/foo', method: 'post' });
3131
expect(req.headers.get('x-my-default-header')).toEqual('2');
3232
});
3333

34-
test('can ignore `undefined` and leave the default', () => {
35-
const { req } = client.buildRequest({
34+
test('can ignore `undefined` and leave the default', async () => {
35+
const { req } = await client.buildRequest({
3636
path: '/foo',
3737
method: 'post',
3838
headers: { 'X-My-Default-Header': undefined },
3939
});
4040
expect(req.headers.get('x-my-default-header')).toEqual('2');
4141
});
4242

43-
test('can be removed with `null`', () => {
44-
const { req } = client.buildRequest({
43+
test('can be removed with `null`', async () => {
44+
const { req } = await client.buildRequest({
4545
path: '/foo',
4646
method: 'post',
4747
headers: { 'X-My-Default-Header': null },
@@ -344,7 +344,7 @@ describe('instantiate client', () => {
344344
});
345345

346346
describe('withOptions', () => {
347-
test('creates a new client with overridden options', () => {
347+
test('creates a new client with overridden options', async () => {
348348
const client = new Isaacus({ baseURL: 'http://localhost:5000/', maxRetries: 3, apiKey: 'My API Key' });
349349

350350
const newClient = client.withOptions({
@@ -365,7 +365,7 @@ describe('instantiate client', () => {
365365
expect(newClient.constructor).toBe(client.constructor);
366366
});
367367

368-
test('inherits options from the parent client', () => {
368+
test('inherits options from the parent client', async () => {
369369
const client = new Isaacus({
370370
baseURL: 'http://localhost:5000/',
371371
defaultHeaders: { 'X-Test-Header': 'test-value' },
@@ -380,7 +380,7 @@ describe('instantiate client', () => {
380380
// Test inherited options remain the same
381381
expect(newClient.buildURL('/foo', null)).toEqual('http://localhost:5001/foo?test-param=test-value');
382382

383-
const { req } = newClient.buildRequest({ path: '/foo', method: 'get' });
383+
const { req } = await newClient.buildRequest({ path: '/foo', method: 'get' });
384384
expect(req.headers.get('x-test-header')).toEqual('test-value');
385385
});
386386

@@ -430,8 +430,8 @@ describe('request building', () => {
430430
const client = new Isaacus({ apiKey: 'My API Key' });
431431

432432
describe('custom headers', () => {
433-
test('handles undefined', () => {
434-
const { req } = client.buildRequest({
433+
test('handles undefined', async () => {
434+
const { req } = await client.buildRequest({
435435
path: '/foo',
436436
method: 'post',
437437
body: { value: 'hello' },
@@ -466,8 +466,8 @@ describe('default encoder', () => {
466466
}
467467
}
468468
for (const jsonValue of [{}, [], { __proto__: null }, new Serializable(), new Collection(['item'])]) {
469-
test(`serializes ${util.inspect(jsonValue)} as json`, () => {
470-
const { req } = client.buildRequest({
469+
test(`serializes ${util.inspect(jsonValue)} as json`, async () => {
470+
const { req } = await client.buildRequest({
471471
path: '/foo',
472472
method: 'post',
473473
body: jsonValue,
@@ -490,7 +490,7 @@ describe('default encoder', () => {
490490
asyncIterable,
491491
]) {
492492
test(`converts ${util.inspect(streamValue)} to ReadableStream`, async () => {
493-
const { req } = client.buildRequest({
493+
const { req } = await client.buildRequest({
494494
path: '/foo',
495495
method: 'post',
496496
body: streamValue,
@@ -503,7 +503,7 @@ describe('default encoder', () => {
503503
}
504504

505505
test(`can set content-type for ReadableStream`, async () => {
506-
const { req } = client.buildRequest({
506+
const { req } = await client.buildRequest({
507507
path: '/foo',
508508
method: 'post',
509509
body: new Response('a\nb\nc\n').body,

0 commit comments

Comments
 (0)