Skip to content

Commit 6da4953

Browse files
chore(internal): version bump (#13)
1 parent 3c840d6 commit 6da4953

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

src/core.ts

+27-8
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
type HeadersInit,
1919
} from './_shims/index';
2020
export { type Response };
21-
import { isMultipartBody } from './uploads';
21+
import { BlobLike, isBlobLike, isMultipartBody } from './uploads';
2222
export {
2323
maybeMultipartFormRequestOptions,
2424
multipartFormRequestOptions,
@@ -235,7 +235,17 @@ export abstract class APIClient {
235235
path: string,
236236
opts?: PromiseOrValue<RequestOptions<Req>>,
237237
): APIPromise<Rsp> {
238-
return this.request(Promise.resolve(opts).then((opts) => ({ method, path, ...opts })));
238+
return this.request(
239+
Promise.resolve(opts).then(async (opts) => {
240+
const body =
241+
opts && isBlobLike(opts?.body) ? new DataView(await opts.body.arrayBuffer())
242+
: opts?.body instanceof DataView ? opts.body
243+
: opts?.body instanceof ArrayBuffer ? new DataView(opts.body)
244+
: opts && ArrayBuffer.isView(opts?.body) ? new DataView(opts.body.buffer)
245+
: opts?.body;
246+
return { method, path, ...opts, body };
247+
}),
248+
);
239249
}
240250

241251
getAPIList<Item, PageClass extends AbstractPage<Item> = AbstractPage<Item>>(
@@ -257,6 +267,8 @@ export abstract class APIClient {
257267
const encoded = encoder.encode(body);
258268
return encoded.length.toString();
259269
}
270+
} else if (ArrayBuffer.isView(body)) {
271+
return body.byteLength.toString();
260272
}
261273

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

268280
const body =
269-
isMultipartBody(options.body) ? options.body.body
281+
ArrayBuffer.isView(options.body) || (options.__binaryRequest && typeof options.body === 'string') ?
282+
options.body
283+
: isMultipartBody(options.body) ? options.body.body
270284
: options.body ? JSON.stringify(options.body, null, 2)
271285
: null;
272286
const contentLength = this.calculateContentLength(body);
@@ -721,7 +735,9 @@ export type Headers = Record<string, string | null | undefined>;
721735
export type DefaultQuery = Record<string, string | undefined>;
722736
export type KeysEnum<T> = { [P in keyof Required<T>]: true };
723737

724-
export type RequestOptions<Req = unknown | Record<string, unknown> | Readable> = {
738+
export type RequestOptions<
739+
Req = unknown | Record<string, unknown> | Readable | BlobLike | ArrayBufferView | ArrayBuffer,
740+
> = {
725741
method?: HTTPMethod;
726742
path?: string;
727743
query?: Req | undefined;
@@ -735,6 +751,7 @@ export type RequestOptions<Req = unknown | Record<string, unknown> | Readable> =
735751
signal?: AbortSignal | undefined | null;
736752
idempotencyKey?: string;
737753

754+
__binaryRequest?: boolean | undefined;
738755
__binaryResponse?: boolean | undefined;
739756
};
740757

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

775+
__binaryRequest: true,
758776
__binaryResponse: true,
759777
};
760778

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

770-
export type FinalRequestOptions<Req = unknown | Record<string, unknown> | Readable> = RequestOptions<Req> & {
771-
method: HTTPMethod;
772-
path: string;
773-
};
788+
export type FinalRequestOptions<Req = unknown | Record<string, unknown> | Readable | DataView> =
789+
RequestOptions<Req> & {
790+
method: HTTPMethod;
791+
path: string;
792+
};
774793

775794
declare const Deno: any;
776795
declare const EdgeRuntime: any;

0 commit comments

Comments
 (0)