UNPKG

@polkadot/api-provider

Version:
114 lines (94 loc) 3.27 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; require("./polyfill"); var _assert = _interopRequireDefault(require("@polkadot/util/assert")); var _logger = _interopRequireDefault(require("@polkadot/util/logger")); var _json = _interopRequireDefault(require("../coder/json")); // Copyright 2017-2018 @polkadot/api-provider authors & contributors // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. const ERROR_SUBSCRIBE = 'HTTP Provider does not have subscriptions, use WebSockets instead'; /** * @name HttpProvider * @summary The HTTP Provider allows sending requests using HTTP to a HTTP RPC server TCP port. * @description It does not support subscriptions so you won't be able to listen to events * such as new blocks or balance changes. It is usually preferrable using the [[WsProvider]]. * * @example * <BR> * * ```javascript * import Api from '@polkadot/api'; * import HttpProvider from '@polkadot/api-provider/http'; * * const provider = new HttpProvider('http://127.0.0.1:9933'); * const api = new Api(provider); * ``` * * @see [[WsProvider]] */ class HttpProvider { /** * @param {string} endpoint The endpoint url starting with http:// */ constructor(endpoint) { this.coder = void 0; this.endpoint = void 0; this.l = void 0; (0, _assert.default)(/^(https|http):\/\//.test(endpoint), `Endpoint should start with 'http://', received '${endpoint}'`); this.coder = (0, _json.default)(); this.endpoint = endpoint; this.l = (0, _logger.default)('api-http'); } /** * @summary Whether the node is connected or not. * @return {boolean} true if connected */ isConnected() { return true; } /** * @summary Events are not supported with the HttpProvider, see [[WsProvider]]. * @description HTTP Provider does not have 'on' emitters. WebSockets should be used instead. */ on(type, sub) { this.l.error(`HTTP Provider does not have 'on' emitters, use WebSockets instead`); } /** * @summary Send HTTP POST Request with Body to configured HTTP Endpoint. */ async send(method, params) { const body = this.coder.encodeJson(method, params); const response = await fetch(this.endpoint, { body, headers: { 'Accept': 'application/json', 'Content-Length': `${body.length}`, 'Content-Type': 'application/json' }, method: 'POST' }); (0, _assert.default)(response.ok, `[${response.status}]: ${response.statusText}`); const result = await response.json(); return this.coder.decodeResponse(result); } /** * @summary Subscriptions are not supported with the HttpProvider, see [[WsProvider]]. */ async subscribe(types, method, params, cb) { this.l.error(ERROR_SUBSCRIBE); throw new Error(ERROR_SUBSCRIBE); } /** * @summary Subscriptions are not supported with the HttpProvider, see [[WsProvider]]. */ async unsubscribe(type, method, id) { this.l.error(ERROR_SUBSCRIBE); throw new Error(ERROR_SUBSCRIBE); } } exports.default = HttpProvider;