118 lines
3.1 KiB
TypeScript
118 lines
3.1 KiB
TypeScript
import type { BusinessListResponse, MarketplaceBusiness } from "@/types/business";
|
|
import { getAccessToken } from "@/lib/meshkee-api";
|
|
|
|
const API_BASE_URL =
|
|
process.env.NEXT_PUBLIC_MARKETPLACE_API_URL?.replace(/\/$/, "") ||
|
|
process.env.NEXT_PUBLIC_ARIYAWAY_API_URL?.replace(/\/$/, "") ||
|
|
"http://localhost:3001/api/v1";
|
|
|
|
export class MarketplaceApiError extends Error {
|
|
status: number;
|
|
|
|
constructor(message: string, status: number) {
|
|
super(message);
|
|
this.name = "MarketplaceApiError";
|
|
this.status = status;
|
|
}
|
|
}
|
|
|
|
async function parseResponse<T>(response: Response): Promise<T> {
|
|
const payload = await response.json().catch(() => null);
|
|
|
|
if (!response.ok) {
|
|
const message =
|
|
typeof payload?.message === "string"
|
|
? payload.message
|
|
: Array.isArray(payload?.message)
|
|
? payload.message.join(", ")
|
|
: `Request failed (${response.status})`;
|
|
throw new MarketplaceApiError(message, response.status);
|
|
}
|
|
|
|
return payload as T;
|
|
}
|
|
|
|
function authHeaders(): HeadersInit {
|
|
const headers: Record<string, string> = {
|
|
"Content-Type": "application/json",
|
|
};
|
|
const token = getAccessToken();
|
|
if (token) headers.Authorization = `Bearer ${token}`;
|
|
return headers;
|
|
}
|
|
|
|
export async function listBusinesses(params?: {
|
|
status?: string;
|
|
q?: string;
|
|
page?: number;
|
|
limit?: number;
|
|
}): Promise<BusinessListResponse> {
|
|
const search = new URLSearchParams();
|
|
search.set("status", params?.status ?? "all");
|
|
search.set("page", String(params?.page ?? 1));
|
|
search.set("limit", String(params?.limit ?? 50));
|
|
if (params?.q) search.set("q", params.q);
|
|
|
|
const response = await fetch(`${API_BASE_URL}/businesses?${search}`, {
|
|
cache: "no-store",
|
|
});
|
|
|
|
return parseResponse<BusinessListResponse>(response);
|
|
}
|
|
|
|
export async function getMarketplaceBusiness(
|
|
id: string,
|
|
): Promise<MarketplaceBusiness> {
|
|
const response = await fetch(`${API_BASE_URL}/businesses/by-id/${id}`, {
|
|
cache: "no-store",
|
|
});
|
|
return parseResponse<MarketplaceBusiness>(response);
|
|
}
|
|
|
|
export async function createMarketplaceBusiness(body: {
|
|
meshkeeBusinessId: string;
|
|
slug: string;
|
|
name?: string;
|
|
meshkeeCategoryIds?: string[];
|
|
status?: string;
|
|
}): Promise<MarketplaceBusiness> {
|
|
const response = await fetch(`${API_BASE_URL}/businesses`, {
|
|
method: "POST",
|
|
headers: authHeaders(),
|
|
body: JSON.stringify(body),
|
|
});
|
|
|
|
return parseResponse<MarketplaceBusiness>(response);
|
|
}
|
|
|
|
export async function updateMarketplaceBusiness(
|
|
id: string,
|
|
body: {
|
|
slug?: string;
|
|
name?: string;
|
|
meshkeeCategoryIds?: string[];
|
|
status?: string;
|
|
isFeatured?: boolean;
|
|
sortWeight?: number;
|
|
},
|
|
): Promise<MarketplaceBusiness> {
|
|
const response = await fetch(`${API_BASE_URL}/businesses/${id}`, {
|
|
method: "PATCH",
|
|
headers: authHeaders(),
|
|
body: JSON.stringify(body),
|
|
});
|
|
|
|
return parseResponse<MarketplaceBusiness>(response);
|
|
}
|
|
|
|
export async function deleteMarketplaceBusiness(id: string): Promise<void> {
|
|
const response = await fetch(`${API_BASE_URL}/businesses/${id}`, {
|
|
method: "DELETE",
|
|
headers: authHeaders(),
|
|
});
|
|
|
|
if (response.status === 204) return;
|
|
|
|
await parseResponse<unknown>(response);
|
|
}
|