Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: problem when providing urls with parameters #46

Merged
merged 4 commits into from
Apr 10, 2024
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