41 lines
1.4 KiB
JavaScript
41 lines
1.4 KiB
JavaScript
// src/services/clinicApi.js
|
|
export async function getClinicDoctors(slug, params = {}) {
|
|
try {
|
|
const baseUrl = process.env.NEXT_PUBLIC_API_URL;
|
|
if (!baseUrl) throw new Error("❌ NEXT_PUBLIC_API_URL is not defined");
|
|
if (!slug) throw new Error("❌ slug is missing");
|
|
|
|
const query = new URLSearchParams(params).toString();
|
|
const finalUrl = `${baseUrl}/api/v1/clinic/doctor-list/${slug}?${query}`;
|
|
|
|
const response = await fetch(finalUrl, { method: "GET" });
|
|
|
|
if (!response.ok) {
|
|
const text = await response.text();
|
|
throw new Error(`HTTP ${response.status}: ${text}`);
|
|
}
|
|
|
|
const json = await response.json();
|
|
|
|
// پاسخ دو لایه nested است: آرایهی پزشکان در json.data.data و صفحهبندی در json.data.meta
|
|
const doctors = json?.data?.data || [];
|
|
const meta = json?.data?.meta;
|
|
|
|
// ✅ خروجی همیشه ساختار ثابت دارد
|
|
return {
|
|
data: doctors,
|
|
page: meta
|
|
? { total_pages: meta.totalPages, current: meta.currentPage }
|
|
: { total_pages: 1, current: 1 },
|
|
};
|
|
} catch (error) {
|
|
console.error("❌ خطا در دریافت دکترهای کلینیک:", error);
|
|
|
|
// ✅ ساختار خروجی در حالت خطا هم مثل حالت عادی
|
|
return {
|
|
data: [],
|
|
page: { total_pages: 1, current: 1 },
|
|
};
|
|
}
|
|
}
|