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"
64import type { Client , Config , RequestOptions , ResolvedRequestOptions } from "./types.gen.js"
75import {
86 buildUrl ,
@@ -51,12 +49,12 @@ export const createClient = (config: Config = {}): Client => {
5149 await opts . requestValidator ( opts )
5250 }
5351
54- if ( opts . body !== undefined && opts . bodySerializer ) {
52+ if ( opts . body && opts . bodySerializer ) {
5553 opts . serializedBody = opts . bodySerializer ( opts . body )
5654 }
5755
5856 // remove Content-Type header if body is empty to avoid sending invalid requests
59- if ( opts . body === undefined || opts . serializedBody === "" ) {
57+ if ( opts . serializedBody === undefined || opts . serializedBody === "" ) {
6058 opts . headers . delete ( "Content-Type" )
6159 }
6260
@@ -71,7 +69,7 @@ export const createClient = (config: Config = {}): Client => {
7169 const requestInit : ReqInit = {
7270 redirect : "follow" ,
7371 ...opts ,
74- body : getValidRequestBody ( opts ) ,
72+ body : opts . serializedBody ,
7573 }
7674
7775 let request = new Request ( url , requestInit )
@@ -99,36 +97,18 @@ export const createClient = (config: Config = {}): Client => {
9997 }
10098
10199 if ( response . ok ) {
102- const parseAs =
103- ( opts . parseAs === "auto" ? getParseAs ( response . headers . get ( "Content-Type" ) ) : opts . parseAs ) ?? "json"
104-
105100 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- }
124101 return opts . responseStyle === "data"
125- ? emptyData
102+ ? { }
126103 : {
127- data : emptyData ,
104+ data : { } ,
128105 ...result ,
129106 }
130107 }
131108
109+ const parseAs =
110+ ( opts . parseAs === "auto" ? getParseAs ( response . headers . get ( "Content-Type" ) ) : opts . parseAs ) ?? "json"
111+
132112 let data : any
133113 switch ( parseAs ) {
134114 case "arrayBuffer" :
@@ -198,53 +178,35 @@ export const createClient = (config: Config = {}): Client => {
198178 }
199179 }
200180
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- } )
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
221194 }
222195
223196 return {
224197 buildUrl,
225- connect : makeMethodFn ( "CONNECT" ) ,
226- delete : makeMethodFn ( "DELETE" ) ,
227- get : makeMethodFn ( "GET" ) ,
198+ connect : makeMethod ( "CONNECT" ) ,
199+ delete : makeMethod ( "DELETE" ) ,
200+ get : makeMethod ( "GET" ) ,
228201 getConfig,
229- head : makeMethodFn ( "HEAD" ) ,
202+ head : makeMethod ( "HEAD" ) ,
230203 interceptors,
231- options : makeMethodFn ( "OPTIONS" ) ,
232- patch : makeMethodFn ( "PATCH" ) ,
233- post : makeMethodFn ( "POST" ) ,
234- put : makeMethodFn ( "PUT" ) ,
204+ options : makeMethod ( "OPTIONS" ) ,
205+ patch : makeMethod ( "PATCH" ) ,
206+ post : makeMethod ( "POST" ) ,
207+ put : makeMethod ( "PUT" ) ,
235208 request,
236209 setConfig,
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" ) ,
210+ trace : makeMethod ( "TRACE" ) ,
249211 } as Client
250212}
0 commit comments