@hocgin/hkit
Version:
125 lines • 5.25 kB
JavaScript
var _class;
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import memoizeOne from "memoize-one";
export var RabbitKit = /*#__PURE__*/_createClass(function RabbitKit() {
_classCallCheck(this, RabbitKit);
});
_class = RabbitKit;
/**
* result => fail, 响应是否失败
* @param result
*/
_defineProperty(RabbitKit, "isFail", function (result) {
return !_class.isSuccess(result);
});
/**
* result => success, 响应是否成功
* @param result
*/
_defineProperty(RabbitKit, "isSuccess", function (result) {
var _result$success;
return (_result$success = result === null || result === void 0 ? void 0 : result.success) !== null && _result$success !== void 0 ? _result$success : false;
});
/**
* result => data, 获取数据
* @param result
*/
_defineProperty(RabbitKit, "thenData", function (result) {
return result === null || result === void 0 ? void 0 : result.data;
});
/**
* result => result, 异常的方式处理错误信息
* @param result
*/
_defineProperty(RabbitKit, "thenTryErrorIfExits", function (result) {
if (_class.isSuccess(result)) {
return result;
}
if (result !== null && result !== void 0 && result.message) {
throw new Error("".concat(result === null || result === void 0 ? void 0 : result.message));
}
return result;
});
/**
* result => data, 异常的方式,提取数据
* 适用组件: 常用 .then(thenDataTryErrorIfExits).
* @param result
*/
_defineProperty(RabbitKit, "thenDataTryErrorIfExits", function (result) {
var _class$thenTryErrorIf;
return (_class$thenTryErrorIf = _class.thenTryErrorIfExits(result)) === null || _class$thenTryErrorIf === void 0 ? void 0 : _class$thenTryErrorIf.data;
});
/**
* result=>[] 获取分页对象数组
* @param result
*/
_defineProperty(RabbitKit, "getPagingListForResult", function (result) {
if (!_class.isSuccess(result)) {
return [];
}
return _class.getPagingList((result === null || result === void 0 ? void 0 : result.data) || {});
});
/**
* data=>[], 获取 paging 对象的列表数据
* @param paging
* @return {*|*[]}
*/
_defineProperty(RabbitKit, "getPagingList", memoizeOne(function (data) {
return (data === null || data === void 0 ? void 0 : data.records) || [];
}));
/**
* data=> {label, value}, 转换为 Option
* 适用组件: <Option/>
* @param data
*/
_defineProperty(RabbitKit, "thenOptions", function () {
var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
return data.map(function (_ref) {
var key = _ref.key,
value = _ref.value;
return {
label: key,
value: value
};
});
});
/**
* data=>{total,pageSize,current,pages},获取分页的设置对象
* @param paging
* @return {{}}
*/
_defineProperty(RabbitKit, "getPagingPagination", memoizeOne(function (data) {
if (!data) {
return {};
}
return {
total: data.total,
pageSize: data.size,
current: data.current,
pages: data.pages
};
}));
/**
* data => table{list, pagination}, 获取表单分页数据结构
* 适用组件: <Table/>
* @param data
*/
_defineProperty(RabbitKit, "getScrollData", function (data) {
return {
list: (data === null || data === void 0 ? void 0 : data.records) || [],
hasMore: data === null || data === void 0 ? void 0 : data.hasMore,
nextId: data === null || data === void 0 ? void 0 : data.nextId
};
});
_defineProperty(RabbitKit, "getTableData", memoizeOne(function (data) {
return {
list: _class.getPagingList(data),
pagination: _class.getPagingPagination(data)
};
}));