@adaas/a-utils 0.1.13 → 0.1.14

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.
@@ -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_ChannelRequestContext } 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_ChannelRequestContext
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_ChannelRequestContext | string
31
+ ) {
32
+ if (A_TypeGuards.isString(context))
33
+ super(originalError, context);
34
+ else
35
+ super(originalError);
36
+
37
+ if (context instanceof A_ChannelRequestContext)
38
+ this._context = context
39
+ }
40
+
41
+ /***
42
+ * Returns Context of the error
43
+ */
44
+ get context(): A_ChannelRequestContext | 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_ChannelRequestContext<
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
+ }