UNPKG

@funded-labs/dab-js

Version:
46 lines (45 loc) 2.37 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const agent_1 = require("@dfinity/agent"); const principal_1 = require("@dfinity/principal"); const constants_1 = require("../constants"); const registry_standard_did_1 = __importDefault(require("../idls/dab_registries/registry_standard.did")); const registry_1 = require("../utils/registry"); const DEFAULT_AGENT = agent_1.HttpAgent.createSync({ fetch, host: constants_1.IC_HOST }); class Registry { constructor(canisterId, agent = DEFAULT_AGENT) { this.name = () => __awaiter(this, void 0, void 0, function* () { return this.actor.name(); }); this.add = (metadata) => __awaiter(this, void 0, void 0, function* () { return this.actor.add(metadata !== null && metadata !== void 0 ? metadata : []); }); this.get = (principalId) => __awaiter(this, void 0, void 0, function* () { const data = yield this.actor.get(principal_1.Principal.fromText(principalId)); if (data.length === 0) return undefined; return (0, registry_1.formatMetadata)(data[0]); }); this.remove = (principalId) => __awaiter(this, void 0, void 0, function* () { return this.actor.remove(principal_1.Principal.fromText(principalId)); }); this.actor = agent_1.Actor.createActor(registry_standard_did_1.default, { agent: agent, canisterId, }); this.canisterId = canisterId; } } exports.default = Registry;