Initial commit: Hedena Gold monorepo.
First commit of the Hedena Gold product (separate from gold-bank): branding, package names, and deploy defaults aligned to the new brand; legacy Henza trees stay local-only via .gitignore.
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
// Ported from legacy henza_admin/src/axios.js.
|
||||
//
|
||||
// Legacy pointed at the live PHP backend:
|
||||
// baseURL = 'https://henzagold.com/oapi/admin/'
|
||||
// The new backend is the local NestJS API. Legacy endpoint names are translated
|
||||
// to clean REST paths by ./endpoints.ts — see the mapping-layer note there.
|
||||
import axios from 'axios';
|
||||
import { tokenName } from '../utils';
|
||||
import { mapEndpoint } from './endpoints';
|
||||
|
||||
export const baseURL = import.meta.env.VITE_API_BASE_URL ?? '/api/';
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
Accept: 'application/json',
|
||||
};
|
||||
|
||||
const initialToken = localStorage.getItem(tokenName);
|
||||
if (initialToken) {
|
||||
headers['Authorization'] = 'Bearer ' + initialToken;
|
||||
}
|
||||
|
||||
const ax = axios.create({
|
||||
baseURL,
|
||||
headers,
|
||||
});
|
||||
|
||||
/**
|
||||
* Legacy re-set the auth header on every request so a token acquired after boot
|
||||
* was picked up without recreating the instance. Kept — plus the endpoint
|
||||
* rewrite, and without legacy's habit of sending a literal "Bearer null" when
|
||||
* no token existed.
|
||||
*/
|
||||
ax.interceptors.request.use(
|
||||
(config) => {
|
||||
if (config.url) {
|
||||
config.url = mapEndpoint(config.url);
|
||||
}
|
||||
const token = localStorage.getItem(tokenName);
|
||||
if (token) {
|
||||
config.headers['Authorization'] = 'Bearer ' + token;
|
||||
} else {
|
||||
delete config.headers['Authorization'];
|
||||
}
|
||||
// The instance-level default Content-Type is 'application/json', and axios
|
||||
// only auto-sets multipart/form-data + boundary when no Content-Type is
|
||||
// already present — an existing default is NOT replaced. Posting a
|
||||
// FormData body (file uploads) with the JSON default left in place sends
|
||||
// the raw FormData object as a JSON-labelled body, so multer/NestJS's
|
||||
// FileInterceptor sees no file at all and 400s. Let axios/the browser set
|
||||
// the correct multipart boundary for FormData bodies instead.
|
||||
if (config.data instanceof FormData) {
|
||||
delete config.headers['Content-Type'];
|
||||
}
|
||||
return config;
|
||||
},
|
||||
(error) => Promise.reject(error),
|
||||
);
|
||||
|
||||
/**
|
||||
* NOT IN LEGACY: legacy had no response interceptor at all, so an expired token
|
||||
* produced a silent failure on every subsequent call with no path back to login.
|
||||
* A 401 now clears the stale session and redirects once.
|
||||
*
|
||||
* The redirect is a hard location assignment rather than a router push, to avoid
|
||||
* importing the router here (circular: router -> stores -> client -> router).
|
||||
*/
|
||||
let redirecting = false;
|
||||
ax.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
if (error?.response?.status === 401 && !redirecting) {
|
||||
const onLoginPage = window.location.pathname.endsWith('/auth/login');
|
||||
if (!onLoginPage) {
|
||||
redirecting = true;
|
||||
localStorage.removeItem(tokenName);
|
||||
window.location.assign(`${import.meta.env.BASE_URL}auth/login`);
|
||||
}
|
||||
}
|
||||
return Promise.reject(error);
|
||||
},
|
||||
);
|
||||
|
||||
export default ax;
|
||||
Reference in New Issue
Block a user