import { cache } from "./utils/cache"; import authConfig from "../../../config/auth"; import { responseOk } from "./utils/fetch"; export const fetchWithKey: typeof fetch = async (endpoint, options) => { const response = await fetch(authConfig.hitphone.CLIENT_SERVICE_URL + '/api/' + endpoint, { ...options, headers: { ...options?.headers, "X-Api-Key": authConfig.hitphone.CLIENT_SERVICE_API_KEY, }, }); return response; }; const CLIENT_EXISTS_CACHE_TTL = 10 * 1000 * 60; const clientExistsCache = cache(CLIENT_EXISTS_CACHE_TTL); export const clientExists = async (id: string): Promise => { const fetcher = async (id: string) => { const response = await fetchWithKey(`/tenants/${id}`, { method: "HEAD" }); await responseOk(response); return true; }; const exists = await clientExistsCache.get(id, () => fetcher(id)); return exists; };