zepio/services/api.js

44 lines
1013 B
JavaScript
Raw Normal View History

2018-12-05 06:43:34 -08:00
// @flow
2019-02-04 20:41:45 -08:00
2018-12-05 06:43:34 -08:00
import got from 'got';
/* eslint-disable-next-line */
import isDev from 'electron-is-dev';
2018-12-05 06:43:34 -08:00
import { METHODS, type APIMethods } from './utils';
import store from '../config/electron-store';
2018-12-05 06:43:34 -08:00
const RPC = {
host: '127.0.0.1',
2019-01-10 12:49:32 -08:00
// port: isDev ? 18232 : 8232,
port: 18232, // TODO: Test purposes only
user: store.get('rpcuser'),
password: store.get('rpcpassword'),
2018-12-05 06:43:34 -08:00
};
const client = got.extend({
method: 'POST',
json: true,
auth: `${RPC.user}:${RPC.password}`,
});
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))
2019-02-04 16:30:46 -08:00
.catch(payload => Promise.reject(new Error(payload.body?.error.message || 'Something went wrong'))),
2018-12-05 06:43:34 -08:00
}),
{},
);
2019-02-04 20:41:45 -08:00
// eslint-disable-next-line
2018-12-05 06:43:34 -08:00
export default api;