1- import type { Client , Config , RequestOptions } from './types' ;
1+ // This file is auto-generated by @hey-api/openapi-ts
2+
3+ import { createSseClient } from '../core/serverSentEvents.gen' ;
4+ import type { HttpMethod } from '../core/types.gen' ;
5+ import type {
6+ Client ,
7+ Config ,
8+ RequestOptions ,
9+ ResolvedRequestOptions ,
10+ } from './types.gen' ;
211import {
312 buildUrl ,
413 createConfig ,
@@ -7,7 +16,7 @@ import {
716 mergeConfigs ,
817 mergeHeaders ,
918 setAuthParams ,
10- } from './utils' ;
19+ } from './utils.gen ' ;
1120
1221type ReqInit = Omit < RequestInit , 'body' | 'headers' > & {
1322 body ?: any ;
@@ -28,15 +37,16 @@ export const createClient = (config: Config = {}): Client => {
2837 Request ,
2938 Response ,
3039 unknown ,
31- RequestOptions
40+ ResolvedRequestOptions
3241 > ( ) ;
3342
34- const request : Client [ 'request' ] = async ( options ) => {
43+ const beforeRequest = async ( options : RequestOptions ) => {
3544 const opts = {
3645 ..._config ,
3746 ...options ,
3847 fetch : options . fetch ?? _config . fetch ?? globalThis . fetch ,
3948 headers : mergeHeaders ( _config . headers , options . headers ) ,
49+ serializedBody : undefined ,
4050 } ;
4151
4252 if ( opts . security ) {
@@ -50,19 +60,27 @@ export const createClient = (config: Config = {}): Client => {
5060 await opts . requestValidator ( opts ) ;
5161 }
5262
53- if ( opts . body && opts . bodySerializer ) {
54- opts . body = opts . bodySerializer ( opts . body ) ;
63+ if ( opts . body !== undefined && opts . bodySerializer ) {
64+ opts . serializedBody = opts . bodySerializer ( opts . body ) ;
5565 }
5666
5767 // remove Content-Type header if body is empty to avoid sending invalid requests
58- if ( opts . body === undefined || opts . body === '' ) {
68+ if ( opts . body === undefined || opts . serializedBody === '' ) {
5969 opts . headers . delete ( 'Content-Type' ) ;
6070 }
6171
6272 const url = buildUrl ( opts ) ;
73+
74+ return { opts, url } ;
75+ } ;
76+
77+ const request : Client [ 'request' ] = async ( options ) => {
78+ // @ts -expect-error
79+ const { opts, url } = await beforeRequest ( options ) ;
6380 const requestInit : ReqInit = {
6481 redirect : 'follow' ,
6582 ...opts ,
83+ body : getValidRequestBody ( opts ) ,
6684 } ;
6785
6886 let request = new Request ( url , requestInit ) ;
@@ -90,23 +108,41 @@ export const createClient = (config: Config = {}): Client => {
90108 } ;
91109
92110 if ( response . ok ) {
111+ const parseAs =
112+ ( opts . parseAs === 'auto'
113+ ? getParseAs ( response . headers . get ( 'Content-Type' ) )
114+ : opts . parseAs ) ?? 'json' ;
115+
93116 if (
94117 response . status === 204 ||
95118 response . headers . get ( 'Content-Length' ) === '0'
96119 ) {
120+ let emptyData : any ;
121+ switch ( parseAs ) {
122+ case 'arrayBuffer' :
123+ case 'blob' :
124+ case 'text' :
125+ emptyData = await response [ parseAs ] ( ) ;
126+ break ;
127+ case 'formData' :
128+ emptyData = new FormData ( ) ;
129+ break ;
130+ case 'stream' :
131+ emptyData = response . body ;
132+ break ;
133+ case 'json' :
134+ default :
135+ emptyData = { } ;
136+ break ;
137+ }
97138 return opts . responseStyle === 'data'
98- ? { }
139+ ? emptyData
99140 : {
100- data : { } ,
141+ data : emptyData ,
101142 ...result ,
102143 } ;
103144 }
104145
105- const parseAs =
106- ( opts . parseAs === 'auto'
107- ? getParseAs ( response . headers . get ( 'Content-Type' ) )
108- : opts . parseAs ) ?? 'json' ;
109-
110146 let data : any ;
111147 switch ( parseAs ) {
112148 case 'arrayBuffer' :
@@ -176,20 +212,76 @@ export const createClient = (config: Config = {}): Client => {
176212 } ;
177213 } ;
178214
215+ function getValidRequestBody ( options : ResolvedRequestOptions ) {
216+ const hasBody = options . body !== undefined ;
217+ const isSerializedBody = hasBody && options . bodySerializer ;
218+
219+ if ( isSerializedBody ) {
220+ const hasSerializedBody =
221+ options . serializedBody !== undefined && options . serializedBody !== '' ;
222+
223+ return hasSerializedBody ? options . serializedBody : null ;
224+ }
225+
226+ // plain/text body
227+ if ( hasBody ) {
228+ return options . body ;
229+ }
230+
231+ // no body was provided
232+ return undefined ;
233+ }
234+
235+ const makeMethodFn =
236+ ( method : Uppercase < HttpMethod > ) => ( options : RequestOptions ) =>
237+ request ( { ...options , method } ) ;
238+
239+ const makeSseFn =
240+ ( method : Uppercase < HttpMethod > ) => async ( options : RequestOptions ) => {
241+ const { opts, url } = await beforeRequest ( options ) ;
242+ return createSseClient ( {
243+ ...opts ,
244+ body : opts . body as BodyInit | null | undefined ,
245+ headers : opts . headers as unknown as Record < string , string > ,
246+ method,
247+ onRequest : async ( url , init ) => {
248+ let request = new Request ( url , init ) ;
249+ for ( const fn of interceptors . request . _fns ) {
250+ if ( fn ) {
251+ request = await fn ( request , opts ) ;
252+ }
253+ }
254+ return request ;
255+ } ,
256+ url,
257+ } ) ;
258+ } ;
259+
179260 return {
180261 buildUrl,
181- connect : ( options ) => request ( { ... options , method : 'CONNECT' } ) ,
182- delete : ( options ) => request ( { ... options , method : 'DELETE' } ) ,
183- get : ( options ) => request ( { ... options , method : 'GET' } ) ,
262+ connect : makeMethodFn ( 'CONNECT' ) ,
263+ delete : makeMethodFn ( 'DELETE' ) ,
264+ get : makeMethodFn ( 'GET' ) ,
184265 getConfig,
185- head : ( options ) => request ( { ... options , method : 'HEAD' } ) ,
266+ head : makeMethodFn ( 'HEAD' ) ,
186267 interceptors,
187- options : ( options ) => request ( { ... options , method : 'OPTIONS' } ) ,
188- patch : ( options ) => request ( { ... options , method : 'PATCH' } ) ,
189- post : ( options ) => request ( { ... options , method : 'POST' } ) ,
190- put : ( options ) => request ( { ... options , method : 'PUT' } ) ,
268+ options : makeMethodFn ( 'OPTIONS' ) ,
269+ patch : makeMethodFn ( 'PATCH' ) ,
270+ post : makeMethodFn ( 'POST' ) ,
271+ put : makeMethodFn ( 'PUT' ) ,
191272 request,
192273 setConfig,
193- trace : ( options ) => request ( { ...options , method : 'TRACE' } ) ,
194- } ;
274+ sse : {
275+ connect : makeSseFn ( 'CONNECT' ) ,
276+ delete : makeSseFn ( 'DELETE' ) ,
277+ get : makeSseFn ( 'GET' ) ,
278+ head : makeSseFn ( 'HEAD' ) ,
279+ options : makeSseFn ( 'OPTIONS' ) ,
280+ patch : makeSseFn ( 'PATCH' ) ,
281+ post : makeSseFn ( 'POST' ) ,
282+ put : makeSseFn ( 'PUT' ) ,
283+ trace : makeSseFn ( 'TRACE' ) ,
284+ } ,
285+ trace : makeMethodFn ( 'TRACE' ) ,
286+ } as Client ;
195287} ;
0 commit comments