@adaas/a-utils 0.1.13 → 0.1.15

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.
@@ -0,0 +1,76 @@
1
+
2
+
3
+ // ===============================================================
4
+ // ADAAS A-Channel Constants
5
+ // ===============================================================
6
+ export enum A_ChannelFeatures {
7
+ /**
8
+ * Allows to extend timeout logic and behavior
9
+ */
10
+ onTimeout = 'onTimeout',
11
+ /**
12
+ * Allows to extend retry logic and behavior
13
+ */
14
+ onRetry = 'onRetry',
15
+ /**
16
+ * Allows to extend circuit breaker logic and behavior
17
+ */
18
+ onCircuitBreakerOpen = 'onCircuitBreakerOpen',
19
+ /**
20
+ * Allows to extend cache logic and behavior
21
+ */
22
+ onCache = 'onCache',
23
+ /**
24
+ * Allows to extend connection logic and behavior
25
+ */
26
+ onConnect = 'onConnect',
27
+ /**
28
+ * Allows to extend disconnection logic and behavior
29
+ */
30
+ onDisconnect = 'onDisconnect',
31
+ /**
32
+ * Allows to extend request preparation logic and behavior
33
+ */
34
+ onBeforeRequest = 'onBeforeRequest',
35
+ /**
36
+ * Allows to extend request sending logic and behavior
37
+ */
38
+ onRequest = 'onRequest',
39
+ /**
40
+ * Allows to extend request post-processing logic and behavior
41
+ */
42
+ onAfterRequest = 'onAfterRequest',
43
+ /**
44
+ * Allows to extend error handling logic and behavior
45
+ *
46
+ * [!] The same approach uses for ALL errors within the channel
47
+ */
48
+ onError = 'onError',
49
+ /**
50
+ * Allows to extend send logic and behavior
51
+ */
52
+ onSend = 'onSend',
53
+ /**
54
+ * Allows to extend consume logic and behavior
55
+ */
56
+ onConsume = 'onConsume',
57
+ }
58
+
59
+
60
+ // ==============================================================
61
+ // A-Channel Request Constants
62
+ // ===============================================================
63
+ export enum A_ChannelRequestStatuses {
64
+ /**
65
+ * Request is pending
66
+ */
67
+ PENDING = 'PENDING',
68
+ /***
69
+ * Request was successful
70
+ */
71
+ SUCCESS = 'SUCCESS',
72
+ /**
73
+ * Request failed
74
+ */
75
+ FAILED = 'FAILED',
76
+ }
@@ -1,8 +1,49 @@
1
- import { A_Error } from "@adaas/a-concept";
1
+ import { A_Error, A_TypeGuards } from "@adaas/a-concept";
2
+ import { A_ChannelRequest } from "./A-ChannelRequest.context";
2
3
 
3
4
 
4
5
  export class A_ChannelError extends A_Error {
5
6
 
7
+ // ==========================================================
8
+ // ==================== Error Types =========================
9
+ // ==========================================================
10
+
11
+
6
12
  static readonly MethodNotImplemented = 'A-Channel Method Not Implemented';
7
13
 
14
+ // ==========================================================
15
+ // ==================== Properties ==========================
16
+ // ==========================================================
17
+
18
+ protected _context?: A_ChannelRequest
19
+
20
+
21
+ /**
22
+ * Channel Error allows to keep track of errors within a channel if something goes wrong
23
+ *
24
+ *
25
+ * @param originalError
26
+ * @param context
27
+ */
28
+ constructor(
29
+ originalError: string | A_Error | Error | any,
30
+ context?: A_ChannelRequest | string
31
+ ) {
32
+ if (A_TypeGuards.isString(context))
33
+ super(originalError, context);
34
+ else
35
+ super(originalError);
36
+
37
+ if (context instanceof A_ChannelRequest)
38
+ this._context = context
39
+ }
40
+
41
+ /***
42
+ * Returns Context of the error
43
+ */
44
+ get context(): A_ChannelRequest | undefined {
45
+ return this._context
46
+ }
47
+
48
+
8
49
  }
@@ -0,0 +1,11 @@
1
+
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
+ };
@@ -0,0 +1,91 @@
1
+ import { A_Error, A_Fragment } from "@adaas/a-concept";
2
+ import { A_ChannelRequestStatuses } from "./A-Channel.constants";
3
+ import { A_ChannelRequestContext_Serialized } from "./A-Channel.types";
4
+
5
+
6
+ export class A_ChannelRequest<
7
+ _ParamsType extends Record<string, any> = Record<string, any>,
8
+ _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
+
18
+
19
+ constructor(
20
+ params: Partial<_ParamsType> = {}
21
+ ) {
22
+ super();
23
+ this._params = params as _ParamsType;
24
+ }
25
+
26
+ /**
27
+ * Returns the status of the request
28
+ */
29
+ get status(): A_ChannelRequestStatuses {
30
+ return this._status;
31
+ }
32
+
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;
50
+ }
51
+
52
+ get errors(): Set<Error> | undefined {
53
+ return this._errors.size > 0 ? this._errors : undefined;
54
+ }
55
+
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
+
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
+ }
90
+ }
91
+ }