Skip to content

Commit

Permalink
feat(api): OpenAPI spec update via Stainless API (#462)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] authored and stainless-bot committed Sep 19, 2024
1 parent baea5ff commit 61f387c
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .stats.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
configured_endpoints: 37
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/finch%2Ffinch-b2ed1e65ba74c2cfa35b2eeb38cd139e89ba1bdd3462e879d118259680680ebc.yml
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/finch%2Ffinch-e915b33a18d4e6592966587ef174bbfe316edd9bc1fd7c17f86f372089bf80eb.yml
2 changes: 1 addition & 1 deletion src/_shims/node-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as fd from 'formdata-node';
export { type Agent } from 'node:http';
export { type Readable } from 'node:stream';
export { type ReadStream as FsReadStream } from 'node:fs';
export { ReadableStream } from 'web-streams-polyfill';
export { ReadableStream } from 'node:stream/web';

export const fetch: typeof nf.default;

Expand Down
16 changes: 12 additions & 4 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,10 @@ export abstract class APIClient {
return null;
}

buildRequest<Req>(options: FinalRequestOptions<Req>): { req: RequestInit; url: string; timeout: number } {
buildRequest<Req>(
options: FinalRequestOptions<Req>,
{ retryCount = 0 }: { retryCount?: number } = {},
): { req: RequestInit; url: string; timeout: number } {
const { method, path, query, headers: headers = {} } = options;

const body =
Expand Down Expand Up @@ -306,7 +309,7 @@ export abstract class APIClient {
headers[this.idempotencyHeader] = options.idempotencyKey;
}

const reqHeaders = this.buildHeaders({ options, headers, contentLength });
const reqHeaders = this.buildHeaders({ options, headers, contentLength, retryCount });

const req: RequestInit = {
method,
Expand All @@ -325,10 +328,12 @@ export abstract class APIClient {
options,
headers,
contentLength,
retryCount,
}: {
options: FinalRequestOptions;
headers: Record<string, string | null | undefined>;
contentLength: string | null | undefined;
retryCount: number;
}): Record<string, string> {
const reqHeaders: Record<string, string> = {};
if (contentLength) {
Expand All @@ -344,6 +349,8 @@ export abstract class APIClient {
delete reqHeaders['content-type'];
}

reqHeaders['x-stainless-retry-count'] = String(retryCount);

this.validateHeaders(reqHeaders, headers);

return reqHeaders;
Expand Down Expand Up @@ -395,13 +402,14 @@ export abstract class APIClient {
retriesRemaining: number | null,
): Promise<APIResponseProps> {
const options = await optionsInput;
const maxRetries = options.maxRetries ?? this.maxRetries;
if (retriesRemaining == null) {
retriesRemaining = options.maxRetries ?? this.maxRetries;
retriesRemaining = maxRetries;
}

await this.prepareOptions(options);

const { req, url, timeout } = this.buildRequest(options);
const { req, url, timeout } = this.buildRequest(options, { retryCount: maxRetries - retriesRemaining });

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

Expand Down
2 changes: 2 additions & 0 deletions src/resources/sandbox/directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ export namespace DirectoryCreateParams {
*/
last_name?: string | null;

latest_rehire_date?: string | null;

location?: HRISAPI.Location | null;

/**
Expand Down
4 changes: 4 additions & 0 deletions src/resources/sandbox/employment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ export interface EmploymentUpdateResponse {
*/
last_name?: string | null;

latest_rehire_date?: string | null;

location?: HRISAPI.Location | null;

/**
Expand Down Expand Up @@ -205,6 +207,8 @@ export interface EmploymentUpdateParams {
*/
last_name?: string | null;

latest_rehire_date?: string | null;

location?: HRISAPI.Location | null;

/**
Expand Down
1 change: 1 addition & 0 deletions tests/api-resources/sandbox/directory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ describe('resource directory', () => {
],
is_active: true,
last_name: 'Smith',
latest_rehire_date: 'latest_rehire_date',
location: {
city: 'city',
country: 'country',
Expand Down
1 change: 1 addition & 0 deletions tests/api-resources/sandbox/employment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ describe('resource employment', () => {
],
is_active: true,
last_name: 'last_name',
latest_rehire_date: 'latest_rehire_date',
location: {
city: 'city',
country: 'country',
Expand Down
31 changes: 31 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,37 @@ describe('retries', () => {
expect(count).toEqual(3);
});

test('retry count header', async () => {
let count = 0;
let capturedRequest: RequestInit | undefined;
const testFetch = async (url: RequestInfo, init: RequestInit = {}): Promise<Response> => {
count++;
if (count <= 2) {
return new Response(undefined, {
status: 429,
headers: {
'Retry-After': '0.1',
},
});
}
capturedRequest = init;
return new Response(JSON.stringify({ a: 1 }), { headers: { 'Content-Type': 'application/json' } });
};

const client = new Finch({
accessToken: 'My Access Token',
clientId: '4ab15e51-11ad-49f4-acae-f343b7794375',
clientSecret: 'My Client Secret',
fetch: testFetch,
maxRetries: 4,
});

expect(await client.request({ path: '/foo', method: 'get' })).toEqual({ a: 1 });

expect((capturedRequest!.headers as Headers)['x-stainless-retry-count']).toEqual('2');
expect(count).toEqual(3);
});

test('retry on 429 with retry-after', async () => {
let count = 0;
const testFetch = async (url: RequestInfo, { signal }: RequestInit = {}): Promise<Response> => {
Expand Down

0 comments on commit 61f387c

Please sign in to comment.