dotbit-sdk-allin
Version:
A complete .bit SDK and utilities in TypeScript
36 lines (32 loc) • 821 B
text/typescript
import fetch from 'cross-fetch'
import { DotbitError } from '../errors/DotbitError'
export class JSONRPC {
id = 0
constructor (
public url: string,
) {}
request<T = any> (method: string, params: any = []): Promise<T> {
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
})
}
}