1
1
/* istanbul ignore file */
2
2
/* tslint:disable */
3
3
/* eslint-disable */
4
- import FormData from 'form-data' ;
5
- import fetch , { Headers } from 'node-fetch' ;
6
- import type { RequestInit , Response } from 'node-fetch' ;
7
- import type { AbortSignal } from 'node-fetch/externals' ;
8
-
9
4
import { ApiError } from './ApiError' ;
10
5
import type { ApiRequestOptions } from './ApiRequestOptions' ;
11
6
import type { ApiResult } from './ApiResult' ;
@@ -42,6 +37,10 @@ const isFormData = (value: any): value is FormData => {
42
37
return value instanceof FormData ;
43
38
} ;
44
39
40
+ const isSuccess = ( status : number ) : boolean => {
41
+ return status >= 200 && status < 300 ;
42
+ } ;
43
+
45
44
const base64 = ( str : string ) : string => {
46
45
try {
47
46
return btoa ( str ) ;
@@ -170,7 +169,7 @@ const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Pr
170
169
if ( options . mediaType ) {
171
170
headers [ 'Content-Type' ] = options . mediaType ;
172
171
} else if ( isBlob ( options . body ) ) {
173
- headers [ 'Content-Type' ] = 'application/octet-stream' ;
172
+ headers [ 'Content-Type' ] = options . body . type || 'application/octet-stream' ;
174
173
} else if ( isString ( options . body ) ) {
175
174
headers [ 'Content-Type' ] = 'text/plain' ;
176
175
} else if ( ! isFormData ( options . body ) ) {
@@ -186,7 +185,7 @@ const getRequestBody = (options: ApiRequestOptions): any => {
186
185
if ( options . mediaType ?. includes ( '/json' ) ) {
187
186
return JSON . stringify ( options . body )
188
187
} else if ( isString ( options . body ) || isBlob ( options . body ) || isFormData ( options . body ) ) {
189
- return options . body as any ;
188
+ return options . body ;
190
189
} else {
191
190
return JSON . stringify ( options . body ) ;
192
191
}
@@ -195,47 +194,52 @@ const getRequestBody = (options: ApiRequestOptions): any => {
195
194
} ;
196
195
197
196
export const sendRequest = async (
197
+ config : OpenAPIConfig ,
198
198
options : ApiRequestOptions ,
199
199
url : string ,
200
200
body : any ,
201
201
formData : FormData | undefined ,
202
202
headers : Headers ,
203
203
onCancel : OnCancel
204
- ) : Promise < Response > => {
205
- const controller = new AbortController ( ) ;
206
-
207
- const request : RequestInit = {
208
- headers,
209
- method : options . method ,
210
- body : body ?? formData ,
211
- signal : controller . signal as AbortSignal ,
212
- } ;
204
+ ) : Promise < XMLHttpRequest > => {
205
+ const xhr = new XMLHttpRequest ( ) ;
206
+ xhr . open ( options . method , url , true ) ;
207
+ xhr . withCredentials = config . WITH_CREDENTIALS ;
213
208
214
- onCancel ( ( ) => controller . abort ( ) ) ;
209
+ headers . forEach ( ( value , key ) => {
210
+ xhr . setRequestHeader ( key , value ) ;
211
+ } ) ;
212
+
213
+ return new Promise < XMLHttpRequest > ( ( resolve , reject ) => {
214
+ xhr . onload = ( ) => resolve ( xhr ) ;
215
+ xhr . onabort = ( ) => reject ( new Error ( 'Request aborted' ) ) ;
216
+ xhr . onerror = ( ) => reject ( new Error ( 'Network error' ) ) ;
217
+ xhr . send ( body ?? formData ) ;
215
218
216
- return await fetch ( url , request ) ;
219
+ onCancel ( ( ) => xhr . abort ( ) ) ;
220
+ } ) ;
217
221
} ;
218
222
219
- const getResponseHeader = ( response : Response , responseHeader ?: string ) : string | undefined => {
223
+ const getResponseHeader = ( xhr : XMLHttpRequest , responseHeader ?: string ) : string | undefined => {
220
224
if ( responseHeader ) {
221
- const content = response . headers . get ( responseHeader ) ;
225
+ const content = xhr . getResponseHeader ( responseHeader ) ;
222
226
if ( isString ( content ) ) {
223
227
return content ;
224
228
}
225
229
}
226
230
return undefined ;
227
231
} ;
228
232
229
- const getResponseBody = async ( response : Response ) : Promise < any > => {
230
- if ( response . status !== 204 ) {
233
+ const getResponseBody = ( xhr : XMLHttpRequest ) : any => {
234
+ if ( xhr . status !== 204 ) {
231
235
try {
232
- const contentType = response . headers . get ( 'Content-Type' ) ;
236
+ const contentType = xhr . getResponseHeader ( 'Content-Type' ) ;
233
237
if ( contentType ) {
234
238
const isJSON = contentType . toLowerCase ( ) . startsWith ( 'application/json' ) ;
235
239
if ( isJSON ) {
236
- return await response . json ( ) ;
240
+ return JSON . parse ( xhr . responseText ) ;
237
241
} else {
238
- return await response . text ( ) ;
242
+ return xhr . responseText ;
239
243
}
240
244
}
241
245
} catch ( error ) {
@@ -283,13 +287,13 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C
283
287
const headers = await getHeaders ( config , options ) ;
284
288
285
289
if ( ! onCancel . isCancelled ) {
286
- const response = await sendRequest ( options , url , body , formData , headers , onCancel ) ;
287
- const responseBody = await getResponseBody ( response ) ;
290
+ const response = await sendRequest ( config , options , url , body , formData , headers , onCancel ) ;
291
+ const responseBody = getResponseBody ( response ) ;
288
292
const responseHeader = getResponseHeader ( response , options . responseHeader ) ;
289
293
290
294
const result : ApiResult = {
291
295
url,
292
- ok : response . ok ,
296
+ ok : isSuccess ( response . status ) ,
293
297
status : response . status ,
294
298
statusText : response . statusText ,
295
299
body : responseHeader ?? responseBody ,
0 commit comments