Files
api/packages/rpc-provider/src/http/index.ts
T
Luke Schoen 267411d778 fix: Fixes #235 TypeDoc updates consistent with renaming (#236)
* fix: Remove docs/api since replaced with docs/rpc-core

* fix: Remove docs/api-provider since replaced with docs/rpc-provider

* fix: Remove docs/api-rx since replaced with docs/rpc-rx

* fix: Remove the guide file that serves no purpose

* fix: Change license from ui-observable to api-observable

* fix: Remove disused mockHttp and mockWs that are duplicates of those in rpc-provider

* fix: Clarifies difference between PropIndex and ProposalIndex from Issue #226

* fix: Update api-observable Readme to refer to rpc-core, previously called api

* misc: Update Readme of rpc-core. See commit message details.

* Update name to be @polkadot/rpc-core instead of @polkadot/api

* Use Yarn instead of NPM for instructions adding dependencies

* Refer to @polkadot/rpc-core as `Rpc`

* Highlight that given block header hash should be a 0x-prefixed hex string with length of 64 (not only length of 10 as shown in the example)

* Showcase more information that you can obtain from the header object (i.e. best block number, parentHash, stateRoot, extrinsicsRoot, and digest)

* Use `header.raw` instead of just `header` to access properties of the header object

* Update the example to console.log the subscription ID

* Add missing semicolon

* fix: Fix name in license header and rename Api to Rpc for rpc-core e2e tests

* fix: Update rpc-provider Readme to use Yarn instead of NPM install

* fix: Fix minor typo

* fix: Rename Api to Rpc for rpc-provider ws

* fix: Refer to rpc-core instead of api in the rpc-rx Readme

* fix: Update rpc-rx files to refer to it as RpcRx instead of RxApi or just Api

* fix: Refer to rpc-core as Rpc instead of Api

* fix: Update type-jsonrpc Readme. Fix typo. Use Yarn not NPM. Refer to TSLint not Flow

* refactor: Move non-type dependency import with non-type dependencies

* docs: Add link to parity-codec repo

* fix: Avoid use of .raw. Fix listen to blocks example

* fix: Add missing error to newHead in listen to blocks example
2018-10-10 20:11:22 +02:00

109 lines
3.3 KiB
TypeScript

// Copyright 2017-2018 @polkadot/rpc-provider authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { Logger } from '@polkadot/util/types';
import { RpcCoder } from '../coder/json/types';
import { ProviderInterface, ProviderInterface$Callback, ProviderInterface$Emitted, ProviderInterface$EmitCb } from '../types';
import './polyfill';
import assert from '@polkadot/util/assert';
import logger from '@polkadot/util/logger';
import coder from '../coder/json';
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 Rpc from '@polkadot/rpc-core';
* import HttpProvider from '@polkadot/rpc-provider/http';
*
* const provider = new HttpProvider('http://127.0.0.1:9933');
* const api = new Rpc(provider);
* ```
*
* @see [[WsProvider]]
*/
export default class HttpProvider implements ProviderInterface {
private coder: RpcCoder;
private endpoint: string;
private l: Logger;
/**
* @param {string} endpoint The endpoint url starting with http://
*/
constructor (endpoint: string) {
assert(/^(https|http):\/\//.test(endpoint), `Endpoint should start with 'http://', received '${endpoint}'`);
this.coder = coder();
this.endpoint = endpoint;
this.l = logger('api-http');
}
/**
* @summary Whether the node is connected or not.
* @return {boolean} true if connected
*/
isConnected (): boolean {
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: ProviderInterface$Emitted, sub: ProviderInterface$EmitCb): void {
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: string, params: Array<any>): Promise<any> {
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'
});
assert(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: string, method: string, params: Array<any>, cb: ProviderInterface$Callback): Promise<number> {
this.l.error(ERROR_SUBSCRIBE);
throw new Error(ERROR_SUBSCRIBE);
}
/**
* @summary Subscriptions are not supported with the HttpProvider, see [[WsProvider]].
*/
async unsubscribe (type: string, method: string, id: number): Promise<boolean> {
this.l.error(ERROR_SUBSCRIBE);
throw new Error(ERROR_SUBSCRIBE);
}
}