|
| 1 | +import axios from "axios"; |
| 2 | +import type { RawAxiosRequestHeaders } from "axios"; |
| 3 | + |
| 4 | +import type { ApiRequestOptions } from "./ApiRequestOptions"; |
| 5 | +import { CancelablePromise } from "./CancelablePromise"; |
| 6 | +import type { OpenAPIConfig } from "./OpenAPI"; |
| 7 | + |
| 8 | +// Optional: Get and link the cancelation token, so the request can be aborted. |
| 9 | +const source = axios.CancelToken.source(); |
| 10 | + |
| 11 | +const axiosInstance = axios.create({ |
| 12 | + // Your custom Axios instance config |
| 13 | + baseURL: "http://localhost:4010", |
| 14 | + headers: { |
| 15 | + // Your custom headers |
| 16 | + } satisfies RawAxiosRequestHeaders, |
| 17 | +}); |
| 18 | + |
| 19 | +// Add a request interceptor |
| 20 | +axiosInstance.interceptors.request.use( |
| 21 | + (config) => { |
| 22 | + // Do something before request is sent |
| 23 | + if (!config.url || !config.params) { |
| 24 | + return config; |
| 25 | + } |
| 26 | + |
| 27 | + for (const [key, value] of Object.entries<string>(config.params)) { |
| 28 | + const stringToSearch = `{${key}}`; |
| 29 | + if ( |
| 30 | + config.url !== undefined && |
| 31 | + config.url.search(stringToSearch) !== -1 |
| 32 | + ) { |
| 33 | + config.url = config.url.replace(`{${key}}`, encodeURIComponent(value)); |
| 34 | + delete config.params[key]; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + return config; |
| 39 | + }, |
| 40 | + (error) => { |
| 41 | + // Do something with request error |
| 42 | + return Promise.reject(error); |
| 43 | + }, |
| 44 | +); |
| 45 | + |
| 46 | +// Add a response interceptor |
| 47 | +axiosInstance.interceptors.response.use( |
| 48 | + (response) => { |
| 49 | + // Any status code that lie within the range of 2xx cause this function to trigger |
| 50 | + // Do something with response data |
| 51 | + return response; |
| 52 | + }, |
| 53 | + (error) => { |
| 54 | + // Any status codes that falls outside the range of 2xx cause this function to trigger |
| 55 | + // Do something with response error |
| 56 | + return Promise.reject(error); |
| 57 | + }, |
| 58 | +); |
| 59 | + |
| 60 | +export const request = <T>( |
| 61 | + config: OpenAPIConfig, |
| 62 | + options: ApiRequestOptions, |
| 63 | +): CancelablePromise<T> => { |
| 64 | + return new CancelablePromise((resolve, reject, onCancel) => { |
| 65 | + onCancel(() => source.cancel("The user aborted a request.")); |
| 66 | + |
| 67 | + let formattedHeaders = options.headers as RawAxiosRequestHeaders; |
| 68 | + if (options.mediaType) { |
| 69 | + formattedHeaders = { |
| 70 | + ...options.headers, |
| 71 | + "Content-Type": options.mediaType, |
| 72 | + } satisfies RawAxiosRequestHeaders; |
| 73 | + } |
| 74 | + |
| 75 | + return axiosInstance |
| 76 | + .request({ |
| 77 | + url: options.url, |
| 78 | + data: options.body, |
| 79 | + method: options.method, |
| 80 | + params: { |
| 81 | + ...options.query, |
| 82 | + ...options.path, |
| 83 | + }, |
| 84 | + headers: formattedHeaders, |
| 85 | + cancelToken: source.token, |
| 86 | + }) |
| 87 | + .then((res) => { |
| 88 | + resolve(res.data); |
| 89 | + }) |
| 90 | + .catch((error) => { |
| 91 | + reject(error); |
| 92 | + }); |
| 93 | + }); |
| 94 | +}; |
0 commit comments