Skip to content

Commit

Permalink
fix: problem when providing urls with parameters (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
Corepex authored Apr 10, 2024
1 parent 6508737 commit 488b693
Showing 1 changed file with 21 additions and 8 deletions.
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

0 comments on commit 488b693

Please sign in to comment.