@helium/http
Version:
HTTP library for interacting with the Helium blockchain API
63 lines • 2.55 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const camelcase_keys_1 = __importDefault(require("camelcase-keys"));
const snakecase_keys_1 = __importDefault(require("snakecase-keys"));
const Block_1 = __importDefault(require("../models/Block"));
const ResourceList_1 = __importDefault(require("../ResourceList"));
function stringIsInt(str) {
return !!str.match(/^\d+$/);
}
class Blocks {
constructor(client) {
this.client = client;
}
fromHeightOrHash(heightOrHash) {
if (typeof heightOrHash === 'number') {
return new Block_1.default(this.client, { height: heightOrHash });
}
if (typeof heightOrHash === 'string') {
if (stringIsInt(heightOrHash)) {
return new Block_1.default(this.client, { height: parseInt(heightOrHash, 10) });
}
return new Block_1.default(this.client, { hash: heightOrHash });
}
throw new Error('heightOrHash must be a number or string');
}
async list(params = {}) {
const { data: { data: blocks, cursor }, } = await this.client.get('/blocks', { cursor: params.cursor });
const data = blocks.map((d) => new Block_1.default(this.client, d));
return new ResourceList_1.default(data, this.list.bind(this), cursor);
}
async get(heightOrHash) {
let url;
if (typeof heightOrHash === 'number') {
url = `/blocks/${heightOrHash}`;
}
else if (typeof heightOrHash === 'string') {
if (stringIsInt(heightOrHash)) {
url = `/blocks/${parseInt(heightOrHash, 10)}`;
}
else {
url = `/blocks/hash/${heightOrHash}`;
}
}
if (!url)
throw new Error('heightOrHash must be a number or string');
const { data: { data: block }, } = await this.client.get(url);
return new Block_1.default(this.client, block);
}
async getHeight(params) {
const path = '/blocks/height';
const { data: { data: { height } } } = await this.client.get(path, (0, snakecase_keys_1.default)(params || {}));
return height;
}
async stats() {
const { data: { data: stats } } = await this.client.get('/blocks/stats');
return (0, camelcase_keys_1.default)(stats);
}
}
exports.default = Blocks;
//# sourceMappingURL=Blocks.js.map