README
Getting Started
import createApi from '@exodus/asset-json-rpc'
const api = createApi(url)
const api = createApi(url, { headers: { FOO: 'BAR' } }) // custom headers
// JSON-RPC
// request: { method: 'hello', params: [], id: 1, jsonrpc: '2.0' }
// response: { result: 'world', id: 1, jsonrpc: '2.0' }
// response with error: { error: { code: 100, message: 'danger', data: {a : 1} }, id: 1, jsonrpc: '2.0' }
const result = await api.post({ method: 'hello' }) // params can be omited
// result -> 'world'
// error -> throw new Error('danger') with properties { code: 100, data: { a: 1 } }
api.post({ method: 'hello', params: [1] }) // pass params
api.post({ method: 'hello', params: [1], id: 2, jsonrpc: '2.0' }) // can override id etc
Handle Errors
const api = createApi(url)
.middlewares([
(next) => async (url, options) => {
const res = await next(url, options)
let retry, message
if ([500].includes(res.status)) {
message = `${res.status} ${res.statusText}`
retry = true
} else {
return res
}
throw new ApiError(message, { retry })
}
])