Files
modstagram-next/src/api/fetchPosts.ts
payacom a1d7db5e7e lang
2026-07-23 15:58:15 +03:30

81 lines
2.1 KiB
TypeScript

import { getApiBaseUrl } from "@/components/main/BaseUrl";
import { normalizeUserLevel } from "@/lib/userLevel";
export async function fetchPosts(
page: number,
limit: number,
filters: {
expertise?: string;
province?: string;
city?: string;
userLevel?: string;
rateFilter?: string;
_id?: string;
type?: "image" | "video";
exploreFilter?: string;
subExpertise?: string;
lat?: string;
lng?: string;
feedMode?: "grid" | "reels";
seedPostId?: string;
sort?: "latest";
reelsTab?: "for_you" | "following" | "saved" | "near_me";
lat?: string;
lng?: string;
heightMin?: string;
heightMax?: string;
weightMin?: string;
weightMax?: string;
sizeMin?: string;
sizeMax?: string;
hair_color?: string;
eye_color?: string;
q?: string;
hashtag?: string;
},
token: string
) {
const normalizedFilters = { ...filters };
if (normalizedFilters.userLevel) {
normalizedFilters.userLevel = normalizeUserLevel(normalizedFilters.userLevel);
}
const validFilters = Object.entries(normalizedFilters || {})
.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,
}).toString();
const apiUrl = `${getApiBaseUrl()}/users/web?${queryParams}`;
const response = await fetch(apiUrl, {
cache: "no-store",
headers: {
Authorization: token ? `Bearer ${token}` : "",
},
});
if (!response.ok) {
throw new Error(`Network response was not ok: ${response.status}`);
}
return response.json() as Promise<{
posts: unknown[];
totalPages?: number;
totalItems?: number;
feedMeta?: {
isColdStart?: boolean;
emptyFollowing?: boolean;
emptySaved?: boolean;
requiresAuth?: boolean;
suggestedUsers?: unknown[];
};
}>;
}