Files
arjmand-pharmacy/lib/meshkee.ts
T

58 lines
1.5 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) : "";
}
export type BusinessMetaTag = {
name: string;
content: string;
};
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);
}
export function metaTagsToOther(tags: BusinessMetaTag[] | undefined) {
const other: Record<string, string> = {};
for (const tag of tags ?? []) {
const name = tag.name?.trim();
const content = tag.content?.trim();
if (name && content) other[name] = content;
}
return Object.keys(other).length > 0 ? other : undefined;
}