diff --git a/src/core.ts b/src/core.ts index 7b39ed1..aa1511d 100644 --- a/src/core.ts +++ b/src/core.ts @@ -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, @@ -235,7 +235,17 @@ export abstract class APIClient { path: string, opts?: PromiseOrValue>, ): APIPromise { - 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 = AbstractPage>( @@ -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; @@ -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); @@ -721,7 +735,9 @@ export type Headers = Record; export type DefaultQuery = Record; export type KeysEnum = { [P in keyof Required]: true }; -export type RequestOptions | Readable> = { +export type RequestOptions< + Req = unknown | Record | Readable | BlobLike | ArrayBufferView | ArrayBuffer, +> = { method?: HTTPMethod; path?: string; query?: Req | undefined; @@ -735,6 +751,7 @@ export type RequestOptions | Readable> = signal?: AbortSignal | undefined | null; idempotencyKey?: string; + __binaryRequest?: boolean | undefined; __binaryResponse?: boolean | undefined; }; @@ -755,6 +772,7 @@ const requestOptionsKeys: KeysEnum = { signal: true, idempotencyKey: true, + __binaryRequest: true, __binaryResponse: true, }; @@ -767,10 +785,11 @@ export const isRequestOptions = (obj: unknown): obj is RequestOptions => { ); }; -export type FinalRequestOptions | Readable> = RequestOptions & { - method: HTTPMethod; - path: string; -}; +export type FinalRequestOptions | Readable | DataView> = + RequestOptions & { + method: HTTPMethod; + path: string; + }; declare const Deno: any; declare const EdgeRuntime: any;