Skip to content

Commit 488b693

Browse files
authored
fix: problem when providing urls with parameters (#46)
1 parent 6508737 commit 488b693

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

examples/react-app/request.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,26 @@ const axiosInstance = axios.create({
1818

1919
// Add a request interceptor
2020
axiosInstance.interceptors.request.use(
21-
function (config) {
22-
// Do something before request is sent
23-
return config;
24-
},
25-
function (error) {
26-
// Do something with request error
27-
return Promise.reject(error);
28-
}
21+
function (config) {
22+
// Do something before request is sent
23+
if (!config.url || !config.params) {
24+
return config;
25+
}
26+
27+
Object.entries<any>(config.params).forEach(([key, value]) => {
28+
const stringToSearch = `{${key}}`;
29+
if(config.url !== undefined && config.url.search(stringToSearch) !== -1) {
30+
config.url = config.url.replace(`{${key}}`, encodeURIComponent(value));
31+
delete config.params[key];
32+
}
33+
});
34+
35+
return config;
36+
},
37+
function (error) {
38+
// Do something with request error
39+
return Promise.reject(error);
40+
}
2941
);
3042

3143
// Add a response interceptor
@@ -62,6 +74,7 @@ export const request = <T>(
6274
url: options.url,
6375
data: options.body,
6476
method: options.method,
77+
params: options.path,
6578
headers: formattedHeaders,
6679
cancelToken: source.token,
6780
})

0 commit comments

Comments
 (0)