Skip to content

Commit

Permalink
feat(api): OpenAPI spec update via Stainless API (#865)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] authored and stainless-bot committed Jun 10, 2024
1 parent 123808c commit c63d0d2
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 14 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: 1343
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/cloudflare%2Fcloudflare-6545c93a16062d2763cd304367d8babf5aaef09046036361dbb077b75536e858.yml
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/cloudflare%2Fcloudflare-f1ab7e5d073ef27b0e08bd9fd83e406c15c1c69017c29b17dadceec459febd7b.yml
35 changes: 27 additions & 8 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
type HeadersInit,
} from './_shims/index';
export { type Response };
import { isMultipartBody } from './uploads';
import { BlobLike, isBlobLike, isMultipartBody } from './uploads';
export {
maybeMultipartFormRequestOptions,
multipartFormRequestOptions,
Expand Down Expand Up @@ -235,7 +235,17 @@ export abstract class APIClient {
path: string,
opts?: PromiseOrValue<RequestOptions<Req>>,
): APIPromise<Rsp> {
return this.request(Promise.resolve(opts).then((opts) => ({ method, path, ...opts })));
return this.request(
Promise.resolve(opts).then(async (opts) => {
const body =
opts && isBlobLike(opts?.body) ? new DataView(await opts.body.arrayBuffer())
: opts?.body instanceof DataView ? opts.body
: opts?.body instanceof ArrayBuffer ? new DataView(opts.body)
: opts && ArrayBuffer.isView(opts?.body) ? new DataView(opts.body.buffer)
: opts?.body;
return { method, path, ...opts, body };
}),
);
}

getAPIList<Item, PageClass extends AbstractPage<Item> = AbstractPage<Item>>(
Expand All @@ -257,6 +267,8 @@ export abstract class APIClient {
const encoded = encoder.encode(body);
return encoded.length.toString();
}
} else if (ArrayBuffer.isView(body)) {
return body.byteLength.toString();
}

return null;
Expand All @@ -266,7 +278,9 @@ export abstract class APIClient {
const { method, path, query, headers: headers = {} } = options;

const body =
isMultipartBody(options.body) ? options.body.body
ArrayBuffer.isView(options.body) || (options.__binaryRequest && typeof options.body === 'string') ?
options.body
: isMultipartBody(options.body) ? options.body.body
: options.body ? JSON.stringify(options.body, null, 2)
: null;
const contentLength = this.calculateContentLength(body);
Expand Down Expand Up @@ -721,7 +735,9 @@ export type Headers = Record<string, string | null | undefined>;
export type DefaultQuery = Record<string, string | undefined>;
export type KeysEnum<T> = { [P in keyof Required<T>]: true };

export type RequestOptions<Req = unknown | Record<string, unknown> | Readable> = {
export type RequestOptions<
Req = unknown | Record<string, unknown> | Readable | BlobLike | ArrayBufferView | ArrayBuffer,
> = {
method?: HTTPMethod;
path?: string;
query?: Req | undefined;
Expand All @@ -735,6 +751,7 @@ export type RequestOptions<Req = unknown | Record<string, unknown> | Readable> =
signal?: AbortSignal | undefined | null;
idempotencyKey?: string;

__binaryRequest?: boolean | undefined;
__binaryResponse?: boolean | undefined;
};

Expand All @@ -755,6 +772,7 @@ const requestOptionsKeys: KeysEnum<RequestOptions> = {
signal: true,
idempotencyKey: true,

__binaryRequest: true,
__binaryResponse: true,
};

Expand All @@ -767,10 +785,11 @@ export const isRequestOptions = (obj: unknown): obj is RequestOptions => {
);
};

export type FinalRequestOptions<Req = unknown | Record<string, unknown> | Readable> = RequestOptions<Req> & {
method: HTTPMethod;
path: string;
};
export type FinalRequestOptions<Req = unknown | Record<string, unknown> | Readable | DataView> =
RequestOptions<Req> & {
method: HTTPMethod;
path: string;
};

declare const Deno: any;
declare const EdgeRuntime: any;
Expand Down
4 changes: 4 additions & 0 deletions src/resources/vectorize/indexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ export class Indexes extends APIResource {
this._client.post(`/accounts/${account_id}/vectorize/indexes/${indexName}/insert`, {
body: body,
...options,
headers: { 'Content-Type': 'application/x-ndjson', ...options?.headers },
__binaryRequest: true,
}) as Core.APIPromise<{ result: IndexInsert | null }>
)._thenUnwrap((obj) => obj.result);
}
Expand Down Expand Up @@ -168,6 +170,8 @@ export class Indexes extends APIResource {
this._client.post(`/accounts/${account_id}/vectorize/indexes/${indexName}/upsert`, {
body: body,
...options,
headers: { 'Content-Type': 'application/x-ndjson', ...options?.headers },
__binaryRequest: true,
}) as Core.APIPromise<{ result: IndexUpsert | null }>
)._thenUnwrap((obj) => obj.result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ export class Scripts extends APIResource {
return (
this._client.put(
`/accounts/${account_id}/workers/dispatch/namespaces/${dispatchNamespace}/scripts/${scriptName}`,
maybeMultipartFormRequestOptions({ body, ...options }),
maybeMultipartFormRequestOptions({
body,
...options,
headers: { 'Content-Type': 'application/javascript', ...options?.headers },
}),
) as Core.APIPromise<{ result: ScriptsAPI.Script }>
)._thenUnwrap((obj) => obj.result);
}
Expand Down
7 changes: 6 additions & 1 deletion src/resources/workers/scripts/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ export class Scripts extends APIResource {
return (
this._client.put(
`/accounts/${account_id}/workers/scripts/${scriptName}`,
maybeMultipartFormRequestOptions({ query: { rollback_to }, body, ...options }),
maybeMultipartFormRequestOptions({
query: { rollback_to },
body,
...options,
headers: { 'Content-Type': 'application/javascript', ...options?.headers },
}),
) as Core.APIPromise<{ result: Script }>
)._thenUnwrap((obj) => obj.result);
}
Expand Down
4 changes: 3 additions & 1 deletion src/resources/zero-trust/dlp/datasets/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export class Upload extends APIResource {
this._client.post(`/accounts/${account_id}/dlp/datasets/${datasetId}/upload/${version}`, {
body: body,
...options,
headers: { 'Content-Type': 'application/octet-stream', ...options?.headers },
__binaryRequest: true,
}) as Core.APIPromise<{ result: DatasetsAPI.Dataset }>
)._thenUnwrap((obj) => obj.result);
}
Expand All @@ -63,7 +65,7 @@ export interface UploadEditParams {
/**
* Body param:
*/
body: unknown;
body: string;
}

export namespace Upload {
Expand Down
4 changes: 2 additions & 2 deletions tests/api-resources/zero-trust/dlp/datasets/upload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('resource upload', () => {
const responsePromise = cloudflare.zeroTrust.dlp.datasets.upload.edit(
'182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e',
0,
{ account_id: 'string', body: {} },
{ account_id: 'string', body: 'string' },
);
const rawResponse = await responsePromise.asResponse();
expect(rawResponse).toBeInstanceOf(Response);
Expand All @@ -52,7 +52,7 @@ describe('resource upload', () => {
const response = await cloudflare.zeroTrust.dlp.datasets.upload.edit(
'182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e',
0,
{ account_id: 'string', body: {} },
{ account_id: 'string', body: 'string' },
);
});
});

0 comments on commit c63d0d2

Please sign in to comment.