Last active
February 9, 2022 10:08
-
-
Save felixmosh/f38f236b897c3889595c4cfd7188790d to your computer and use it in GitHub Desktop.
Abort axios request on DEV mode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { devModeInterceptor } from './devModeInterceptor'; | |
class CriticalAPI { | |
private axios: AxiosInstance; | |
constructor() { | |
this.axios = Axios.create({ | |
baseURL: process.env.BASE_URL, | |
}); | |
this.axios.interceptors.request.use(devModeInterceptor); | |
} | |
public chargeCC(ccId: string) { | |
return this.axios.post(`/charge-cc`, { | |
ccId, | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { AxiosRequestConfig } from 'axios'; | |
export function devModeInterceptor(config: AxiosRequestConfig) { | |
if (process.env.NODE_ENV === 'production') { | |
return config; | |
} | |
console.info( | |
{ | |
method: config.method, | |
baseUrl: config.baseURL, | |
url: config.url, | |
headers: Object.keys(config.headers).reduce((newHeaders, key) => { | |
if (typeof config.headers[key] === 'string') { | |
newHeaders[key] = config.headers[key]; | |
} | |
return newHeaders; | |
}, {}), | |
params: config.params, | |
data: config.data, | |
}, | |
`Request skipped in ${process.env.NODE_ENV} mode` | |
); | |
return { adapter: () => Promise.resolve({ data: {} }) }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist demonstrate how you can cancel Axios request on none
production
mode.