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,12 +69,12 @@ 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 )
7876
79- for ( const fn of interceptors . request . fns ) {
77+ for ( const fn of interceptors . request . _fns ) {
8078 if ( fn ) {
8179 request = await fn ( request , opts )
8280 }
@@ -85,37 +83,9 @@ export const createClient = (config: Config = {}): Client => {
8583 // fetch must be assigned here, otherwise it would throw the error:
8684 // TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
8785 const _fetch = opts . fetch !
88- let response : Response
86+ let response = await _fetch ( request )
8987
90- try {
91- response = await _fetch ( request )
92- } catch ( error ) {
93- // Handle fetch exceptions (AbortError, network errors, etc.)
94- let finalError = error
95-
96- for ( const fn of interceptors . error . fns ) {
97- if ( fn ) {
98- finalError = ( await fn ( error , undefined as any , request , opts ) ) as unknown
99- }
100- }
101-
102- finalError = finalError || ( { } as unknown )
103-
104- if ( opts . throwOnError ) {
105- throw finalError
106- }
107-
108- // Return error response
109- return opts . responseStyle === "data"
110- ? undefined
111- : {
112- error : finalError ,
113- request,
114- response : undefined as any ,
115- }
116- }
117-
118- for ( const fn of interceptors . response . fns ) {
88+ for ( const fn of interceptors . response . _fns ) {
11989 if ( fn ) {
12090 response = await fn ( response , request , opts )
12191 }
@@ -127,36 +97,18 @@ export const createClient = (config: Config = {}): Client => {
12797 }
12898
12999 if ( response . ok ) {
130- const parseAs =
131- ( opts . parseAs === "auto" ? getParseAs ( response . headers . get ( "Content-Type" ) ) : opts . parseAs ) ?? "json"
132-
133100 if ( response . status === 204 || response . headers . get ( "Content-Length" ) === "0" ) {
134- let emptyData : any
135- switch ( parseAs ) {
136- case "arrayBuffer" :
137- case "blob" :
138- case "text" :
139- emptyData = await response [ parseAs ] ( )
140- break
141- case "formData" :
142- emptyData = new FormData ( )
143- break
144- case "stream" :
145- emptyData = response . body
146- break
147- case "json" :
148- default :
149- emptyData = { }
150- break
151- }
152101 return opts . responseStyle === "data"
153- ? emptyData
102+ ? { }
154103 : {
155- data : emptyData ,
104+ data : { } ,
156105 ...result ,
157106 }
158107 }
159108
109+ const parseAs =
110+ ( opts . parseAs === "auto" ? getParseAs ( response . headers . get ( "Content-Type" ) ) : opts . parseAs ) ?? "json"
111+
160112 let data : any
161113 switch ( parseAs ) {
162114 case "arrayBuffer" :
@@ -205,7 +157,7 @@ export const createClient = (config: Config = {}): Client => {
205157 const error = jsonError ?? textError
206158 let finalError = error
207159
208- for ( const fn of interceptors . error . fns ) {
160+ for ( const fn of interceptors . error . _fns ) {
209161 if ( fn ) {
210162 finalError = ( await fn ( error , response , request , opts ) ) as string
211163 }
@@ -226,53 +178,35 @@ export const createClient = (config: Config = {}): Client => {
226178 }
227179 }
228180
229- const makeMethodFn = ( method : Uppercase < HttpMethod > ) => ( options : RequestOptions ) => request ( { ...options , method } )
230-
231- const makeSseFn = ( method : Uppercase < HttpMethod > ) => async ( options : RequestOptions ) => {
232- const { opts, url } = await beforeRequest ( options )
233- return createSseClient ( {
234- ...opts ,
235- body : opts . body as BodyInit | null | undefined ,
236- headers : opts . headers as unknown as Record < string , string > ,
237- method,
238- onRequest : async ( url , init ) => {
239- let request = new Request ( url , init )
240- for ( const fn of interceptors . request . fns ) {
241- if ( fn ) {
242- request = await fn ( request , opts )
243- }
244- }
245- return request
246- } ,
247- url,
248- } )
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
249194 }
250195
251196 return {
252197 buildUrl,
253- connect : makeMethodFn ( "CONNECT" ) ,
254- delete : makeMethodFn ( "DELETE" ) ,
255- get : makeMethodFn ( "GET" ) ,
198+ connect : makeMethod ( "CONNECT" ) ,
199+ delete : makeMethod ( "DELETE" ) ,
200+ get : makeMethod ( "GET" ) ,
256201 getConfig,
257- head : makeMethodFn ( "HEAD" ) ,
202+ head : makeMethod ( "HEAD" ) ,
258203 interceptors,
259- options : makeMethodFn ( "OPTIONS" ) ,
260- patch : makeMethodFn ( "PATCH" ) ,
261- post : makeMethodFn ( "POST" ) ,
262- put : makeMethodFn ( "PUT" ) ,
204+ options : makeMethod ( "OPTIONS" ) ,
205+ patch : makeMethod ( "PATCH" ) ,
206+ post : makeMethod ( "POST" ) ,
207+ put : makeMethod ( "PUT" ) ,
263208 request,
264209 setConfig,
265- sse : {
266- connect : makeSseFn ( "CONNECT" ) ,
267- delete : makeSseFn ( "DELETE" ) ,
268- get : makeSseFn ( "GET" ) ,
269- head : makeSseFn ( "HEAD" ) ,
270- options : makeSseFn ( "OPTIONS" ) ,
271- patch : makeSseFn ( "PATCH" ) ,
272- post : makeSseFn ( "POST" ) ,
273- put : makeSseFn ( "PUT" ) ,
274- trace : makeSseFn ( "TRACE" ) ,
275- } ,
276- trace : makeMethodFn ( "TRACE" ) ,
210+ trace : makeMethod ( "TRACE" ) ,
277211 } as Client
278212}
0 commit comments