Skip to content
Merged
Changes from all 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
29 changes: 21 additions & 8 deletions examples/react-app/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,26 @@ const axiosInstance = axios.create({

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

Object.entries<any>(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
Expand Down Expand Up @@ -62,6 +74,7 @@ export const request = <T>(
url: options.url,
data: options.body,
method: options.method,
params: options.path,
headers: formattedHeaders,
cancelToken: source.token,
})
Expand Down