UNPKG

hyjs-test

Version:
198 lines (197 loc) 6.4 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.send = exports.getStakeOfByAccid = exports.getBalanceOfByAccid = exports.getProcesses = exports.getNodesByProcess = exports.getNode = exports.getNodes = exports.getAssignByMessage = exports.getAssignByNonce = exports.getMessageByNonce = exports.getMessage = exports.getResults = exports.getResult = exports.getInfo = exports.sendRequest = void 0; const axios_1 = __importDefault(require("axios")); const isObject_1 = __importDefault(require("lodash/isObject")); const isString_1 = __importDefault(require("lodash/isString")); const query_string_1 = require("query-string"); const utils_1 = require("../utils"); // `validateStatus` defines whether to resolve or reject the promise for a given // HTTP response status code. If `validateStatus` returns `true` (or is set to `null` // or `undefined`), the promise will be resolved; otherwise, the promise will be rejected. const validateStatus = function (status) { return status >= 200 && status < 300; // default }; const rConfig = { timeout: 15000, validateStatus, headers: { 'Content-Type': 'application/json' } }; const sendRequest = async (config) => { return await new Promise((resolve, reject) => { (0, axios_1.default)({ ...rConfig, ...config }).then((res) => { var _a; if (res.data !== undefined) { resolve(res); } else { reject(new Error(`${(_a = config.url) !== null && _a !== void 0 ? _a : ''}: null response`)); } }).catch(error => { if ((0, isString_1.default)(error)) { reject(new Error(error)); } else if ((0, isObject_1.default)(error.response) && (0, isObject_1.default)(error.response.data)) { // like { error: 'err_invalid_signature' } reject(new Error(error.response.data.error)); } else { reject(new Error(error)); } }); }); }; exports.sendRequest = sendRequest; const getInfo = async (apiHost) => { const url = `${apiHost}/info`; const result = await (0, exports.sendRequest)({ ...rConfig, url, method: 'GET' }); return result.data; }; exports.getInfo = getInfo; const getResult = async (apiHost, msgId) => { const url = `${apiHost}/result/${msgId}`; const result = await (0, exports.sendRequest)({ ...rConfig, url, method: 'GET' }); return result.data; }; exports.getResult = getResult; const getResults = async (apiHost, processId, limit) => { const queryStr = (0, query_string_1.stringify)({ sort: 'DESC', limit }); const url = `${apiHost}/results/${processId}${queryStr !== '' ? `?${queryStr}` : ''}`; const result = await (0, exports.sendRequest)({ ...rConfig, url, method: 'GET' }); return result.data; }; exports.getResults = getResults; const getMessage = async (apiHost, msgId) => { const url = `${apiHost}/message/${msgId}`; const result = await (0, exports.sendRequest)({ ...rConfig, url, method: 'GET' }); return result.data; }; exports.getMessage = getMessage; const getMessageByNonce = async (apiHost, processId, nonce) => { const url = `${apiHost}/messageByNonce/${processId}/${nonce}`; const result = await (0, exports.sendRequest)({ ...rConfig, url, method: 'GET' }); return result.data; }; exports.getMessageByNonce = getMessageByNonce; const getAssignByNonce = async (apiHost, processId, nonce) => { const url = `${apiHost}/assignmentByNonce/${processId}/${nonce}`; const result = await (0, exports.sendRequest)({ ...rConfig, url, method: 'GET' }); return result.data; }; exports.getAssignByNonce = getAssignByNonce; const getAssignByMessage = async (apiHost, msgId) => { const url = `${apiHost}/assignmentByMessage/${msgId}`; const result = await (0, exports.sendRequest)({ ...rConfig, url, method: 'GET' }); return result.data; }; exports.getAssignByMessage = getAssignByMessage; const getNodes = async (apiHost) => { const url = `${apiHost}/nodes`; const result = await (0, exports.sendRequest)({ ...rConfig, url, method: 'GET' }); return result.data; }; exports.getNodes = getNodes; const getNode = async (apiHost, accid) => { const url = `${apiHost}/node/${accid}`; const result = await (0, exports.sendRequest)({ ...rConfig, url, method: 'GET' }); return result.data; }; exports.getNode = getNode; const getNodesByProcess = async (apiHost, processId) => { const url = `${apiHost}/nodesByProcess/${processId}`; const result = await (0, exports.sendRequest)({ ...rConfig, url, method: 'GET' }); return result.data; }; exports.getNodesByProcess = getNodesByProcess; const getProcesses = async (apiHost, accid) => { const url = `${apiHost}/processes/${accid}`; const result = await (0, exports.sendRequest)({ ...rConfig, url, method: 'GET' }); return result.data; }; exports.getProcesses = getProcesses; const getBalanceOfByAccid = async (apiHost, accid) => { const url = `${apiHost}/balanceof/${accid}`; const result = await (0, exports.sendRequest)({ ...rConfig, url, method: 'GET' }); return (0, utils_1.toBN)(result.data).toString(); }; exports.getBalanceOfByAccid = getBalanceOfByAccid; const getStakeOfByAccid = async (apiHost, accid) => { const url = `${apiHost}/stakeof/${accid}`; const result = await (0, exports.sendRequest)({ ...rConfig, url, method: 'GET' }); return (0, utils_1.toBN)(result.data).toString(); }; exports.getStakeOfByAccid = getStakeOfByAccid; const send = async (apiHost, data) => { const url = `${apiHost}/`; const result = await (0, exports.sendRequest)({ ...rConfig, url, method: 'POST', data }); return result.data; }; exports.send = send;