30 lines
1.4 KiB
TypeScript
30 lines
1.4 KiB
TypeScript
import { cache } from 'react';
|
|
export const DOMAIN = 'sgemed.com';
|
|
export const API_BASE = 'https://api.meshkee.com/api/v1';
|
|
export const CUSTOMER_URL = 'https://customer.sgemed.com';
|
|
export const CUSTOMER_PROFILE_URL = `${CUSTOMER_URL}/profile`;
|
|
export const WEBSITE_MANAGEMENT_URL = 'https://business.sgemed.com/website';
|
|
export class ApiError extends Error {
|
|
constructor(public status: number) { super(`Meshkee request failed (${status})`); }
|
|
}
|
|
export async function api<T>(path: string, init?: RequestInit): Promise<T> {
|
|
const response = await fetch(`${API_BASE}${path}`, { ...init, cache: 'no-store', signal: AbortSignal.timeout(12000) });
|
|
if (!response.ok) throw new ApiError(response.status);
|
|
return response.json() as Promise<T>;
|
|
}
|
|
export const getTenant = cache(() => api<{id: number | string; nameFa: string}>(`/tenants/${DOMAIN}`));
|
|
export async function tenantApi<T>(path: string, init?: RequestInit): Promise<T> {
|
|
await getTenant();
|
|
return api<T>(`/tenants/${DOMAIN}${path}`, init);
|
|
}
|
|
export function safeUrl(value: unknown): string | undefined {
|
|
if (typeof value !== 'string' || !value) return;
|
|
try { const url = new URL(value); if (url.protocol === 'https:') return url.href; } catch {}
|
|
}
|
|
export async function getFavicon() {
|
|
try {
|
|
const data = await tenantApi<{faviconUrl: string | null}>('/website/favicon');
|
|
return safeUrl(data.faviconUrl) || '/favicon.svg';
|
|
} catch { return '/favicon.svg'; }
|
|
}
|