Add documentation and examples (#145)
* Add documentation * Justification u8a decoding and json encoding (#146) * Decode Justifications * Json encoding for justifications * Bump deps * Allow extrinsic length encoding to operate on u8a[] (#148) * Fix config and move generated doc to a subfolder * Cleanup extrinsic encoding (#150) * Cleanup extrinsic encoding * Removed now-defunct type * Add uncheckedLength encoding * Add examples * Ignore package-lock since we use yarn * Cleanup examples * Add gitbook config and some base files for the doc * Do not make full doc mandatory for now * Add contributing guide, polish doc and samples * Update the deps of all samples and add yarn scripts Fix and regenerate the doc * Fix Contributing guide
This commit is contained in:
@@ -2,6 +2,11 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
/**
|
||||
* A function returning any value passed to it
|
||||
* @param {any} value The input value
|
||||
* @return {any} The value passed as input
|
||||
*/
|
||||
export default function echo (value: any): any {
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -15,6 +15,20 @@ import coder from '../coder/json';
|
||||
|
||||
const ERROR_SUBSCRIBE = 'HTTP Provider does not have subscriptions, use WebSockets instead';
|
||||
|
||||
/**
|
||||
* The HTTP Provider allows sending requests using HTTP. 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
|
||||
* ```javascript
|
||||
*
|
||||
* import createApi from '@polkadot/api';
|
||||
* import WsProvider from '@polkadot/api-provider/ws';
|
||||
* const provider = new WsProvider('http://127.0.0.1:9933');
|
||||
* const api = createApi(provider);
|
||||
* ```
|
||||
* @see [[WsProvider]]
|
||||
*/
|
||||
export default class HttpProvider implements ProviderInterface {
|
||||
private coder: RpcCoder;
|
||||
private endpoint: string;
|
||||
@@ -28,10 +42,17 @@ export default class HttpProvider implements ProviderInterface {
|
||||
this.l = logger('api-http');
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the node is connected or not.
|
||||
* @return {boolean} true if connected
|
||||
*/
|
||||
isConnected (): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Events are not supported with the HttpProvider, see [[WsProvider]].
|
||||
*/
|
||||
on (type: ProviderInterface$Emitted, sub: ProviderInterface$EmitCb): void {
|
||||
this.l.error(`HTTP Provider does not have 'on' emitters, use WebSockets instead`);
|
||||
}
|
||||
@@ -55,12 +76,18 @@ export default class HttpProvider implements ProviderInterface {
|
||||
return this.coder.decodeResponse(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscriptions are not supported with the HttpProvider, see [[WsProvider]].
|
||||
*/
|
||||
async unsubscribe (type: string, method: string, id: number): Promise<boolean> {
|
||||
this.l.error(ERROR_SUBSCRIBE);
|
||||
|
||||
|
||||
@@ -11,6 +11,10 @@ import state from './state';
|
||||
import subscribe from './subscribe';
|
||||
import unsubscribe from './unsubscribe';
|
||||
|
||||
/**
|
||||
* A moock provider mainly used for testing.
|
||||
* @return {ProviderInterface} The mock provider
|
||||
*/
|
||||
export default function mockProvider (): ProviderInterface {
|
||||
const self = state();
|
||||
|
||||
|
||||
@@ -37,6 +37,20 @@ interface WSProviderInterface extends ProviderInterface {
|
||||
connect (): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* The WebSocket Provider allows sending requests using WebSocket. Unlike the [[HttpProvider]],
|
||||
* it does support subscriptions and allows listening to events such as new blocks or balance changes.
|
||||
* [[include:create_api.adoc]]
|
||||
* @example
|
||||
* ```javascript
|
||||
*
|
||||
* import createApi from '@polkadot/api';
|
||||
* import WsProvider from '@polkadot/api-provider/ws';
|
||||
* const provider = new WsProvider('ws://127.0.0.1:9944');
|
||||
* const api = createApi(provider);
|
||||
* ```
|
||||
* @see [[HttpProvider]]
|
||||
*/
|
||||
export default class WsProvider extends E3.EventEmitter implements WSProviderInterface {
|
||||
private autoConnect: boolean;
|
||||
private coder: RpcCoder;
|
||||
@@ -54,6 +68,10 @@ export default class WsProvider extends E3.EventEmitter implements WSProviderInt
|
||||
};
|
||||
private websocket: WebSocket | null;
|
||||
|
||||
/**
|
||||
* @param {string} endpoint The endpoint url. Usually `ws://ip:9944` or `wss://ip:9944`
|
||||
* @param {boolean = true} autoConnect Whether to connect automatically or not.
|
||||
*/
|
||||
constructor (endpoint: string, autoConnect: boolean = true) {
|
||||
super();
|
||||
|
||||
@@ -74,6 +92,10 @@ export default class WsProvider extends E3.EventEmitter implements WSProviderInt
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The [[WsProvider]] connects automatically by default. if you decided otherwise, you may
|
||||
* connect manually using this method.
|
||||
*/
|
||||
connect (): void {
|
||||
try {
|
||||
this.websocket = new WebSocket(this.endpoint);
|
||||
@@ -87,10 +109,20 @@ export default class WsProvider extends E3.EventEmitter implements WSProviderInt
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the node is connected or not.
|
||||
* @return {boolean} true if connected
|
||||
*/
|
||||
isConnected (): boolean {
|
||||
return this._isConnected;
|
||||
}
|
||||
|
||||
/**
|
||||
* Listens on events after having subscribed using the [[subscribe]] function.
|
||||
* @param {ProviderInterface$Emitted} type Event
|
||||
* @param {ProviderInterface$EmitCb} sub Callback
|
||||
* @return {this} [description]
|
||||
*/
|
||||
on (type: ProviderInterface$Emitted, sub: ProviderInterface$EmitCb): this {
|
||||
return super.on(type, sub);
|
||||
}
|
||||
@@ -128,12 +160,34 @@ export default class WsProvider extends E3.EventEmitter implements WSProviderInt
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows subscribing to a specific event.
|
||||
* @param {string} type Subscription type
|
||||
* @param {string} method Subscription method
|
||||
* @param {Array<any>} params Parameters
|
||||
* @param {ProviderInterface$Callback} callback Callback
|
||||
* @return {Promise<number>} Promise resolving to the dd of the subscription you can use with [[unsubscribe]]
|
||||
* @example
|
||||
* ```javascript
|
||||
*
|
||||
* const provider = new WsProvider('ws://127.0.0.1:9944')
|
||||
* const api = createApi(provider)
|
||||
* api.state.storage([[storage.staking.public.freeBalanceOf, <Address>]], (_, values) => {
|
||||
* console.log(values)
|
||||
* }).then((subscriptionId) => {
|
||||
* console.log('balance changes subscription id: ', subscriptionId)
|
||||
* })
|
||||
* ```
|
||||
*/
|
||||
async subscribe (type: string, method: string, params: Array<any>, callback: ProviderInterface$Callback): Promise<number> {
|
||||
const id = await this.send(method, params, { callback, type });
|
||||
|
||||
return id as number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows unsubscribing to subscriptions made with [[subscribe]].
|
||||
*/
|
||||
async unsubscribe (type: string, method: string, id: number): Promise<boolean> {
|
||||
const subscription = `${type}::${id}`;
|
||||
|
||||
|
||||
@@ -13,6 +13,17 @@ import Ws from '@polkadot/api-provider/ws';
|
||||
import defaults from './defaults';
|
||||
import createInterface from './interface';
|
||||
|
||||
/**
|
||||
* An RxJs wrapper around the [[api]].
|
||||
* @example
|
||||
* ```javascript
|
||||
*
|
||||
* import RxApi from '@polkadot/api';
|
||||
* import WsProvider from '@polkadot/api-provider/ws';
|
||||
* const provider = new WsProvider('http://127.0.0.1:9944');
|
||||
* const rxapi = new RxApi(provider);
|
||||
* ```
|
||||
*/
|
||||
export default class RxApi implements RxApiInterface {
|
||||
private _api: ApiInterface;
|
||||
private _isConnected: BehaviorSubject<boolean>;
|
||||
@@ -21,6 +32,9 @@ export default class RxApi implements RxApiInterface {
|
||||
readonly state: RxApiInterface$Section;
|
||||
readonly system: RxApiInterface$Section;
|
||||
|
||||
/**
|
||||
* @param {ProviderInterface} provider An API provider using HTTP or WebSocket
|
||||
*/
|
||||
constructor (provider: ProviderInterface = new Ws(defaults.WS_URL)) {
|
||||
this._api = new Api(provider);
|
||||
this._isConnected = new BehaviorSubject(provider.isConnected());
|
||||
|
||||
@@ -4,12 +4,20 @@
|
||||
|
||||
import { ProviderInterface } from '@polkadot/api-provider/types';
|
||||
import { ApiInterface, ApiInterface$Section } from './types';
|
||||
|
||||
import assert from '@polkadot/util/assert';
|
||||
import isFunction from '@polkadot/util/is/function';
|
||||
|
||||
import createInterface from './create/interface';
|
||||
|
||||
/**
|
||||
* @example
|
||||
* ```javascript
|
||||
*
|
||||
* import Api from '@polkadot/api';
|
||||
* import WsProvider from '@polkadot/api-provider/ws';
|
||||
* const provider = new WsProvider('http://127.0.0.1:9944');
|
||||
* const api = new Api(provider);
|
||||
* ```
|
||||
*/
|
||||
export default class Api implements ApiInterface {
|
||||
private _provider: ProviderInterface;
|
||||
readonly author: ApiInterface$Section;
|
||||
@@ -17,6 +25,11 @@ export default class Api implements ApiInterface {
|
||||
readonly state: ApiInterface$Section;
|
||||
readonly system: ApiInterface$Section;
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* Default constructor for the Api Object
|
||||
* @param {ProviderInterface} provider An API provider using HTTP or WebSocket
|
||||
*/
|
||||
constructor (provider: ProviderInterface) {
|
||||
assert(provider && isFunction(provider.send), 'Expected Provider to API create');
|
||||
|
||||
|
||||
@@ -7,5 +7,3 @@
|
||||
[](https://coveralls.io/github/polkadot-js/api?branch=master)
|
||||
[](https://david-dm.org/polkadot-js/api?path=packages/type-params)
|
||||
[](https://david-dm.org/polkadot-js/api?path=packages/type-params#info=devDependencies)
|
||||
|
||||
# @polkadot/params
|
||||
|
||||
Reference in New Issue
Block a user