@adaas/a-utils 0.1.18 → 0.1.20

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 (50) hide show
  1. package/dist/index.cjs +45 -0
  2. package/dist/index.cjs.map +1 -0
  3. package/dist/index.d.mts +964 -354
  4. package/dist/index.d.ts +964 -354
  5. package/dist/index.mjs +44 -2372
  6. package/dist/index.mjs.map +1 -1
  7. package/examples/A-Channel-examples.ts +13 -11
  8. package/examples/A-Command-examples-2.ts +429 -0
  9. package/examples/A-Command-examples.ts +487 -202
  10. package/examples/A-StateMachine-examples.ts +609 -0
  11. package/package.json +3 -2
  12. package/src/index.ts +1 -2
  13. package/src/lib/A-Channel/A-Channel.component.ts +14 -74
  14. package/src/lib/A-Channel/A-Channel.error.ts +5 -5
  15. package/src/lib/A-Channel/A-Channel.types.ts +2 -10
  16. package/src/lib/A-Channel/A-ChannelRequest.context.ts +25 -74
  17. package/src/lib/A-Command/A-Command.constants.ts +78 -23
  18. package/src/lib/A-Command/A-Command.entity.ts +447 -119
  19. package/src/lib/A-Command/A-Command.error.ts +11 -0
  20. package/src/lib/A-Command/A-Command.types.ts +96 -20
  21. package/src/lib/A-Command/A-CommandExecution.context.ts +0 -0
  22. package/src/lib/A-Command/README.md +164 -68
  23. package/src/lib/A-Config/A-Config.container.ts +2 -2
  24. package/src/lib/A-Config/A-Config.context.ts +19 -5
  25. package/src/lib/A-Config/components/ConfigReader.component.ts +1 -1
  26. package/src/lib/A-Logger/A-Logger.component.ts +211 -35
  27. package/src/lib/A-Logger/A-Logger.constants.ts +50 -10
  28. package/src/lib/A-Logger/A-Logger.env.ts +17 -1
  29. package/src/lib/A-Memory/A-Memory.component.ts +440 -0
  30. package/src/lib/A-Memory/A-Memory.constants.ts +49 -0
  31. package/src/lib/A-Memory/A-Memory.context.ts +14 -118
  32. package/src/lib/A-Memory/A-Memory.error.ts +21 -0
  33. package/src/lib/A-Memory/A-Memory.types.ts +21 -0
  34. package/src/lib/A-Operation/A-Operation.context.ts +58 -0
  35. package/src/lib/A-Operation/A-Operation.types.ts +47 -0
  36. package/src/lib/A-StateMachine/A-StateMachine.component.ts +258 -0
  37. package/src/lib/A-StateMachine/A-StateMachine.constants.ts +18 -0
  38. package/src/lib/A-StateMachine/A-StateMachine.error.ts +10 -0
  39. package/src/lib/A-StateMachine/A-StateMachine.types.ts +20 -0
  40. package/src/lib/A-StateMachine/A-StateMachineTransition.context.ts +41 -0
  41. package/src/lib/A-StateMachine/README.md +391 -0
  42. package/tests/A-Channel.test.ts +17 -14
  43. package/tests/A-Command.test.ts +548 -460
  44. package/tests/A-Logger.test.ts +8 -4
  45. package/tests/A-Memory.test.ts +151 -115
  46. package/tests/A-Schedule.test.ts +2 -2
  47. package/tests/A-StateMachine.test.ts +760 -0
  48. package/tsup.config.ts +30 -13
  49. package/dist/index.js +0 -2398
  50. package/dist/index.js.map +0 -1
@@ -1,9 +1,8 @@
1
1
  import { A_Component, A_Context, A_Error, A_Feature, A_IdentityHelper, A_Inject, A_Scope, A_TYPES__InjectableConstructors, A_TYPES__InjectableTargets } from "@adaas/a-concept";
2
2
  import { A_ChannelError } from "./A-Channel.error";
3
3
  import { A_ChannelFeatures } from "./A-Channel.constants";
4
+ import { A_OperationContext } from "../A-Operation/A-Operation.context";
4
5
  import { A_ChannelRequest } from "./A-ChannelRequest.context";
