@thzero/library_server 0.15.9 → 0.15.17

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.
Files changed (66) hide show
  1. package/README.md +89 -89
  2. package/boot/index.js +387 -384
  3. package/boot/koa/index.js +157 -157
  4. package/boot/plugins/admin/index.js +6 -6
  5. package/boot/plugins/admin/news.js +35 -35
  6. package/boot/plugins/admin/users.js +35 -35
  7. package/boot/plugins/api.js +58 -58
  8. package/boot/plugins/apiFront.js +31 -31
  9. package/boot/plugins/index.js +71 -71
  10. package/boot/plugins/news.js +46 -46
  11. package/boot/plugins/users.js +48 -48
  12. package/boot/plugins/usersExtended.js +32 -32
  13. package/constants.js +45 -45
  14. package/data/baseNews.js +42 -42
  15. package/data/baseSettingsUser.js +9 -9
  16. package/data/baseUser.js +28 -28
  17. package/data/index.js +24 -24
  18. package/data/named.js +20 -20
  19. package/errors/tokenExpired.js +7 -7
  20. package/license.md +8 -8
  21. package/middleware/authentication.js +67 -67
  22. package/middleware/authorization.js +180 -180
  23. package/package.json +50 -60
  24. package/repository/index.js +151 -151
  25. package/repository/usageMetrics/devnull.js +11 -11
  26. package/routes/admin/index.js +111 -111
  27. package/routes/admin/news.js +14 -14
  28. package/routes/admin/users.js +18 -18
  29. package/routes/baseNews.js +27 -27
  30. package/routes/baseUsers.js +106 -106
  31. package/routes/home.js +16 -16
  32. package/routes/index.js +31 -31
  33. package/routes/news.js +6 -6
  34. package/routes/plans.js +24 -24
  35. package/routes/users.js +6 -6
  36. package/routes/utility.js +34 -34
  37. package/routes/version.js +24 -24
  38. package/service/admin/baseNews.js +45 -45
  39. package/service/admin/index.js +130 -130
  40. package/service/admin/news.js +6 -6
  41. package/service/admin/users.js +107 -107
  42. package/service/baseSecurity.js +44 -44
  43. package/service/baseUser.js +122 -122
  44. package/service/communication.js +6 -6
  45. package/service/config.js +32 -32
  46. package/service/crypto.js +16 -16
  47. package/service/discovery/index.js +6 -6
  48. package/service/discovery/resources/index.js +101 -101
  49. package/service/external.js +19 -19
  50. package/service/externalRest.js +19 -19
  51. package/service/index.js +20 -20
  52. package/service/monitoring.js +13 -0
  53. package/service/news/base.js +49 -49
  54. package/service/news/index.js +6 -6
  55. package/service/news/validation/index.js +6 -6
  56. package/service/plans.js +27 -27
  57. package/service/restCommunication.js +21 -21
  58. package/service/usageMetrics.js +63 -63
  59. package/service/utility.js +37 -37
  60. package/service/version.js +32 -32
  61. package/utility/injector.js +59 -59
  62. package/utility/internalIp/index.js +48 -48
  63. package/utility/list/doubleLinked.js +88 -88
  64. package/utility/list/priorityQueue.js +109 -109
  65. package/utility/list/queue.js +72 -72
  66. package/utility/os.js +22 -22
