change package managers
This commit is contained in:
7
src/shared/config/api/URLs.ts
Normal file
7
src/shared/config/api/URLs.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
const BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'https://food.felixits.uz/';
|
||||
|
||||
const ENDP_BANNER = '/banner/';
|
||||
const ENDP_CATEGORY = '/category/';
|
||||
const ENDP_PRODUCT = '/products/';
|
||||
|
||||
export { BASE_URL, ENDP_BANNER, ENDP_CATEGORY, ENDP_PRODUCT };
|
||||
35
src/shared/config/api/httpClient.ts
Normal file
35
src/shared/config/api/httpClient.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import getLocaleCS from '@/shared/lib/getLocaleCS';
|
||||
import axios from 'axios';
|
||||
import { getLocale } from 'next-intl/server';
|
||||
import { LanguageRoutes } from '../i18n/types';
|
||||
import { BASE_URL } from './URLs';
|
||||
|
||||
const httpClient = axios.create({
|
||||
baseURL: BASE_URL,
|
||||
timeout: 10000,
|
||||
});
|
||||
|
||||
httpClient.interceptors.request.use(
|
||||
async (config) => {
|
||||
let language = LanguageRoutes.RU;
|
||||
|
||||
try {
|
||||
if (typeof window === 'undefined') {
|
||||
// Serverda ishlayapti
|
||||
language = (await getLocale()) as LanguageRoutes;
|
||||
} else {
|
||||
// Clientda ishlayapti
|
||||
language = getLocaleCS() || LanguageRoutes.RU;
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('Locale olishda xato:', e);
|
||||
}
|
||||
|
||||
config.headers['Accept-Language'] = language;
|
||||
|
||||
return config;
|
||||
},
|
||||
(error) => Promise.reject(error),
|
||||
);
|
||||
|
||||
export default httpClient;
|
||||
46
src/shared/config/api/sendToBot.ts
Normal file
46
src/shared/config/api/sendToBot.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const body = await req.json();
|
||||
const { name, phone } = body;
|
||||
|
||||
const BOT_TOKEN = process.env.BOT_TOKEN;
|
||||
const CHAT_IDS = process.env.BOT_CHAT_IDS?.split(',') || [];
|
||||
|
||||
if (!BOT_TOKEN || CHAT_IDS.length === 0) {
|
||||
return NextResponse.json(
|
||||
{ success: false, message: 'Server not configured properly' },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
|
||||
const text = `
|
||||
📩 *Новая заявка на обратный звонок*
|
||||
|
||||
👤 *Имя:* _${name}_
|
||||
📞 *Телефон:* _${phone}_
|
||||
`;
|
||||
|
||||
const sendMessage = async (chatId: string) => {
|
||||
return fetch(`https://api.telegram.org/bot${BOT_TOKEN}/sendMessage`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ chat_id: chatId, text, parse_mode: 'Markdown' }),
|
||||
});
|
||||
};
|
||||
|
||||
const results = await Promise.allSettled(
|
||||
CHAT_IDS.map((id) => sendMessage(id)),
|
||||
);
|
||||
|
||||
const hasSuccess = results.some((r) => r.status === 'fulfilled');
|
||||
|
||||
if (!hasSuccess) {
|
||||
return NextResponse.json(
|
||||
{ success: false, message: 'Telegram error' },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
}
|
||||
101
src/shared/config/api/testApi.ts
Normal file
101
src/shared/config/api/testApi.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
import {
|
||||
ReqWithPagination,
|
||||
ResWithPagination,
|
||||
ResWithPaginationOneItem,
|
||||
} from './types';
|
||||
import { AxiosResponse } from 'axios';
|
||||
import {
|
||||
IBanner,
|
||||
ICategory,
|
||||
IProduct,
|
||||
IProductDetail,
|
||||
} from '@/shared/types/testApi';
|
||||
import httpClient from './httpClient';
|
||||
import { ENDP_BANNER, ENDP_CATEGORY, ENDP_PRODUCT } from './URLs';
|
||||
|
||||
const getBanner = async (
|
||||
pagination?: ReqWithPagination,
|
||||
): Promise<ResWithPagination<IBanner>['data']> => {
|
||||
const response: AxiosResponse<ResWithPagination<IBanner>> =
|
||||
await httpClient.get(ENDP_BANNER, { params: pagination });
|
||||
return response.data.data;
|
||||
};
|
||||
|
||||
const getCategory = async (
|
||||
pagination?: ReqWithPagination,
|
||||
): Promise<ResWithPagination<ICategory>['data']> => {
|
||||
const response: AxiosResponse<ResWithPagination<ICategory>> =
|
||||
await httpClient.get(ENDP_CATEGORY, { params: pagination });
|
||||
return response.data.data;
|
||||
};
|
||||
|
||||
const getAllProduct = async (
|
||||
pagination?: ReqWithPagination,
|
||||
): Promise<ResWithPagination<IProduct>['data']> => {
|
||||
const firstResponse: AxiosResponse<ResWithPagination<IProduct>> =
|
||||
await httpClient.get(ENDP_PRODUCT, { params: { ...pagination, page: 1 } });
|
||||
const pageCount = firstResponse.data.data.total_pages;
|
||||
const allResults = [...firstResponse.data.data.results];
|
||||
const requests = [];
|
||||
|
||||
for (let i = 2; i <= pageCount; i++) {
|
||||
requests.push(
|
||||
httpClient.get<ResWithPagination<IProduct>>(ENDP_PRODUCT, {
|
||||
params: { ...pagination, page: i },
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
const responses = await Promise.allSettled(requests);
|
||||
|
||||
for (const res of responses) {
|
||||
if (res.status === 'fulfilled') {
|
||||
allResults.push(...res.value.data.data.results);
|
||||
} else {
|
||||
console.error('Error:', res.reason);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
...firstResponse.data.data,
|
||||
results: allResults,
|
||||
};
|
||||
};
|
||||
|
||||
const getProduct = async (
|
||||
pagination?: ReqWithPagination,
|
||||
): Promise<ResWithPagination<IProduct>['data']> => {
|
||||
const response: AxiosResponse<ResWithPagination<IProduct>> =
|
||||
await httpClient.get(ENDP_PRODUCT, {
|
||||
params: pagination,
|
||||
});
|
||||
return response.data.data;
|
||||
};
|
||||
|
||||
const getBestProduct = async (
|
||||
pagination?: ReqWithPagination,
|
||||
): Promise<ResWithPagination<IProduct>['data']> => {
|
||||
const response: AxiosResponse<ResWithPagination<IProduct>> =
|
||||
await httpClient.get(ENDP_PRODUCT, {
|
||||
params: pagination,
|
||||
});
|
||||
return response.data.data;
|
||||
};
|
||||
|
||||
const getOneProduct = async (
|
||||
id?: number,
|
||||
pagination?: ReqWithPagination,
|
||||
): Promise<IProductDetail> => {
|
||||
const response: AxiosResponse<ResWithPaginationOneItem<IProductDetail>> =
|
||||
await httpClient.get(`${ENDP_PRODUCT}${id}`, { params: pagination });
|
||||
return response.data.data;
|
||||
};
|
||||
|
||||
export {
|
||||
getBanner,
|
||||
getCategory,
|
||||
getProduct,
|
||||
getAllProduct,
|
||||
getOneProduct,
|
||||
getBestProduct,
|
||||
};
|
||||
28
src/shared/config/api/types.ts
Normal file
28
src/shared/config/api/types.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
export interface ResWithPagination<T> {
|
||||
data: {
|
||||
links: Links;
|
||||
total_items: number;
|
||||
total_pages: number;
|
||||
page_size: number;
|
||||
current_page: number;
|
||||
results: T[];
|
||||
};
|
||||
}
|
||||
|
||||
export interface ResWithPaginationOneItem<T> {
|
||||
data: T;
|
||||
}
|
||||
|
||||
interface Links {
|
||||
next: number | null;
|
||||
previous: number | null;
|
||||
}
|
||||
|
||||
export interface ReqWithPagination {
|
||||
_start?: number;
|
||||
_limit?: number;
|
||||
page?: number;
|
||||
category?: number | undefined;
|
||||
popular?: boolean;
|
||||
search?: string;
|
||||
}
|
||||
Reference in New Issue
Block a user