miolo 0.0.20 → 0.0.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -86,13 +86,17 @@ module.exports = {
86
86
  // }
87
87
  // ],
88
88
  // },
89
- // queries: [
90
- // {
91
- // path: '/crud/fo/bar',
92
- // method: 'GET',
93
- // callback: method_receiving_params_ctx_conn
94
- // }
95
- // ]
89
+ // queries: {
90
+ // options: {...},
91
+ // routes: [
92
+ // {
93
+ // path: '/crud/fo/bar',
94
+ // auth: {...},
95
+ // method: 'GET',
96
+ // callback: method_receiving_params_ctx_conn
97
+ // }
98
+ // ]
99
+ // }
96
100
  },
97
101
  cacher: {
98
102
  redis: {
@@ -9,16 +9,54 @@ var _router = _interopRequireDefault(require("@koa/router"));
9
9
 
10
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
11
 
12
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
13
+
14
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
15
+
16
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
17
+
12
18
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
13
19
 
14
20
  function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
15
21
 
22
+ function _check_auth(ctx, auth, method) {
23
+ var check = auth.require === true || auth.require === 'read-only' && method === 'POST';
24
+
25
+ if (check) {
26
+ var uid = undefined;
27
+
28
+ try {
29
+ uid = ctx.state.user.id;
30
+ } catch (e) {}
31
+
32
+ if (uid === undefined) {
33
+ if (auth.action == 'error') {
34
+ ctx.miolo.logger.error("Unauthorized access. Throwing error ".concat(auth.error_code || 401));
35
+ return ctx.throw(auth.error_code || 401, null, {});
36
+ } else {
37
+ ctx.miolo.logger.error("Unauthorized access. Redirecting to ".concat(auth.redirect_path || '/'));
38
+ ctx.redirect(auth.redirect_path || '/');
39
+ }
40
+ }
41
+ }
42
+ }
43
+
16
44
  function init_queries_router(_x, _x2, _x3) {
17
45
  return _init_queries_router.apply(this, arguments);
18
46
  }
19
47
 
20
48
  function _init_queries_router() {
21
- _init_queries_router = _asyncToGenerator(function* (app, conn, routes) {
49
+ _init_queries_router = _asyncToGenerator(function* (app, conn, queries) {
50
+ var routes = (queries === null || queries === void 0 ? void 0 : queries.routes) || [];
51
+
52
+ if (routes.length == 0) {
53
+ return;
54
+ }
55
+
56
+ var options = (queries === null || queries === void 0 ? void 0 : queries.options) || {};
57
+ var global_auth = (options === null || options === void 0 ? void 0 : options.auth) || {
58
+ require: false
59
+ };
22
60
  var router = new _router.default();
23
61
  routes.map(route => {
24
62
  function route_callback(_x4) {
@@ -27,6 +65,17 @@ function _init_queries_router() {
27
65
 
28
66
  function _route_callback() {
29
67
  _route_callback = _asyncToGenerator(function* (ctx) {
68
+ ctx.miolo.logger.debug("[queries] ".concat(route.path));
69
+
70
+ try {
71
+ var auth = _objectSpread(_objectSpread({}, global_auth), route.auth || {});
72
+
73
+ _check_auth(ctx, auth, route.method);
74
+ } catch (e) {
75
+ ctx.miolo.logger.error("[queries] error on ".concat(route.path));
76
+ ctx.miolo.logger.error(e);
77
+ }
78
+
30
79
  var result = yield route.callback(ctx, conn);
31
80
  return result;
32
81
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "miolo",
3
- "version": "0.0.20",
3
+ "version": "0.0.23",
4
4
  "description": "miolo",
5
5
  "main": "lib/index.js",
6
6
  "author": "Afialapis <info@afialapis.com>",
@@ -81,13 +81,17 @@ module.exports= {
81
81
  // }
82
82
  // ],
83
83
  // },
84
- // queries: [
85
- // {
86
- // path: '/crud/fo/bar',
87
- // method: 'GET',
88
- // callback: method_receiving_params_ctx_conn
89
- // }
90
- // ]
84
+ // queries: {
85
+ // options: {...},
86
+ // routes: [
87
+ // {
88
+ // path: '/crud/fo/bar',
89
+ // auth: {...},
90
+ // method: 'GET',
91
+ // callback: method_receiving_params_ctx_conn
92
+ // }
93
+ // ]
94
+ // }
91
95
  },
92
96
  cacher: {
93
97
  redis: {
@@ -1,12 +1,63 @@
1
1
  import Router from '@koa/router'
2
2
 
3
3
 
4
- async function init_queries_router (app, conn, routes) {
4
+ function _check_auth(ctx, auth, method) {
5
+ const check= (auth.require===true) || (auth.require==='read-only' && method==='POST')
6
+
7
+ if (check) {
8
+ let uid= undefined
9
+ try {
10
+ uid= ctx.state.user.id
11
+ } catch(e) {}
12
+
13
+ if (uid===undefined) {
14
+
15
+ if (auth.action=='error') {
16
+
17
+ ctx.miolo.logger.error(`Unauthorized access. Throwing error ${auth.error_code || 401}`)
18
+ return ctx.throw(
19
+ auth.error_code || 401,
20
+ null,
21
+ {}
22
+ )
23
+
24
+ } else {
25
+ ctx.miolo.logger.error(`Unauthorized access. Redirecting to ${auth.redirect_path || '/'}`)
26
+ ctx.redirect(auth.redirect_path || '/')
27
+ }
28
+ }
29
+ }
30
+ }
31
+
32
+
33
+ async function init_queries_router (app, conn, queries) {
34
+
35
+ const routes= queries?.routes || []
36
+ if (routes.length==0) {
37
+ return
38
+ }
39
+
40
+ const options= queries?.options || {}
41
+ const global_auth = options?.auth || {require: false}
42
+
5
43
  const router = new Router()
6
44
 
7
45
  routes.map(route => {
8
46
 
9
47
  async function route_callback(ctx) {
48
+
49
+ ctx.miolo.logger.debug(`[queries] ${route.path}`)
50
+ try {
51
+ const auth= {
52
+ ...global_auth,
53
+ ...route.auth || {}
54
+ }
55
+ _check_auth(ctx, auth, route.method)
56
+ } catch(e) {
57
+ ctx.miolo.logger.error(`[queries] error on ${route.path}`)
58
+ ctx.miolo.logger.error(e)
59
+ }
60
+
10
61
  const result= await route.callback(ctx, conn)
11
62
  return result
12
63
  }