improve api structure
This commit is contained in:
1 parent
29eb856a45
commit
fb1c015d9a
4 files changed
+108
-61
No files matched your search
@@ -1,18 +1,8 @@
|
||||
import axios, { AxiosError, InternalAxiosRequestConfig } from 'axios';
|
||||
import axios, { InternalAxiosRequestConfig } from 'axios';
|
||||
|
||||
import { getAdminAuthSnapshot } from '~/shared/libs/admin-auth';
|
||||
|
||||
const API_BASE_PATH = '/api/v1';
|
||||
const API_ENDPOINTS = [
|
||||
'https://my-public-node-8.projscale.dev',
|
||||
'http://localhost:3000',
|
||||
] as const;
|
||||
|
||||
const API_BASE_URLS = API_ENDPOINTS.map((endpoint) => {
|
||||
return `${endpoint.replace(/\/$/, '')}${API_BASE_PATH}`;
|
||||
});
|
||||
|
||||
const ENDPOINT_INDEX_KEY = '__apiEndpointIndex__';
|
||||
|
||||
const isBrowser = typeof window !== 'undefined';
|
||||
|
||||
@@ -28,10 +18,23 @@ const readStorage = (key: string) => {
|
||||
}
|
||||
};
|
||||
|
||||
type RequestConfig = InternalAxiosRequestConfig & {
|
||||
[ENDPOINT_INDEX_KEY]?: number;
|
||||
type RequestConfig = InternalAxiosRequestConfig;
|
||||
|
||||
const resolveDefaultBaseUrl = () => {
|
||||
const envBase = (import.meta.env.VITE_API_BASE_URL ?? '').trim();
|
||||
if (envBase) {
|
||||
return envBase.replace(/\/$/, '');
|
||||
}
|
||||
|
||||
if (isBrowser) {
|
||||
return `${window.location.origin.replace(/\/$/, '')}${API_BASE_PATH}`;
|
||||
}
|
||||
|
||||
return API_BASE_PATH;
|
||||
};
|
||||
|
||||
const DEFAULT_API_BASE_URL = resolveDefaultBaseUrl();
|
||||
|
||||
export const request = axios.create({ withCredentials: true });
|
||||
|
||||
request.interceptors.request.use((config: RequestConfig) => {
|
||||
@@ -54,49 +57,11 @@ request.interceptors.request.use((config: RequestConfig) => {
|
||||
}
|
||||
}
|
||||
|
||||
const hasCustomBaseUrl = Boolean(config.baseURL && !API_BASE_URLS.includes(config.baseURL));
|
||||
if (hasCustomBaseUrl) {
|
||||
return config;
|
||||
if (!config.baseURL) {
|
||||
config.baseURL = DEFAULT_API_BASE_URL;
|
||||
}
|
||||
|
||||
const attempt = typeof config[ENDPOINT_INDEX_KEY] === 'number' ? (config[ENDPOINT_INDEX_KEY] as number) : 0;
|
||||
config[ENDPOINT_INDEX_KEY] = attempt;
|
||||
config.baseURL = API_BASE_URLS[attempt] ?? API_BASE_URLS[0];
|
||||
|
||||
return config;
|
||||
});
|
||||
|
||||
request.interceptors.response.use(
|
||||
(response) => response,
|
||||
async (error: AxiosError) => {
|
||||
const config = error.config as RequestConfig | undefined;
|
||||
|
||||
if (!config) {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
|
||||
const hasCustomBaseUrl = Boolean(config.baseURL && !API_BASE_URLS.includes(config.baseURL));
|
||||
if (hasCustomBaseUrl) {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
|
||||
const isAbortError = error.code === 'ERR_CANCELED' || error.message === 'canceled';
|
||||
const isNetworkError = !error.response && !isAbortError;
|
||||
|
||||
if (!isNetworkError) {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
|
||||
const currentIndex = typeof config[ENDPOINT_INDEX_KEY] === 'number' ? (config[ENDPOINT_INDEX_KEY] as number) : 0;
|
||||
const nextIndex = currentIndex + 1;
|
||||
|
||||
if (nextIndex >= API_BASE_URLS.length) {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
|
||||
config[ENDPOINT_INDEX_KEY] = nextIndex;
|
||||
config.baseURL = API_BASE_URLS[nextIndex];
|
||||
|
||||
return request(config);
|
||||
},
|
||||
);
|
||||
request.interceptors.response.use((response) => response);
|
||||
Reference in new issue
Block a user