projeto-hit/backend/src/services/External/HitphoneServices/ClientExists.ts

32 lines
909 B
TypeScript
Raw Normal View History

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<boolean>(CLIENT_EXISTS_CACHE_TTL);
export const clientExists = async (id: string): Promise<boolean> => {
const fetcher = async (id: string) => {
const response = await fetchWithKey(`/clients/${id}`, { method: "HEAD" });
await responseOk(response);
return true;
};
const exists = await clientExistsCache.get(id, () => fetcher(id));
return exists;
};