UNPKG

lighthouse-encryption-sdk-browser

Version:

Encryption SDK: Build your trustless, decentralized and fault resistance Application using distributed key shades with threshold cryptography

50 lines (49 loc) 2.19 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.API_NODE_HANDLER = exports.isEqual = exports.isCidReg = void 0; const config_1 = __importDefault(require("../config")); const isCidReg = (cid) => /Qm[1-9A-HJ-NP-Za-km-z]{44}|b[A-Za-z2-7]{58}|B[A-Z2-7]{58}|z[1-9A-HJ-NP-Za-km-z]{48}|F[0-9A-F]{50}/.test(cid); exports.isCidReg = isCidReg; const isEqual = (...objects) => objects.every((obj) => JSON.stringify(obj) === JSON.stringify(objects[0])); exports.isEqual = isEqual; async function API_NODE_HANDLER(endpoint, verb, auth_token = "", body = null, retryCount = 3) { const url = new URL(`${config_1.default.isDev ? config_1.default.lighthouseBLSNodeDev : config_1.default.lighthouseAuthNode}${endpoint}`); const init = { method: verb, headers: { "Content-type": "application/json", Authorization: auth_token ? `Bearer ${auth_token}` : "", }, }; if ((verb === "POST" || verb === "PUT" || verb === "DELETE") && body) { init.body = JSON.stringify(body); } for (let i = 0; i < retryCount; i++) { try { const response = await fetch(url.toString(), init); if (!response.ok) { if (response.status == 404) { throw new Error(JSON.stringify({ message: "fetch Error", statusCode: response.status, })); } const errorBody = await response.json(); throw new Error(JSON.stringify({ ...errorBody, statusCode: response.status })); } return response.json(); } catch (error) { if (!error?.message?.includes("fetch")) { throw error; } if (i === retryCount - 1) throw error; // Throw error on the last attempt await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait for 1 second } } } exports.API_NODE_HANDLER = API_NODE_HANDLER;