connetcted to backend: form request

This commit is contained in:
nabijonovdavronbek619@gmail.com
2026-02-05 11:02:57 +05:00
parent ca3e28779e
commit 3c862ea104
13 changed files with 458 additions and 90 deletions

35
request/api.ts Normal file
View 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
View 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
View 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/",
},
};