Files
hedena_gold/apps/admin-panel/src/api/client.ts
T
s.esmaeili b80fa9bcfb Redesign product detail page and fix icon/cart reactivity bugs
- SingleProduct.vue: rebuild gallery/header/action bar as a self-scrolling
  layout with a sticky photo, solid-on-scroll header matching PageBar, and
  a full-screen image preview (blurred overlay, centered thumbnail strip)
- Add GallerySlider.vue: pointer-based swipe carousel isolated in an
  LTR subtree, fixing RTL/transform sign mismatch that caused slide jumps
- Fix Icon.vue: icon was read into a plain ref once at setup and never
  updated on prop change; switch to computed so icon swaps (e.g. cart
  qty stepper trash/minus toggle) actually re-render
- Fix quantity stepper DOM order in SingleProduct.vue/CartItem.vue so
  plus/minus render correctly under RTL flex
- prisma/assign-downloaded-images.js: seed real product photos (FEATURED/
  FEATURED_MOBILE/GALLERY/GALLERY_MOBILE) onto the 5 placeholder products
2026-09-18 16:22:31 +03:30

85 lines
2.8 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'];
}
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;