@adaas/a-utils 0.1.25 → 0.1.27

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adaas/a-utils",
3
- "version": "0.1.25",
3
+ "version": "0.1.27",
4
4
  "description": "A-Utils is a set of utilities that are used across the ADAAS ecosystem. This package is designed to be a collection of utilities that are used across the ADAAS ecosystem.",
5
5
  "license": "Apache-2.0",
6
6
  "main": "./dist/index.cjs",
package/src/index.ts CHANGED
@@ -71,6 +71,13 @@ export { A_ExecutionContext } from './lib/A-Execution/A-Execution.context';
71
71
  export { A_OperationContext } from './lib/A-Operation/A-Operation.context';
72
72
  export * from './lib/A-Operation/A-Operation.types';
73
73
 
74
+ // ============================================================================
75
+ // A-Service Container
76
+ // ============================================================================
77
+ export { A_Service } from './lib/A-Service/A-Service.container';
78
+ // export * from './lib/A-Service/A-Service.types';
79
+ export * from './lib/A-Service/A-Service.constants';
80
+
74
81
  // ============================================================================
75
82
  // A-Polyfill Components
76
83
  // ============================================================================
@@ -0,0 +1,17 @@
1
+ export enum A_ServiceFeatures {
2
+
3
+ onBeforeLoad = '_A_Service_onBeforeLoad',
4
+ onLoad = '_A_Service_onLoad',
5
+ onAfterLoad = '_A_Service_onAfterLoad',
6
+
7
+ onBeforeStart = '_A_Service_onBeforeStart',
8
+ onStart = '_A_Service_onStart',
9
+ onAfterStart = '_A_Service_onAfterStart',
10
+
11
+ onBeforeStop = '_A_Service_onBeforeStop',
12
+ onStop = '_A_Service_onStop',
13
+ onAfterStop = '_A_Service_onAfterStop',
14
+
15
+
16
+ onError = '_A_Service_onError',
17
+ }
@@ -0,0 +1,209 @@
1
+ import { A_Concept, A_Container, A_Error, A_Feature, A_Inject } from "@adaas/a-concept";
2
+ import { A_ServiceFeatures } from "./A-Service.constants";
3
+ import { A_Polyfill } from "../A-Polyfill/A-Polyfill.component";
4
+ import { A_Config } from "../A-Config/A-Config.context";
5
+ import { A_Logger } from "../A-Logger/A-Logger.component";
6
+ import { A_Service_Error } from "./A-Service.error";
7
+
8
+
9
+
10
+
11
+ /**
12
+ * A-Service is a container that can run different types of services, such as HTTP servers, workers, etc.
13
+ * Depending on the provided config and configuration, it will load the necessary components and start the service.
14
+ *
15
+ */
16
+ export class A_Service extends A_Container {
17
+
18
+
19
+ @A_Concept.Load()
20
+ /**
21
+ * Load the service
22
+ */
23
+ async load() {
24
+ try {
25
+ await this.call(A_ServiceFeatures.onBeforeLoad);
26
+
27
+ await this.call(A_ServiceFeatures.onLoad);
28
+
29
+ await this.call(A_ServiceFeatures.onAfterLoad);
30
+
31
+ } catch (error) {
32
+
33
+ let wrappedError;
34
+
35
+ switch (true) {
36
+ case error instanceof A_Service_Error:
37
+ wrappedError = error;
38
+ break;
39
+
40
+ case error instanceof A_Error && error.originalError instanceof A_Service_Error:
41
+ wrappedError = error.originalError;
42
+ break;
43
+
44
+ default:
45
+ wrappedError = new A_Service_Error({
46
+ title: A_Service_Error.ServiceLoadError,
47
+ description: 'An error occurred while processing the request.',
48
+ originalError: error
49
+ })
50
+ break;
51
+ }
52
+
53
+ this.scope.register(wrappedError);
54
+
55
+ await this.call(A_ServiceFeatures.onError);
56
+ }
57
+
58
+ }
59
+
60
+
61
+ @A_Concept.Start()
62
+ /**
63
+ * Start the server
64
+ */
65
+ async start() {
66
+ try {
67
+ await this.call(A_ServiceFeatures.onBeforeStart);
68
+
69
+ await this.call(A_ServiceFeatures.onStart);
70
+
71
+ await this.call(A_ServiceFeatures.onAfterStart);
72
+
73
+ } catch (error) {
74
+
75
+ let wrappedError;
76
+
77
+ switch (true) {
78
+ case error instanceof A_Service_Error:
79
+ wrappedError = error;
80
+ break;
81
+
82
+ case error instanceof A_Error && error.originalError instanceof A_Service_Error:
83
+ wrappedError = error.originalError;
84
+ break;
85
+
86
+ default:
87
+ wrappedError = new A_Service_Error({
88
+ title: A_Service_Error.ServiceStartError,
89
+ description: 'An error occurred while processing the request.',
90
+ originalError: error
91
+ })
92
+ break;
93
+ }
94
+
95
+ this.scope.register(wrappedError);
96
+
97
+ await this.call(A_ServiceFeatures.onError);
98
+ }
99
+
100
+ }
101
+
102
+ @A_Concept.Stop()
103
+ /**
104
+ * Stop the server
105
+ */
106
+ async stop() {
107
+ try {
108
+ await this.call(A_ServiceFeatures.onBeforeStop);
109
+
110
+ await this.call(A_ServiceFeatures.onStop);
111
+
112
+ await this.call(A_ServiceFeatures.onAfterStop);
113
+
114
+ } catch (error) {
115
+
116
+ let wrappedError;
117
+
118
+ switch (true) {
119
+ case error instanceof A_Service_Error:
120
+ wrappedError = error;
121
+ break;
122
+
123
+ case error instanceof A_Error && error.originalError instanceof A_Service_Error:
124
+ wrappedError = error.originalError;
125
+ break;
126
+
127
+ default:
128
+ wrappedError = new A_Service_Error({
129
+ title: A_Service_Error.ServiceStopError,
130
+ description: 'An error occurred while processing the request.',
131
+ originalError: error
132
+ })
133
+ break;
134
+ }
135
+
136
+ this.scope.register(wrappedError);
137
+
138
+ await this.call(A_ServiceFeatures.onError);
139
+ }
140
+ }
141
+
142
+
143
+
144
+
145
+ // ======================================================================================
146
+ // ============================= A-Service Lifecycle =================================
147
+ // ======================================================================================
148
+
149
+ @A_Feature.Extend()
150
+ protected async [A_ServiceFeatures.onBeforeLoad](
151
+ @A_Inject(A_Polyfill) polyfill: A_Polyfill,
152
+ ...args: any[]
153
+ ): Promise<void> {
154
+ // Initialize Polyfill
155
+ if (!polyfill) {
156
+ this.scope.register(A_Polyfill);
157
+ polyfill = this.scope.resolve(A_Polyfill)!
158
+ }
159
+ }
160
+
161
+ @A_Feature.Extend()
162
+ protected async [A_ServiceFeatures.onLoad](...args: any[]) { }
163
+
164
+ @A_Feature.Extend()
165
+ protected async [A_ServiceFeatures.onAfterLoad](...args: any[]) { }
166
+
167
+
168
+ @A_Feature.Extend()
169
+ protected async [A_ServiceFeatures.onBeforeStart](...args: any[]) { }
170
+
171
+ @A_Feature.Extend()
172
+ protected async [A_ServiceFeatures.onStart](...args: any[]) { }
173
+
174
+ @A_Feature.Extend()
175
+ protected async [A_ServiceFeatures.onAfterStart](...args: any[]) { }
176
+
177
+
178
+
179
+ @A_Feature.Extend()
180
+ protected async [A_ServiceFeatures.onBeforeStop](...args: any[]) { }
181
+
182
+ @A_Feature.Extend()
183
+ protected async [A_ServiceFeatures.onStop](...args: any[]) { }
184
+
185
+ @A_Feature.Extend()
186
+ protected async [A_ServiceFeatures.onAfterStop](...args: any[]) { }
187
+
188
+
189
+
190
+ @A_Feature.Extend({
191
+ before: /.*/
192
+ })
193
+ protected async [A_ServiceFeatures.onError](
194
+ @A_Inject(A_Error) error: A_Error,
195
+ @A_Inject(A_Logger) logger?: A_Logger,
196
+ ...args: any[]
197
+ ) {
198
+ logger?.error(error);
199
+ }
200
+
201
+ }
202
+
203
+
204
+
205
+
206
+
207
+
208
+
209
+
@@ -0,0 +1,15 @@
1
+ import { A_Error } from "@adaas/a-concept";
2
+
3
+
4
+
5
+ export class A_Service_Error extends A_Error {
6
+
7
+ static readonly ServiceLoadError = 'Service load error';
8
+
9
+ static readonly ServiceStartError = 'Service start error';
10
+
11
+ static readonly ServiceStopError = 'Service stop error';
12
+
13
+
14
+
15
+ }
@@ -0,0 +1,32 @@
1
+ import { A_Component, A_Fragment } from "@adaas/a-concept";
2
+
3
+
4
+ export type A_SERVER_TYPES__ServerFeatures = [
5
+ A_SERVER_TYPES__ServerFeature.beforeStart,
6
+ A_SERVER_TYPES__ServerFeature.afterStart,
7
+ A_SERVER_TYPES__ServerFeature.beforeStop,
8
+ A_SERVER_TYPES__ServerFeature.afterStop,
9
+ A_SERVER_TYPES__ServerFeature.onRequest
10
+ ]
11
+
12
+ export enum A_SERVER_TYPES__ServerFeature {
13
+ beforeStart = 'beforeStart',
14
+ afterStart = 'afterStart',
15
+ beforeStop = 'beforeStop',
16
+ afterStop = 'afterStop',
17
+ beforeRequest = 'beforeRequest',
18
+ onRequest = 'onRequest',
19
+ afterRequest = 'afterRequest',
20
+ }
21
+
22
+
23
+ export type A_ServiceConstructor = {
24
+ name: string,
25
+ version: string,
26
+ controllers: Array<A_Component>,
27
+ entities: Array<A_Fragment>,
28
+ extensions: Array<A_Component>
29
+ }
30
+
31
+
32
+