Skip to content

Commit 9245053

Browse files
chore: make some internal functions async
1 parent 862e363 commit 9245053

File tree

5 files changed

+44
-41
lines changed

5 files changed

+44
-41
lines changed

src/azure.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,10 @@ export class AzureOpenAI extends OpenAI {
125125
this.deploymentName = deployment;
126126
}
127127

128-
override buildRequest(
128+
override async buildRequest(
129129
options: FinalRequestOptions,
130130
props: { retryCount?: number } = {},
131-
): { req: RequestInit & { headers: Headers }; url: string; timeout: number } {
131+
): Promise<{ req: RequestInit & { headers: Headers }; url: string; timeout: number }> {
132132
if (_deployments_endpoints.has(options.path) && options.method === 'post' && options.body !== undefined) {
133133
if (!isObj(options.body)) {
134134
throw new Error('Expected request body to be an object');
@@ -154,7 +154,7 @@ export class AzureOpenAI extends OpenAI {
154154
return undefined;
155155
}
156156

157-
protected override authHeaders(opts: FinalRequestOptions): NullableHeaders | undefined {
157+
protected override async authHeaders(opts: FinalRequestOptions): Promise<NullableHeaders | undefined> {
158158
return;
159159
}
160160

src/client.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ export class OpenAI {
370370
* Create a new client instance re-using the same options given to the current client with optional overriding.
371371
*/
372372
withOptions(options: Partial<ClientOptions>): this {
373-
return new (this.constructor as any as new (props: ClientOptions) => typeof this)({
373+
const client = new (this.constructor as any as new (props: ClientOptions) => typeof this)({
374374
...this._options,
375375
baseURL: this.baseURL,
376376
maxRetries: this.maxRetries,
@@ -385,6 +385,7 @@ export class OpenAI {
385385
webhookSecret: this.webhookSecret,
386386
...options,
387387
});
388+
return client;
388389
}
389390

390391
/**
@@ -402,7 +403,7 @@ export class OpenAI {
402403
return;
403404
}
404405

405-
protected authHeaders(opts: FinalRequestOptions): NullableHeaders | undefined {
406+
protected async authHeaders(opts: FinalRequestOptions): Promise<NullableHeaders | undefined> {
406407
return buildHeaders([{ Authorization: `Bearer ${this.apiKey}` }]);
407408
}
408409

@@ -518,7 +519,9 @@ export class OpenAI {
518519

519520
await this.prepareOptions(options);
520521

521-
const { req, url, timeout } = this.buildRequest(options, { retryCount: maxRetries - retriesRemaining });
522+
const { req, url, timeout } = await this.buildRequest(options, {
523+
retryCount: maxRetries - retriesRemaining,
524+
});
522525

523526
await this.prepareRequest(req, { url, options });
524527

@@ -600,7 +603,7 @@ export class OpenAI {
600603
} with status ${response.status} in ${headersTime - startTime}ms`;
601604

602605
if (!response.ok) {
603-
const shouldRetry = this.shouldRetry(response);
606+
const shouldRetry = await this.shouldRetry(response);
604607
if (retriesRemaining && shouldRetry) {
605608
const retryMessage = `retrying, ${retriesRemaining} attempts remaining`;
606609

@@ -718,7 +721,7 @@ export class OpenAI {
718721
}
719722
}
720723

721-
private shouldRetry(response: Response): boolean {
724+
private async shouldRetry(response: Response): Promise<boolean> {
722725
// Note this is not a standard header.
723726
const shouldRetryHeader = response.headers.get('x-should-retry');
724727

@@ -795,18 +798,18 @@ export class OpenAI {
795798
return sleepSeconds * jitter * 1000;
796799
}
797800

798-
buildRequest(
801+
async buildRequest(
799802
inputOptions: FinalRequestOptions,
800803
{ retryCount = 0 }: { retryCount?: number } = {},
801-
): { req: FinalizedRequestInit; url: string; timeout: number } {
804+
): Promise<{ req: FinalizedRequestInit; url: string; timeout: number }> {
802805
const options = { ...inputOptions };
803806
const { method, path, query, defaultBaseURL } = options;
804807

805808
const url = this.buildURL(path!, query as Record<string, unknown>, defaultBaseURL);
806809
if ('timeout' in options) validatePositiveInteger('timeout', options.timeout);
807810
options.timeout = options.timeout ?? this.timeout;
808811
const { bodyHeaders, body } = this.buildBody({ options });
809-
const reqHeaders = this.buildHeaders({ options: inputOptions, method, bodyHeaders, retryCount });
812+
const reqHeaders = await this.buildHeaders({ options: inputOptions, method, bodyHeaders, retryCount });
810813

811814
const req: FinalizedRequestInit = {
812815
method,
@@ -822,7 +825,7 @@ export class OpenAI {
822825
return { req, url, timeout: options.timeout };
823826
}
824827

825-
private buildHeaders({
828+
private async buildHeaders({
826829
options,
827830
method,
828831
bodyHeaders,
@@ -832,7 +835,7 @@ export class OpenAI {
832835
method: HTTPMethod;
833836
bodyHeaders: HeadersLike;
834837
retryCount: number;
835-
}): Headers {
838+
}): Promise<Headers> {
836839
let idempotencyHeaders: HeadersLike = {};
837840
if (this.idempotencyHeader && method !== 'get') {
838841
if (!options.idempotencyKey) options.idempotencyKey = this.defaultIdempotencyKey();
@@ -850,7 +853,7 @@ export class OpenAI {
850853
'OpenAI-Organization': this.organization,
851854
'OpenAI-Project': this.project,
852855
},
853-
this.authHeaders(options),
856+
await this.authHeaders(options),
854857
this._options.defaultHeaders,
855858
bodyHeaders,
856859
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 OpenAI({ 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 OpenAI({
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 OpenAI({ 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,

tests/lib/azure.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,31 @@ describe('instantiate azure client', () => {
3030
apiVersion,
3131
});
3232

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

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

47-
test('can be removed with `null`', () => {
48-
const { req } = client.buildRequest({
47+
test('can be removed with `null`', async () => {
48+
const { req } = await client.buildRequest({
4949
path: '/foo',
5050
method: 'post',
5151
headers: { 'X-My-Default-Header': null },
5252
});
5353
expect(req.headers.has('x-my-default-header')).toBe(false);
5454
});
5555

56-
test('includes retry count', () => {
57-
const { req } = client.buildRequest(
56+
test('includes retry count', async () => {
57+
const { req } = await client.buildRequest(
5858
{
5959
path: '/foo',
6060
method: 'post',
@@ -593,8 +593,8 @@ describe('azure request building', () => {
593593
});
594594

595595
describe('custom headers', () => {
596-
test('handles undefined', () => {
597-
const { req } = client.buildRequest({
596+
test('handles undefined', async () => {
597+
const { req } = await client.buildRequest({
598598
path: '/foo',
599599
method: 'post',
600600
body: { value: 'hello' },

tests/log.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ describe('debug()', () => {
7171
defaultHeaders: authorizationTest,
7272
});
7373

74-
const { req } = client.buildRequest({ path: '/foo', method: 'post' });
74+
const { req } = await client.buildRequest({ path: '/foo', method: 'post' });
7575
await client.post('/foo', {});
7676

7777
// Verify that the original headers weren't mutated
@@ -100,7 +100,7 @@ describe('debug()', () => {
100100
fetch: opts.fetch,
101101
});
102102

103-
const { req } = client.buildRequest({ path: '/foo', method: 'post' });
103+
const { req } = await client.buildRequest({ path: '/foo', method: 'post' });
104104
await client.post('/foo', {});
105105

106106
// Verify that the original headers weren't mutated

0 commit comments

Comments
 (0)