UNPKG

@yhiot/utils

Version:
409 lines (352 loc) 11.8 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports.preGetModel = preGetModel; exports.preGetModels = preGetModels; exports.preCreateModel = preCreateModel; exports.preUpdateModel = preUpdateModel; exports.preRemoveModel = preRemoveModel; exports.postCRUDCreator = postCRUDCreator; var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); var _Errcode = _interopRequireWildcard(require("../Errcode")); var _dbcached = require("@yhiot/dbcached"); var _debug2 = _interopRequireDefault(require("debug")); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } var debug = (0, _debug2.default)('app:preCRUD'); /** * @api {GET} /apis/v1/mqtt/:model/:_id 通用获取单条记录 * @apiDescription model:`user,company,vehicle,device` * @apiName getModel * @apiGroup apiMqttV2 * @apiVersion 1.0.0 * @apiParam {String} model Mandatory modelname. * @apiParam {String} _id Mandatory * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK * { * errcode: 0, * result: { * "id": "5ba27cc3a70db45dd108b53f", * "status": 0, * ... * } * } * @apiErrorExample {json} Error-Response: * { * errcode: !=0, * message: "error message" * } */ function preGetModel(_x, _x2) { return _preGetModel.apply(this, arguments); } /** * @api {GET} /apis/v1/mqtt/:model 通用获取记录列表 * @apiDescription model:`user,company,vehicle,device` * @apiName getModals * @apiGroup apiMqttV2 * @apiVersion 1.0.0 * @apiParam {String} model Mandatory modelname. * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK * { * errcode: 0, * result: { * total: 102, // 总条数,中间有记录插入删除改变状态,可能导致每次查到总量不一致 * items: [ * { * id: "5bd863882da794797a73001e", * status: 0, * author: "5ba27cc3a70db45dd108b53f", * ... * }, * { * id: "5bd00a2222580a229225245e", * status: 0, * author: "5ba27cc3a70db45dd108b53f", * ... * } * ] * } // result * } * @apiErrorExample {json} Error-Response: * { * errcode: !=0, * message: "error message" * } */ function _preGetModel() { _preGetModel = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(ctx, next) { var qstr, options, _id; return _regenerator.default.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: qstr = ctx.params.qs || ctx.request.querystring; options = (0, _dbcached.memParse)(qstr); _id = ctx.params._id; if (_id) { _context2.next = 5; break; } throw new _Errcode.default('error! _id is null', _Errcode.EC.ERR_PARAM_ERROR); case 5: ctx.state._type = 'get'; ctx.state._id = _id; ctx.state._options = options; _context2.next = 10; return next(); case 10: case "end": return _context2.stop(); } } }, _callee2); })); return _preGetModel.apply(this, arguments); } function preGetModels(_x3, _x4) { return _preGetModels.apply(this, arguments); } /** * @api {POST} /apis/v1/mqtt/:model 通用创建一条记录 * @apiDescription model:`user,company,vehicle,device` * @apiName createModal * @apiGroup apiMqttV2 * @apiVersion 1.0.0 * @apiParam {String} model Mandatory modelname. * @apiParam {String} [_id] Option id. Mandatory at device/vehicle. * @apiParamExample {json} Request-Example: * { * status, * ..., * } * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK * { * errcode: 0, * result: { * "id": "5ba27cc3a70db45dd108b53f", * "status": 0, * ... * } * } * @apiErrorExample {json} Error-Response: * { * errcode: !=0, * message: "error message" * } */ function _preGetModels() { _preGetModels = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(ctx, next) { var qstr, options; return _regenerator.default.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: // /apis/v1/mongo/query/log/info.method=$regex-POST // /apis/v1/mongo/query/log/$or[0][info.method]=$regex-POST&$or[1][info.url]=$regex-api //let options = qs.parse(ctx.params.qs, { allowDots: true }); qstr = ctx.params.qs || ctx.request.querystring; if (qstr) { qstr = decodeURIComponent(qstr); } debug('getModels', qstr); if (qstr && qstr.startsWith('?')) { qstr = qstr.substr(1); } options = (0, _dbcached.memParse)(qstr); // debug('options:', options); ctx.state._type = 'retrieve'; ctx.state._options = options; _context3.next = 9; return next(); case 9: case "end": return _context3.stop(); } } }, _callee3); })); return _preGetModels.apply(this, arguments); } function preCreateModel(_x5, _x6) { return _preCreateModel.apply(this, arguments); } /** * @api {PUT} /apis/v1/mqtt/:model/:_id 通用更新一条记录 * @apiDescription model:`user,company,vehicle,device` * @apiName updateModal * @apiGroup apiMqttV2 * @apiVersion 1.0.0 * @apiParam {String} model Mandatory modelname. * @apiParam {String} _id Mandatory id. * @apiParamExample {json} Request-Example: * { * status, * ..., * } * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK * { * errcode: 0, * result: { * "id": "5ba27cc3a70db45dd108b53f", * "status": 0, * ... * } * } */ function _preCreateModel() { _preCreateModel = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee4(ctx, next) { var args; return _regenerator.default.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: args = ctx.request.body; if (args) { _context4.next = 3; break; } throw new _Errcode.default('error! args is null', _Errcode.EC.ERR_PARAM_ERROR); case 3: ctx.state._type = 'create'; ctx.state._args = args; _context4.next = 7; return next(); case 7: case "end": return _context4.stop(); } } }, _callee4); })); return _preCreateModel.apply(this, arguments); } function preUpdateModel(_x7, _x8) { return _preUpdateModel.apply(this, arguments); } /** * @api {DELETE} /apis/v1/mqtt/:model/:_id 通用删除一条记录 * @apiDescription model:`user,company,vehicle,device` * @apiName removeModal * @apiGroup apiMqttV2 * @apiVersion 1.0.0 * @apiParam {String} model Mandatory modelname. * @apiParam {String} _id Mandatory id. * @apiSuccessExample {json} Success-Response: * HTTP/1.1 200 OK * { * errcode: 0, * result: { * "id": "5ba27cc3a70db45dd108b53f", * "status": 0, * ... * } * } * @apiErrorExample {json} Error-Response: * { * errcode: !=0, * message: "error message" * } */ function _preUpdateModel() { _preUpdateModel = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee5(ctx, next) { var _id, args; return _regenerator.default.wrap(function _callee5$(_context5) { while (1) { switch (_context5.prev = _context5.next) { case 0: _id = ctx.params._id; args = ctx.request.body; if (_id) { _context5.next = 4; break; } throw new _Errcode.default('error! _id is null', _Errcode.EC.ERR_PARAM_ERROR); case 4: if (args) { _context5.next = 6; break; } throw new _Errcode.default('error! args is null', _Errcode.EC.ERR_PARAM_ERROR); case 6: ctx.state._type = 'update'; ctx.state._id = _id; ctx.state._args = args; _context5.next = 11; return next(); case 11: case "end": return _context5.stop(); } } }, _callee5); })); return _preUpdateModel.apply(this, arguments); } function preRemoveModel(_x9, _x10) { return _preRemoveModel.apply(this, arguments); } function _preRemoveModel() { _preRemoveModel = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee6(ctx, next) { var _id; return _regenerator.default.wrap(function _callee6$(_context6) { while (1) { switch (_context6.prev = _context6.next) { case 0: _id = ctx.params._id; if (_id) { _context6.next = 3; break; } throw new _Errcode.default('error! _id is null', _Errcode.EC.ERR_PARAM_ERROR); case 3: ctx.state._type = 'remove'; ctx.state._id = _id; _context6.next = 7; return next(); case 7: case "end": return _context6.stop(); } } }, _callee6); })); return _preRemoveModel.apply(this, arguments); } function postCRUDCreator(crudFunction) { return /*#__PURE__*/function () { var _crudResult = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(ctx) { var result; return _regenerator.default.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.next = 2; return crudFunction(ctx.state); case 2: result = _context.sent; // debug('crudResult2:', ctx.state, result); ctx.body = { errcode: 0, result: result }; case 4: case "end": return _context.stop(); } } }, _callee); })); function crudResult(_x11) { return _crudResult.apply(this, arguments); } return crudResult; }(); }