import axios , { AxiosInstance , AxiosRequestConfig , AxiosResponse , AxiosInterceptorManager } from "axios" ; import * as qs from "qs" ; import { IInterceptors , ClientInstance } from "@decathlon/moon" ; const paramsSerializer = ( params ) => qs . stringify ( params , { arrayFormat : "repeat" } ) ; function updateCancelToken ( config ? ) { if ( ! config ?. cancelToken ) { const cancelTokenSource = axios . CancelToken . source ( ) ; const cancelToken = cancelTokenSource . token ; const newConfig = config || { } ; newConfig . cancelToken = cancelToken ; return ( ) => cancelTokenSource . cancel ( "Query was cancelled by Moon" ) ; } return undefined ; } export class MoonAxiosInstance implements ClientInstance { instance : AxiosInstance ; interceptors : { request : AxiosInterceptorManager < AxiosRequestConfig > ; response : AxiosInterceptorManager < AxiosResponse > ; } ; constructor ( config ? , interceptors ? ) { this . instance = axios . create ( config ) ; this . interceptors = this . instance . interceptors ; if ( interceptors && interceptors . request && interceptors . request . length > 0 && this . interceptors . request ) { interceptors . request . forEach ( interceptor => this . interceptors . request . use ( interceptor . onFulfilled , interceptor . onRejected ) ) ; } if ( interceptors && interceptors . response && interceptors . response . length > 0 && this . interceptors . response ) { interceptors . response . forEach ( interceptor => this . interceptors . response . use ( interceptor . onFulfilled , interceptor . onRejected ) ) ; } } get < T = any , R = AxiosResponse < T > > ( url , config ? ) { const cancel = updateCancelToken ( config ) ; const getPromise = this . instance . get < T , R > ( url , config ) ; getPromise . cancel = cancel ; return getPromise ; } post < T = any , R = AxiosResponse < T > > ( url , data ? , config ? ) { const cancel = updateCancelToken ( config ) ; const postPromise = this . instance . post < T , R > ( url , data , config ) ; postPromise . cancel = cancel ; return postPromise ; } delete < T = any , R = AxiosResponse < T > > ( url , config ? ) { const cancel = updateCancelToken ( config ) ; const deletePromise = this . instance . delete < T , R > ( url , config ) ; deletePromise . cancel = cancel ; return deletePromise ; } put < T = any , R = AxiosResponse < T > > ( url , data ? , config ? ) { const cancel = updateCancelToken ( config ) ; const putPromise = this . instance . put < T , R > ( url , data , config ) ; putPromise . cancel = cancel ; return putPromise ; } patch < T = any , R = AxiosResponse < T > > ( url , data ? , config ? ) { const cancel = updateCancelToken ( config ) ; const patchPromise = this . instance . patch < T , R > ( url , data , config ) ; patchPromise . cancel = cancel ; return patchPromise ; } } export default function createAxiosClient ( config ? , interceptors ? ) { return new MoonAxiosInstance ( { ... config , paramsSerializer : config ?. paramsSerializer || paramsSerializer } , interceptors ) ; }