Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions examples/react-app/request.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import axios from "axios";
import type { AxiosError, AxiosRequestConfig, AxiosResponse } from "axios";

import type { ApiRequestOptions } from "./ApiRequestOptions";
import { CancelablePromise } from "./CancelablePromise";
Expand All @@ -17,19 +16,31 @@ const axiosInstance = axios.create({
});

// Add a request interceptor
axios.interceptors.request.use(
function (config) {
// Do something before request is sent
return config;
},
function (error) {
// Do something with request error
return Promise.reject(error);
}
axiosInstance.interceptors.request.use(
function (config) {
// Do something before request is sent
if (!config.url || !config.params) {
return config;
}

Object.entries(config.params).forEach(([key, value]) => {
const stringToSearch = `{${key}}`;
if(config.url !== undefined && config.url.search(stringToSearch) !== -1) {
config.url = config.url.replace(`{${key}}`, encodeURIComponent(value));
delete config.params[key];
}
});

return config;
},
function (error) {
// Do something with request error
return Promise.reject(error);
}
);

// Add a response interceptor
axios.interceptors.response.use(
axiosInstance.interceptors.response.use(
function (response) {
// Any status code that lie within the range of 2xx cause this function to trigger
// Do something with response data
Expand All @@ -54,6 +65,7 @@ export const request = <T>(
url: options.url,
data: options.body,
method: options.method,
params: options.path,
cancelToken: source.token,
})
.then((res) => {
Expand Down