package/package.json CHANGED
@@ -1,61 +1,51 @@
1
- {
2
- "name": "@thzero/library_server",
3
- "type": "module",
4
- "version": "0.15.9",
5
- "version_major": 0,
6
- "version_minor": 15,
7
- "version_patch": 9,
8
- "version_date": "04/07/2022",
9
- "description": "An opinionated library of common functionality to bootstrap a Koa based API application using MongoDb and Firebase.",
10
- "author": "thZero",
11
- "license": "MIT",
12
- "repository": {
13
- "type": "git",
14
- "url": "git+https://github.com/thzero/library_server.git"
15
- },
16
- "bugs": {
17
- "url": "https://github.com/thzero/library_server/issues"
18
- },
19
- "homepage": "https://github.com/thzero/library_server#readme",
20
- "engines": {
21
- "node": ">=12.8.3"
22
- },
23
- "scripts": {
24
- "cli-update": "./node_modules/.bin/library-cli --updateversion --pi",
25
- "cli-update-w": ".\\node_modules\\.bin\\library-cli --updateversion --pi",
26
- "test": "echo \"Error: no test specified\" && exit 1"
27
- },
28
- "dependencies": {
29
- "@godaddy/terminus": "^4.10.2",
30
- "@koa/cors": "^3.3.0",
31
- "@koa/router": "^10.1.1",
32
- "async-mutex": "^0.3.2",
33
- "config": "^3.3.7",
34
- "dayjs": "^1.11.0",
35
- "dayjs-plugin-utc": "^0.1.2",
36
- "default-gateway": "^6.0.3",
37
- "easy-rbac": "^3.2.0",
38
- "esm": "^3.2.25",
39
- "ipaddr.js": "^2.0.1",
40
- "koa": "^2.13.4",
41
- "koa-body": "^4.2.0",
42
- "koa-helmet": "^6.1.0",
43
- "koa-static": "^5.0.0"
44
- },
45
- "devDependencies": {
46
- "@thzero/library_cli": "^0.15"
47
- },
48
- "peerDependencies": {
49
- "@thzero/library_common": "^0.15",
50
- "@thzero/library_common_service": "^0.15",
51
- "@babel/core": "^7.16.7",
52
- "@babel/plugin-proposal-class-properties": "^7.16.7",
53
- "@babel/preset-env": "^7.16.7",
54
- "babel-loader": "^8.2.3",
55
- "eslint": "^8.5.0",
56
- "eslint-plugin-node": "^11.1.0",
57
- "pino-pretty": "^7.3.0",
58
- "webpack": "^5.65.0",
59
- "webpack-cli": "^4.9.1"
60
- }
1
+ {
2
+ "name": "@thzero/library_server",
3
+ "type": "module",
4
+ "version": "0.15.17",
5
+ "version_major": 0,
6
+ "version_minor": 15,
7
+ "version_patch": 17,
8
+ "version_date": "04/14/2022",
9
+ "description": "An opinionated library of common functionality to bootstrap a Koa based API application using MongoDb and Firebase.",
10
+ "author": "thZero",
11
+ "license": "MIT",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/thzero/library_server.git"
15
+ },
16
+ "bugs": {
17
+ "url": "https://github.com/thzero/library_server/issues"
18
+ },
19
+ "homepage": "https://github.com/thzero/library_server#readme",
20
+ "engines": {
21
+ "node": ">=12.8.3"
22
+ },
23
+ "scripts": {
24
+ "cli-update": "./node_modules/.bin/library-cli --updateversion --pi",
25
+ "cli-update-w": ".\\node_modules\\.bin\\library-cli --updateversion --pi",
26
+ "test": "echo \"Error: no test specified\" && exit 1"
27
+ },
28
+ "dependencies": {
29
+ "@godaddy/terminus": "^4.10.2",
30
+ "@koa/cors": "^3.3.0",
31
+ "@koa/router": "^10.1.1",
32
+ "async-mutex": "^0.3.2",
33
+ "config": "^3.3.7",
34
+ "dayjs": "^1.11.0",
35
+ "dayjs-plugin-utc": "^0.1.2",
36
+ "default-gateway": "^6.0.3",
37
+ "easy-rbac": "^3.2.0",
38
+ "ipaddr.js": "^2.0.1",
39
+ "koa": "^2.13.4",
40
+ "koa-body": "^4.2.0",
41
+ "koa-helmet": "^6.1.0",
42
+ "koa-static": "^5.0.0"
43
+ },
44
+ "devDependencies": {
45
+ "@thzero/library_cli": "^0.15"
46
+ },
47
+ "peerDependencies": {
48
+ "@thzero/library_common": "^0.15",
49
+ "@thzero/library_common_service": "^0.15"
50
+ }
61
51
  }
