melhorias de caches

This commit is contained in:
Luiz Silva 2025-07-16 11:20:53 -03:00
parent 29ae7400cf
commit 00e9c4d698
18 changed files with 210 additions and 66 deletions

View file

@ -10,7 +10,9 @@ export type tipo_retorno_vinculo_listas = {
_inquilino_nome: string;
_inquilino_base_url: string;
};
export declare const listarVinculos: ({ token, url_api_autenticacao, }: {
export declare const listarVinculos: ({ token, url_api_autenticacao, desativarCache, }: {
url_api_autenticacao: string;
token: string;
/** por padrão será 10 segundos */
desativarCache?: boolean;
}) => Promise<tipoResposta<tipo_retorno_vinculo_listas[]>>;

View file

@ -6,27 +6,47 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.listarVinculos = exports.tx_vinculos__listar = void 0;
const cross_fetch_1 = __importDefault(require("cross-fetch"));
const p_respostas_1 = require("p-respostas");
const node_cache_1 = require("./plugins/node-cache");
const uuid_1 = require("./plugins/uuid");
exports.tx_vinculos__listar = "vinculos__listar";
const listarVinculos = async ({ token, url_api_autenticacao, }) => {
const url = `${url_api_autenticacao}/api/${exports.tx_vinculos__listar}`;
return (0, cross_fetch_1.default)(url, {
headers: { token, "Content-Type": "application/json" },
body: "{}",
method: "post",
})
.then(async (a) => {
const texto = await a.text();
const listarVinculos = async ({ token, url_api_autenticacao, desativarCache, }) => {
const chaveCache = (0, uuid_1.uuidV3)({ token, url_api_autenticacao });
// Buscar promeiro no cache
if (!desativarCache) {
const valorCache = node_cache_1.cacheAuDrive.get(chaveCache);
if (valorCache)
return valorCache;
}
const res = (async () => {
const url = `${url_api_autenticacao}/api/${exports.tx_vinculos__listar}`;
return (0, cross_fetch_1.default)(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 p_respostas_1.respostaComuns.erro(`Erro ao listar cidades: ${error.message}`, [texto, error]);
}
})
.catch((error) => p_respostas_1.respostaComuns.erro(`Erro ao listar cidades: ${error.message}`, [
error,
]));
})();
node_cache_1.cacheAuDrive.set(chaveCache, res.then((a) => {
try {
const res = JSON.parse(texto);
return res;
if (a.eErro) {
node_cache_1.cacheAuDrive.del(chaveCache);
}
}
catch (error) {
return p_respostas_1.respostaComuns.erro(`Erro ao listar cidades: ${error.message}`, [
texto,
error,
]);
}
})
.catch((error) => p_respostas_1.respostaComuns.erro(`Erro ao listar cidades: ${error.message}`, [error]));
catch { }
return a;
}), 10);
return res;
};
exports.listarVinculos = listarVinculos;

2
dist-require/plugins/node-cache.d.ts vendored Normal file
View file

@ -0,0 +1,2 @@
import NodeCache from "node-cache";
export declare const cacheAuDrive: NodeCache;

View file

@ -0,0 +1,8 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.cacheAuDrive = void 0;
const node_cache_1 = __importDefault(require("node-cache"));
exports.cacheAuDrive = new node_cache_1.default();

3
dist-require/plugins/uuid.d.ts vendored Normal file
View file

@ -0,0 +1,3 @@
import { v4 } from "uuid";
export declare const uuidV3: (qualquerCoisa: any) => string;
export declare const uuidV4: typeof v4;

View file

@ -0,0 +1,11 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.uuidV4 = exports.uuidV3 = void 0;
const uuid_1 = require("uuid");
const uuidV3 = (qualquerCoisa) => (0, uuid_1.v3)(typeof qualquerCoisa == "string"
? qualquerCoisa
: typeof qualquerCoisa == "number"
? String(qualquerCoisa)
: JSON.stringify(qualquerCoisa), uuid_1.NIL);
exports.uuidV3 = uuidV3;
exports.uuidV4 = uuid_1.v4;