Skip to content

Commit ac410b3

Browse files
chore: make some internal functions async
1 parent 8c480ed commit ac410b3

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
@@ -406,7 +406,7 @@ export class Gitpod {
406406
* Create a new client instance re-using the same options given to the current client with optional overriding.
407407
*/
408408
withOptions(options: Partial<ClientOptions>): this {
409-
return new (this.constructor as any as new (props: ClientOptions) => typeof this)({
409+
const client = new (this.constructor as any as new (props: ClientOptions) => typeof this)({
410410
...this._options,
411411
baseURL: this.baseURL,
412412
maxRetries: this.maxRetries,
@@ -418,6 +418,7 @@ export class Gitpod {
418418
bearerToken: this.bearerToken,
419419
...options,
420420
});
421+
return client;
421422
}
422423

423424
/**
@@ -435,7 +436,7 @@ export class Gitpod {
435436
return;
436437
}
437438

438-
protected authHeaders(opts: FinalRequestOptions): NullableHeaders | undefined {
439+
protected async authHeaders(opts: FinalRequestOptions): Promise<NullableHeaders | undefined> {
439440
return buildHeaders([{ Authorization: `Bearer ${this.bearerToken}` }]);
440441
}
441442

@@ -567,7 +568,9 @@ export class Gitpod {
567568

568569
await this.prepareOptions(options);
569570

570-
const { req, url, timeout } = this.buildRequest(options, { retryCount: maxRetries - retriesRemaining });
571+
const { req, url, timeout } = await this.buildRequest(options, {
572+
retryCount: maxRetries - retriesRemaining,
573+
});
571574

572575
await this.prepareRequest(req, { url, options });
573576

@@ -645,7 +648,7 @@ export class Gitpod {
645648
} with status ${response.status} in ${headersTime - startTime}ms`;
646649

647650
if (!response.ok) {
648-
const shouldRetry = this.shouldRetry(response);
651+
const shouldRetry = await this.shouldRetry(response);
649652
if (retriesRemaining && shouldRetry) {
650653
const retryMessage = `retrying, ${retriesRemaining} attempts remaining`;
651654

@@ -763,7 +766,7 @@ export class Gitpod {
763766
}
764767
}
765768

766-
private shouldRetry(response: Response): boolean {
769+
private async shouldRetry(response: Response): Promise<boolean> {
767770
// Note this is not a standard header.
768771
const shouldRetryHeader = response.headers.get('x-should-retry');
769772

@@ -840,18 +843,18 @@ export class Gitpod {
840843
return sleepSeconds * jitter * 1000;
841844
}
842845

843-
buildRequest(
846+
async buildRequest(
844847
inputOptions: FinalRequestOptions,
845848
{ retryCount = 0 }: { retryCount?: number } = {},
846-
): { req: FinalizedRequestInit; url: string; timeout: number } {
849+
): Promise<{ req: FinalizedRequestInit; url: string; timeout: number }> {
847850
const options = { ...inputOptions };
848851
const { method, path, query, defaultBaseURL } = options;
849852

850853
const url = this.buildURL(path!, query as Record<string, unknown>, defaultBaseURL);
851854
if ('timeout' in options) validatePositiveInteger('timeout', options.timeout);
852855
options.timeout = options.timeout ?? this.timeout;
853856
const { bodyHeaders, body } = this.buildBody({ options });
854-
const reqHeaders = this.buildHeaders({ options: inputOptions, method, bodyHeaders, retryCount });
857+
const reqHeaders = await this.buildHeaders({ options: inputOptions, method, bodyHeaders, retryCount });
855858

856859
const req: FinalizedRequestInit = {
857860
method,
@@ -867,7 +870,7 @@ export class Gitpod {
867870
return { req, url, timeout: options.timeout };
868871
}
869872

870-
private buildHeaders({
873+
private async buildHeaders({
871874
options,
872875
method,
873876
bodyHeaders,
@@ -877,7 +880,7 @@ export class Gitpod {
877880
method: HTTPMethod;
878881
bodyHeaders: HeadersLike;
879882
retryCount: number;
880-
}): Headers {
883+
}): Promise<Headers> {
881884
let idempotencyHeaders: HeadersLike = {};
882885
if (this.idempotencyHeader && method !== 'get') {
883886
if (!options.idempotencyKey) options.idempotencyKey = this.defaultIdempotencyKey();
@@ -893,7 +896,7 @@ export class Gitpod {
893896
...(options.timeout ? { 'X-Stainless-Timeout': String(Math.trunc(options.timeout / 1000)) } : {}),
894897
...getPlatformHeaders(),
895898
},
896-
this.authHeaders(options),
899+
await this.authHeaders(options),
897900
this._options.defaultHeaders,
898901
bodyHeaders,
899902
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
bearerToken: 'My Bearer Token',
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 },
@@ -354,7 +354,7 @@ describe('instantiate client', () => {
354354
});
355355

356356
describe('withOptions', () => {
357-
test('creates a new client with overridden options', () => {
357+
test('creates a new client with overridden options', async () => {
358358
const client = new Gitpod({
359359
baseURL: 'http://localhost:5000/',
360360
maxRetries: 3,
@@ -379,7 +379,7 @@ describe('instantiate client', () => {
379379
expect(newClient.constructor).toBe(client.constructor);
380380
});
381381

382-
test('inherits options from the parent client', () => {
382+
test('inherits options from the parent client', async () => {
383383
const client = new Gitpod({
384384
baseURL: 'http://localhost:5000/',
385385
defaultHeaders: { 'X-Test-Header': 'test-value' },
@@ -394,7 +394,7 @@ describe('instantiate client', () => {
394394
// Test inherited options remain the same
395395
expect(newClient.buildURL('/foo', null)).toEqual('http://localhost:5001/foo?test-param=test-value');
396396

397-
const { req } = newClient.buildRequest({ path: '/foo', method: 'get' });
397+
const { req } = await newClient.buildRequest({ path: '/foo', method: 'get' });
398398
expect(req.headers.get('x-test-header')).toEqual('test-value');
399399
});
400400

@@ -448,8 +448,8 @@ describe('request building', () => {
448448
const client = new Gitpod({ bearerToken: 'My Bearer Token' });
449449

450450
describe('custom headers', () => {
451-
test('handles undefined', () => {
452-
const { req } = client.buildRequest({
451+
test('handles undefined', async () => {
452+
const { req } = await client.buildRequest({
453453
path: '/foo',
454454
method: 'post',
455455
body: { value: 'hello' },
@@ -484,8 +484,8 @@ describe('default encoder', () => {
484484
}
485485
}
486486
for (const jsonValue of [{}, [], { __proto__: null }, new Serializable(), new Collection(['item'])]) {
487-
test(`serializes ${util.inspect(jsonValue)} as json`, () => {
488-
const { req } = client.buildRequest({
487+
test(`serializes ${util.inspect(jsonValue)} as json`, async () => {
488+
const { req } = await client.buildRequest({
489489
path: '/foo',
490490
method: 'post',
491491
body: jsonValue,
@@ -508,7 +508,7 @@ describe('default encoder', () => {
508508
asyncIterable,
509509
]) {
510510
test(`converts ${util.inspect(streamValue)} to ReadableStream`, async () => {
511-
const { req } = client.buildRequest({
511+
const { req } = await client.buildRequest({
512512
path: '/foo',
513513
method: 'post',
514514
body: streamValue,
@@ -521,7 +521,7 @@ describe('default encoder', () => {
521521
}
522522

523523
test(`can set content-type for ReadableStream`, async () => {
524-
const { req } = client.buildRequest({
524+
const { req } = await client.buildRequest({
525525
path: '/foo',
526526
method: 'post',
527527
body: new Response('a\nb\nc\n').body,

0 commit comments

Comments
 (0)