5
- import { A_Logger } from "../A-Logger/A-Logger.component";
6
- import { A_Config } from "../A-Config/A-Config.context";
7
6
 
8
7
  /**
9
8
  * A-Channel - A powerful, extensible communication channel component
@@ -406,7 +405,6 @@ export class A_Channel extends A_Component {
406
405
 
407
406
  try {
408
407
  // Set up dependency injection scope
409
- requestScope.inherit(A_Context.scope(this));
410
408
  requestScope.register(context);
411
409
 
412
410
  // Execute request lifecycle
@@ -415,6 +413,7 @@ export class A_Channel extends A_Component {
415
413
  await this.call(A_ChannelFeatures.onAfterRequest, requestScope);
416
414
 
417
415
  this._processing = false;
416
+
418
417
  return context;
419
418
 
420
419
  } catch (error) {
@@ -423,11 +422,14 @@ export class A_Channel extends A_Component {
423
422
  // Create channel-specific error
424
423
  const channelError = new A_ChannelError(error);
425
424
  context.fail(channelError);
425
+ requestScope.register(channelError);
426
426
 
427
427
  // Call error handling hook
428
428
  await this.call(A_ChannelFeatures.onError, requestScope);
429
429
 
430
- return context;
430
+ requestScope.destroy();
431
+
432
+ throw channelError;
431
433
  }
432
434
  }
433
435
 
@@ -488,7 +490,7 @@ export class A_Channel extends A_Component {
488
490
  });
489
491
 
490
492
  // Create request context for the message
491
- const context = new A_ChannelRequest<_ParamsType>(message);
493
+ const context = new A_OperationContext('send', message);
492
494
 
493
495
  try {
494
496
  // Set up dependency injection scope
@@ -505,11 +507,14 @@ export class A_Channel extends A_Component {
505
507
 
506
508
  // Create channel-specific error
507
509
  const channelError = new A_ChannelError(error);
510
+ requestScope.register(channelError);
508
511
  context.fail(channelError);
509
512
 
510
513
  // Call error handling hook
511
514
  await this.call(A_ChannelFeatures.onError, requestScope);
512
515
 
516
+ requestScope.destroy();
517
+
513
518
  // Note: We don't re-throw the error for fire-and-forget operations
514
519
  // The error is handled by the onError hook
515
520
  }
@@ -523,14 +528,14 @@ export class A_Channel extends A_Component {
523
528
  * For fire-and-forget pattern: Use send()
524
529
  * For consumer patterns: Implement custom consumer logic using request() in a loop
525
530
  */
