@helium/http
Version:
HTTP library for interacting with the Helium blockchain API
148 lines • 5.72 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = __importDefault(require("axios"));
const rax = __importStar(require("retry-axios"));
const qs_1 = __importDefault(require("qs"));
const Network_1 = __importDefault(require("./Network"));
const Transactions_1 = __importDefault(require("./resources/Transactions"));
const Blocks_1 = __importDefault(require("./resources/Blocks"));
const Accounts_1 = __importDefault(require("./resources/Accounts"));
const Hotspots_1 = __importDefault(require("./resources/Hotspots"));
const Challenges_1 = __importDefault(require("./resources/Challenges"));
const Stats_1 = __importDefault(require("./resources/Stats"));
const Vars_1 = __importDefault(require("./resources/Vars"));
const Oracle_1 = __importDefault(require("./resources/Oracle"));
const PendingTransactions_1 = __importDefault(require("./resources/PendingTransactions"));
const Elections_1 = __importDefault(require("./resources/Elections"));
const Cities_1 = __importDefault(require("./resources/Cities"));
const Validators_1 = __importDefault(require("./resources/Validators"));
const StateChannels_1 = __importDefault(require("./resources/StateChannels"));
const Rewards_1 = __importDefault(require("./resources/Rewards"));
const Locations_1 = __importDefault(require("./resources/Locations"));
class Client {
constructor(network = Network_1.default.production, options) {
this.network = network;
this.axios = axios_1.default.create({
baseURL: this.network.endpoint,
headers: options === null || options === void 0 ? void 0 : options.headers,
});
this.name = options === null || options === void 0 ? void 0 : options.name;
this.userAgent = options === null || options === void 0 ? void 0 : options.userAgent;
this.retry = (options === null || options === void 0 ? void 0 : options.retry) === undefined ? 5 : options === null || options === void 0 ? void 0 : options.retry;
if (this.retry > 0) {
this.axios.defaults.raxConfig = {
instance: this.axios,
retry: this.retry,
noResponseRetries: this.retry,
};
rax.attach(this.axios);
}
}
get accounts() {
return new Accounts_1.default(this);
}
get account() {
return this.accounts.fromAddress.bind(this.accounts);
}
get blocks() {
return new Blocks_1.default(this);
}
get block() {
return this.blocks.fromHeightOrHash.bind(this.blocks);
}
get transactions() {
return new Transactions_1.default(this);
}
get hotspots() {
return new Hotspots_1.default(this);
}
get validators() {
return new Validators_1.default(this);
}
get elections() {
return new Elections_1.default(this);
}
get hotspot() {
return this.hotspots.fromAddress.bind(this.hotspots);
}
get validator() {
return this.validators.fromAddress.bind(this.validators);
}
get challenges() {
return new Challenges_1.default(this);
}
get stateChannels() {
return new StateChannels_1.default(this);
}
get pendingTransactions() {
return new PendingTransactions_1.default(this);
}
get stats() {
return new Stats_1.default(this);
}
get vars() {
return new Vars_1.default(this);
}
get oracle() {
return new Oracle_1.default(this);
}
get cities() {
return new Cities_1.default(this);
}
get city() {
return this.cities.fromId.bind(this.cities);
}
get rewards() {
return new Rewards_1.default(this);
}
get locations() {
return new Locations_1.default(this);
}
get clientOptions() {
const opts = { headers: {} };
if (this.name) {
opts.headers['x-client-name'] = this.name;
}
if (this.userAgent) {
opts.headers['User-Agent'] = this.userAgent;
}
const hasHeaders = Object.keys(opts.headers).length > 0;
return hasHeaders ? opts : undefined;
}
async get(path, params = {}) {
const query = qs_1.default.stringify(params);
const url = query.length > 0 ? [path, query].join('?') : path;
return this.axios.get(url, this.clientOptions);
}
async post(path, params = {}) {
return this.axios.post(path, params, this.clientOptions);
}
}
exports.default = Client;
//# sourceMappingURL=Client.js.map