@thzero/library_server 0.15.20 → 0.15.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.
package/boot/koa/index.js CHANGED
@@ -132,14 +132,21 @@ class KoaBootMain extends BootMain {
132
132
  await pluginRoute.initRoutes(this._routes);
133
133
 
134
134
  await this._initRoutes();
135
+
136
+ console.log();
135
137
 
136
138
  for (const route of this._routes) {
137
- await route.init(injector)
139
+ await route.init(injector, this._appConfig);
138
140
  app
139
141
  .use(route.router.routes())
140
142
  .use(route.router.allowedMethods());
141
- console.log(route.router.stack.map(i => i.path));
142
- console.log(route.router.stack.map(i => i.methods));
143
+
144
+ console.log([ route.id ]);
145
+
146
+ for (let i = 0; i < route.router.stack.length; i++)
147
+ console.log([ route.router.stack[i].path, route.router.stack[i].methods ]);
148
+
149
+ console.log();
143
150
  }
144
151
 
145
152
  return app;
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@thzero/library_server",
3
3
  "type": "module",
4
- "version": "0.15.20",
4
+ "version": "0.15.23",
5
5
  "version_major": 0,
6
6
  "version_minor": 15,
7
- "version_patch": 20,
8
- "version_date": "04/14/2022",
7
+ "version_patch": 23,
8
+ "version_date": "04/16/2022",
9
9
  "description": "An opinionated library of common functionality to bootstrap a Koa based API application using MongoDb and Firebase.",
10
10
  "author": "thZero",
11
11
  "license": "MIT",
@@ -12,7 +12,7 @@ class AdminBaseRoute extends BaseRoute {
12
12
  if (!urlFragment)
13
13
  throw Error('Invalid url fragment');
14
14
 
15
- super(`/api/admin/${urlFragment}`);
15
+ super(`/admin/${urlFragment}`);
16
16
 
17
17
  this._options = {
18
18
  role: role,
@@ -9,6 +9,14 @@ class NewsAdminRoute extends AdminRoute {
9
9
  serviceKey = serviceKey ? serviceKey : LibraryConstants.InjectorKeys.SERVICE_ADMIN_NEWS;
10
10
  super(urlFragment, role, serviceKey);
11
11
  }
12
+
13
+ get id() {
14
+ return 'admin-news';
15
+ }
16
+
17
+ get _version() {
18
+ return 'v1';
19
+ }
12
20
  }
13
21
 
14
22
  export default NewsAdminRoute;
@@ -10,9 +10,17 @@ class UsersAdminRoute extends AdminRoute {
10
10
  super(urlFragment, role, serviceKey);
11
11
  }
12
12
 
13
+ get id() {
14
+ return 'admin-users';
15
+ }
16
+
13
17
  _allowsCreate() {
14
18
  return false;
15
19
  }
20
+
21
+ get _version() {
22
+ return 'v1';
23
+ }
16
24
  }
17
25
 
18
26
  export default UsersAdminRoute;
@@ -7,8 +7,12 @@ import BaseRoute from './index';
7
7
  import authentication from '../middleware/authentication';
8
8
 
9
9
  class BaseNewsRoute extends BaseRoute {
10
- constructor(prefix) {
11
- super(prefix ? prefix : '/api/news');
10
+ constructor(prefix, version) {
11
+ super(prefix ? prefix : '/news');
12
+ }
13
+
14
+ get id() {
15
+ return 'news';
12
16
  }
13
17
 
14
18
  _initializeRoutes(router) {
@@ -10,8 +10,12 @@ import authentication from '../middleware/authentication';
10
10
  import authorization from '../middleware/authorization';
11
11
 
12
12
  class BaseUsersRoute extends BaseRoute {
13
- constructor(prefix) {
14
- super(prefix ? prefix : '/api/users');
13
+ constructor(prefix, version) {
14
+ super(prefix ? prefix : '/users');
15
+ }
16
+
17
+ get id() {
18
+ return 'users';
15
19
  }
16
20
 
17
21
  _initializeRoutes(router) {
package/routes/home.js CHANGED
@@ -2,7 +2,11 @@ import BaseRoute from './index';
2
2
 
3
3
  class HomeRoute extends BaseRoute {
4
4
  constructor(prefix) {
5
- super(prefix ? prefix : '/api');
5
+ super(prefix ? prefix : '');
6
+ }
7
+
8
+ get id() {
9
+ return 'home';
6
10
  }
7
11
 
8
12
  _initializeRoutes(router) {
@@ -11,6 +15,14 @@ class HomeRoute extends BaseRoute {
11
15
  ctx.status = 404;
12
16
  });
13
17
  }
18
+
19
+ get _ignoreApi() {
20
+ return true;
21
+ }
22
+
23
+ get _version() {
24
+ return '';
25
+ }
14
26
  }
15
27
 
16
28
  export default HomeRoute;
package/routes/index.js CHANGED
@@ -2,19 +2,52 @@ import koaRouter from '@koa/router';
2
2
 
3
3
  class BaseRoute {
4
4
  constructor(prefix) {
5
- if (!prefix)
5
+ if (prefix === null || prefix === undefined)
6
6
  throw Error('Invalid prefix');
7
7
 
8
8
  this._prefix = prefix;
9
9
  }
10
10
 
11
+ get id() {
12
+ return null;
13
+ }
14
+
11
15
  get router() {
12
16
  return this._router;
13
17
  }
14
18
 
15
- async init(injector) {
19
+ async init(injector, config) {
20
+ if (!injector)
21
+ throw Error('Invalid injector for route.');
22
+ if (!config)
23
+ throw Error('Invalid injector for route.');
24
+ if (this._prefix === null || this._prefix === undefined)
25
+ throw Error('Invalid prefix for route.');
26
+
16
27
  this._injector = injector;
17
28
 
29
+ const api = config.get('api', { });
30
+ let prefix = '';
31
+ if (!this._ignoreApi) {
32
+ prefix = api.prefix !== null && api.prefix !== undefined ? api.prefix : 'api';
33
+ if (!String.isNullOrEmpty(prefix) && (prefix !== '/'))
34
+ prefix = '/' + prefix;
35
+ }
36
+
37
+ let version = !String.isNullOrEmpty(api.version) ? api.version : null;
38
+ if (this._version !== null && this._version !== undefined)
39
+ version = this._version;
40
+ if (!String.isNullOrEmpty(this.id) && api.apis && Array.isArray(api.apis))
41
+ version = api.apis[this.id];
42
+
43
+ if (!String.isNullOrEmpty(version))
44
+ prefix += '/' + version;
45
+
46
+ if (!String.isNullOrEmpty(this._prefix) && (this._prefix[0] !== '/'))
47
+ this._prefix = '/' + this._prefix;
48
+
49
+ this._prefix = prefix + this._prefix;
50
+
18
51
  this._router = new koaRouter({
19
52
  prefix: this._prefix
20
53
  });
@@ -26,6 +59,14 @@ class BaseRoute {
26
59
 
27
60
  _initializeRoutes(router) {
28
61
  }
62
+
63
+ get _ignoreApi() {
64
+ return false;
65
+ }
66
+
67
+ get _version() {
68
+ return null;
69
+ }
29
70
  }
30
71
 
31
72
  export default BaseRoute;
package/routes/plans.js CHANGED
@@ -6,11 +6,15 @@ import BaseRoute from './index';
6
6
 
7
7
  class PlansRoute extends BaseRoute {
8
8
  constructor(prefix) {
9
- super(prefix ? prefix : '/api');
9
+ super(prefix ? prefix : '/plans');
10
+ }
11
+
12
+ get id() {
13
+ return 'plans';
10
14
  }
11
15
 
12
16
  _initializeRoutes(router) {
13
- router.get('/plans',
17
+ router.get('/',
14
18
  // eslint-disable-next-line
15
19
  async (ctx, next) => {
16
20
  const service = this._injector.getService(LibraryConstants.InjectorKeys.SERVICE_PLANS);
@@ -19,6 +23,10 @@ class PlansRoute extends BaseRoute {
19
23
  }
20
24
  );
21
25
  }
26
+
27
+ get _version() {
28
+ return 'v1';
29
+ }
22
30
  }
23
31
 
24
32
  export default PlansRoute;
package/routes/utility.js CHANGED
@@ -11,7 +11,11 @@ import authentication from '../middleware/authentication';
11
11
 
12
12
  class UtilityRoute extends BaseRoute {
13
13
  constructor(prefix) {
14
- super(prefix ? prefix : '/api/utility');
14
+ super(prefix ? prefix : '/utility');
15
+ }
16
+
17
+ get id() {
18
+ return 'utility';
15
19
  }
16
20
 
17
21
  _initializeRoutes(router) {
@@ -29,6 +33,10 @@ class UtilityRoute extends BaseRoute {
29
33
  }
30
34
  );
31
35
  }
36
+
37
+ get _version() {
38
+ return 'v1';
39
+ }
32
40
  }
33
41
 
34
42
  export default UtilityRoute;
package/routes/version.js CHANGED
@@ -6,7 +6,11 @@ import BaseRoute from './index';
6
6
 
7
7
  class VersionRoute extends BaseRoute {
8
8
  constructor(prefix) {
9
- super(prefix ? prefix : '/api');
9
+ super(prefix ? prefix : '');
10
+ }
11
+
12
+ get id() {
13
+ return 'version';
10
14
  }
11
15
 
12
16
  _initializeRoutes(router) {
@@ -19,6 +23,10 @@ class VersionRoute extends BaseRoute {
19
23
  }
20
24
  );
21
25
  }
26
+
27
+ get _version() {
28
+ return 'v1';
29
+ }
22
30
  }
23
31
 
24
32
  export default VersionRoute;