zepio/services/api.js

55 lines
1.3 KiB
JavaScript

// @flow
import got from 'got';
import { METHODS, type APIMethods } from './utils';
import store from '../config/electron-store';
import { isTestnet } from '../config/is-testnet';
const RPC = {
host: '127.0.0.1',
port: isTestnet() ? 18232 : 8232,
user: store.get('rpcuser'),
password: store.get('rpcpassword'),
};
const client = got.extend({
method: 'POST',
json: true,
auth: `${RPC.user}:${RPC.password}`,
});
const getMessage = (statusCode) => {
switch (statusCode) {
case 401:
return 'Not authorized to access Zcash RPC, please check your rpcuser and rpcpassword';
default:
return 'Something went wrong';
}
};
const api: APIMethods = METHODS.reduce(
(obj, method) => ({
...obj,
[method]: (...args) => client
.post(`http://${RPC.host}:${RPC.port}`, {
body: {
method,
jsonrpc: '2.0',
id: Date.now(),
params: args,
},
})
.then(data => Promise.resolve(data.body && data.body.result))
// eslint-disable-next-line
.catch(payload => Promise.reject({
message: payload.body?.error?.message || getMessage(payload.statusCode),
statusCode: payload.statusCode,
})),
}),
{},
);
// eslint-disable-next-line
export default api;