evaengine 1.0.14 → 3.0.0

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 (202) hide show
  1. package/README.md +15 -7
  2. package/bin/engine +2 -31
  3. package/dist/bin.d.ts +1 -0
  4. package/dist/bin.js +32 -0
  5. package/dist/bin.js.map +1 -0
  6. package/{src/commands/index.js → dist/commands/index.d.ts} +1 -8
  7. package/dist/commands/index.js +6 -0
  8. package/dist/commands/index.js.map +1 -0
  9. package/dist/commands/interface.d.ts +18 -0
  10. package/dist/commands/interface.js +40 -0
  11. package/dist/commands/interface.js.map +1 -0
  12. package/dist/commands/make_entity.d.ts +47 -0
  13. package/dist/commands/make_entity.js +410 -0
  14. package/dist/commands/make_entity.js.map +1 -0
  15. package/dist/commands/tramp.d.ts +7 -0
  16. package/dist/commands/tramp.js +29 -0
  17. package/dist/commands/tramp.js.map +1 -0
  18. package/dist/config/index.d.ts +92 -0
  19. package/dist/config/index.js +96 -0
  20. package/dist/config/index.js.map +1 -0
  21. package/dist/di.d.ts +131 -0
  22. package/dist/di.js +197 -0
  23. package/dist/di.js.map +1 -0
  24. package/dist/engine.d.ts +85 -0
  25. package/dist/engine.js +404 -0
  26. package/dist/engine.js.map +1 -0
  27. package/dist/entities/index.d.ts +54 -0
  28. package/dist/entities/index.js +228 -0
  29. package/dist/entities/index.js.map +1 -0
  30. package/dist/exceptions/index.d.ts +153 -0
  31. package/dist/exceptions/index.js +466 -0
  32. package/dist/exceptions/index.js.map +1 -0
  33. package/dist/index.d.ts +41 -0
  34. package/dist/index.js +43 -0
  35. package/dist/index.js.map +1 -0
  36. package/dist/middlewares/auth.d.ts +5 -0
  37. package/dist/middlewares/auth.js +63 -0
  38. package/dist/middlewares/auth.js.map +1 -0
  39. package/dist/middlewares/auth_kong.d.ts +2 -0
  40. package/dist/middlewares/auth_kong.js +24 -0
  41. package/dist/middlewares/auth_kong.js.map +1 -0
  42. package/dist/middlewares/debug.d.ts +3 -0
  43. package/dist/middlewares/debug.js +50 -0
  44. package/dist/middlewares/debug.js.map +1 -0
  45. package/{src/middlewares/index.js → dist/middlewares/index.d.ts} +1 -11
  46. package/dist/middlewares/index.js +9 -0
  47. package/dist/middlewares/index.js.map +1 -0
  48. package/dist/middlewares/providers.d.ts +25 -0
  49. package/dist/middlewares/providers.js +63 -0
  50. package/dist/middlewares/providers.js.map +1 -0
  51. package/dist/middlewares/session.d.ts +5 -0
  52. package/dist/middlewares/session.js +46 -0
  53. package/dist/middlewares/session.js.map +1 -0
  54. package/dist/middlewares/trace.d.ts +52 -0
  55. package/dist/middlewares/trace.js +225 -0
  56. package/dist/middlewares/trace.js.map +1 -0
  57. package/dist/middlewares/validator.d.ts +10 -0
  58. package/dist/middlewares/validator.js +43 -0
  59. package/dist/middlewares/validator.js.map +1 -0
  60. package/dist/middlewares/view_cache.d.ts +14 -0
  61. package/dist/middlewares/view_cache.js +102 -0
  62. package/dist/middlewares/view_cache.js.map +1 -0
  63. package/dist/services/cache.d.ts +68 -0
  64. package/dist/services/cache.js +206 -0
  65. package/dist/services/cache.js.map +1 -0
  66. package/dist/services/config.d.ts +17 -0
  67. package/dist/services/config.js +67 -0
  68. package/dist/services/config.js.map +1 -0
  69. package/dist/services/env.d.ts +12 -0
  70. package/dist/services/env.js +24 -0
  71. package/dist/services/env.js.map +1 -0
  72. package/dist/services/event_manager.d.ts +30 -0
  73. package/dist/services/event_manager.js +74 -0
  74. package/dist/services/event_manager.js.map +1 -0
  75. package/dist/services/http_client.d.ts +67 -0
  76. package/dist/services/http_client.js +61 -0
  77. package/dist/services/http_client.js.map +1 -0
  78. package/{src/services/index.js → dist/services/index.d.ts} +1 -15
  79. package/dist/services/index.js +14 -0
  80. package/dist/services/index.js.map +1 -0
  81. package/{src/services/interface.js → dist/services/interface.d.ts} +1 -3
  82. package/dist/services/interface.js +6 -0
  83. package/dist/services/interface.js.map +1 -0
  84. package/{src/services/joi.js → dist/services/joi.d.ts} +2 -9
  85. package/dist/services/joi.js +11 -0
  86. package/dist/services/joi.js.map +1 -0
  87. package/dist/services/jwt_token.d.ts +25 -0
  88. package/dist/services/jwt_token.js +76 -0
  89. package/dist/services/jwt_token.js.map +1 -0
  90. package/dist/services/jwt_token_kong.d.ts +26 -0
  91. package/dist/services/jwt_token_kong.js +64 -0
  92. package/dist/services/jwt_token_kong.js.map +1 -0
  93. package/dist/services/logger.d.ts +31 -0
  94. package/dist/services/logger.js +125 -0
  95. package/dist/services/logger.js.map +1 -0
  96. package/dist/services/namespace.d.ts +62 -0
  97. package/dist/services/namespace.js +171 -0
  98. package/dist/services/namespace.js.map +1 -0
  99. package/dist/services/now.d.ts +15 -0
  100. package/dist/services/now.js +42 -0
  101. package/dist/services/now.js.map +1 -0
  102. package/dist/services/providers.d.ts +55 -0
  103. package/dist/services/providers.js +134 -0
  104. package/dist/services/providers.js.map +1 -0
  105. package/dist/services/redis.d.ts +19 -0
  106. package/dist/services/redis.js +52 -0
  107. package/dist/services/redis.js.map +1 -0
  108. package/dist/services/rest_client.d.ts +49 -0
  109. package/dist/services/rest_client.js +95 -0
  110. package/dist/services/rest_client.js.map +1 -0
  111. package/dist/swagger/index.d.ts +223 -0
  112. package/dist/swagger/index.js +564 -0
  113. package/dist/swagger/index.js.map +1 -0
  114. package/dist/types/express.d.ts +14 -0
  115. package/dist/types/vendor.d.ts +30 -0
  116. package/dist/utils/api_scaffold.d.ts +74 -0
  117. package/dist/utils/api_scaffold.js +220 -0
  118. package/dist/utils/api_scaffold.js.map +1 -0
  119. package/dist/utils/case_converter.d.ts +7 -0
  120. package/dist/utils/case_converter.js +29 -0
  121. package/dist/utils/case_converter.js.map +1 -0
  122. package/dist/utils/crc32.d.ts +2 -0
  123. package/dist/utils/crc32.js +43 -0
  124. package/dist/utils/crc32.js.map +1 -0
  125. package/dist/utils/cron.d.ts +27 -0
  126. package/dist/utils/cron.js +186 -0
  127. package/dist/utils/cron.js.map +1 -0
  128. package/dist/utils/datetime.d.ts +4 -0
  129. package/dist/utils/datetime.js +6 -0
  130. package/dist/utils/datetime.js.map +1 -0
  131. package/dist/utils/host.d.ts +18 -0
  132. package/dist/utils/host.js +33 -0
  133. package/dist/utils/host.js.map +1 -0
  134. package/{src/utils/index.js → dist/utils/index.d.ts} +2 -26
  135. package/dist/utils/index.js +12 -0
  136. package/dist/utils/index.js.map +1 -0
  137. package/dist/utils/jwt.d.ts +16 -0
  138. package/dist/utils/jwt.js +93 -0
  139. package/dist/utils/jwt.js.map +1 -0
  140. package/dist/utils/pagination.d.ts +150 -0
  141. package/dist/utils/pagination.js +242 -0
  142. package/dist/utils/pagination.js.map +1 -0
  143. package/dist/utils/random.d.ts +3 -0
  144. package/dist/utils/random.js +18 -0
  145. package/dist/utils/random.js.map +1 -0
  146. package/dist/utils/request_client.d.ts +33 -0
  147. package/dist/utils/request_client.js +153 -0
  148. package/dist/utils/request_client.js.map +1 -0
  149. package/dist/utils/smart_query.d.ts +144 -0
  150. package/dist/utils/smart_query.js +310 -0
  151. package/dist/utils/smart_query.js.map +1 -0
  152. package/dist/utils/test.d.ts +14 -0
  153. package/dist/utils/test.js +37 -0
  154. package/dist/utils/test.js.map +1 -0
  155. package/dist/utils/wrapper.d.ts +3 -0
  156. package/dist/utils/wrapper.js +5 -0
  157. package/dist/utils/wrapper.js.map +1 -0
  158. package/package.json +22 -15
  159. package/index.js +0 -2
  160. package/src/commands/interface.js +0 -45
  161. package/src/commands/make_entity.js +0 -490
  162. package/src/commands/tramp.js +0 -34
  163. package/src/config/index.js +0 -95
  164. package/src/di.js +0 -96
  165. package/src/engine.js +0 -556
  166. package/src/entities/index.js +0 -272
  167. package/src/exceptions/index.js +0 -501
  168. package/src/index.js +0 -55
  169. package/src/middlewares/auth.js +0 -67
  170. package/src/middlewares/auth_kong.js +0 -28
  171. package/src/middlewares/debug.js +0 -88
  172. package/src/middlewares/providers.js +0 -73
  173. package/src/middlewares/session.js +0 -63
  174. package/src/middlewares/trace.js +0 -254
  175. package/src/middlewares/validator.js +0 -50
  176. package/src/middlewares/view_cache.js +0 -127
  177. package/src/services/cache.js +0 -223
  178. package/src/services/config.js +0 -115
  179. package/src/services/env.js +0 -28
  180. package/src/services/event_manager.js +0 -84
  181. package/src/services/http_client.js +0 -69
  182. package/src/services/jwt_token.js +0 -89
  183. package/src/services/jwt_token_kong.js +0 -75
  184. package/src/services/logger.js +0 -150
  185. package/src/services/namespace.js +0 -176
  186. package/src/services/now.js +0 -49
  187. package/src/services/providers.js +0 -158
  188. package/src/services/redis.js +0 -63
  189. package/src/services/rest_client.js +0 -112
  190. package/src/swagger/index.js +0 -665
  191. package/src/utils/api_scaffold.js +0 -240
  192. package/src/utils/case_converter.js +0 -33
  193. package/src/utils/crc32.js +0 -44
  194. package/src/utils/cron.js +0 -208
  195. package/src/utils/datetime.js +0 -13
  196. package/src/utils/host.js +0 -39
  197. package/src/utils/pagination.js +0 -260
  198. package/src/utils/random.js +0 -23
  199. package/src/utils/request_client.js +0 -179
  200. package/src/utils/smart_query.js +0 -352
  201. package/src/utils/test.js +0 -48
  202. package/src/utils/wrapper.js +0 -8
