Sending X-Debug: 1 makes the five wholesale list endpoints return a counts-only `debug` block next to the rows: the WHERE that actually ran, how many customers the scope matches before the grid's own filters, how many customers exist per kind/active/archived/step and how many are an expert's trading account. The panel sends the header and console-logs the block when localStorage.debug === '1'. Off by default; DIAG_LOG=false disables it server-side. No names or mobiles. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
99 lines
3.4 KiB
TypeScript
99 lines
3.4 KiB
TypeScript
// 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'];
|
|
}
|
|
// Opt-in troubleshooting: run `localStorage.setItem('debug', '1')` in the
|
|
// console and reload; list endpoints then answer with a `debug` block.
|
|
if (localStorage.getItem('debug') === '1') {
|
|
config.headers['X-Debug'] = '1';
|
|
}
|
|
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) => {
|
|
const debug = response.data?.debug;
|
|
if (debug && localStorage.getItem('debug') === '1') {
|
|
console.groupCollapsed(`[DEBUG] ${response.config.url}`);
|
|
console.log(debug);
|
|
if (Array.isArray(debug.by_kind)) console.table(debug.by_kind);
|
|
console.groupEnd();
|
|
}
|
|
return 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;
|