UNPKG

nimiq-rpc-client-ts

Version:
45 lines (43 loc) 1.32 kB
let _baseUrl; let _auth; const urlEnvName = "ALBATROSS_RPC_NODE_URL"; const usernameEnvName = "ALBATROSS_RPC_NODE_USERNAME"; const passwordEnvName = "ALBATROSS_RPC_NODE_PASSWORD"; function inBrowser() { return typeof window !== "undefined" && typeof window.document !== "undefined"; } async function getEnvVar(name) { if (inBrowser()) { return void 0; } try { const { env } = await import('node:process'); return env[name]; } catch { return void 0; } } function initRpcClient(config) { _baseUrl = typeof config.url === "string" ? new URL(config.url) : config.url; _auth = config.auth; } async function __getBaseUrl() { if (_baseUrl) return _baseUrl; const envUrl = await getEnvVar(urlEnvName); if (envUrl) return new URL(envUrl); throw new Error( inBrowser() ? `RPC client not initialized: call initRpcClient() with the API URL in browser environments` : `RPC client not initialized: either call initRpcClient() or set ${urlEnvName} environment variable` ); } async function __getAuth() { if (_auth) return _auth; const username = await getEnvVar(usernameEnvName); const password = await getEnvVar(passwordEnvName); if (username && password) return { username, password }; return void 0; } export { __getAuth, __getBaseUrl, inBrowser, initRpcClient };