@@ -1,150 +0,0 @@
1
- import constitute from 'constitute';
2
- import moment from 'moment-timezone';
3
- import winston from 'winston';
4
- import { inspect } from 'util';
5
- import Env from './env.js';
6
- import Config from './config.js';
7
- import Namespace from './namespace.js';
8
- import ServiceInterface from './interface.js';
9
-
10
-
11
- class Logger extends ServiceInterface {
12
- /**
13
- * @param env {Env}
14
- * @param config {Config}
15
- * @param namespace {Namespace}
16
- */
17
- constructor(env, config, namespace) {
18
- super();
19
- this.env = env;
20
- this.config = config;
21
- this.namespace = namespace;
22
-
23
- let level = env.isProduction() ? 'info' : 'debug';
24
- if (process.env.LOG_LEVEL) {
25
- level = process.env.LOG_LEVEL;
26
- }
27
- this.level = level;
28
- winston.level = level;
29
- this.instance = null;
30
- this.label = null;
31
- this.logfile = null;
32
- }
33
-
34
- getProto() {
35
- return winston;
36
- }
37
-
38
- getWinston() {
39
- return winston;
40
- }
41
-
42
- setLogFile(logfile) {
43
- this.logfile = logfile;
44
- return this;
45
- }
46
-
47
- setLabel(label) {
48
- this.label = label;
49
- return this;
50
- }
51
-
52
- setLevel(level) {
53
- this.level = level;
54
- return this;
55
- }
56
-
57
- /**
58
- * @returns {winston.Logger}
59
- */
60
- getInstance() {
61
- if (this.instance) {
62
- return this.instance;
63
- }
64
- const logPath = this.logfile || this.config.get('logger.file');
65
- this.instance = this.factory(logPath);
66
- return this.instance;
67
- }
68
-
69
- /**
70
- * @param logPath
71
- * @param key
72
- * @param level
73
- * @returns {winston.Logger}
74
- */
75
- factory(logPath, key = 'global', level = this.level) {
76
- const timestamp = () => moment().format();
77
- return logPath ? winston.createLogger({
78
- format: winston.format.combine(winston.format.splat(), winston.format.json()),
79
- transports: [
80
- new (winston.transports.Console)({
81
- name: `${key}-console`,
82
- timestamp,
83
- level,
84
- label: this.label
85
- }),
86
- new (winston.transports.File)({
87
- name: `${key}-file`,
88
- timestamp,
89
- json: false,
90
- level,
91
- label: this.label,
92
- filename: logPath
93
- })
94
- ]
95
- }) : winston.createLogger({
96
- format: winston.format.combine(winston.format.splat(), winston.format.json()),
97
- transports: [
98
- new (winston.transports.Console)({
99
- name: `${key}-console`,
100
- timestamp,
101
- level,
102
- label: this.label,
103
- colorize: !this.env.isProduction(),
104
- prettyPrint: !this.env.isProduction()
105
- })
106
- ]
107
- });
108
- }
109
-
110
- populateTraceId(args) {
111
- if (!this.namespace.get('tracer')) {
112
- return args;
113
- }
114
- const { spanId, traceId } = this.namespace.get('tracer') || {};
115
- if (spanId) {
116
- args.push(spanId);
117
- }
118
- if (spanId !== traceId) {
119
- args.push(traceId);
120
- }
121
- return args;
122
- }
123
-
124
- debug(...args) {
125
- return this.getInstance().debug(...this.populateTraceId(args));
126
- }
127
-
128
- verbose(...args) {
129
- return this.getInstance().verbose(...this.populateTraceId(args));
130
- }
131
-
132
- info(...args) {
133
- return this.getInstance().info(...this.populateTraceId(args));
134
- }
135
-
136
- warn(...args) {
137
- return this.getInstance().warn(...this.populateTraceId(args));
138
- }
139
-
140
- error(...args) {
141
- return this.getInstance().error(...this.populateTraceId(args));
142
- }
143
-
144
- dump(obj) {
145
- return this.debug(inspect(obj, { depth: null, colors: true }));
146
- }
147
- }
148
-
149
- constitute.Dependencies(Env, Config, Namespace)(Logger);
150
- export default Logger;
@@ -1,176 +0,0 @@
1
- import constitute from 'constitute';
2
- import { createNamespace, getNamespace, destroyNamespace, reset } from 'continuation-local-storage';
3
- import Config from './config.js';
4
- import { OperationUnsupportedException } from '../exceptions/index.js';
5
- import ServiceInterface from './interface.js';
6
-
7
- const stores = {};
8
-
9
- export class Store {
10
- get() {
11
- return null;
12
- }
13
-
14
- set() {
15
- return this;
16
- }
17
-
18
- bind() {
19
- return this;
20
- }
21
-
22
- reset() {
23
- return this;
24
- }
25
-
26
- active() {
27
- }
28
-
29
- run(callback) {
30
- return callback();
31
- }
32
-
33
- bindEmitter() {
34
- return this;
35
- }
36
-
37
- getContext() {
38
- throw new OperationUnsupportedException('Not able to get namespace context when it be disabled');
39
- }
40
-
41
- createContext() {
42
- }
43
- }
44
-
45
- export class NullStore extends Store {
46
- }
47
-
48
- export class NsStore extends Store {
49
- constructor(name) {
50
- super();
51
- this.name = name;
52
- createNamespace(name);
53
- }
54
-
55
- get(key) {
56
- return getNamespace(this.name).get(key);
57
- }
58
-
59
- set(key, value) {
60
- getNamespace(this.name).set(key, value);
61
- return this;
62
- }
63
-
64
- bind(...args) {
65
- return getNamespace(this.name).bind(...args);
66
- }
67
-
68
- active() {
69
- return getNamespace(this.name).active();
70
- }
71
-
72
- run(...args) {
73
- return getNamespace(this.name).run(...args);
74
- }
75
-
76
- bindEmitter(...args) {
77
- return getNamespace(this.name).bindEmitter(...args);
78
- }
79
-
80
- getContext() {
81
- return getNamespace(this.name);
82
- }
83
-
84
- createContext() {
85
- return getNamespace(this.name).createContext();
86
- }
87
-
88
- reset() {
89
- reset(this.name);
90
- return this;
91
- }
92
- }
93
-
94
- class Namespace extends ServiceInterface {
95
- /**
96
- * @param config {Config}
97
- */
98
- constructor(config) {
99
- super();
100
- this.config = config.get('namespace');
101
- this.defaultName = config.defaultName || 'eva.ns';
102
- this.store = null;
103
- this.name = null;
104
- }
105
-
106
- isEnabled() {
107
- return this.config.enable;
108
- }
109
-
110
- getDefaultName() {
111
- return this.defaultName;
112
- }
113
-
114
- setDefaultName(name) {
115
- this.defaultName = name;
116
- return this;
117
- }
118
-
119
- getName() {
120
- return this.name;
121
- }
122
-
123
- /**
124
- * @returns {Store}
125
- */
126
- getStore() {
127
- if ({}.hasOwnProperty.call(stores, this.name)) {
128
- return stores[this.name];
129
- }
130
- stores[this.name] = this.config.enable === true ?
131
- new NsStore(this.name) : new NullStore(this.name);
132
- return stores[this.name];
133
- }
134
-
135
- destroy(name) {
136
- destroyNamespace(name);
137
- delete stores[name];
138
- return this;
139
- }
140
-
141
- use(ns) {
142
- this.name = ns || this.defaultName;
143
- return this.getStore();
144
- }
145
-
146
- get(key) {
147
- return this.use().get(key);
148
- }
149
-
150
- set(key, value) {
151
- return this.use().set(key, value);
152
- }
153
-
154
- bind(...args) {
155
- return this.use().bind(...args);
156
- }
157
-
158
- active() {
159
- return this.use().active();
160
- }
161
-
162
- run(...args) {
163
- return this.use().run(...args);
164
- }
165
-
166
- bindEmitter(...args) {
167
- return this.use().bindEmitter(...args);
168
- }
169
-
170
- createContext() {
171
- return this.use().createContext();
172
- }
173
- }
174
-
175
- constitute.Dependencies(Config)(Namespace);
176
- export default Namespace;
@@ -1,49 +0,0 @@
1
- import constitute from 'constitute';
2
- import moment from 'moment-timezone';
3
- import { getTimestamp, getDatabaseDatetime } from '../utils/datetime.js';
4
- import Logger from './logger.js';
5
- import ServiceInterface from './interface.js';
6
-
7
- class Now extends ServiceInterface {
8
- /**
9
- * @param {Logger} logger
10
- */
11
- constructor(logger) {
12
- super();
13
- this.logger = logger;
14
- this.now = null;
15
- }
16
-
17
- setNow(now) {
18
- if (typeof now === 'number') {
19
- this.now = now;
20
- } else if (now instanceof moment) {
21
- this.now = now.unix();
22
- } else {
23
- this.now = moment(now).unix();
24
- }
25
- this.logger.warn('Now has been force changed to %s', moment.unix(this.now));
26
- return this;
27
- }
28
-
29
- clear() {
30
- this.now = null;
31
- return this;
32
- }
33
-
34
- getTimestamp() {
35
- return this.now || getTimestamp();
36
- }
37
-
38
- getMoment() {
39
- return this.now ? moment.unix(this.now) : moment();
40
- }
41
-
42
- getDatabaseDatetime() {
43
- return this.now ? moment.unix(this.now).format('YYYY-MM-DD HH:mm:ss') : getDatabaseDatetime();
44
- }
45
- }
46
-
47
- constitute.Dependencies(Logger)(Now);
48
- export default Now;
49
-
@@ -1,158 +0,0 @@
1
- import Cache from './cache.js';
2
- import Config from './config.js';
3
- import Env from './env.js';
4
- import HttpClient from './http_client.js';
5
- import JsonWebToken from './jwt_token.js';
6
- import KongJsonWebToken from './jwt_token_kong.js';
7
- import Logger from './logger.js';
8
- import Redis from './redis.js';
9
- import RestClient from './rest_client.js';
10
- import Namespace from './namespace.js';
11
- import Now from './now.js';
12
- import ValidatorBase from './joi.js';
13
- import EventManager from './event_manager.js';
14
- import DI from '../di.js';
15
-
16
- export class ServiceProvider {
17
- constructor(engine) {
18
- this.engine = engine;
19
- }
20
-
21
- get name() {
22
- return 'default';
23
- }
24
-
25
- register() {
26
- }
27
- }
28
-
29
- export class EnvProvider extends ServiceProvider {
30
- get name() {
31
- return 'env';
32
- }
33
-
34
- register() {
35
- DI.bindClass(this.name, Env);
36
- }
37
- }
38
-
39
- export class ConfigProvider extends ServiceProvider {
40
- get name() {
41
- return 'config';
42
- }
43
-
44
- register() {
45
- const config = DI.get(Config);
46
- config.setPath(this.engine.getMeta().configPath);
47
- DI.bindClass(this.name, Config, [config]);
48
- }
49
- }
50
-
51
- export class LoggerProvider extends ServiceProvider {
52
- get name() {
53
- return 'logger';
54
- }
55
-
56
- register() {
57
- const logger = DI.get(Logger);
58
- const { mode } = this.engine.getMeta();
59
- const loggerLabel = mode === 'web' ? mode + this.engine.getMeta().port
60
- : process.env.CLI_NAME || 'cli';
61
- logger.setLabel(loggerLabel);
62
- DI.bindClass(this.name, Logger, [logger]);
63
- }
64
- }
65
-
66
- export class RedisProvider extends ServiceProvider {
67
- get name() {
68
- return 'redis';
69
- }
70
-
71
- register() {
72
- DI.bindClass(this.name, Redis);
73
- }
74
- }
75
-
76
- export class JsonWebTokenProvider extends ServiceProvider {
77
- get name() {
78
- return 'jwt';
79
- }
80
-
81
- register() {
82
- if (DI.get('config').get('token.provider') === 'kong') {
83
- DI.bindClass(this.name, KongJsonWebToken);
84
- } else {
85
- DI.bindClass(this.name, JsonWebToken);
86
- }
87
- }
88
- }
89
-
90
- export class HttpClientProvider extends ServiceProvider {
91
- get name() {
92
- return 'http_client';
93
- }
94
-
95
- register() {
96
- DI.bindClass(this.name, HttpClient);
97
- }
98
- }
99
-
100
- export class RestClientProvider extends ServiceProvider {
101
- get name() {
102
- return 'rest_client';
103
- }
104
-
105
- register() {
106
- DI.bindClass(this.name, RestClient);
107
- }
108
- }
109
-
110
- export class CacheProvider extends ServiceProvider {
111
- get name() {
112
- return 'cache';
113
- }
114
-
115
- register() {
116
- DI.bindClass(this.name, Cache);
117
- }
118
- }
119
-
120
- export class NamespaceProvider extends ServiceProvider {
121
- get name() {
122
- return 'namespace';
123
- }
124
-
125
- register() {
126
- DI.bindClass(this.name, Namespace);
127
- }
128
- }
129
-
130
- export class ValidatorBaseProvider extends ServiceProvider {
131
- get name() {
132
- return 'validator_base';
133
- }
134
-
135
- register() {
136
- DI.bindClass(this.name, ValidatorBase);
137
- }
138
- }
139
-
140
- export class EventManagerProvider extends ServiceProvider {
141
- get name() {
142
- return 'event_manager';
143
- }
144
-
145
- register() {
146
- DI.bindClass(this.name, EventManager);
147
- }
148
- }
149
-
150
- export class NowProvider extends ServiceProvider {
151
- get name() {
152
- return 'now';
153
- }
154
-
155
- register() {
156
- DI.bindClass(this.name, Now);
157
- }
158
- }
@@ -1,63 +0,0 @@
1
- import Ioredis from 'ioredis';
2
- import constitute from 'constitute';
3
- import DI from '../di.js';
4
- import Config from './config.js';
5
- import ServiceInterface from './interface.js';
6
-
7
- class Redis extends ServiceInterface {
8
- /**
9
- * @param config Config
10
- */
11
- constructor(config) {
12
- super();
13
- this.config = config;
14
- this.options = null;
15
- this.client = null;
16
- }
17
-
18
- getProto() {
19
- return Ioredis;
20
- }
21
-
22
- getRedis() {
23
- return Ioredis;
24
- }
25
-
26
- setOptions(options) {
27
- this.options = options;
28
- return this;
29
- }
30
-
31
- isConnected() {
32
- return this.client !== null;
33
- }
34
-
35
- cleanup() {
36
- return this.getInstance().end();
37
- }
38
-
39
- /**
40
- * @returns {Ioredis}
41
- */
42
- getInstance() {
43
- if (this.client) {
44
- return this.client;
45
- }
46
- this.client = new Ioredis(Object.assign({
47
- enableOfflineQueue: true //make redis connect failings throw error
48
- }, this.options || this.config.get('redis')));
49
- this.client.on('error', (err) => {
50
- try {
51
- DI.get('logger').error('Redis client error:', err);
52
- } catch {
53
- //Logger not bound yet, keep the error visible on stderr
54
- console.error('Redis client error:', err);
55
- }
56
- });
57
- return this.client;
58
- }
59
- }
60
-
61
- constitute.Dependencies(Config)(Redis);
62
- export default Redis;
63
-
@@ -1,112 +0,0 @@
1
- import constitute from 'constitute';
2
- import HttpClient from './http_client.js';
3
- import Namespace from './namespace.js';
4
- import { RestServiceLogicException, RestServiceIOException } from '../exceptions/index.js';
5
- import ServiceInterface from './interface.js';
6
-
7
- class RestClient extends ServiceInterface {
8
- /**
9
- * @param {HttpClient} client
10
- * @param {Namespace} ns
11
- */
12
- constructor(client, ns) {
13
- super();
14
- this.client = client;
15
- this.ns = ns;
16
- this.baseUrl = null;
17
- }
18
-
19
- setBaseUrl(baseUrl) {
20
- this.baseUrl = baseUrl;
21
- return this;
22
- }
23
-
24
- getBaseUrl() {
25
- return this.baseUrl;
26
- }
27
-
28
- getProto() {
29
- return this.client.getProto();
30
- }
31
-
32
- getInstance() {
33
- return this.client.getInstance();
34
- }
35
-
36
- populateTrace(params) {
37
- const {
38
- traceId, spanId, sampled
39
- } = this.ns.get('tracer') || {};
40
- if (!traceId || !spanId) {
41
- return params;
42
- }
43
-
44
- if (!params.headers) {
45
- Object.assign(params, { headers: {} });
46
- }
47
- Object.assign(params.headers, {
48
- 'X-B3-TraceId': traceId,
49
- 'X-B3-SpanId': spanId,
50
- 'X-B3-Sampled': sampled
51
- });
52
- return params;
53
- }
54
-
55
- saveToTracer(headers) {
56
- if (!headers || Object.keys(headers).length < 1) {
57
- return;
58
- }
59
-
60
- const tracer = this.ns.get('tracer');
61
- if (!tracer) {
62
- return;
63
- }
64
- Object.entries(headers).forEach(([key, value]) => {
65
- if (key.startsWith('x-debug-')) {
66
- tracer.debug[key] = value;
67
- }
68
- });
69
- }
70
-
71
- rawRequest(params) {
72
- if (this.baseUrl) {
73
- Object.assign(params, { url: this.baseUrl + (params.url || params.uri) });
74
- if (params.uri) {
75
- delete params.uri;
76
- }
77
- }
78
- return this.client.getInstance()(this.populateTrace(params));
79
- }
80
-
81
- async request(params) {
82
- if (this.baseUrl) {
83
- Object.assign(params, {
84
- url: this.baseUrl + (params.url || params.uri)
85
- });
86
- if (params.uri) {
87
- delete params.uri;
88
- }
89
- }
90
- try {
91
- const { headers, body } = await this.client.getInstance()(Object.assign(
92
- this.populateTrace(params),
93
- {
94
- json: true,
95
- resolveWithFullResponse: true
96
- }
97
- ));
98
- this.saveToTracer(headers);
99
- return body;
100
- } catch (e) {
101
- //FIXME TypeError无法被记录?
102
- const { statusCode } = e;
103
- if (statusCode && statusCode >= 400 && statusCode < 500) {
104
- throw new RestServiceLogicException(e);
105
- }
106
- throw new RestServiceIOException(e);
107
- }
108
- }
109
- }
110
-
111
- constitute.Dependencies(HttpClient, Namespace)(RestClient);
112
- export default RestClient;