import cFetch from "cross-fetch"; import { respostaComuns } from "p-respostas"; import { cacheAuDrive } from "./plugins/node-cache"; import { uuidV3 } from "./plugins/uuid"; export const tx_vinculos__listar = "vinculos__listar"; export const listarVinculos = async ({ token, url_api_autenticacao, desativarCache, }) => { const chaveCache = uuidV3({ token, url_api_autenticacao }); // Buscar promeiro no cache if (!desativarCache) { const valorCache = cacheAuDrive.get(chaveCache); if (valorCache) return valorCache; } const res = (async () => { const url = `${url_api_autenticacao}/api/${tx_vinculos__listar}`; return cFetch(url, { headers: { token, "Content-Type": "application/json" }, body: "{}", method: "post", }) .then(async (a) => { const texto = await a.text(); try { const res = JSON.parse(texto); return res; } catch (error) { return respostaComuns.erro(`Erro ao listar cidades: ${error.message}`, [texto, error]); } }) .catch((error) => respostaComuns.erro(`Erro ao listar cidades: ${error.message}`, [ error, ])); })(); cacheAuDrive.set(chaveCache, res.then((a) => { try { if (a.eErro) { cacheAuDrive.del(chaveCache); } } catch { } return a; }), 10); return res; };