Files
modstagram-next/src/api/fetchBillboards.ts
2026-07-09 15:26:46 +03:30

58 lines
1.6 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-unused-vars */
import { getApiBaseUrl } from "@/components/main/BaseUrl";
export async function fetchBillboards(
page: number,
limit: number,
filters: {
province?: string;
city?: string;
category?: string;
sort?: string;
search?: string;
},
token: string
) {
const validFilters = Object.entries(filters || {})
.filter(([_, value]) => value !== undefined && value !== "")
.reduce((acc, [key, value]) => {
acc[key] = value;
return acc;
}, {} as Record<string, string>);
const queryParams = new URLSearchParams({
page: page.toString(),
limit: limit.toString(),
...validFilters,
timestamp: Date.now().toString(), // اضافه کردن timestamp برای جلوگیری از کش
}).toString();
const apiUrl = `${getApiBaseUrl()}/advertising/web?${queryParams}`;
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 90_000);
let response: Response;
try {
response = await fetch(apiUrl, {
cache: "no-store",
signal: controller.signal,
headers: {
Authorization: token ? `Bearer ${token}` : "",
},
});
} catch (error) {
console.error("[fetchBillboards] fetch failed:", apiUrl, error);
return { advertisings: [], totalItems: 0, totalPages: 0 };
} finally {
clearTimeout(timeoutId);
}
if (!response.ok) {
console.error("[fetchBillboards] bad response:", response.status, apiUrl);
return { advertisings: [], totalItems: 0, totalPages: 0 };
}
return response.json();
}