UNPKG

@helium/http

Version:

HTTP library for interacting with the Helium blockchain API

71 lines 3.38 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const Hotspot_1 = __importDefault(require("../models/Hotspot")); const ResourceList_1 = __importDefault(require("../ResourceList")); const Account_1 = __importDefault(require("../models/Account")); const City_1 = __importDefault(require("../models/City")); class Hotspots { constructor(client, context) { this.client = client; this.context = context; } fromAddress(address) { return new Hotspot_1.default(this.client, { address }); } async search(term) { const url = 'hotspots/name'; const response = await this.client.get(url, { search: term }); const { data: { data: hotspots }, } = response; const data = hotspots.map((d) => new Hotspot_1.default(this.client, d)); return new ResourceList_1.default(data, this.list.bind(this)); } async list(params = {}) { const { hotspots, cursor } = await this.fetchList(params); const data = hotspots.map((d) => new Hotspot_1.default(this.client, d)); return new ResourceList_1.default(data, this.list.bind(this), cursor); } async fetchList(params = {}) { let url = '/hotspots'; if (this.context instanceof Account_1.default) { const account = this.context; url = `/accounts/${account.address}/hotspots`; } if (this.context instanceof City_1.default) { const city = this.context; url = `/cities/${city.cityId}/hotspots`; } const response = await this.client.get(url, { cursor: params.cursor }); const { data: { data: hotspots, cursor }, } = response; return { hotspots, cursor }; } async get(address) { const url = `/hotspots/${address}`; const { data: { data: hotspot }, } = await this.client.get(url); return new Hotspot_1.default(this.client, hotspot); } async elected(block) { const url = block === undefined ? '/hotspots/elected' : `/hotspots/elected/${block}`; const response = await this.client.get(url); const { data: { data: hotspots }, } = response; const data = hotspots.map((h) => new Hotspot_1.default(this.client, h)); return new ResourceList_1.default(data, this.list.bind(this)); } async hex(index, params) { const response = await this.client.get(`/hotspots/hex/${index}`, { cursor: params === null || params === void 0 ? void 0 : params.cursor }); const { data: { data: hotspots, cursor }, } = response; const data = hotspots.map((h) => new Hotspot_1.default(this.client, h)); const fetchMore = (nextParams) => this.hex(index, nextParams); return new ResourceList_1.default(data, fetchMore, cursor); } async locationDistance(params) { const response = await this.client.get('/hotspots/location/distance', params); const { data: { data: hotspots, cursor }, } = response; const data = hotspots.map((h) => new Hotspot_1.default(this.client, h)); return new ResourceList_1.default(data, this.locationDistance.bind(this), cursor); } } exports.default = Hotspots; //# sourceMappingURL=Hotspots.js.map