@starport/client-js
Advanced tools
Comparing version 0.1.51 to 0.1.52-beta.0
@@ -62,3 +62,5 @@ "use strict"; | ||
rpcAddr = _ref.rpcAddr, | ||
wsAddr = _ref.wsAddr; | ||
wsAddr = _ref.wsAddr, | ||
refresh = _ref.refresh, | ||
offline = _ref.offline; | ||
@@ -75,2 +77,6 @@ _classCallCheck(this, SPClient); | ||
_defineProperty(_assertThisInitialized(_this), "offline", void 0); | ||
_defineProperty(_assertThisInitialized(_this), "refresh", void 0); | ||
_defineProperty(_assertThisInitialized(_this), "socket", void 0); | ||
@@ -87,6 +93,8 @@ | ||
_this.wsAddr = wsAddr; | ||
_this.offline = offline; | ||
_this.refresh = refresh; | ||
var poll = _this.connectivityTest.bind(_assertThisInitialized(_this)); | ||
_this.timer = setInterval(poll, 5000); | ||
_this.timer = setInterval(poll, _this.refresh); | ||
@@ -195,12 +203,18 @@ _this.connectivityTest(); | ||
case 0: | ||
if (this.offline) { | ||
this.emit('newblock', { | ||
dummy: true | ||
}); | ||
} | ||
if (!this.apiAddr) { | ||
_context3.next = 13; | ||
_context3.next = 14; | ||
break; | ||
} | ||
_context3.prev = 1; | ||
_context3.next = 4; | ||
_context3.prev = 2; | ||
_context3.next = 5; | ||
return _axios["default"].get(this.apiAddr + '/node_info'); | ||
case 4: | ||
case 5: | ||
status = _context3.sent; | ||
@@ -210,8 +224,8 @@ this.emit('chain-id', status.data.node_info.network); | ||
this.emit('api-status', true); | ||
_context3.next = 13; | ||
_context3.next = 14; | ||
break; | ||
case 10: | ||
_context3.prev = 10; | ||
_context3.t0 = _context3["catch"](1); | ||
case 11: | ||
_context3.prev = 11; | ||
_context3.t0 = _context3["catch"](2); | ||
@@ -225,20 +239,20 @@ if (!_context3.t0.response) { | ||
case 13: | ||
case 14: | ||
if (!this.rpcAddr) { | ||
_context3.next = 23; | ||
_context3.next = 24; | ||
break; | ||
} | ||
_context3.prev = 14; | ||
_context3.next = 17; | ||
_context3.prev = 15; | ||
_context3.next = 18; | ||
return _axios["default"].get(this.rpcAddr); | ||
case 17: | ||
case 18: | ||
this.emit('rpc-status', true); | ||
_context3.next = 23; | ||
_context3.next = 24; | ||
break; | ||
case 20: | ||
_context3.prev = 20; | ||
_context3.t1 = _context3["catch"](14); | ||
case 21: | ||
_context3.prev = 21; | ||
_context3.t1 = _context3["catch"](15); | ||
@@ -252,3 +266,3 @@ if (!_context3.t1.response) { | ||
case 23: | ||
case 24: | ||
case "end": | ||
@@ -258,3 +272,3 @@ return _context3.stop(); | ||
} | ||
}, _callee3, this, [[1, 10], [14, 20]]); | ||
}, _callee3, this, [[2, 11], [15, 21]]); | ||
})); | ||
@@ -261,0 +275,0 @@ |
{ | ||
"name": "@starport/client-js", | ||
"version": "0.1.51", | ||
"version": "0.1.52-beta.0+f907e5b", | ||
"description": "A generic JS client for the Websocket, high-level (Cosmos) and low-level (Tendermint) APIs", | ||
@@ -49,3 +49,3 @@ "author": "Tendermint, Inc <hello@tendermint.com>", | ||
}, | ||
"gitHead": "e3c17b754c1e409c22619a833b981705f6a6996e" | ||
"gitHead": "f907e5b150c631b9a071cafd9d38b73364c356eb" | ||
} |
@@ -1,18 +0,20 @@ | ||
import { EventEmitter } from 'events'; | ||
import ReconnectingWebSocket from 'reconnecting-websocket'; | ||
import axios, { AxiosPromise, AxiosResponse } from 'axios'; | ||
import { SigningStargateClient } from '@cosmjs/stargate'; | ||
import { OfflineDirectSigner, Registry } from '@cosmjs/proto-signing'; | ||
import SpClientError from './errors/SpClientError'; | ||
import { EventEmitter } from 'events' | ||
import ReconnectingWebSocket from 'reconnecting-websocket' | ||
import axios, { AxiosPromise, AxiosResponse } from 'axios' | ||
import { SigningStargateClient } from '@cosmjs/stargate' | ||
import { OfflineDirectSigner, Registry } from '@cosmjs/proto-signing' | ||
import SpClientError from './errors/SpClientError' | ||
export interface IClientConfig { | ||
apiAddr: string; | ||
rpcAddr?: string; | ||
wsAddr?: string; | ||
apiAddr: string | ||
rpcAddr?: string | ||
wsAddr?: string | ||
refresh?: number | ||
offline?: boolean | ||
} | ||
export type QueryParamsType = Record<string | number, unknown>; | ||
export type QueryParamsType = Record<string | number, unknown> | ||
export interface IFullRequestParams { | ||
body?: unknown; | ||
path: string; | ||
query?: QueryParamsType; | ||
method: 'GET' | 'POST' | 'PUT'; | ||
body?: unknown | ||
path: string | ||
query?: QueryParamsType | ||
method: 'GET' | 'POST' | 'PUT' | ||
} | ||
@@ -26,3 +28,3 @@ export interface IResponse { | ||
export interface IAPIResponse { | ||
data:unknown | ||
data: unknown | ||
} | ||
@@ -33,70 +35,69 @@ export interface IParsedResponse { | ||
export default class SPClient extends EventEmitter { | ||
private apiAddr: string; | ||
private rpcAddr: string; | ||
private wsAddr: string; | ||
private apiAddr: string | ||
private rpcAddr: string | ||
private wsAddr: string | ||
private offline: boolean | ||
private refresh: number | ||
private socket: ReconnectingWebSocket | ||
private socket: ReconnectingWebSocket; | ||
public signingClient | ||
private signer: OfflineDirectSigner | ||
private timer: ReturnType<typeof setInterval> | ||
public signingClient; | ||
private signer: OfflineDirectSigner; | ||
private timer: ReturnType<typeof setInterval>; | ||
constructor({ apiAddr, rpcAddr, wsAddr }: IClientConfig) { | ||
super(); | ||
this.apiAddr = apiAddr; | ||
this.rpcAddr = rpcAddr; | ||
this.wsAddr = wsAddr; | ||
const poll: () =>Promise<void> = this.connectivityTest.bind(this); | ||
this.timer = setInterval(poll, 5000); | ||
this.connectivityTest(); | ||
constructor({ apiAddr, rpcAddr, wsAddr, refresh, offline }: IClientConfig) { | ||
super() | ||
this.apiAddr = apiAddr | ||
this.rpcAddr = rpcAddr | ||
this.wsAddr = wsAddr | ||
this.offline = offline | ||
this.refresh = refresh | ||
const poll: () => Promise<void> = this.connectivityTest.bind(this) | ||
this.timer = setInterval(poll, this.refresh) | ||
this.connectivityTest() | ||
if (this.wsAddr) { | ||
this.socket = new ReconnectingWebSocket(this.wsAddr); | ||
this.socket = new ReconnectingWebSocket(this.wsAddr) | ||
this.socket.onopen = this.onOpenWS.bind(this); | ||
this.socket.onmessage = this.onMessageWS.bind(this); | ||
this.socket.onerror = this.onErrorWS.bind(this); | ||
this.socket.onclose = this.onCloseWS.bind(this); | ||
this.socket.onopen = this.onOpenWS.bind(this) | ||
this.socket.onmessage = this.onMessageWS.bind(this) | ||
this.socket.onerror = this.onErrorWS.bind(this) | ||
this.socket.onclose = this.onCloseWS.bind(this) | ||
} | ||
} | ||
public async useSigner(signer: OfflineDirectSigner): Promise<void> { | ||
this.signingClient = await SigningStargateClient.connectWithSigner( | ||
this.rpcAddr, | ||
signer, | ||
); | ||
this.signer = signer; | ||
this.signingClient = await SigningStargateClient.connectWithSigner(this.rpcAddr, signer) | ||
this.signer = signer | ||
} | ||
public switchAPI(apiAddr: string): void { | ||
this.apiAddr = apiAddr; | ||
this.apiAddr = apiAddr | ||
} | ||
public switchWS(wsAddr: string): void { | ||
this.emit('ws-status', false); | ||
this.wsAddr = wsAddr; | ||
this.socket = new ReconnectingWebSocket(this.wsAddr); | ||
this.emit('ws-status', false) | ||
this.wsAddr = wsAddr | ||
this.socket = new ReconnectingWebSocket(this.wsAddr) | ||
} | ||
public async switchRPC(rpcAddr: string): Promise<void> { | ||
this.rpcAddr = rpcAddr; | ||
this.rpcAddr = rpcAddr | ||
if (this.signingClient) { | ||
const registry: Registry = { ...this.signingClient.registry }; | ||
this.signingClient = await SigningStargateClient.connectWithSigner( | ||
this.rpcAddr, | ||
this.signer, | ||
{ registry }, | ||
); | ||
const registry: Registry = { ...this.signingClient.registry } | ||
this.signingClient = await SigningStargateClient.connectWithSigner(this.rpcAddr, this.signer, { registry }) | ||
} | ||
} | ||
private async connectivityTest(): Promise<void> { | ||
if (this.offline) { | ||
this.emit('newblock', { dummy: true }) | ||
} | ||
if (this.apiAddr) { | ||
try { | ||
const status= await axios.get(this.apiAddr + '/node_info'); | ||
const status = await axios.get(this.apiAddr + '/node_info') | ||
this.emit('chain-id', status.data.node_info.network) | ||
status.data.application_version.name?this.emit('chain-name', status.data.application_version.name):this.emit('chain-name', status.data.node_info.network) | ||
this.emit('api-status', true); | ||
status.data.application_version.name | ||
? this.emit('chain-name', status.data.application_version.name) | ||
: this.emit('chain-name', status.data.node_info.network) | ||
this.emit('api-status', true) | ||
} catch (error) { | ||
if (!error.response) { | ||
this.emit('api-status', false); | ||
console.error( | ||
new SpClientError('Client-js:API', 'API Node unavailable'), | ||
); | ||
this.emit('api-status', false) | ||
console.error(new SpClientError('Client-js:API', 'API Node unavailable')) | ||
} else { | ||
this.emit('api-status', true); | ||
this.emit('api-status', true) | ||
} | ||
@@ -107,12 +108,10 @@ } | ||
try { | ||
await axios.get(this.rpcAddr); | ||
this.emit('rpc-status', true); | ||
await axios.get(this.rpcAddr) | ||
this.emit('rpc-status', true) | ||
} catch (error) { | ||
if (!error.response) { | ||
console.error( | ||
new SpClientError('Client-js:API', 'RPC Node unavailable'), | ||
); | ||
this.emit('rpc-status', false); | ||
console.error(new SpClientError('Client-js:API', 'RPC Node unavailable')) | ||
this.emit('rpc-status', false) | ||
} else { | ||
this.emit('rpc-status', true); | ||
this.emit('rpc-status', true) | ||
} | ||
@@ -123,11 +122,9 @@ } | ||
private onErrorWS(): void { | ||
console.error( | ||
new SpClientError('Client-js:WS', 'Could not connect to websocket.'), | ||
); | ||
console.error(new SpClientError('Client-js:WS', 'Could not connect to websocket.')) | ||
} | ||
private onCloseWS(): void { | ||
this.emit('ws-status', false); | ||
this.emit('ws-status', false) | ||
} | ||
private onOpenWS(): void { | ||
this.emit('ws-status', true); | ||
this.emit('ws-status', true) | ||
this.socket.send( | ||
@@ -140,8 +137,8 @@ JSON.stringify({ | ||
}), | ||
); | ||
) | ||
} | ||
private onMessageWS(msg: IResponse): void { | ||
const result: IParsedResponse = JSON.parse(msg.data).result; | ||
const result: IParsedResponse = JSON.parse(msg.data).result | ||
if (result.data && result.data.type === 'tendermint/event/NewBlock') { | ||
this.emit('newblock', JSON.parse(msg.data).result); | ||
this.emit('newblock', JSON.parse(msg.data).result) | ||
} | ||
@@ -151,8 +148,6 @@ } | ||
try { | ||
const response: IAPIResponse = await axios.get(this.apiAddr + url + params); | ||
return response.data; | ||
const response: IAPIResponse = await axios.get(this.apiAddr + url + params) | ||
return response.data | ||
} catch (e) { | ||
console.error( | ||
new SpClientError('Client-js:API', 'Could not access API: ' + url), | ||
); | ||
console.error(new SpClientError('Client-js:API', 'Could not access API: ' + url)) | ||
} | ||
@@ -162,3 +157,3 @@ } | ||
private addQueryParam(query: QueryParamsType, key: string): string { | ||
const value: unknown = query[key]; | ||
const value: unknown = query[key] | ||
@@ -168,16 +163,8 @@ return ( | ||
'=' + | ||
encodeURIComponent( | ||
Array.isArray(value) | ||
? value.join(',') | ||
: typeof value === 'number' | ||
? value | ||
: `${value}`, | ||
) | ||
); | ||
encodeURIComponent(Array.isArray(value) ? value.join(',') : typeof value === 'number' ? value : `${value}`) | ||
) | ||
} | ||
protected toQueryString(rawQuery?: QueryParamsType): string { | ||
const query: QueryParamsType = rawQuery || {}; | ||
const keys: string[] = Object.keys(query).filter( | ||
(key) => 'undefined' !== typeof query[key], | ||
); | ||
const query: QueryParamsType = rawQuery || {} | ||
const keys: string[] = Object.keys(query).filter((key) => 'undefined' !== typeof query[key]) | ||
return keys | ||
@@ -189,15 +176,10 @@ .map((key) => | ||
) | ||
.join('&'); | ||
.join('&') | ||
} | ||
protected addQueryParams(rawQuery?: QueryParamsType): string { | ||
const queryString: string = this.toQueryString(rawQuery); | ||
return queryString ? `?${queryString}` : ''; | ||
const queryString: string = this.toQueryString(rawQuery) | ||
return queryString ? `?${queryString}` : '' | ||
} | ||
public async request<T = unknown>({ | ||
body, | ||
path, | ||
query, | ||
method, | ||
}: IFullRequestParams): Promise<AxiosResponse<T>> { | ||
const url: string = this.apiAddr + path + this.addQueryParams(query); | ||
public async request<T = unknown>({ body, path, query, method }: IFullRequestParams): Promise<AxiosResponse<T>> { | ||
const url: string = this.apiAddr + path + this.addQueryParams(query) | ||
try { | ||
@@ -212,11 +194,9 @@ const response: AxiosPromise<T> = axios({ | ||
}, | ||
}); | ||
const data: AxiosResponse<T> = await response; | ||
return data; | ||
}) | ||
const data: AxiosResponse<T> = await response | ||
return data | ||
} catch (e) { | ||
console.error( | ||
new SpClientError('Client-js:API', 'Could not access API: ' + url), | ||
); | ||
console.error(new SpClientError('Client-js:API', 'Could not access API: ' + url)) | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
58395
593
1