UNPKG

ic0

Version:

An easy-to-use JavaScript API for the Internet Computer.

46 lines 1.74 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.deferredReplica = exports.replica = void 0; const agent_1 = require("@dfinity/agent"); const cross_fetch_1 = __importDefault(require("cross-fetch")); const agentCanister_1 = require("./canister/agentCanister"); // Define an IC replica from the given hostname (e.g. `https://icp-api.io`) const replica = (host, options = {}) => { let agent; if (!host) { agent = agent_1.HttpAgent.createSync({ fetch: cross_fetch_1.default }); } else if (typeof host === 'string') { agent = agent_1.HttpAgent.createSync({ host, fetch: cross_fetch_1.default }); } else if (host instanceof agent_1.HttpAgent) { agent = host; } else { throw new Error(`Invalid host (expected \`string\` or \`HttpAgent\`, received \`${typeof host}\`)`); } // Fetch root key for local replica if (options.local) { agent.fetchRootKey().catch((err) => { console.warn('Unable to fetch root key (check to ensure that your local replica is running)'); console.error(err); }); } return (id) => (0, agentCanister_1.agentCanister)(agent, id); }; exports.replica = replica; // Defer creating the agent for built-in replica values const deferredReplica = (...args) => { let deferred; return (...replicaArgs) => { if (!deferred) { deferred = (0, exports.replica)(...args); } return deferred(...replicaArgs); }; }; exports.deferredReplica = deferredReplica; //# sourceMappingURL=replica.js.map