1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 | Object.defineProperty(exports, "__esModule", {
|
5 | value: true
|
6 | });
|
7 | exports["default"] = exports.Api = void 0;
|
8 | var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
9 | var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
10 | var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
11 | var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
12 | var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
13 | var _get3 = _interopRequireDefault(require("@babel/runtime/helpers/get"));
|
14 | var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
|
15 | var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
|
16 | var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
|
17 | var _env = require("@lskjs/env");
|
18 | var _err = _interopRequireDefault(require("@lskjs/err"));
|
19 | var _module = _interopRequireDefault(require("@lskjs/module"));
|
20 | var _get4 = _interopRequireDefault(require("lodash/get"));
|
21 | function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
|
22 | function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | var Api = function (_Module) {
|
36 | (0, _inherits2["default"])(Api, _Module);
|
37 | var _super = _createSuper(Api);
|
38 | function Api(parent) {
|
39 | var _this2;
|
40 | var _this;
|
41 | (0, _classCallCheck2["default"])(this, Api);
|
42 | for (var _len = arguments.length, propsArray = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
43 | propsArray[_key - 1] = arguments[_key];
|
44 | }
|
45 | _this = _super.call.apply(_super, [this, parent].concat(propsArray));
|
46 | _this.__api = true;
|
47 | (_this2 = _this).setProps.apply(_this2, propsArray.concat([{
|
48 | '__lifecycle.create': new Date(),
|
49 | __parent: parent,
|
50 | app: parent && parent.app,
|
51 | config: parent && parent.config
|
52 | }]));
|
53 |
|
54 |
|
55 |
|
56 | return _this;
|
57 | }
|
58 | (0, _createClass2["default"])(Api, [{
|
59 | key: "init",
|
60 | value: function () {
|
61 | var _init = (0, _asyncToGenerator2["default"])( _regenerator["default"].mark(function _callee() {
|
62 | var _get2;
|
63 | var _len2,
|
64 | propsArray,
|
65 | _key2,
|
66 | _args = arguments;
|
67 | return _regenerator["default"].wrap(function _callee$(_context) {
|
68 | while (1) switch (_context.prev = _context.next) {
|
69 | case 0:
|
70 | for (_len2 = _args.length, propsArray = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
71 | propsArray[_key2] = _args[_key2];
|
72 | }
|
73 | _context.next = 3;
|
74 | return (_get2 = (0, _get3["default"])((0, _getPrototypeOf2["default"])(Api.prototype), "init", this)).call.apply(_get2, [this].concat(propsArray));
|
75 | case 3:
|
76 | if (this.app) {
|
77 | _context.next = 5;
|
78 | break;
|
79 | }
|
80 | throw new _err["default"]("API_APP_MISSING', 'Please use new ".concat(this.name, "(this) instead new ").concat(this.name, "(this.app)"));
|
81 | case 5:
|
82 | if (this.__parent) {
|
83 | _context.next = 7;
|
84 | break;
|
85 | }
|
86 | throw new _err["default"]("API_PARENT_MISSING', 'Please use new ".concat(this.name, "(this) instead new ").concat(this.name, "(this.app)"));
|
87 | case 7:
|
88 | this.paths = [].concat((0, _toConsumableArray2["default"])(this.__parent.paths || []), [this.path]);
|
89 | case 8:
|
90 | case "end":
|
91 | return _context.stop();
|
92 | }
|
93 | }, _callee, this);
|
94 | }));
|
95 | function init() {
|
96 | return _init.apply(this, arguments);
|
97 | }
|
98 | return init;
|
99 | }()
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | }, {
|
105 | key: "checkAuth",
|
106 | value: function () {
|
107 | var _checkAuth = (0, _asyncToGenerator2["default"])( _regenerator["default"].mark(function _callee2(req) {
|
108 | return _regenerator["default"].wrap(function _callee2$(_context2) {
|
109 | while (1) switch (_context2.prev = _context2.next) {
|
110 | case 0:
|
111 | if (!req.__errJwt) {
|
112 | _context2.next = 2;
|
113 | break;
|
114 | }
|
115 | throw req.__errJwt;
|
116 | case 2:
|
117 | if ((0, _get4["default"])(req, 'user._id')) {
|
118 | _context2.next = 4;
|
119 | break;
|
120 | }
|
121 | throw new _err["default"]('auth.unauthorized', {
|
122 | status: 401
|
123 | });
|
124 | case 4:
|
125 | case "end":
|
126 | return _context2.stop();
|
127 | }
|
128 | }, _callee2);
|
129 | }));
|
130 | function checkAuth(_x) {
|
131 | return _checkAuth.apply(this, arguments);
|
132 | }
|
133 | return checkAuth;
|
134 | }()
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | }, {
|
141 | key: "checkAdmin",
|
142 | value: function () {
|
143 | var _checkAdmin = (0, _asyncToGenerator2["default"])( _regenerator["default"].mark(function _callee3(req) {
|
144 | var props,
|
145 | _args3 = arguments;
|
146 | return _regenerator["default"].wrap(function _callee3$(_context3) {
|
147 | while (1) switch (_context3.prev = _context3.next) {
|
148 | case 0:
|
149 | props = _args3.length > 1 && _args3[1] !== undefined ? _args3[1] : {};
|
150 | if (!(props.dev && _env.isDev)) {
|
151 | _context3.next = 3;
|
152 | break;
|
153 | }
|
154 | return _context3.abrupt("return");
|
155 | case 3:
|
156 | if (!req.__errJwt) {
|
157 | _context3.next = 5;
|
158 | break;
|
159 | }
|
160 | throw req.__errJwt;
|
161 | case 5:
|
162 | if (!((0, _get4["default"])(req, 'user.role') !== 'admin')) {
|
163 | _context3.next = 7;
|
164 | break;
|
165 | }
|
166 | throw new _err["default"]('auth.notadmin', {
|
167 | status: 403
|
168 | });
|
169 | case 7:
|
170 | case "end":
|
171 | return _context3.stop();
|
172 | }
|
173 | }, _callee3);
|
174 | }));
|
175 | function checkAdmin(_x2) {
|
176 | return _checkAdmin.apply(this, arguments);
|
177 | }
|
178 | return checkAdmin;
|
179 | }()
|
180 | }, {
|
181 | key: "url",
|
182 | value: function url(path, params) {
|
183 | return this.app.url((this.path || '/api') + path, params);
|
184 | }
|
185 | }, {
|
186 | key: "useMiddleware",
|
187 | value: function () {
|
188 | var _useMiddleware = (0, _asyncToGenerator2["default"])( _regenerator["default"].mark(function _callee5(middleware, req, res) {
|
189 | return _regenerator["default"].wrap(function _callee5$(_context5) {
|
190 | while (1) switch (_context5.prev = _context5.next) {
|
191 | case 0:
|
192 | return _context5.abrupt("return", new Promise(function (resolve, reject) {
|
193 | return middleware(req, res, function () {
|
194 | var _ref = (0, _asyncToGenerator2["default"])( _regenerator["default"].mark(function _callee4(err) {
|
195 | return _regenerator["default"].wrap(function _callee4$(_context4) {
|
196 | while (1) switch (_context4.prev = _context4.next) {
|
197 | case 0:
|
198 | if (!err) {
|
199 | _context4.next = 2;
|
200 | break;
|
201 | }
|
202 | return _context4.abrupt("return", reject(err));
|
203 | case 2:
|
204 | return _context4.abrupt("return", resolve());
|
205 | case 3:
|
206 | case "end":
|
207 | return _context4.stop();
|
208 | }
|
209 | }, _callee4);
|
210 | }));
|
211 | return function (_x6) {
|
212 | return _ref.apply(this, arguments);
|
213 | };
|
214 | }());
|
215 | }));
|
216 | case 1:
|
217 | case "end":
|
218 | return _context5.stop();
|
219 | }
|
220 | }, _callee5);
|
221 | }));
|
222 | function useMiddleware(_x3, _x4, _x5) {
|
223 | return _useMiddleware.apply(this, arguments);
|
224 | }
|
225 | return useMiddleware;
|
226 | }()
|
227 | }, {
|
228 | key: "__getRoutes",
|
229 | value: function () {
|
230 | var _getRoutes = (0, _asyncToGenerator2["default"])( _regenerator["default"].mark(function _callee6() {
|
231 | return _regenerator["default"].wrap(function _callee6$(_context6) {
|
232 | while (1) switch (_context6.prev = _context6.next) {
|
233 | case 0:
|
234 | if (this.__lifecycle.runStart) {
|
235 | _context6.next = 3;
|
236 | break;
|
237 | }
|
238 | _context6.next = 3;
|
239 | return this.__run();
|
240 | case 3:
|
241 | return _context6.abrupt("return", this.getRoutes());
|
242 | case 4:
|
243 | case "end":
|
244 | return _context6.stop();
|
245 | }
|
246 | }, _callee6, this);
|
247 | }));
|
248 | function __getRoutes() {
|
249 | return _getRoutes.apply(this, arguments);
|
250 | }
|
251 | return __getRoutes;
|
252 | }()
|
253 | }, {
|
254 | key: "getRoutes",
|
255 | value: function getRoutes() {
|
256 | return {
|
257 | '/': this.index.bind(this)
|
258 | };
|
259 | }
|
260 | }, {
|
261 | key: "index",
|
262 | value: function () {
|
263 | var _index = (0, _asyncToGenerator2["default"])( _regenerator["default"].mark(function _callee7() {
|
264 | return _regenerator["default"].wrap(function _callee7$(_context7) {
|
265 | while (1) switch (_context7.prev = _context7.next) {
|
266 | case 0:
|
267 | return _context7.abrupt("return", "".concat(this.name, " \u2013 ok"));
|
268 | case 1:
|
269 | case "end":
|
270 | return _context7.stop();
|
271 | }
|
272 | }, _callee7, this);
|
273 | }));
|
274 | function index() {
|
275 | return _index.apply(this, arguments);
|
276 | }
|
277 | return index;
|
278 | }()
|
279 | }, {
|
280 | key: "e404",
|
281 | value: function e404(req) {
|
282 | throw new _err["default"]('E_404', {
|
283 | status: 404,
|
284 | message: "Not found path ".concat(req.path),
|
285 | path: req.path,
|
286 | level: 'warn'
|
287 | });
|
288 | }
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 |
|
376 |
|
377 |
|
378 |
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 |
|
401 |
|
402 |
|
403 |
|
404 |
|
405 |
|
406 |
|
407 |
|
408 |
|
409 |
|
410 |
|
411 |
|
412 |
|
413 |
|
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 |
|
432 |
|
433 |
|
434 |
|
435 |
|
436 |
|
437 |
|
438 |
|
439 |
|
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 |
|
450 |
|
451 |
|
452 |
|
453 |
|
454 |
|
455 |
|
456 | }]);
|
457 | return Api;
|
458 | }(_module["default"]);
|
459 | exports.Api = Api;
|
460 | var _default = Api;
|
461 | exports["default"] = _default;
|
462 |
|
\ | No newline at end of file |