import fetch from 'cross-fetch' import { DotbitError } from '../errors/DotbitError' export class JSONRPC { id = 0 constructor ( public url: string, ) {} request (method: string, params: any = []): Promise { return fetch(this.url, { method: 'POST', body: JSON.stringify({ jsonrpc: '2.0', id: this.id++, method, params, }), headers: { 'Content-Type': 'application/json', }, }) .then(res => res.json()) .then(res => { if (res.error) { throw new DotbitError(res.error.message, res.error.code) } if (res.result.errno) { throw new DotbitError(res.result.errmsg, res.result.errno) } return res.result }) } }