Initial commit - modstagram-next
This commit is contained in:
53
src/api/fetchBillboards.ts
Normal file
53
src/api/fetchBillboards.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||
import { BASE_URL } 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 = `${BASE_URL}/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}` : "",
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
clearTimeout(timeoutId);
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error("Network response was not ok");
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
49
src/api/fetchPostById.ts
Normal file
49
src/api/fetchPostById.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { BASE_URL } from "@/components/main/BaseUrl";
|
||||
import { Post } from "@/types/types";
|
||||
|
||||
/** Fetch a single post by id (tries dedicated endpoint, then list fallback) */
|
||||
export async function fetchPostById(
|
||||
id: string,
|
||||
token?: string
|
||||
): Promise<Post | null> {
|
||||
const headers: HeadersInit = token
|
||||
? { Authorization: `Bearer ${token}` }
|
||||
: {};
|
||||
|
||||
const tryUrls = [
|
||||
`${BASE_URL}/posts/${id}`,
|
||||
`${BASE_URL}/posts/web/${id}`,
|
||||
`${BASE_URL}/posts/get/${id}`,
|
||||
];
|
||||
|
||||
for (const url of tryUrls) {
|
||||
try {
|
||||
const res = await fetch(url, {
|
||||
cache: "no-store",
|
||||
headers,
|
||||
});
|
||||
if (!res.ok) continue;
|
||||
const data = await res.json();
|
||||
const post = data?.post ?? data?.data ?? data;
|
||||
if (post?._id) return post as Post;
|
||||
} catch {
|
||||
/* try next */
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch(
|
||||
`${BASE_URL}/users/web?page=1&limit=50&_id=${id}`,
|
||||
{ cache: "no-store", headers }
|
||||
);
|
||||
if (res.ok) {
|
||||
const data = await res.json();
|
||||
const found = (data?.posts as Post[])?.find((p) => p._id === id);
|
||||
if (found) return found;
|
||||
}
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
44
src/api/fetchPosts.ts
Normal file
44
src/api/fetchPosts.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { BASE_URL } from "@/components/main/BaseUrl";
|
||||
|
||||
export async function fetchPosts(
|
||||
page: number,
|
||||
limit: number,
|
||||
filters: {
|
||||
expertise?: string;
|
||||
province?: string;
|
||||
city?: string;
|
||||
userLevel?: string;
|
||||
rateFilter?: string;
|
||||
_id?: string;
|
||||
type?: 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,
|
||||
}).toString();
|
||||
|
||||
const apiUrl = `${BASE_URL}/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();
|
||||
}
|
||||
45
src/api/fetchProjects.ts
Normal file
45
src/api/fetchProjects.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||
import { BASE_URL } from "@/components/main/BaseUrl";
|
||||
|
||||
export async function fetchProjects(
|
||||
page: number,
|
||||
limit: number,
|
||||
filters: {
|
||||
expertise?: string;
|
||||
most_requests?: string;
|
||||
most_price?: string;
|
||||
age?: string;
|
||||
gender?: 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 = `${BASE_URL}/projects?${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");
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
42
src/api/fetchSearch.ts
Normal file
42
src/api/fetchSearch.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||
import { BASE_URL } from "@/components/main/BaseUrl";
|
||||
|
||||
export async function fetchSearch(
|
||||
page: number,
|
||||
limit: number,
|
||||
filters: {
|
||||
type?: 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 = `${BASE_URL}/search-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');
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
Reference in New Issue
Block a user