From ff2e96be155f2eaccfb463f14bcd8b29d60731b8 Mon Sep 17 00:00:00 2001 From: dappsar Date: Sat, 20 Jul 2024 09:31:26 -0300 Subject: [PATCH] [fix] :bug: add middleware to handle alpha data (#375) --- src/services/middleware.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/services/middleware.js diff --git a/src/services/middleware.js b/src/services/middleware.js new file mode 100644 index 00000000..03a57a76 --- /dev/null +++ b/src/services/middleware.js @@ -0,0 +1,27 @@ +const cache = new Map() + +export const fetchWithRetry = async (key, requestFunc, retries = 5, delay = 2000) => { + // Verifica si el resultado ya está en caché + if (cache.has(key)) { + console.log('get from cache: ', key) + return cache.get(key) + } + + console.log('NO from cache: ', key) + for (let i = 0; i < retries; i++) { + try { + const result = await requestFunc() + // Guarda el resultado en caché + cache.set(key, result) + console.log('save in cache:', key, result) + return result + } catch (error) { + console.log('retry: ', key, i + 1) + if (error.code === -32005 && i < retries - 1) { + await new Promise((resolve) => setTimeout(resolve, delay)) + } else { + throw error + } + } + } +}