11// This file is auto-generated by @hey-api/openapi-ts
22
33import { createSseClient } from "../core/serverSentEvents.gen.js"
4+ import type { HttpMethod } from "../core/types.gen.js"
5+ import { getValidRequestBody } from "../core/utils.gen.js"
46import type { Client , Config , RequestOptions , ResolvedRequestOptions } from "./types.gen.js"
57import {
68 buildUrl ,
@@ -49,12 +51,12 @@ export const createClient = (config: Config = {}): Client => {
4951 await opts . requestValidator ( opts )
5052 }
5153
52- if ( opts . body && opts . bodySerializer ) {
54+ if ( opts . body !== undefined && opts . bodySerializer ) {
5355 opts . serializedBody = opts . bodySerializer ( opts . body )
5456 }
5557
5658 // remove Content-Type header if body is empty to avoid sending invalid requests
57- if ( opts . serializedBody === undefined || opts . serializedBody === "" ) {
59+ if ( opts . body === undefined || opts . serializedBody === "" ) {
5860 opts . headers . delete ( "Content-Type" )
5961 }
6062
@@ -69,7 +71,7 @@ export const createClient = (config: Config = {}): Client => {
6971 const requestInit : ReqInit = {
7072 redirect : "follow" ,
7173 ...opts ,
72- body : opts . serializedBody ,
74+ body : getValidRequestBody ( opts ) ,
7375 }
7476
7577 let request = new Request ( url , requestInit )
@@ -97,18 +99,36 @@ export const createClient = (config: Config = {}): Client => {
9799 }
98100
99101 if ( response . ok ) {
102+ const parseAs =
103+ ( opts . parseAs === "auto" ? getParseAs ( response . headers . get ( "Content-Type" ) ) : opts . parseAs ) ?? "json"
104+
100105 if ( response . status === 204 || response . headers . get ( "Content-Length" ) === "0" ) {
106+ let emptyData : any
107+ switch ( parseAs ) {
108+ case "arrayBuffer" :
109+ case "blob" :
110+ case "text" :
111+ emptyData = await response [ parseAs ] ( )
112+ break
113+ case "formData" :
114+ emptyData = new FormData ( )
115+ break
116+ case "stream" :
117+ emptyData = response . body
118+ break
119+ case "json" :
120+ default :
121+ emptyData = { }
122+ break
123+ }
101124 return opts . responseStyle === "data"
102- ? { }
125+ ? emptyData
103126 : {
104- data : { } ,
127+ data : emptyData ,
105128 ...result ,
106129 }
107130 }
108131
109- const parseAs =
110- ( opts . parseAs === "auto" ? getParseAs ( response . headers . get ( "Content-Type" ) ) : opts . parseAs ) ?? "json"
111-
112132 let data : any
113133 switch ( parseAs ) {
114134 case "arrayBuffer" :
@@ -178,35 +198,53 @@ export const createClient = (config: Config = {}): Client => {
178198 }
179199 }
180200
181- const makeMethod = ( method : Required < Config > [ "method" ] ) => {
182- const fn = ( options : RequestOptions ) => request ( { ...options , method } )
183- fn . sse = async ( options : RequestOptions ) => {
184- const { opts, url } = await beforeRequest ( options )
185- return createSseClient ( {
186- ...opts ,
187- body : opts . body as BodyInit | null | undefined ,
188- headers : opts . headers as unknown as Record < string , string > ,
189- method,
190- url,
191- } )
192- }
193- return fn
201+ const makeMethodFn = ( method : Uppercase < HttpMethod > ) => ( options : RequestOptions ) => request ( { ...options , method } )
202+
203+ const makeSseFn = ( method : Uppercase < HttpMethod > ) => async ( options : RequestOptions ) => {
204+ const { opts, url } = await beforeRequest ( options )
205+ return createSseClient ( {
206+ ...opts ,
207+ body : opts . body as BodyInit | null | undefined ,
208+ headers : opts . headers as unknown as Record < string , string > ,
209+ method,
210+ onRequest : async ( url , init ) => {
211+ let request = new Request ( url , init )
212+ for ( const fn of interceptors . request . _fns ) {
213+ if ( fn ) {
214+ request = await fn ( request , opts )
215+ }
216+ }
217+ return request
218+ } ,
219+ url,
220+ } )
194221 }
195222
196223 return {
197224 buildUrl,
198- connect : makeMethod ( "CONNECT" ) ,
199- delete : makeMethod ( "DELETE" ) ,
200- get : makeMethod ( "GET" ) ,
225+ connect : makeMethodFn ( "CONNECT" ) ,
226+ delete : makeMethodFn ( "DELETE" ) ,
227+ get : makeMethodFn ( "GET" ) ,
201228 getConfig,
202- head : makeMethod ( "HEAD" ) ,
229+ head : makeMethodFn ( "HEAD" ) ,
203230 interceptors,
204- options : makeMethod ( "OPTIONS" ) ,
205- patch : makeMethod ( "PATCH" ) ,
206- post : makeMethod ( "POST" ) ,
207- put : makeMethod ( "PUT" ) ,
231+ options : makeMethodFn ( "OPTIONS" ) ,
232+ patch : makeMethodFn ( "PATCH" ) ,
233+ post : makeMethodFn ( "POST" ) ,
234+ put : makeMethodFn ( "PUT" ) ,
208235 request,
209236 setConfig,
210- trace : makeMethod ( "TRACE" ) ,
237+ sse : {
238+ connect : makeSseFn ( "CONNECT" ) ,
239+ delete : makeSseFn ( "DELETE" ) ,
240+ get : makeSseFn ( "GET" ) ,
241+ head : makeSseFn ( "HEAD" ) ,
242+ options : makeSseFn ( "OPTIONS" ) ,
243+ patch : makeSseFn ( "PATCH" ) ,
244+ post : makeSseFn ( "POST" ) ,
245+ put : makeSseFn ( "PUT" ) ,
246+ trace : makeSseFn ( "TRACE" ) ,
247+ } ,
248+ trace : makeMethodFn ( "TRACE" ) ,
211249 } as Client
212250}
0 commit comments