526
- async consume<T extends Record<string, any> = Record<string, any>>(): Promise<A_ChannelRequest<any, T>> {
531
+ async consume<T extends Record<string, any> = Record<string, any>>(): Promise<A_OperationContext<any, T>> {
527
532
  await this.initialize;
528
533
 
529
534
  this._processing = true;
530
535
 
531
536
  const requestScope = new A_Scope({ name: `a-channel@scope:consume:${A_IdentityHelper.generateTimeId()}` });
532
537
 
533
- const context = new A_ChannelRequest<any, T>();
538
+ const context = new A_OperationContext('consume', {} as T);
534
539
 
535
540
  try {
536
541
  requestScope.inherit(A_Context.scope(this));
@@ -541,7 +546,7 @@ export class A_Channel extends A_Component {
541
546
 
542
547
  this._processing = false;
543
548
 
544
- return context
549
+ return context as A_OperationContext<any, T>;
545
550
 
546
551
  } catch (error) {
547
552
 
@@ -553,73 +558,8 @@ export class A_Channel extends A_Component {
553
558
 
554
559
  await this.call(A_ChannelFeatures.onError, requestScope);
555
560
 
556
- return context;
561
+ return context as A_OperationContext<any, T>;
557
562
  }
558
563
  }
559
564
 
560
- }
561
-
562
-
563
-
564
-
565
- class HttpChannel extends A_Channel {
566
-
567
- protected baseUrl!: string
568
- }
569
-
570
-
571
- class SSOChannel extends HttpChannel {
572
- constructor() {
573
- super();
574
-
575
- this.baseUrl = 'https://sso.example.com';
576
- }
577
- }
578
-
579
-
580
- class PrstChannel extends HttpChannel {
581
-
582
- constructor() {
583
- super();
584
-
585
- this.baseUrl = 'https://prst.example.com';
586
- }
587
- }
588
-
589
-
590
- class PollyspotChannel extends HttpChannel {
591
-
592
- constructor() {
593
- super();
594
-
595
- this.baseUrl = 'https://pollyspot.example.com';
596
- }
597
- }
598
-
599
-
600
- class GlobalErrorhandler extends A_Component {
601
-
602
- @A_Feature.Extend({
603
- name: A_ChannelFeatures.onError,
604
- scope: [PollyspotChannel]
605
- })
606
- async handleError(
607
- @A_Inject(A_ChannelRequest) context: A_ChannelRequest<any, any>,
608
- @A_Inject(A_Logger) logger: A_Logger,
609
- @A_Inject(A_Config) config: A_Config
610
- ) {
611
- // Handle the error
612
- }
613
-
614
- @A_Feature.Extend({
615
- name: A_ChannelFeatures.onError,
616
- })
617
- async anotherError(
618
- @A_Inject(A_ChannelRequest) context: A_ChannelRequest<any, any>,
619
- @A_Inject(A_Logger) logger: A_Logger,
620
- @A_Inject(A_Config) config: A_Config
621
- ) {
622
- // Handle the error
623
-
624
- }
625
565
  }
@@ -1,5 +1,5 @@
1
1
  import { A_Error, A_TypeGuards } from "@adaas/a-concept";
2
- import { A_ChannelRequest } from "./A-ChannelRequest.context";
2
+ import { A_OperationContext } from "../A-Operation/A-Operation.context";
3
3
 
4
4
 
5
5
  export class A_ChannelError extends A_Error {
@@ -15,7 +15,7 @@ export class A_ChannelError extends A_Error {
15
15
  // ==================== Properties ==========================
16
16
  // ==========================================================
17
17
 
18
- protected _context?: A_ChannelRequest
18
+ protected _context?: A_OperationContext
19
19
 
20
20
 
21
21
  /**
@@ -27,21 +27,21 @@ export class A_ChannelError extends A_Error {
27
27
  */
28
28
  constructor(
29
29
  originalError: string | A_Error | Error | any,
30
- context?: A_ChannelRequest | string
30
+ context?: A_OperationContext | string
31
31
  ) {
32
32
  if (A_TypeGuards.isString(context))
33
33
  super(originalError, context);
34
34
  else
35
35
  super(originalError);
36
36
 
37
- if (context instanceof A_ChannelRequest)
37
+ if (context instanceof A_OperationContext)
38
38
  this._context = context
39
39
  }
40
40
 
41
41
  /***
42
42
  * Returns Context of the error
43
43
  */
44
- get context(): A_ChannelRequest | undefined {
44
+ get context(): A_OperationContext | undefined {
45
45
  return this._context
46
46
  }
47
47
 
@@ -1,11 +1,3 @@
1
+ import { A_ChannelRequestStatuses } from "./A-Channel.constants";
2
+ import { A_OperationContext } from "../A-Operation/A-Operation.context";
1
3
 
2
-
3
- export type A_ChannelRequestContext_Serialized<
4
- _ParamsType extends Record<string, any> = Record<string, any>,
5
- _ResultType extends Record<string, any> = Record<string, any>,
6
- > = {
7
- params: _ParamsType;
8
- result?: _ResultType;
9
- status: string;
10
- errors?: string[];
11
- };
@@ -1,91 +1,42 @@
1
- import { A_Error, A_Fragment } from "@adaas/a-concept";
1
+ import { A_OperationContext } from "../A-Operation/A-Operation.context";
2
2
  import { A_ChannelRequestStatuses } from "./A-Channel.constants";
3
- import { A_ChannelRequestContext_Serialized } from "./A-Channel.types";
4
-
5
3
 
6
4
  export class A_ChannelRequest<
7
5
  _ParamsType extends Record<string, any> = Record<string, any>,
8
6
  _ResultType extends Record<string, any> = Record<string, any>,
9
- > extends A_Fragment {
10
-
11
-
12
- protected _params: _ParamsType;
13
- protected _result?: _ResultType;
14
- protected _errors: Set<Error> = new Set();
15
-
16
- protected _status: A_ChannelRequestStatuses = A_ChannelRequestStatuses.PENDING;
17
-
7
+ > extends A_OperationContext<
8
+ 'request',
9
+ _ParamsType,
10
+ {
11
+ data?: _ResultType;
12
+ status?: A_ChannelRequestStatuses;
13
+ }
14
+ > {
18
15
 
19
16
  constructor(
20
- params: Partial<_ParamsType> = {}
17
+ params?: _ParamsType,
21
18
  ) {
22
- super();
23
- this._params = params as _ParamsType;
19
+ super('request', params);
24
20
  }
25
21
 
26
- /**
27
- * Returns the status of the request
28
- */
29
- get status(): A_ChannelRequestStatuses {
30
- return this._status;
31
- }
32
22
 
33
- /**
34
- * Returns the parameters of the request
35
- */
36
- get failed(): boolean {
37
- return this._errors.size > 0;
38
- }
39
- /**
40
- * Returns the Params of the Request
41
- */
42
- get params() {
43
- return this._params;
44
- }
45
- /**
46
- * Returns the Result of the Request
47
- */
48
- get data() {
49
- return this._result;
23
+ get status(): A_ChannelRequestStatuses | undefined {
24
+ return this.result?.status;
50
25
  }
51
26
 
52
- get errors(): Set<Error> | undefined {
53
- return this._errors.size > 0 ? this._errors : undefined;
27
+ get data(): _ResultType | undefined {
28
+ return this.result?.data;
54
29
  }
55
30
 
56
- // ==========================================================
57
- // ==================== Mutations ===========================
58
- // ==========================================================
59
- /**
60
- * Adds an error to the context
61
- *
62
- * @param error
63
- */
64
- fail(error: A_Error) {
65
- this._status = A_ChannelRequestStatuses.FAILED;
66
- this._errors.add(error);
67
- }
68
- /**
69
- * Sets the result of the request
70
- *
71
- * @param result
72
- */
73
- succeed(result: _ResultType) {
74
- this._status = A_ChannelRequestStatuses.SUCCESS;
75
- this._result = result;
76
- }
77
31
 
78
- /**
79
- * Serializes the context to a JSON object
80
- *
81
- * @returns
82
- */
83
- toJSON(): A_ChannelRequestContext_Serialized<_ParamsType, _ResultType> {
84
- return {
85
- params: this._params,
86
- result: this._result,
87
- status: this._status,
88
- errors: this.errors ? Array.from(this._errors).map(err => err.toString()) : undefined,
89
- }
32
+ succeed(result: _ResultType): void {
33
+ const existed = this.result;
34
+
35
+ super.succeed({
36
+ ...existed,
37
+ data: result,
38
+ status: A_ChannelRequestStatuses.SUCCESS,
39
+ });
90
40
  }
91
- }
41
+
42
+ }
@@ -1,71 +1,126 @@
1
1
  /**
2
- * A-Command Statuses
2
+ * A-Command Status Enumeration
3
+ *
4
+ * Defines all possible states a command can be in during its lifecycle.
5
+ * Commands progress through these states in a specific order:
6
+ * CREATED → INITIALIZED → COMPILED → EXECUTING → COMPLETED/FAILED
3
7
  */
4
- export enum A_CONSTANTS__A_Command_Status {
8
+ export enum A_Command_Status {
5
9
  /**
6
- * Command has been created but not yet initialized
10
+ * Initial state when command is instantiated but not yet ready for execution
7
11
  */
8
12
  CREATED = 'CREATED',
9
- /**
10
- * Command is initializing
11
- */
12
- INITIALIZATION = 'INITIALIZATION',
13
+
13
14
  /**
14
- * Command has been initialized
15
+ * Command has been initialized with execution scope and dependencies
15
16
  */
16
17
  INITIALIZED = 'INITIALIZED',
18
+
17
19
  /**
18
- * Command is compiling
19
- */
20
- COMPILATION = 'COMPILATION',
21
- /**
22
- * Command is compiled
20
+ * Command has been compiled and is ready for execution
23
21
  */
24
22
  COMPILED = 'COMPILED',
23
+
25
24
  /**
26
- * Command is executing
25
+ * Command is currently being executed
27
26
  */
28
- IN_PROGRESS = 'IN_PROGRESS',
27
+ EXECUTING = 'EXECUTING',
28
+
29
29
  /**
30
30
  * Command has completed successfully
31
31
  */
32
32
  COMPLETED = 'COMPLETED',
33
+
33
34
  /**
34
- * Command has failed
35
+ * Command execution has failed with errors
35
36
  */
36
37
  FAILED = 'FAILED',
37
38
  }
38
39
 
40
+
41
+ /**
42
+ * A-Command State Transitions
43
+ *
44
+ * Defines valid state transitions for command lifecycle management.
45
+ * These transitions are used by the StateMachine to enforce proper command flow.
46
+ */
47
+ export enum A_CommandTransitions {
48
+ /** Transition from CREATED to INITIALIZED state */
49
+ CREATED_TO_INITIALIZED = 'created_initialized',
50
+
51
+ /** Transition from INITIALIZED to EXECUTING state */
52
+ INITIALIZED_TO_EXECUTING = 'initialized_executing',
53
+
54
+ /** Transition from EXECUTING to COMPLETED state (success path) */
55
+ EXECUTING_TO_COMPLETED = 'executing_completed',
56
+
57
+ /** Transition from EXECUTING to FAILED state (error path) */
58
+ EXECUTING_TO_FAILED = 'executing_failed',
59
+ }
60
+
39
61
  /**
40
62
  * A-Command Lifecycle Features
63
+ *
64
+ * Defines feature extension points that components can implement to customize
65
+ * command behavior at different stages of the lifecycle.
66
+ *
67
+ * Components can use @A_Feature.Extend() decorator with these feature names
68
+ * to inject custom logic into command execution.
41
69
  */
42
70
  export enum A_CommandFeatures {
43
71
  /**
44
- * Allows to extend initialization logic and behavior
72
+ * Triggered during command initialization phase
73
+ * Use to set up execution environment, validate parameters, or prepare resources
45
74
  */
46
75
  onInit = 'onInit',
76
+
47
77
  /**
48
- * Allows to extend compilation logic and behavior
78
+ * Triggered before command execution starts
79
+ * Use for pre-execution validation, logging, or setup tasks
49
80
  */
50
- onCompile = 'onCompile',
81
+ onBeforeExecute = 'onBeforeExecute',
82
+
51
83
  /**
52
- * Allows to extend execution logic and behavior
84
+ * Main command execution logic
85
+ * Core business logic should be implemented here
53
86
  */
54
87
  onExecute = 'onExecute',
88
+
55
89
  /**
56
- * Allows to extend completion logic and behavior
90
+ * Triggered after command execution completes (success or failure)
91
+ * Use for cleanup, logging, or post-processing tasks
92
+ */
93
+ onAfterExecute = 'onAfterExecute',
94
+
95
+ /**
96
+ * Triggered when command completes successfully
97
+ * Use for success-specific operations like notifications or result processing
57
98
  */
58
99
  onComplete = 'onComplete',
100
+
59
101
  /**
60
- *
102
+ * Triggered when command execution fails
103
+ * Use for error handling, cleanup, or failure notifications
61
104
  */
62
105
  onFail = 'onFail',
106
+
107
+ /**
108
+ * Triggered when an error occurs during execution
109
+ * Use for error logging, transformation, or recovery attempts
110
+ */
111
+ onError = 'onError',
63
112
  }
64
113
 
65
114
 
66
115
 
67
116
 
68
- export type A_CONSTANTS__A_Command_Event = keyof typeof A_CommandFeatures;
117
+ /**
118
+ * Type alias for command lifecycle event names
119
+ * Represents all available events that can be listened to on a command instance
120
+ */
121
+ export type A_Command_Event = keyof typeof A_CommandFeatures;
122
+
123
+
69
124
 
70
125
 
71
126