@@ -18,7 +18,7 @@ import {
18
18
type HeadersInit ,
19
19
} from './_shims/index' ;
20
20
export { type Response } ;
21
- import { isMultipartBody } from './uploads' ;
21
+ import { BlobLike , isBlobLike , isMultipartBody } from './uploads' ;
22
22
export {
23
23
maybeMultipartFormRequestOptions ,
24
24
multipartFormRequestOptions ,
@@ -235,7 +235,17 @@ export abstract class APIClient {
235
235
path : string ,
236
236
opts ?: PromiseOrValue < RequestOptions < Req > > ,
237
237
) : 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
+ ) ;
239
249
}
240
250
241
251
getAPIList < Item , PageClass extends AbstractPage < Item > = AbstractPage < Item > > (
@@ -257,6 +267,8 @@ export abstract class APIClient {
257
267
const encoded = encoder . encode ( body ) ;
258
268
return encoded . length . toString ( ) ;
259
269
}
270
+ } else if ( ArrayBuffer . isView ( body ) ) {
271
+ return body . byteLength . toString ( ) ;
260
272
}
261
273
262
274
return null ;
@@ -266,7 +278,9 @@ export abstract class APIClient {
266
278
const { method, path, query, headers : headers = { } } = options ;
267
279
268
280
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
270
284
: options . body ? JSON . stringify ( options . body , null , 2 )
271
285
: null ;
272
286
const contentLength = this . calculateContentLength ( body ) ;
@@ -721,7 +735,9 @@ export type Headers = Record<string, string | null | undefined>;
721
735
export type DefaultQuery = Record < string , string | undefined > ;
722
736
export type KeysEnum < T > = { [ P in keyof Required < T > ] : true } ;
723
737
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
+ > = {
725
741
method ?: HTTPMethod ;
726
742
path ?: string ;
727
743
query ?: Req | undefined ;
@@ -735,6 +751,7 @@ export type RequestOptions<Req = unknown | Record<string, unknown> | Readable> =
735
751
signal ?: AbortSignal | undefined | null ;
736
752
idempotencyKey ?: string ;
737
753
754
+ __binaryRequest ?: boolean | undefined ;
738
755
__binaryResponse ?: boolean | undefined ;
739
756
} ;
740
757
@@ -755,6 +772,7 @@ const requestOptionsKeys: KeysEnum<RequestOptions> = {
755
772
signal : true ,
756
773
idempotencyKey : true ,
757
774
775
+ __binaryRequest : true ,
758
776
__binaryResponse : true ,
759
777
} ;
760
778
@@ -767,10 +785,11 @@ export const isRequestOptions = (obj: unknown): obj is RequestOptions => {
767
785
) ;
768
786
} ;
769
787
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
+ } ;
774
793
775
794
declare const Deno : any ;
776
795
declare const EdgeRuntime : any ;
0 commit comments