evaengine 1.0.0 → 1.0.2

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 (47) hide show
  1. package/package.json +1 -1
  2. package/src/commands/index.js +12 -0
  3. package/src/commands/interface.js +45 -0
  4. package/src/commands/make_entity.js +490 -0
  5. package/src/commands/tramp.js +34 -0
  6. package/src/config/index.js +96 -0
  7. package/src/di.js +84 -0
  8. package/src/engine.js +551 -0
  9. package/src/entities/index.js +273 -0
  10. package/src/exceptions/index.js +501 -0
  11. package/src/middlewares/auth.js +67 -0
  12. package/src/middlewares/auth_kong.js +28 -0
  13. package/src/middlewares/debug.js +88 -0
  14. package/src/middlewares/index.js +18 -0
  15. package/src/middlewares/providers.js +73 -0
  16. package/src/middlewares/session.js +58 -0
  17. package/src/middlewares/trace.js +229 -0
  18. package/src/middlewares/validator.js +50 -0
  19. package/src/middlewares/view_cache.js +126 -0
  20. package/src/services/cache.js +223 -0
  21. package/src/services/config.js +111 -0
  22. package/src/services/env.js +28 -0
  23. package/src/services/event_manager.js +84 -0
  24. package/src/services/http_client.js +160 -0
  25. package/src/services/index.js +27 -0
  26. package/src/services/interface.js +5 -0
  27. package/src/services/joi.js +13 -0
  28. package/src/services/jwt_token.js +89 -0
  29. package/src/services/jwt_token_kong.js +75 -0
  30. package/src/services/logger.js +148 -0
  31. package/src/services/namespace.js +176 -0
  32. package/src/services/now.js +49 -0
  33. package/src/services/providers.js +158 -0
  34. package/src/services/redis.js +55 -0
  35. package/src/services/rest_client.js +112 -0
  36. package/src/swagger/index.js +669 -0
  37. package/src/utils/api_scaffold.js +240 -0
  38. package/src/utils/case_converter.js +33 -0
  39. package/src/utils/crc32.js +44 -0
  40. package/src/utils/datetime.js +13 -0
  41. package/src/utils/host.js +39 -0
  42. package/src/utils/index.js +35 -0
  43. package/src/utils/pagination.js +260 -0
  44. package/src/utils/random.js +23 -0
  45. package/src/utils/smart_query.js +352 -0
  46. package/src/utils/test.js +48 -0
  47. package/src/utils/wrapper.js +8 -0
@@ -0,0 +1,55 @@
1
+ import Ioredis from 'ioredis';
2
+ import constitute from 'constitute';
3
+ import Config from './config.js';
4
+ import ServiceInterface from './interface.js';
5
+
6
+ class Redis extends ServiceInterface {
7
+ /**
8
+ * @param config Config
9
+ */
10
+ constructor(config) {
11
+ super();
12
+ this.config = config;
13
+ this.options = null;
14
+ this.client = null;
15
+ }
16
+
17
+ getProto() {
18
+ return Ioredis;
19
+ }
20
+
21
+ getRedis() {
22
+ return Ioredis;
23
+ }
24
+
25
+ setOptions(options) {
26
+ this.options = options;
27
+ return this;
28
+ }
29
+
30
+ isConnected() {
31
+ return this.client !== null;
32
+ }
33
+
34
+ cleanup() {
35
+ return this.getInstance().end();
36
+ }
37
+
38
+ /**
39
+ * @returns {Ioredis}
40
+ */
41
+ getInstance() {
42
+ if (this.client) {
43
+ return this.client;
44
+ }
45
+ this.client = new Ioredis(Object.assign({
46
+ enableOfflineQueue: true //make redis connect failings throw error
47
+ }, this.options || this.config.get('redis')));
48
+ this.client.on('error', () => {});
49
+ return this.client;
50
+ }
51
+ }
52
+
53
+ constitute.Dependencies(Config)(Redis);
54
+ export default Redis;
55
+
@@ -0,0 +1,112 @@
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;