@@ -1,151 +1,151 @@
1
- import LibraryCommonServiceConstants from '@thzero/library_common_service/constants';
2
-
3
- import Response from '@thzero/library_common/response';
4
- import ExtractResponse from '@thzero/library_common/response/extract';
5
-
6
- class Repository {
7
- async init(injector) {
8
- this._injector = injector;
9
- }
10
-
11
- get _config() {
12
- return this._injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_CONFIG)
13
- }
14
-
15
- _enforce(clazz, method, value, name, correlationId) {
16
- if (!value) {
17
- this._logger.error(clazz, method, `Invalid ${name}`, null, correlationId);
18
- const error = Error(`Invalid ${name}`, true);
19
- error.correlationId = correlationId;
20
- throw error;
21
- }
22
- }
23
-
24
- _enforceNotEmpty(clazz, method, value, name, correlationId) {
25
- if (String.isNullOrEmpty(value)) {
26
- this._logger.error(clazz, method, `Invalid ${name}`, null, correlationId);
27
- const error = Error(`Invalid ${name}`, true);
28
- error.correlationId = correlationId;
29
- throw error;
30
- }
31
- }
32
-
33
- _enforceNotNull(clazz, method, value, name, correlationId) {
34
- if (!value) {
35
- this._logger.error(clazz, method, `Invalid ${name}`, null, correlationId);
36
- const error = Error(`Invalid ${name}`, true);
37
- error.correlationId = correlationId;
38
- throw error;
39
- }
40
- }
41
-
42
- _enforce(clazz, method, value, name, correlationId) {
43
- if (!value) {
44
- this._logger.error(clazz, method, `Invalid ${name}`, null, correlationId);
45
- return Response.error(clazz, method, `Invalid ${name}`, null, null, null, correlationId);
46
- }
47
-
48
- return this._success(correlationId);
49
- }
50
-
51
- _enforceNotEmptyResponse(clazz, method, value, name, correlationId) {
52
- if (String.isNullOrEmpty(value)) {
53
- this._logger.error(clazz, method, `Invalid ${name}`, null, correlationId);
54
- return Response.error(clazz, method, `Invalid ${name}`, null, null, null, correlationId);
55
- }
56
-
57
- return this._success(correlationId);
58
- }
59
-
60
- _enforceNotNullResponse(clazz, method, value, name, correlationId) {
61
- if (!value) {
62
- this._logger.error(clazz, method, `Invalid ${name}`, null, correlationId);
63
- return Response.error(clazz, method, `Invalid ${name}`, null, null, null, correlationId);
64
- }
65
-
66
- return this._success(correlationId);
67
- }
68
-
69
- _enforceNotEmptyAsResponse(clazz, method, value, name, correlationId) {
70
- if (String.isNullOrEmpty(value)) {
71
- this._logger.error(clazz, method, `Invalid ${name}`, null, correlationId);
72
- return Response.error(clazz, method, `Invalid ${name}`, null, null, null, correlationId);
73
- }
74
-
75
- return this._successResponse(null, correlationId);
76
- }
77
-
78
- _enforceNotNullAsResponse(clazz, method, value, name, correlationId) {
79
- if (!value) {
80
- this._logger.error(clazz, method, `Invalid ${name}`, null, correlationId);
81
- return Response.error(clazz, method, `Invalid ${name}`, null, null, null, correlationId);
82
- }
83
-
84
- return this._successResponse(null, correlationId);
85
- }
86
-
87
- _enforceResponse(clazz, method, response, name, correlationId) {
88
- if (!response || (response && !response.success)) {
89
- this._logger.error(clazz, method, `Invalid ${name}`, null, correlationId);
90
- const error = Error(`Unsuccessful response for ${name}`, true);
91
- error.correlationId = correlationId;
92
- throw error;
93
- }
94
- }
95
-
96
- _error(clazz, method, message, err, code, errors, correlationId) {
97
- if (message)
98
- this._logger.error(clazz, method, message, null, correlationId);
99
- if (err)
100
- this._logger.exception(clazz, method, err, correlationId);
101
- if (code)
102
- this._logger.error(clazz, method, 'code', code, correlationId);
103
- if (errors) {
104
- for (const error of errors)
105
- this._logger.exception(clazz, method, error, correlationId);
106
- }
107
- return Response.error(clazz, method, message, err, code, errors, correlationId);
108
- }
109
-
110
- _hasFailed(response) {
111
- return Response.hasFailed(response);
112
- }
113
-
114
- _hasSucceeded(response) {
115
- return Response.hasSucceeded(response);
116
- }
117
-
118
- _initResponse(correlationId) {
119
- return new Response(correlationId);
120
- }
121
-
122
- _initResponseExtract(correlationId) {
123
- return new ExtractResponse(correlationId);
124
- }
125
-
126
- get _logger() {
127
- return this._injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER)
128
- }
129
-
130
- _success(correlationId) {
131
- return Response.success(correlationId);
132
- }
133
-
134
- _successResponse(value, correlationId) {
135
- return Response.success(correlationId, value);
136
- }
137
-
138
- _warn(clazz, method, message, err, code, errors, correlationId) {
139
- if (message)
140
- this._logger.warn(clazz, method, message, null, correlationId);
141
- if (err)
142
- this._logger.warn(clazz, method, err.message, null, correlationId);
143
- if (code)
144
- this._logger.warn(clazz, method, 'code', code, correlationId);
145
- if (errors)
146
- this._logger.warn(clazz, method, null, errors, correlationId);
147
- return Response.error(clazz, method, message, err, code, errors, correlationId);
148
- }
149
- }
150
-
151
- export default Repository;
1
+ import LibraryCommonServiceConstants from '@thzero/library_common_service/constants';
2
+
3
+ import Response from '@thzero/library_common/response';
4
+ import ExtractResponse from '@thzero/library_common/response/extract';
5
+
6
+ class Repository {
7
+ async init(injector) {
8
+ this._injector = injector;
9
+ }
10
+
11
+ get _config() {
12
+ return this._injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_CONFIG)
13
+ }
14
+
15
+ _enforce(clazz, method, value, name, correlationId) {
16
+ if (!value) {
17
+ this._logger.error(clazz, method, `Invalid ${name}`, null, correlationId);
18
+ const error = Error(`Invalid ${name}`, true);
19
+ error.correlationId = correlationId;
20
+ throw error;
21
+ }
22
+ }
23
+
24
+ _enforceNotEmpty(clazz, method, value, name, correlationId) {
25
+ if (String.isNullOrEmpty(value)) {
26
+ this._logger.error(clazz, method, `Invalid ${name}`, null, correlationId);
27
+ const error = Error(`Invalid ${name}`, true);
28
+ error.correlationId = correlationId;
29
+ throw error;
30
+ }
31
+ }
32
+
33
+ _enforceNotNull(clazz, method, value, name, correlationId) {
34
+ if (!value) {
35
+ this._logger.error(clazz, method, `Invalid ${name}`, null, correlationId);
36
+ const error = Error(`Invalid ${name}`, true);
37
+ error.correlationId = correlationId;
38
+ throw error;
39
+ }
40
+ }
41
+
42
+ _enforce(clazz, method, value, name, correlationId) {
43
+ if (!value) {
44
+ this._logger.error(clazz, method, `Invalid ${name}`, null, correlationId);
45
+ return Response.error(clazz, method, `Invalid ${name}`, null, null, null, correlationId);
46
+ }
47
+
48
+ return this._success(correlationId);
49
+ }
50
+
51
+ _enforceNotEmptyResponse(clazz, method, value, name, correlationId) {
52
+ if (String.isNullOrEmpty(value)) {
53
+ this._logger.error(clazz, method, `Invalid ${name}`, null, correlationId);
54
+ return Response.error(clazz, method, `Invalid ${name}`, null, null, null, correlationId);
55
+ }
56
+
57
+ return this._success(correlationId);
58
+ }
59
+
60
+ _enforceNotNullResponse(clazz, method, value, name, correlationId) {
61
+ if (!value) {
62
+ this._logger.error(clazz, method, `Invalid ${name}`, null, correlationId);
63
+ return Response.error(clazz, method, `Invalid ${name}`, null, null, null, correlationId);
64
+ }
65
+
66
+ return this._success(correlationId);
67
+ }
68
+
69
+ _enforceNotEmptyAsResponse(clazz, method, value, name, correlationId) {
70
+ if (String.isNullOrEmpty(value)) {
71
+ this._logger.error(clazz, method, `Invalid ${name}`, null, correlationId);
72
+ return Response.error(clazz, method, `Invalid ${name}`, null, null, null, correlationId);
73
+ }
74
+
75
+ return this._successResponse(null, correlationId);
76
+ }
77
+
78
+ _enforceNotNullAsResponse(clazz, method, value, name, correlationId) {
79
+ if (!value) {
80
+ this._logger.error(clazz, method, `Invalid ${name}`, null, correlationId);
81
+ return Response.error(clazz, method, `Invalid ${name}`, null, null, null, correlationId);
82
+ }
83
+
84
+ return this._successResponse(null, correlationId);
85
+ }
86
+
87
+ _enforceResponse(clazz, method, response, name, correlationId) {
88
+ if (!response || (response && !response.success)) {
89
+ this._logger.error(clazz, method, `Invalid ${name}`, null, correlationId);
90
+ const error = Error(`Unsuccessful response for ${name}`, true);
91
+ error.correlationId = correlationId;
92
+ throw error;
93
+ }
94
+ }
95
+
96
+ _error(clazz, method, message, err, code, errors, correlationId) {
97
+ if (message)
98
+ this._logger.error(clazz, method, message, null, correlationId);
99
+ if (err)
100
+ this._logger.exception(clazz, method, err, correlationId);
101
+ if (code)
102
+ this._logger.error(clazz, method, 'code', code, correlationId);
103
+ if (errors) {
104
+ for (const error of errors)
105
+ this._logger.exception(clazz, method, error, correlationId);
106
+ }
107
+ return Response.error(clazz, method, message, err, code, errors, correlationId);
108
+ }
109
+
110
+ _hasFailed(response) {
111
+ return Response.hasFailed(response);
112
+ }
113
+
114
+ _hasSucceeded(response) {
115
+ return Response.hasSucceeded(response);
116
+ }
117
+
118
+ _initResponse(correlationId) {
119
+ return new Response(correlationId);
120
+ }
121
+
122
+ _initResponseExtract(correlationId) {
123
+ return new ExtractResponse(correlationId);
124
+ }
125
+
126
+ get _logger() {
127
+ return this._injector.getService(LibraryCommonServiceConstants.InjectorKeys.SERVICE_LOGGER)
128
+ }
129
+
130
+ _success(correlationId) {
131
+ return Response.success(correlationId);
132
+ }
133
+
134
+ _successResponse(value, correlationId) {
135
+ return Response.success(correlationId, value);
136
+ }
137
+
138
+ _warn(clazz, method, message, err, code, errors, correlationId) {
139
+ if (message)
140
+ this._logger.warn(clazz, method, message, null, correlationId);
141
+ if (err)
142
+ this._logger.warn(clazz, method, err.message, null, correlationId);
143
+ if (code)
144
+ this._logger.warn(clazz, method, 'code', code, correlationId);
145
+ if (errors)
146
+ this._logger.warn(clazz, method, null, errors, correlationId);
147
+ return Response.error(clazz, method, message, err, code, errors, correlationId);
148
+ }
149
+ }
150
+
151
+ export default Repository;
@@ -1,11 +1,11 @@
1
- import Repository from '../index';
2
-
3
- class DevNullUsageMetricsRepository extends Repository {
4
- async register(usageMetrics) {
5
- const correlationId = usageMetrics ? usageMetrics.correlationId : null;
6
- // Do nothing here.
7
- return this._success(correlationId);
8
- }
9
- }
10
-
11
- export default DevNullUsageMetricsRepository;
1
+ import Repository from '../index';
2
+
3
+ class DevNullUsageMetricsRepository extends Repository {
4
+ async register(usageMetrics) {
5
+ const correlationId = usageMetrics ? usageMetrics.correlationId : null;
6
+ // Do nothing here.
7
+ return this._success(correlationId);
8
+ }
9
+ }
10
+
11
+ export default DevNullUsageMetricsRepository;
@@ -1,111 +1,111 @@
1
- import koaBody from 'koa-body';
2
-
3
- import Utility from '@thzero/library_common/utility';
4
-
5
- import BaseRoute from '../index';
6
-
7
- import authentication from '../../middleware/authentication';
8
- import authorization from '../../middleware/authorization';
9
-
10
- class AdminBaseRoute extends BaseRoute {
11
- constructor(urlFragment, role, serviceKey) {
12
- if (!urlFragment)
13
- throw Error('Invalid url fragment');
14
-
15
- super(`/api/admin/${urlFragment}`);
16
-
17
- this._options = {
18
- role: role,
19
- serviceKey: serviceKey
20
- }
21
- }
22
-
23
- _allowsCreate() {
24
- return true;
25
- }
26
-
27
- _allowsDelete() {
28
- return true;
29
- }
30
-
31
- _allowsUpdate() {
32
- return true;
33
- }
34
-
35
- _initializeRoutesCreate(router) {
36
- const self = this;
37
- router.post('/',
38
- authentication(true),
39
- authorization([ `${self._options.role}.create` ]),
40
- koaBody({
41
- text: false,
42
- }),
43
- // eslint-disable-next-line
44
- async (ctx, next) => {
45
- const service = this._injector.getService(self._options.serviceKey);
46
- const response = (await service.create(ctx.correlationId, ctx.state.user, ctx.request.body)).check(ctx);
47
- ctx.body = Utility.stringify(response);
48
- }
49
- );
50
- }
51
-
52
- _initializeRoutesDelete(router) {
53
- const self = this;
54
- router.delete('/:id',
55
- authentication(true),
56
- authorization([ `${self._options.role}.delete` ]),
57
- // eslint-disable-next-line
58
- async (ctx, next) => {
59
- const service = this._injector.getService(self._options.serviceKey);
60
- const response = (await service.delete(ctx.correlationId, ctx.state.user, ctx.params.id)).check(ctx);
61
- ctx.body = Utility.stringify(response);
62
- }
63
- );
64
- }
65
-
66
- _initializeRoutesUpdate(router) {
67
- const self = this;
68
- router.post('/:id',
69
- authentication(true),
70
- authorization([ `${self._options.role}.update` ]),
71
- koaBody({
72
- text: false,
73
- }),
74
- // eslint-disable-next-line
75
- async (ctx, next) => {
76
- const service = this._injector.getService(self._options.serviceKey);
77
- const response = (await service.update(ctx.correlationId, ctx.state.user, ctx.params.id, ctx.request.body)).check(ctx);
78
- ctx.body = Utility.stringify(response);
79
- }
80
- );
81
- }
82
-
83
- _initializeRoutes(router) {
84
- if (this._allowsDelete)
85
- this._initializeRoutesDelete(router);
86
-
87
- router.post('/search',
88
- authentication(true),
89
- authorization([ `${this._options.role}.search` ]),
90
- koaBody({
91
- text: false,
92
- }),
93
- // eslint-disable-next-line
94
- async (ctx, next) => {
95
- const service = this._injector.getService(this._options.serviceKey);
96
- const response = (await service.search(ctx.correlationId, ctx.state.user, ctx.request.body)).check(ctx);
97
- ctx.body = Utility.stringify(response);
98
- }
99
- );
100
-
101
- if (this._allowsUpdate())
102
- this._initializeRoutesUpdate(router);
103
-
104
- if (this._allowsCreate())
105
- this._initializeRoutesCreate(router);
106
-
107
- return router;
108
- }
109
- }
110
-
111
- export default AdminBaseRoute;
1
+ import koaBody from 'koa-body';
2
+
3
+ import Utility from '@thzero/library_common/utility';
4
+
5
+ import BaseRoute from '../index';
6
+
7
+ import authentication from '../../middleware/authentication';
8
+ import authorization from '../../middleware/authorization';
9
+
10
+ class AdminBaseRoute extends BaseRoute {
11
+ constructor(urlFragment, role, serviceKey) {
12
+ if (!urlFragment)
13
+ throw Error('Invalid url fragment');
14
+
15
+ super(`/api/admin/${urlFragment}`);
16
+
17
+ this._options = {
18
+ role: role,
19
+ serviceKey: serviceKey
20
+ }
21
+ }
22
+
23
+ _allowsCreate() {
24
+ return true;
25
+ }
26
+
27
+ _allowsDelete() {
28
+ return true;
29
+ }
30
+
31
+ _allowsUpdate() {
32
+ return true;
33
+ }
34
+
35
+ _initializeRoutesCreate(router) {
36
+ const self = this;
37
+ router.post('/',
38
+ authentication(true),
39
+ authorization([ `${self._options.role}.create` ]),
40
+ koaBody({
41
+ text: false,
42
+ }),
43
+ // eslint-disable-next-line
44
+ async (ctx, next) => {
45
+ const service = this._injector.getService(self._options.serviceKey);
46
+ const response = (await service.create(ctx.correlationId, ctx.state.user, ctx.request.body)).check(ctx);
47
+ ctx.body = Utility.stringify(response);
48
+ }
49
+ );
50
+ }
51
+
52
+ _initializeRoutesDelete(router) {
53
+ const self = this;
54
+ router.delete('/:id',
55
+ authentication(true),
56
+ authorization([ `${self._options.role}.delete` ]),
57
+ // eslint-disable-next-line
58
+ async (ctx, next) => {
59
+ const service = this._injector.getService(self._options.serviceKey);
60
+ const response = (await service.delete(ctx.correlationId, ctx.state.user, ctx.params.id)).check(ctx);
61
+ ctx.body = Utility.stringify(response);
62
+ }
63
+ );
64
+ }
65
+
66
+ _initializeRoutesUpdate(router) {
67
+ const self = this;
68
+ router.post('/:id',
69
+ authentication(true),
70
+ authorization([ `${self._options.role}.update` ]),
71
+ koaBody({
72
+ text: false,
73
+ }),
74
+ // eslint-disable-next-line
75
+ async (ctx, next) => {
76
+ const service = this._injector.getService(self._options.serviceKey);
77
+ const response = (await service.update(ctx.correlationId, ctx.state.user, ctx.params.id, ctx.request.body)).check(ctx);
78
+ ctx.body = Utility.stringify(response);
79
+ }
80
+ );
81
+ }
82
+
83
+ _initializeRoutes(router) {
84
+ if (this._allowsDelete)
85
+ this._initializeRoutesDelete(router);
86
+
87
+ router.post('/search',
88
+ authentication(true),
89
+ authorization([ `${this._options.role}.search` ]),
90
+ koaBody({
91
+ text: false,
92
+ }),
93
+ // eslint-disable-next-line
94
+ async (ctx, next) => {
95
+ const service = this._injector.getService(this._options.serviceKey);
96
+ const response = (await service.search(ctx.correlationId, ctx.state.user, ctx.request.body)).check(ctx);
97
+ ctx.body = Utility.stringify(response);
98
+ }
99
+ );
100
+
101
+ if (this._allowsUpdate())
102
+ this._initializeRoutesUpdate(router);
103
+
104
+ if (this._allowsCreate())
105
+ this._initializeRoutesCreate(router);
106
+
107
+ return router;
108
+ }
109
+ }
110
+
111
+ export default AdminBaseRoute;