22/* istanbul ignore file */
33/* tslint:disable */
44/* eslint-disable */
5+ import axios from 'axios' ;
6+ import type { AxiosError , AxiosRequestConfig , AxiosResponse , AxiosInstance } from 'axios' ;
7+ import FormData from 'form-data' ;
8+
59import { ApiError } from './ApiError' ;
610import type { ApiRequestOptions } from './ApiRequestOptions' ;
711import type { ApiResult } from './ApiResult' ;
@@ -38,6 +42,10 @@ export const isFormData = (value: any): value is FormData => {
3842 return value instanceof FormData ;
3943} ;
4044
45+ export const isSuccess = ( status : number ) : boolean => {
46+ return status >= 200 && status < 300 ;
47+ } ;
48+
4149export const base64 = ( str : string ) : string => {
4250 try {
4351 return btoa ( str ) ;
@@ -136,22 +144,24 @@ export const resolve = async <T>(options: ApiRequestOptions, resolver?: T | Reso
136144 return resolver ;
137145} ;
138146
139- export const getHeaders = async ( config : OpenAPIConfig , options : ApiRequestOptions ) : Promise < Headers > => {
147+ export const getHeaders = async ( config : OpenAPIConfig , options : ApiRequestOptions , formData ?: FormData ) : Promise < Record < string , string > > => {
140148 const token = await resolve ( options , config . TOKEN ) ;
141149 const username = await resolve ( options , config . USERNAME ) ;
142150 const password = await resolve ( options , config . PASSWORD ) ;
143151 const additionalHeaders = await resolve ( options , config . HEADERS ) ;
152+ const formHeaders = typeof formData ?. getHeaders === 'function' && formData ?. getHeaders ( ) || { }
144153
145154 const headers = Object . entries ( {
146155 Accept : 'application/json' ,
147156 ...additionalHeaders ,
148157 ...options . headers ,
158+ ...formHeaders ,
149159 } )
150- . filter ( ( [ _ , value ] ) => isDefined ( value ) )
151- . reduce ( ( headers , [ key , value ] ) => ( {
152- ...headers ,
153- [ key ] : String ( value ) ,
154- } ) , { } as Record < string , string > ) ;
160+ . filter ( ( [ _ , value ] ) => isDefined ( value ) )
161+ . reduce ( ( headers , [ key , value ] ) => ( {
162+ ...headers ,
163+ [ key ] : String ( value ) ,
164+ } ) , { } as Record < string , string > ) ;
155165
156166 if ( isStringWithValue ( token ) ) {
157167 headers [ 'Authorization' ] = `Bearer ${ token } ` ;
@@ -174,75 +184,63 @@ export const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptio
174184 }
175185 }
176186
177- return new Headers ( headers ) ;
187+ return headers ;
178188} ;
179189
180190export const getRequestBody = ( options : ApiRequestOptions ) : any => {
181- if ( options . body !== undefined ) {
182- if ( options . mediaType ?. includes ( '/json' ) ) {
183- return JSON . stringify ( options . body )
184- } else if ( isString ( options . body ) || isBlob ( options . body ) || isFormData ( options . body ) ) {
185- return options . body ;
186- } else {
187- return JSON . stringify ( options . body ) ;
188- }
191+ if ( options . body ) {
192+ return options . body ;
189193 }
190194 return undefined ;
191195} ;
192196
193- export const sendRequest = async (
197+ export const sendRequest = async < T > (
194198 config : OpenAPIConfig ,
195199 options : ApiRequestOptions ,
196200 url : string ,
197201 body : any ,
198202 formData : FormData | undefined ,
199- headers : Headers ,
200- onCancel : OnCancel
201- ) : Promise < Response > => {
202- const controller = new AbortController ( ) ;
203-
204- const request : RequestInit = {
203+ headers : Record < string , string > ,
204+ onCancel : OnCancel ,
205+ axiosClient : AxiosInstance
206+ ) : Promise < AxiosResponse < T > > => {
207+ const source = axios . CancelToken . source ( ) ;
208+
209+ const requestConfig : AxiosRequestConfig = {
210+ url,
205211 headers,
206- body : body ?? formData ,
212+ data : body ?? formData ,
207213 method : options . method ,
208- signal : controller . signal ,
214+ withCredentials : config . WITH_CREDENTIALS ,
215+ cancelToken : source . token ,
209216 } ;
210217
211- if ( config . WITH_CREDENTIALS ) {
212- request . credentials = config . CREDENTIALS ;
213- }
214-
215- onCancel ( ( ) => controller . abort ( ) ) ;
218+ onCancel ( ( ) => source . cancel ( 'The user aborted a request.' ) ) ;
216219
217- return await fetch ( url , request ) ;
220+ try {
221+ return await axiosClient . request ( requestConfig ) ;
222+ } catch ( error ) {
223+ const axiosError = error as AxiosError < T > ;
224+ if ( axiosError . response ) {
225+ return axiosError . response ;
226+ }
227+ throw error ;
228+ }
218229} ;
219230
220- export const getResponseHeader = ( response : Response , responseHeader ?: string ) : string | undefined => {
231+ export const getResponseHeader = ( response : AxiosResponse < any > , responseHeader ?: string ) : string | undefined => {
221232 if ( responseHeader ) {
222- const content = response . headers . get ( responseHeader ) ;
233+ const content = response . headers [ responseHeader ] ;
223234 if ( isString ( content ) ) {
224235 return content ;
225236 }
226237 }
227238 return undefined ;
228239} ;
229240
230- export const getResponseBody = async ( response : Response ) : Promise < any > => {
241+ export const getResponseBody = ( response : AxiosResponse < any > ) : any => {
231242 if ( response . status !== 204 ) {
232- try {
233- const contentType = response . headers . get ( 'Content-Type' ) ;
234- if ( contentType ) {
235- const jsonTypes = [ 'application/json' , 'application/problem+json' ]
236- const isJSON = jsonTypes . some ( type => contentType . toLowerCase ( ) . startsWith ( type ) ) ;
237- if ( isJSON ) {
238- return await response . json ( ) ;
239- } else {
240- return await response . text ( ) ;
241- }
242- }
243- } catch ( error ) {
244- console . error ( error ) ;
245- }
243+ return response . data ;
246244 }
247245 return undefined ;
248246} ;
@@ -285,25 +283,26 @@ export const catchErrorCodes = (options: ApiRequestOptions, result: ApiResult):
285283 * Request method
286284 * @param config The OpenAPI configuration object
287285 * @param options The request options from the service
286+ * @param axiosClient The axios client instance to use
288287 * @returns CancelablePromise<T>
289288 * @throws ApiError
290289 */
291- export const request = < T > ( config : OpenAPIConfig , options : ApiRequestOptions ) : CancelablePromise < T > => {
290+ export const request = < T > ( config : OpenAPIConfig , options : ApiRequestOptions , axiosClient : AxiosInstance = axios ) : CancelablePromise < T > => {
292291 return new CancelablePromise ( async ( resolve , reject , onCancel ) => {
293292 try {
294293 const url = getUrl ( config , options ) ;
295294 const formData = getFormData ( options ) ;
296295 const body = getRequestBody ( options ) ;
297- const headers = await getHeaders ( config , options ) ;
296+ const headers = await getHeaders ( config , options , formData ) ;
298297
299298 if ( ! onCancel . isCancelled ) {
300- const response = await sendRequest ( config , options , url , body , formData , headers , onCancel ) ;
301- const responseBody = await getResponseBody ( response ) ;
299+ const response = await sendRequest < T > ( config , options , url , body , formData , headers , onCancel , axiosClient ) ;
300+ const responseBody = getResponseBody ( response ) ;
302301 const responseHeader = getResponseHeader ( response , options . responseHeader ) ;
303302
304303 const result : ApiResult = {
305304 url,
306- ok : response . ok ,
305+ ok : isSuccess ( response . status ) ,
307306 status : response . status ,
308307 statusText : response . statusText ,
309308 body : responseHeader ?? responseBody ,
0 commit comments