UNPKG

anvisa-med

Version:

Biblioteca para acessar medicamentos na ANVISA

39 lines (38 loc) 1.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CacheInterceptor = void 0; const crypto_1 = require("crypto"); class CacheInterceptor { constructor() { this.cache = new Map(); this.requestInterceptor = (config) => { if (config.headers["local-cache"]) { let hash = this.generateKey(config); const response = this.cache.get(hash); if (response) { config.adapter = function (config) { response.config = config; return Promise.resolve(response); }; } } return config; }; this.responseInterceptor = (response) => { if (response.config.headers["local-cache"]) { let hash = this.generateKey(response.config); this.cache.set(hash, response); } return response; }; } generateKey(config) { const str = JSON.stringify({ url: config.url, method: config.method, data: config.data, }); return (0, crypto_1.createHash)("md5").update(str).digest("base64"); } } exports.CacheInterceptor = CacheInterceptor;