63 lines
1.9 KiB
TypeScript
63 lines
1.9 KiB
TypeScript
import { meshkeeApiBase, websiteDomain } from "@/lib/config";
|
|
|
|
export async function meshkeeGet<T>(path: string, fallback: T, revalidate = 60): Promise<T> {
|
|
try {
|
|
const response = await fetch(`${meshkeeApiBase}${path}`, {
|
|
next: { revalidate },
|
|
});
|
|
if (!response.ok) return fallback;
|
|
return (await response.json()) as T;
|
|
} catch {
|
|
return fallback;
|
|
}
|
|
}
|
|
|
|
export function tenantPath(suffix: string) {
|
|
return `/tenants/${encodeURIComponent(websiteDomain)}${suffix}`;
|
|
}
|
|
|
|
type Tenant = { id: number | string; nameFa?: string; name?: string; domain?: string };
|
|
|
|
export async function getTenant() {
|
|
return meshkeeGet<Tenant | null>(`/tenants/${encodeURIComponent(websiteDomain)}`, null);
|
|
}
|
|
|
|
export async function getBusinessId() {
|
|
const tenant = await getTenant();
|
|
return tenant?.id != null ? String(tenant.id) : "";
|
|
}
|
|
|
|
/** Full sanitized `<meta …>` HTML from business-info. */
|
|
export type BusinessMetaTag = {
|
|
html: string;
|
|
};
|
|
|
|
/** Parse a single `<meta …>` tag into React attribute props for the document head. */
|
|
export function parseMetaTagHtml(html: string): Record<string, string> | null {
|
|
const match = html.trim().match(/^<meta\b([^>]*)\/?\s*>$/i);
|
|
if (!match) return null;
|
|
const attrs: Record<string, string> = {};
|
|
const attrRe = /([:\w.-]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/g;
|
|
let found: RegExpExecArray | null;
|
|
while ((found = attrRe.exec(match[1]))) {
|
|
const key = found[1].toLowerCase();
|
|
if (key === "/") continue;
|
|
attrs[key] = found[2] ?? found[3] ?? found[4] ?? "";
|
|
}
|
|
return Object.keys(attrs).length > 0 ? attrs : null;
|
|
}
|
|
|
|
export type BusinessTrustBadge = {
|
|
kind: "enamad" | "samandehi";
|
|
embedHtml: string;
|
|
};
|
|
|
|
export type BusinessInfo = {
|
|
metaTags?: BusinessMetaTag[];
|
|
trustBadges?: BusinessTrustBadge[];
|
|
};
|
|
|
|
export async function getBusinessInfo() {
|
|
return meshkeeGet<BusinessInfo | null>(tenantPath("/website/business-info"), null);
|
|
}
|