hyjs-test
Version:
first hymatrix-js sdk
177 lines (176 loc) • 5.02 kB
JavaScript
import axios from 'axios';
import isObject from 'lodash/isObject';
import isString from 'lodash/isString';
import { stringify as qsStringify } from 'query-string';
import { toBN } from '../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'
}
};
export const sendRequest = async (config) => {
return await new Promise((resolve, reject) => {
axios({
...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 (isString(error)) {
reject(new Error(error));
}
else if (isObject(error.response) && isObject(error.response.data)) {
// like { error: 'err_invalid_signature' }
reject(new Error(error.response.data.error));
}
else {
reject(new Error(error));
}
});
});
};
export const getInfo = async (apiHost) => {
const url = `${apiHost}/info`;
const result = await sendRequest({
...rConfig,
url,
method: 'GET'
});
return result.data;
};
export const getResult = async (apiHost, msgId) => {
const url = `${apiHost}/result/${msgId}`;
const result = await sendRequest({
...rConfig,
url,
method: 'GET'
});
return result.data;
};
export const getResults = async (apiHost, processId, limit) => {
const queryStr = qsStringify({
sort: 'DESC',
limit
});
const url = `${apiHost}/results/${processId}${queryStr !== '' ? `?${queryStr}` : ''}`;
const result = await sendRequest({
...rConfig,
url,
method: 'GET'
});
return result.data;
};
export const getMessage = async (apiHost, msgId) => {
const url = `${apiHost}/message/${msgId}`;
const result = await sendRequest({
...rConfig,
url,
method: 'GET'
});
return result.data;
};
export const getMessageByNonce = async (apiHost, processId, nonce) => {
const url = `${apiHost}/messageByNonce/${processId}/${nonce}`;
const result = await sendRequest({
...rConfig,
url,
method: 'GET'
});
return result.data;
};
export const getAssignByNonce = async (apiHost, processId, nonce) => {
const url = `${apiHost}/assignmentByNonce/${processId}/${nonce}`;
const result = await sendRequest({
...rConfig,
url,
method: 'GET'
});
return result.data;
};
export const getAssignByMessage = async (apiHost, msgId) => {
const url = `${apiHost}/assignmentByMessage/${msgId}`;
const result = await sendRequest({
...rConfig,
url,
method: 'GET'
});
return result.data;
};
export const getNodes = async (apiHost) => {
const url = `${apiHost}/nodes`;
const result = await sendRequest({
...rConfig,
url,
method: 'GET'
});
return result.data;
};
export const getNode = async (apiHost, accid) => {
const url = `${apiHost}/node/${accid}`;
const result = await sendRequest({
...rConfig,
url,
method: 'GET'
});
return result.data;
};
export const getNodesByProcess = async (apiHost, processId) => {
const url = `${apiHost}/nodesByProcess/${processId}`;
const result = await sendRequest({
...rConfig,
url,
method: 'GET'
});
return result.data;
};
export const getProcesses = async (apiHost, accid) => {
const url = `${apiHost}/processes/${accid}`;
const result = await sendRequest({
...rConfig,
url,
method: 'GET'
});
return result.data;
};
export const getBalanceOfByAccid = async (apiHost, accid) => {
const url = `${apiHost}/balanceof/${accid}`;
const result = await sendRequest({
...rConfig,
url,
method: 'GET'
});
return toBN(result.data).toString();
};
export const getStakeOfByAccid = async (apiHost, accid) => {
const url = `${apiHost}/stakeof/${accid}`;
const result = await sendRequest({
...rConfig,
url,
method: 'GET'
});
return toBN(result.data).toString();
};
export const send = async (apiHost, data) => {
const url = `${apiHost}/`;
const result = await sendRequest({
...rConfig,
url,
method: 'POST',
data
});
return result.data;
};