connetcted to backend: form request
This commit is contained in:
35
request/api.ts
Normal file
35
request/api.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import axios, { InternalAxiosRequestConfig } from "axios";
|
||||
import { getRouteLang } from "./getLang";
|
||||
|
||||
const baseUrl = process.env.NEXT_PUBLIC_BASE_URL;
|
||||
|
||||
const httpClient = axios.create({
|
||||
baseURL: baseUrl,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
}
|
||||
});
|
||||
|
||||
// Request interceptor
|
||||
httpClient.interceptors.request.use(
|
||||
(config: InternalAxiosRequestConfig) => {
|
||||
const language = getRouteLang();
|
||||
config.headers["Accept-Language"] = language;
|
||||
return config;
|
||||
},
|
||||
(error) => {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
// Response interceptor (xatoliklarni boshqarish uchun)
|
||||
httpClient.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
// Xatolikni formatlash
|
||||
const message = error.response?.data?.message || error.message;
|
||||
return Promise.reject(message);
|
||||
}
|
||||
);
|
||||
|
||||
export default httpClient;
|
||||
18
request/getLang.ts
Normal file
18
request/getLang.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
// utils/getRouteLang.ts
|
||||
import { locales, defaultLocale } from "@/i18n/config";
|
||||
|
||||
export function getRouteLang(): string {
|
||||
if (typeof window === "undefined") return defaultLocale;
|
||||
|
||||
// 1)Fall back to the first non-empty pathname segment
|
||||
const rawSegments = window.location.pathname.split("/"); // e.g. ['', 'uz', 'path', ...]
|
||||
const segments = rawSegments.filter(Boolean); // removes empty strings
|
||||
if (segments.length > 0) {
|
||||
const candidate = segments[0]; // first segment after root
|
||||
if (locales.includes(candidate as (typeof locales)[number]))
|
||||
return candidate;
|
||||
}
|
||||
|
||||
// 2) final fallback
|
||||
return defaultLocale;
|
||||
}
|
||||
23
request/links.ts
Normal file
23
request/links.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
export const endPoints = {
|
||||
category: {
|
||||
all: "category/",
|
||||
},
|
||||
subCategory: {
|
||||
byId: (id: number) => `subCategory/?category=${id}`,
|
||||
},
|
||||
product: {
|
||||
byCategory: (categoryId: number) => `product/?category=${categoryId}`,
|
||||
bySubCategory: (subCategoryId: number) =>
|
||||
`product/?subCategory=${subCategoryId}`,
|
||||
detail: (id: number) => `product/${id}/`,
|
||||
},
|
||||
faq: "faq/",
|
||||
gallery: "gallery/",
|
||||
contact: "contact/",
|
||||
statistics: "statistics/",
|
||||
post: {
|
||||
sendNumber: "callBack/",
|
||||
productContact: "customer/",
|
||||
contact: "question/",
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user