Skip to content

Commit

Permalink
added code to replace param placeholders with given params
Browse files Browse the repository at this point in the history
  • Loading branch information
Corepex committed Mar 22, 2024
1 parent 0ae1568 commit 61ed6a9
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 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 Down

0 comments on commit 61ed6a9

Please sign in to comment.