@@ -19,7 +19,7 @@ import {
19
19
type HeadersInit ,
20
20
} from './_shims/index' ;
21
21
export { type Response } ;
22
- import { isMultipartBody } from './uploads' ;
22
+ import { BlobLike , isBlobLike , isMultipartBody } from './uploads' ;
23
23
export {
24
24
maybeMultipartFormRequestOptions ,
25
25
multipartFormRequestOptions ,
@@ -249,7 +249,17 @@ export abstract class APIClient {
249
249
path : string ,
250
250
opts ?: PromiseOrValue < RequestOptions < Req > > ,
251
251
) : APIPromise < Rsp > {
252
- return this . request ( Promise . resolve ( opts ) . then ( ( opts ) => ( { method, path, ...opts } ) ) ) ;
252
+ return this . request (
253
+ Promise . resolve ( opts ) . then ( async ( opts ) => {
254
+ const body =
255
+ opts && isBlobLike ( opts ?. body ) ? new DataView ( await opts . body . arrayBuffer ( ) )
256
+ : opts ?. body instanceof DataView ? opts . body
257
+ : opts ?. body instanceof ArrayBuffer ? new DataView ( opts . body )
258
+ : opts && ArrayBuffer . isView ( opts ?. body ) ? new DataView ( opts . body . buffer )
259
+ : opts ?. body ;
260
+ return { method, path, ...opts , body } ;
261
+ } ) ,
262
+ ) ;
253
263
}
254
264
255
265
getAPIList < Item , PageClass extends AbstractPage < Item > = AbstractPage < Item > > (
@@ -271,6 +281,8 @@ export abstract class APIClient {
271
281
const encoded = encoder . encode ( body ) ;
272
282
return encoded . length . toString ( ) ;
273
283
}
284
+ } else if ( ArrayBuffer . isView ( body ) ) {
285
+ return body . byteLength . toString ( ) ;
274
286
}
275
287
276
288
return null ;
@@ -280,7 +292,9 @@ export abstract class APIClient {
280
292
const { method, path, query, headers : headers = { } } = options ;
281
293
282
294
const body =
283
- isMultipartBody ( options . body ) ? options . body . body
295
+ ArrayBuffer . isView ( options . body ) || ( options . __binaryRequest && typeof options . body === 'string' ) ?
296
+ options . body
297
+ : isMultipartBody ( options . body ) ? options . body . body
284
298
: options . body ? JSON . stringify ( options . body , null , 2 )
285
299
: null ;
286
300
const contentLength = this . calculateContentLength ( body ) ;
@@ -735,7 +749,9 @@ export type Headers = Record<string, string | null | undefined>;
735
749
export type DefaultQuery = Record < string , string | undefined > ;
736
750
export type KeysEnum < T > = { [ P in keyof Required < T > ] : true } ;
737
751
738
- export type RequestOptions < Req = unknown | Record < string , unknown > | Readable > = {
752
+ export type RequestOptions <
753
+ Req = unknown | Record < string , unknown > | Readable | BlobLike | ArrayBufferView | ArrayBuffer ,
754
+ > = {
739
755
method ?: HTTPMethod ;
740
756
path ?: string ;
741
757
query ?: Req | undefined ;
@@ -749,6 +765,7 @@ export type RequestOptions<Req = unknown | Record<string, unknown> | Readable> =
749
765
signal ?: AbortSignal | undefined | null ;
750
766
idempotencyKey ?: string ;
751
767
768
+ __binaryRequest ?: boolean | undefined ;
752
769
__binaryResponse ?: boolean | undefined ;
753
770
__streamClass ?: typeof Stream ;
754
771
} ;
@@ -770,6 +787,7 @@ const requestOptionsKeys: KeysEnum<RequestOptions> = {
770
787
signal : true ,
771
788
idempotencyKey : true ,
772
789
790
+ __binaryRequest : true ,
773
791
__binaryResponse : true ,
774
792
__streamClass : true ,
775
793
} ;
@@ -783,10 +801,11 @@ export const isRequestOptions = (obj: unknown): obj is RequestOptions => {
783
801
) ;
784
802
} ;
785
803
786
- export type FinalRequestOptions < Req = unknown | Record < string , unknown > | Readable > = RequestOptions < Req > & {
787
- method : HTTPMethod ;
788
- path : string ;
789
- } ;
804
+ export type FinalRequestOptions < Req = unknown | Record < string , unknown > | Readable | DataView > =
805
+ RequestOptions < Req > & {
806
+ method : HTTPMethod ;
807
+ path : string ;
808
+ } ;
790
809
791
810
declare const Deno : any ;
792
811
declare const EdgeRuntime : any ;
0 commit comments