@zenflux/core 0.0.1-dev.2 → 0.0.1-dev.3

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 (178) hide show
  1. package/dist/es/src/bases/controller-base.d.ts +55 -0
  2. package/dist/es/src/bases/controller-base.d.ts.map +1 -0
  3. package/dist/es/src/bases/index.d.ts +3 -0
  4. package/dist/es/src/bases/index.d.ts.map +1 -0
  5. package/dist/es/src/bases/object-base.d.ts +32 -0
  6. package/dist/es/src/bases/object-base.d.ts.map +1 -0
  7. package/dist/es/{clients → src/clients}/http.d.ts +16 -7
  8. package/dist/es/src/clients/http.d.ts.map +1 -0
  9. package/dist/es/src/command-bases/command-base.d.ts +29 -0
  10. package/dist/es/src/command-bases/command-base.d.ts.map +1 -0
  11. package/dist/es/src/command-bases/command-internal.d.ts +9 -0
  12. package/dist/es/src/command-bases/command-internal.d.ts.map +1 -0
  13. package/dist/es/src/command-bases/command-public.d.ts +9 -0
  14. package/dist/es/src/command-bases/command-public.d.ts.map +1 -0
  15. package/dist/es/src/command-bases/command-restful.d.ts +22 -0
  16. package/dist/es/src/command-bases/command-restful.d.ts.map +1 -0
  17. package/dist/es/src/command-bases/index.d.ts +8 -0
  18. package/dist/es/src/command-bases/index.d.ts.map +1 -0
  19. package/dist/es/src/errors/command-already-registered.d.ts +11 -0
  20. package/dist/es/src/errors/command-already-registered.d.ts.map +1 -0
  21. package/dist/es/src/errors/command-not-found.d.ts +10 -0
  22. package/dist/es/src/errors/command-not-found.d.ts.map +1 -0
  23. package/dist/es/src/errors/controller-already-registered.d.ts +11 -0
  24. package/dist/es/src/errors/controller-already-registered.d.ts.map +1 -0
  25. package/dist/es/src/errors/controller-already-set.d.ts +11 -0
  26. package/dist/es/src/errors/controller-already-set.d.ts.map +1 -0
  27. package/dist/es/src/errors/force-method-implementation.d.ts +14 -0
  28. package/dist/es/src/errors/force-method-implementation.d.ts.map +1 -0
  29. package/dist/es/src/errors/index-public.d.ts +2 -0
  30. package/dist/es/src/errors/index-public.d.ts.map +1 -0
  31. package/dist/es/src/errors/index.d.ts +6 -0
  32. package/dist/es/src/errors/index.d.ts.map +1 -0
  33. package/dist/es/src/exports.d.ts +37 -0
  34. package/dist/es/src/exports.d.ts.map +1 -0
  35. package/dist/es/src/index.d.ts +6 -0
  36. package/dist/es/src/index.d.ts.map +1 -0
  37. package/dist/es/src/initializer.d.ts +36 -0
  38. package/dist/es/src/initializer.d.ts.map +1 -0
  39. package/dist/es/{interfaces → src/interfaces}/commands.d.ts +3 -3
  40. package/dist/es/src/interfaces/commands.d.ts.map +1 -0
  41. package/dist/es/src/interfaces/config.d.ts +21 -0
  42. package/dist/es/src/interfaces/config.d.ts.map +1 -0
  43. package/dist/es/src/interfaces/index.d.ts +8 -0
  44. package/dist/es/src/interfaces/index.d.ts.map +1 -0
  45. package/dist/es/src/interfaces/logger.d.ts +20 -0
  46. package/dist/es/src/interfaces/logger.d.ts.map +1 -0
  47. package/dist/es/{interfaces/data.d.ts → src/interfaces/restful.d.ts} +2 -2
  48. package/dist/es/src/interfaces/restful.d.ts.map +1 -0
  49. package/dist/es/src/managers/commands.d.ts +68 -0
  50. package/dist/es/src/managers/commands.d.ts.map +1 -0
  51. package/dist/es/src/managers/controllers.d.ts +17 -0
  52. package/dist/es/src/managers/controllers.d.ts.map +1 -0
  53. package/dist/es/src/managers/export.d.ts +16 -0
  54. package/dist/es/src/managers/export.d.ts.map +1 -0
  55. package/dist/es/src/managers/index.d.ts +8 -0
  56. package/dist/es/src/managers/index.d.ts.map +1 -0
  57. package/dist/es/src/managers/internal.d.ts +9 -0
  58. package/dist/es/src/managers/internal.d.ts.map +1 -0
  59. package/dist/es/{manager-bases/data.d.ts → src/managers/restful.d.ts} +9 -10
  60. package/dist/es/src/managers/restful.d.ts.map +1 -0
  61. package/dist/es/tsdoc-metadata.json +11 -0
  62. package/dist/es/zenflux-core.d.ts +470 -0
  63. package/dist/es/zenflux-core.js +704 -1017
  64. package/package.json +29 -47
  65. package/src/bases/controller-base.ts +96 -0
  66. package/src/bases/index.ts +2 -0
  67. package/src/bases/object-base.ts +83 -0
  68. package/src/clients/http.ts +48 -35
  69. package/src/command-bases/command-base.ts +34 -38
  70. package/src/command-bases/command-internal.ts +5 -7
  71. package/src/command-bases/command-public.ts +5 -6
  72. package/src/command-bases/command-restful.ts +55 -0
  73. package/src/command-bases/index.ts +5 -5
  74. package/src/errors/command-already-registered.ts +6 -5
  75. package/src/errors/command-not-found.ts +6 -4
  76. package/src/errors/controller-already-registered.ts +6 -5
  77. package/src/errors/controller-already-set.ts +8 -5
  78. package/src/errors/force-method-implementation.ts +23 -0
  79. package/src/errors/index-public.ts +1 -0
  80. package/src/errors/index.ts +5 -5
  81. package/src/exports.ts +47 -9
  82. package/src/index.ts +3 -4
  83. package/src/initializer.ts +13 -13
  84. package/src/interfaces/commands.ts +3 -3
  85. package/src/interfaces/config.ts +10 -3
  86. package/src/interfaces/index.ts +5 -6
  87. package/src/interfaces/logger.ts +18 -0
  88. package/src/interfaces/{data.ts → restful.ts} +10 -10
  89. package/src/{manager-bases → managers}/commands.ts +52 -60
  90. package/src/managers/controllers.ts +39 -0
  91. package/src/managers/export.ts +30 -0
  92. package/src/managers/index.ts +5 -28
  93. package/src/managers/internal.ts +11 -0
  94. package/src/{manager-bases/data.ts → managers/restful.ts} +18 -19
  95. package/LICENSE +0 -21
  96. package/README.md +0 -2
  97. package/dist/es/clients/http.d.ts.map +0 -1
  98. package/dist/es/command-bases/command-base.d.ts +0 -27
  99. package/dist/es/command-bases/command-base.d.ts.map +0 -1
  100. package/dist/es/command-bases/command-data.d.ts +0 -18
  101. package/dist/es/command-bases/command-data.d.ts.map +0 -1
  102. package/dist/es/command-bases/command-internal.d.ts +0 -10
  103. package/dist/es/command-bases/command-internal.d.ts.map +0 -1
  104. package/dist/es/command-bases/command-public.d.ts +0 -10
  105. package/dist/es/command-bases/command-public.d.ts.map +0 -1
  106. package/dist/es/command-bases/index.d.ts +0 -8
  107. package/dist/es/command-bases/index.d.ts.map +0 -1
  108. package/dist/es/core/controller.d.ts +0 -36
  109. package/dist/es/core/controller.d.ts.map +0 -1
  110. package/dist/es/core/index.d.ts +0 -3
  111. package/dist/es/core/index.d.ts.map +0 -1
  112. package/dist/es/core/object-base.d.ts +0 -15
  113. package/dist/es/core/object-base.d.ts.map +0 -1
  114. package/dist/es/errors/command-already-registered.d.ts +0 -9
  115. package/dist/es/errors/command-already-registered.d.ts.map +0 -1
  116. package/dist/es/errors/command-not-found.d.ts +0 -8
  117. package/dist/es/errors/command-not-found.d.ts.map +0 -1
  118. package/dist/es/errors/controller-already-registered.d.ts +0 -9
  119. package/dist/es/errors/controller-already-registered.d.ts.map +0 -1
  120. package/dist/es/errors/controller-already-set.d.ts +0 -8
  121. package/dist/es/errors/controller-already-set.d.ts.map +0 -1
  122. package/dist/es/errors/force-method.d.ts +0 -12
  123. package/dist/es/errors/force-method.d.ts.map +0 -1
  124. package/dist/es/errors/index.d.ts +0 -6
  125. package/dist/es/errors/index.d.ts.map +0 -1
  126. package/dist/es/exports.d.ts +0 -12
  127. package/dist/es/exports.d.ts.map +0 -1
  128. package/dist/es/index.d.ts +0 -6
  129. package/dist/es/index.d.ts.map +0 -1
  130. package/dist/es/initializer.d.ts +0 -22
  131. package/dist/es/initializer.d.ts.map +0 -1
  132. package/dist/es/interfaces/commands.d.ts.map +0 -1
  133. package/dist/es/interfaces/config.d.ts +0 -15
  134. package/dist/es/interfaces/config.d.ts.map +0 -1
  135. package/dist/es/interfaces/data.d.ts.map +0 -1
  136. package/dist/es/interfaces/index.d.ts +0 -9
  137. package/dist/es/interfaces/index.d.ts.map +0 -1
  138. package/dist/es/interfaces/managers.d.ts +0 -14
  139. package/dist/es/interfaces/managers.d.ts.map +0 -1
  140. package/dist/es/interfaces/object-base.d.ts +0 -11
  141. package/dist/es/interfaces/object-base.d.ts.map +0 -1
  142. package/dist/es/manager-bases/commands.d.ts +0 -76
  143. package/dist/es/manager-bases/commands.d.ts.map +0 -1
  144. package/dist/es/manager-bases/controllers.d.ts +0 -19
  145. package/dist/es/manager-bases/controllers.d.ts.map +0 -1
  146. package/dist/es/manager-bases/data.d.ts.map +0 -1
  147. package/dist/es/manager-bases/index.d.ts +0 -8
  148. package/dist/es/manager-bases/index.d.ts.map +0 -1
  149. package/dist/es/manager-bases/internal.d.ts +0 -10
  150. package/dist/es/manager-bases/internal.d.ts.map +0 -1
  151. package/dist/es/managers/index.d.ts +0 -16
  152. package/dist/es/managers/index.d.ts.map +0 -1
  153. package/dist/es/modules/index.d.ts +0 -2
  154. package/dist/es/modules/index.d.ts.map +0 -1
  155. package/dist/es/modules/logger.d.ts +0 -118
  156. package/dist/es/modules/logger.d.ts.map +0 -1
  157. package/dist/es/utils/index.d.ts +0 -3
  158. package/dist/es/utils/index.d.ts.map +0 -1
  159. package/dist/es/utils/object.d.ts +0 -7
  160. package/dist/es/utils/object.d.ts.map +0 -1
  161. package/dist/es/utils/rgb.d.ts +0 -7
  162. package/dist/es/utils/rgb.d.ts.map +0 -1
  163. package/src/command-bases/command-data.ts +0 -54
  164. package/src/core/controller.ts +0 -61
  165. package/src/core/index.ts +0 -2
  166. package/src/core/object-base.ts +0 -46
  167. package/src/errors/force-method.ts +0 -22
  168. package/src/interfaces/managers.ts +0 -16
  169. package/src/interfaces/object-base.ts +0 -12
  170. package/src/manager-bases/controllers.ts +0 -37
  171. package/src/manager-bases/index.ts +0 -7
  172. package/src/manager-bases/internal.ts +0 -13
  173. package/src/modules/index.ts +0 -1
  174. package/src/modules/logger.ts +0 -459
  175. package/src/utils/index.ts +0 -2
  176. package/src/utils/object.ts +0 -18
  177. package/src/utils/rgb.ts +0 -33
  178. package/types/index.d.ts +0 -6
package/package.json CHANGED
@@ -1,49 +1,31 @@
1
1
  {
2
- "name": "@zenflux/core",
3
- "version": "0.0.1-dev.2",
4
- "description": "ZenFlux core",
5
- "keywords": [
6
- "zenflux",
7
- "zen",
8
- "flux",
9
- "commands",
10
- "manager",
11
- "commands manager",
12
- "core",
13
- "zenflux-core"
14
- ],
15
- "repository": {
16
- "type": "git",
17
- "url": "git+https://github.com/zenflux/core.git"
18
- },
19
- "license": "MIT",
20
- "author": "Leonid Vinikov <leonidvinikov@gmail.com> (https://github.com/iNewLegend)",
21
- "main": "dist/cjs/zenflux-core.js",
22
- "unpkg": "dist/umd/zenflux-core.js",
23
- "module": "dist/es/zenflux-core.js",
24
- "types": "types/index.d.ts",
25
- "files": [
26
- "dist",
27
- "src",
28
- "types"
29
- ],
30
- "scripts": {
31
- "test": "export flow_modules_logger=off && jest",
32
- "toolkit-build": "node_modules/@zenflux/rollup-toolkit/bin/run @build",
33
- "toolkit-build-dev": "node_modules/@zenflux/rollup-toolkit/bin/dev @build",
34
- "toolkit-watch": "node_modules/@zenflux/rollup-toolkit/bin/run @watch",
35
- "toolkit-watch-dev": "node_modules/@zenflux/rollup-toolkit/bin/dev @watch"
36
- },
37
- "devDependencies": {
38
- "@babel/plugin-syntax-import-assertions": "^7.20.0",
39
- "@babel/preset-env": "^7.20.2",
40
- "@babel/preset-typescript": "^7.18.6",
41
- "@types/jest": "^29.4.0",
42
- "@typescript-eslint/eslint-plugin": "^5.50.0",
43
- "@typescript-eslint/parser": "^5.50.0",
44
- "@zenflux/rollup-toolkit": "^0.0.0-alpha.15",
45
- "babel-jest": "^29.4.1",
46
- "ts-jest": "^29.0.5",
47
- "tslib": "^2.5.0"
48
- }
2
+ "name": "@zenflux/core",
3
+ "version": "0.0.1-dev.3",
4
+ "module": "dist/es/zenflux-core.js",
5
+ "types": "dist/es/zenflux-core.d.ts",
6
+ "type": "module",
7
+ "files": [
8
+ "dist",
9
+ "src",
10
+ "types"
11
+ ],
12
+ "scripts": {
13
+ "@z-core--test": "jest",
14
+ "@z-core--build": "@zenflux-cli @build",
15
+ "@z-core--dev": "@zenflux-cli:dev @watch",
16
+ "@z-core--d-rollup": "api-extractor run --local"
17
+ },
18
+ "devDependencies": {
19
+ "@zenflux/cli": "workspace:*"
20
+ },
21
+ "exports": {
22
+ ".": {
23
+ "import": "./dist/es/zenflux-core.js",
24
+ "default": "./dist/es/zenflux-core.js"
25
+ },
26
+ "./package.json": "./package.json"
27
+ },
28
+ "publishConfig": {
29
+ "access": "public"
30
+ }
49
31
  }
@@ -0,0 +1,96 @@
1
+ /**
2
+ * The `ControllerBase` class is an abstract base class responsible for managing commands and providing a structure
3
+ * for controllers or command spaces.
4
+ *
5
+ * @author Leonid Vinikov <leonidvinikov@gmail.com>
6
+ */
7
+ import { ObjectBase } from "@z-core/bases/object-base";
8
+
9
+ import { CommandPublic } from "@z-core/command-bases/command-public";
10
+ import { CommandRestful } from "@z-core/command-bases/command-restful";
11
+ import { CommandInternal } from "@z-core/command-bases/command-internal";
12
+
13
+ import * as managers from "@z-core/managers/export";
14
+
15
+ // noinspection TypeScriptAbstractClassConstructorCanBeMadeProtected
16
+
17
+ export abstract class ControllerBase extends ObjectBase {
18
+ private commands: { [ key: string ]: typeof CommandPublic };
19
+ private restful: { [ key: string ]: typeof CommandRestful };
20
+ private internal: { [ key: string ]: typeof CommandInternal };
21
+
22
+ public static getName() {
23
+ return "ZenFlux/Core/ControllerBase";
24
+ }
25
+
26
+ /**
27
+ * Constructor for `ControllerBase` instances.
28
+ * Initializes the base class and invokes the `initialize()` method.
29
+ */
30
+ public constructor() {
31
+ super();
32
+
33
+ // Initialize the controller
34
+ this.initialize();
35
+ }
36
+
37
+ /**
38
+ * Initializes the controller by calling the `register()` method and optionally the `setupHooks()` method.
39
+ */
40
+ protected initialize() {
41
+ // Register commands and command types
42
+ this.register();
43
+
44
+ // Optionally, set up hooks if the `setupHooks()` method is defined in derived classes
45
+ this.setupHooks && this.setupHooks();
46
+ }
47
+
48
+ /**
49
+ * Registers commands and command types by calling manager functions.
50
+ */
51
+ protected register() {
52
+ // Register public commands
53
+ this.commands = managers.commands.register( this.getCommands(), this ) as {
54
+ [ key: string ]: typeof CommandPublic
55
+ };
56
+
57
+ // Register RESTful commands
58
+ this.restful = managers.restful.register( this.getRestful(), this ) as {
59
+ [ key: string ]: typeof CommandRestful
60
+ };
61
+
62
+ // Register internal commands
63
+ this.internal = managers.internal.register( this.getInternal(), this ) as {
64
+ [ key: string ]: typeof CommandInternal
65
+ };
66
+ }
67
+
68
+ /**
69
+ * Retrieve the public commands associated with this controller.
70
+ * Derived classes can override this method to specify their own commands.
71
+ */
72
+ protected getCommands(): { [ key: string ]: typeof CommandPublic } {
73
+ return {};
74
+ }
75
+
76
+ /**
77
+ * Retrieve the RESTful commands associated with this controller.
78
+ * Derived classes can override this method to specify their own RESTful commands.
79
+ */
80
+ protected getRestful(): { [ key: string ]: typeof CommandRestful } {
81
+ return {};
82
+ }
83
+
84
+ /**
85
+ * Retrieve the internal commands associated with this controller.
86
+ * Derived classes can override this method to specify their own internal commands.
87
+ */
88
+ protected getInternal(): { [ key: string ]: typeof CommandInternal } {
89
+ return {};
90
+ }
91
+
92
+ /**
93
+ * A hook method that can be optionally overridden in derived classes to set up hooks or event listeners.
94
+ */
95
+ protected setupHooks?() {}
96
+ }
@@ -0,0 +1,2 @@
1
+ export { ControllerBase } from "@z-core/bases/controller-base";
2
+ export { ObjectBase } from "@z-core/bases/object-base";
@@ -0,0 +1,83 @@
1
+ /**
2
+ * The `ObjectBase` class is a foundational class serves as the base for other classes in the system.
3
+ * It is responsible for managing the name and unique identifier of derived classes.
4
+ * This class provides the basic structure for classes that need to have a unique identifier
5
+ * and a common way to retrieve their names.
6
+ *
7
+ * @author Leonid Vinikov <leonidvinikov@gmail.com>
8
+ */
9
+ import { ForceMethodBase } from "@z-core/errors/force-method-implementation";
10
+
11
+ export abstract class ObjectBase {
12
+ private readonly _id: string;
13
+ private readonly name: string = "__UNDEFINED_NAME__";
14
+
15
+ /**
16
+ * Initializes the name and unique identifier for the instance.
17
+ */
18
+ protected constructor() {
19
+ // Initialize the name by calling the abstract method getName()
20
+ this.name = this.getName();
21
+
22
+ // Generate a unique identifier using a combination of timestamp and random numbers
23
+ const timestamp = performance.now() * 1000000;
24
+ const random1 = BigInt( Math.floor( Math.random() * 1000000 ) );
25
+ const random2 = BigInt( Math.floor( Math.random() * 1000000 ) );
26
+
27
+ this._id = `${ timestamp }${ random1 }${ random2 }`;
28
+ }
29
+
30
+ /**
31
+ * Static method to retrieve the name of the class. This method is abstract and must be implemented
32
+ * by derived classes.
33
+ */
34
+ public static getName(): string {
35
+ throw new ForceMethodBase( "ObjectBase", "getName" );
36
+ }
37
+
38
+ /**
39
+ * Retrieves the name of the current instance by calling the static `getName()` method of the
40
+ * constructor (derived class).
41
+ */
42
+ public getName() {
43
+ return ( this.constructor as typeof ObjectBase ).getName();
44
+ }
45
+
46
+ /**
47
+ * Retrieves the unique identifier of the current instance.
48
+ */
49
+ public getUniqueId() {
50
+ return this._id;
51
+ }
52
+
53
+ /**
54
+ * Retrieves the initial name that was set during construction. This name may be "__UNDEFINED_NAME__"
55
+ * if the `getName()` method of the derived class does not provide a valid name.
56
+ */
57
+ public getInitialName() {
58
+ return this.name;
59
+ }
60
+
61
+ /**
62
+ * Retrieves an array of hierarchical class names from the current instance up to the `ObjectBase` class.
63
+ */
64
+ public getHierarchyNames() {
65
+ let classNames = [];
66
+ let obj = Object.getPrototypeOf( this );
67
+ let className: string;
68
+
69
+ // Traverse the prototype chain and collect class names until the base ObjectBase class is reached.
70
+ while ( ( className = obj.getName() ) !== "Object" ) {
71
+ classNames.push( className );
72
+ obj = Object.getPrototypeOf( obj );
73
+
74
+ // Break the loop if the prototype's constructor is ObjectBase (reached the base class).
75
+ if ( obj.constructor === ObjectBase ) {
76
+ break;
77
+ }
78
+ }
79
+
80
+ return classNames;
81
+ }
82
+ }
83
+
@@ -1,77 +1,83 @@
1
1
  /**
2
- * @author: Leonid Vinikov <leonidvinikov@gmail.com>
3
- * @description: Providing a simple wrapper for fetch API.
2
+ * The `Http` class provides a simple wrapper for the Fetch API.
3
+ *
4
+ * @author Leonid Vinikov <leonidvinikov@gmail.com>
4
5
  */
5
- import ObjectBase from "../core/object-base";
6
-
7
- import Logger from '../modules/logger';
8
6
 
9
7
  import {
10
8
  E_HTTP_METHOD_TYPE,
9
+
10
+ ILogger,
11
+
11
12
  TErrorHandlerCallbackType,
12
13
  TResponseFilterCallbackType,
13
14
  TResponseHandlerCallbackType,
14
- } from "../interfaces/";
15
+ } from "@z-core/interfaces";
16
+
17
+ import { ObjectBase } from "@z-core/bases/object-base";
15
18
 
16
19
  // noinspection ExceptionCaughtLocallyJS
17
20
 
18
21
  export class Http extends ObjectBase {
19
- private readonly logger: Logger;
20
-
22
+ private readonly logger: ILogger;
21
23
  private readonly apiBaseUrl: string;
22
-
23
24
  private readonly requestInit: RequestInit;
24
25
 
25
26
  private errorHandler?: TErrorHandlerCallbackType = undefined;
26
27
  private responseFilter?: TResponseFilterCallbackType = undefined;
27
28
  private responseHandler?: TResponseHandlerCallbackType = undefined;
28
29
 
29
- static getName() {
30
- return 'Core/Clients/Http';
30
+ public static getName(): string {
31
+ return "ZenFlux/Core/Clients/Http";
31
32
  }
32
33
 
33
34
  /**
34
- * Function constructor() : Create the http.
35
+ * Initializes the base class and sets up configuration parameters.
35
36
  */
36
- constructor( apiBaseUrl = 'http://localhost', requestInit: RequestInit = { 'credentials': 'include' } ) {
37
+ public constructor( apiBaseUrl = "http://localhost", requestInit: RequestInit = { "credentials": "include" } ) {
37
38
  super();
38
39
 
39
- this.logger = new Logger( Http.getName(), true );
40
-
41
- this.logger.startWith( { apiBaseUrl } );
40
+ this.logger = new ZenCore.classes.Logger( Http );
42
41
 
43
- this.apiBaseUrl = apiBaseUrl + '/';
42
+ this.logger.startsWith( this.constructor, { apiBaseUrl } );
44
43
 
44
+ this.apiBaseUrl = apiBaseUrl + "/";
45
45
  this.requestInit = requestInit;
46
46
  }
47
47
 
48
48
  /**
49
- * Function fetch() : Fetch api.
49
+ * Fetches data from the specified path using the given HTTP method and optional request body.
50
50
  */
51
- async fetch( path: string, method: E_HTTP_METHOD_TYPE, body: any = {} ) {
52
- this.logger.startWith( { path, method, body } );
51
+ public async fetch( path: string, method: E_HTTP_METHOD_TYPE, body: any = {} ) {
52
+ this.logger.startsWith( this.constructor, { path, method, body } );
53
53
 
54
- const params = Object.assign( {}, this.requestInit ),
55
- headers = {};
54
+ const params = Object.assign( {}, this.requestInit );
55
+ const headers = {};
56
56
 
57
57
  if ( method === E_HTTP_METHOD_TYPE.GET ) {
58
58
  Object.assign( params, { headers } );
59
59
  } else {
60
- Object.assign( headers, { 'Content-Type': 'application/json' } );
60
+ Object.assign( headers, { "Content-Type": "application/json" } );
61
61
  Object.assign( params, {
62
62
  method,
63
63
  headers: headers,
64
- body: JSON.stringify( body )
64
+ body: JSON.stringify( body ),
65
65
  } );
66
66
  }
67
67
 
68
- const response = await globalThis.fetch( this.apiBaseUrl + path, params );
68
+ const fetchPromise = globalThis.fetch( this.apiBaseUrl + path, params );
69
+
70
+ let response = await fetchPromise;
71
+
72
+ if ( ! response ) {
73
+ return false;
74
+ }
69
75
 
70
76
  let data = undefined;
71
77
 
72
78
  try {
73
79
  if ( ! response.ok ) {
74
- throw response;
80
+ throw new Error( response.statusText );
75
81
  }
76
82
 
77
83
  let responseText = await response.text();
@@ -79,7 +85,7 @@ export class Http extends ObjectBase {
79
85
  responseText = this.applyResponseFilter( responseText );
80
86
 
81
87
  // TODO: Currently support JSON and plain text.
82
- if ( response.headers?.get( 'Content-Type' )?.includes( 'application/json' ) ) {
88
+ if ( response.headers?.get( "Content-Type" )?.includes( "application/json" ) ) {
83
89
  data = JSON.parse( responseText );
84
90
  } else {
85
91
  data = responseText;
@@ -96,37 +102,46 @@ export class Http extends ObjectBase {
96
102
  await Promise.reject( e );
97
103
  }
98
104
 
99
- this.logger.drop( { path }, data );
105
+ this.logger.drop( this.fetch, { path }, data );
100
106
 
101
107
  return data;
102
108
  }
103
109
 
110
+ /**
111
+ * Sets the error handler callback for handling errors during fetch requests.
112
+ */
104
113
  public setErrorHandler( callback: TErrorHandlerCallbackType ) {
105
114
  if ( this.errorHandler ) {
106
- throw new Error( 'Error handler already set.' );
115
+ throw new Error( "Error handler already set." );
107
116
  }
108
117
 
109
118
  this.errorHandler = callback;
110
119
  }
111
120
 
121
+ /**
122
+ * Sets the response filter callback for filtering the response text.
123
+ */
112
124
  public setResponseFilter( callback: TResponseFilterCallbackType ) {
113
125
  if ( this.responseFilter ) {
114
- throw new Error( 'Response filter already set.' );
126
+ throw new Error( "Response filter already set." );
115
127
  }
116
128
 
117
129
  this.responseFilter = callback;
118
130
  }
119
131
 
132
+ /**
133
+ * Sets the response handler callback for handling the response data.
134
+ */
120
135
  public setResponseHandler( callback: TResponseHandlerCallbackType ) {
121
136
  if ( this.responseHandler ) {
122
- throw new Error( 'Response handler already set.' );
137
+ throw new Error( "Response handler already set." );
123
138
  }
124
139
 
125
140
  this.responseHandler = callback;
126
141
  }
127
142
 
128
143
  private applyErrorHandler( error: any ) {
129
- return this.errorHandler && this.errorHandler( error );
144
+ return !! this.errorHandler && this.errorHandler( error );
130
145
  }
131
146
 
132
147
  private applyResponseFilter( text: string ) {
@@ -134,8 +149,6 @@ export class Http extends ObjectBase {
134
149
  }
135
150
 
136
151
  private applyResponseHandler( text: string ) {
137
- return this.responseHandler && this.responseHandler( text );
152
+ return !! this.responseHandler && this.responseHandler( text );
138
153
  }
139
154
  }
140
-
141
- export default Http;
@@ -1,31 +1,30 @@
1
1
  /**
2
- * @author: Leonid Vinikov <leonidvinikov@gmail.com>
3
- * @description: Base for all commands, they are all will be managed by the `Commands` managers.
4
- * @TODO
2
+ * @author Leonid Vinikov <leonidvinikov@gmail.com>
3
+ * @description Base for all commands, they are all will be managed by the `Commands` managers.
4
+ *
5
+ * TODO
5
6
  * - Add validateArgs method or find something better.
6
7
  */
7
- import ObjectBase from "../core/object-base";
8
- import Controller from "../core/controller";
8
+ import { ControllerBase, ObjectBase } from "@z-core/bases";
9
+ import { ControllerAlreadySet, ForceMethodImplementation } from "@z-core/errors";
9
10
 
10
- import ForceMethod from "../errors/force-method";
11
-
12
- import Logger from "../modules/logger";
13
-
14
- import { ControllerAlreadySet } from "../errors";
15
-
16
- import { ICommandArgsInterface, ICommandOptionsInterface } from "../interfaces/";
11
+ import { ICommandArgsInterface, ICommandOptionsInterface, ILogger } from "@z-core/interfaces";
17
12
 
18
13
  export abstract class CommandBase extends ObjectBase {
19
- private static controller: Controller;
14
+ private static controller: ControllerBase;
20
15
 
21
16
  protected args: ICommandArgsInterface = {};
22
17
  protected options: ICommandOptionsInterface = {};
23
18
 
24
- private readonly logger: Logger;
19
+ private readonly logger: ILogger;
20
+
21
+ public static getName() {
22
+ return "ZenFlux/Core/CommandBases/CommandBase";
23
+ }
25
24
 
26
- public static setController( controller: Controller ) {
25
+ public static setController( controller: ControllerBase ) {
27
26
  if ( this.controller ) {
28
- throw new ControllerAlreadySet();
27
+ throw new ControllerAlreadySet( this.controller );
29
28
  }
30
29
 
31
30
  this.controller = controller;
@@ -35,30 +34,41 @@ export abstract class CommandBase extends ObjectBase {
35
34
  return this.controller;
36
35
  }
37
36
 
38
- constructor( args: ICommandArgsInterface = {}, options = {} ) {
37
+ public constructor( args: ICommandArgsInterface = {}, options = {} ) {
39
38
  super();
40
39
 
41
- const name = ( this.constructor as typeof CommandBase ).getName();
40
+ const type = ( this.constructor as typeof CommandBase );
42
41
 
43
- this.logger = new Logger( name, true, {
44
- sameColor: true,
42
+ this.logger = new ZenCore.classes.Logger( type, {
43
+ // Happens or occurs many times, often in a similar or identical manner.
44
+ repeatedly: true,
45
45
  } );
46
46
 
47
- this.logger.startWith( { args, options } );
47
+ this.logger.startsWith( this.constructor, { args, options } );
48
48
 
49
49
  this.initialize( args, options );
50
50
  }
51
51
 
52
- public initialize( args: ICommandArgsInterface, options: ICommandOptionsInterface ) {
52
+ protected initialize( args: ICommandArgsInterface, options: ICommandOptionsInterface ) {
53
53
  this.args = args;
54
54
  this.options = options;
55
55
  }
56
56
 
57
- public apply( args = this.args, options = this.options ): any {// eslint-disable-line @typescript-eslint/no-unused-vars
58
- throw new ForceMethod( this, "apply" );
57
+ /**
58
+ * TODO: Maybe abstract, currently not sure about parameters initialization.
59
+ */
60
+ protected apply( args = this.args, options = this.options ): any {// eslint-disable-line @typescript-eslint/no-unused-vars
61
+ throw new ForceMethodImplementation( this, "apply" );
59
62
  }
60
63
 
64
+ protected onBeforeApply?():void;
65
+ protected onAfterApply?():void;
66
+
61
67
  public async run() {
68
+ return this.runInternal();
69
+ }
70
+
71
+ private async runInternal() {
62
72
  this.onBeforeApply && this.onBeforeApply();
63
73
 
64
74
  const result = await this.apply( this.args, this.options );
@@ -67,18 +77,4 @@ export abstract class CommandBase extends ObjectBase {
67
77
 
68
78
  return result;
69
79
  }
70
-
71
- public getArgs() {
72
- return this.args;
73
- }
74
-
75
- public getOptions() {
76
- return this.options;
77
- }
78
-
79
- public onBeforeApply?():void;
80
-
81
- public onAfterApply?():void;
82
80
  }
83
-
84
- export default CommandBase;
@@ -1,13 +1,11 @@
1
1
  /**
2
- * @author: Leonid Vinikov <leonidvinikov@gmail.com>
3
- * @description: CommandInternal, is used when part of the logic needed to be in the command but not represent a user action.
2
+ * @author Leonid Vinikov <leonidvinikov@gmail.com>
3
+ * @description CommandInternal, is used when part of the logic needed to be in the command but not represent a user action.
4
4
  */
5
- import CommandBase from "./command-base";
5
+ import { CommandBase } from "@z-core/command-bases/command-base";
6
6
 
7
7
  export class CommandInternal extends CommandBase {
8
- static getName() {
9
- return 'Core/CommandBases/CommandInternal';
8
+ public static getName() {
9
+ return "ZenFlux/Core/CommandBases/CommandInternal";
10
10
  }
11
11
  }
12
-
13
- export default CommandInternal;
@@ -1,13 +1,12 @@
1
1
  /**
2
- * @author: Leonid Vinikov <leonidvinikov@gmail.com>
3
- * @description: CommandPublic represents a USER action, every class which inherit from this class will USER action.
2
+ * @author Leonid Vinikov <leonidvinikov@gmail.com>
3
+ * @description CommandPublic represents a USER action, every class which inherit from this class will USER action.
4
4
  * */
5
- import CommandBase from "./command-base";
5
+ import { CommandBase } from "@z-core/command-bases/command-base";
6
6
 
7
7
  export class CommandPublic extends CommandBase {
8
- static getName() {
9
- return 'Core/CommandBases/CommandPublic';
8
+ public static getName() {
9
+ return "ZenFlux/Core/CommandBases/CommandPublic";
10
10
  }
11
11
  }
12
12
 
13
- export default CommandPublic;
@@ -0,0 +1,55 @@
1
+ /**
2
+ * @author Leonid Vinikov <leonidvinikov@gmail.com>
3
+ * @description Each rest command should represent a final REST endpoint.
4
+ */
5
+ import { CommandBase } from "@z-core/command-bases/command-base";
6
+
7
+ import { ForceMethodImplementation } from "@z-core/errors";
8
+
9
+ import * as managers from "@z-core/managers/export";
10
+
11
+ export abstract class CommandRestful extends CommandBase {
12
+ public static getName() {
13
+ return "ZenFlux/Core/CommandBases/CommandRestful";
14
+ }
15
+
16
+ /**
17
+ * Override this method is required to determine endpoint and magic query params.
18
+ *
19
+ * @example
20
+ * ```
21
+ * args = { query: { id: 1 } };
22
+ * getEndpoint() = '/api/v1/users/{id}';
23
+ * result = '/api/v1/users/1';
24
+ * ```
25
+ */
26
+ public getEndpoint(): string {
27
+ throw new ForceMethodImplementation( this, "getEndpoint" );
28
+ }
29
+
30
+ protected apply( args = this.args, options = this.options ) {// eslint-disable-line @typescript-eslint/no-unused-vars
31
+ const endpoint = this.applyEndpointFormat( this.getEndpoint(), args );
32
+
33
+ return managers.restful.getClient().fetch(
34
+ endpoint, managers.restful.currentHttpMethod,
35
+ args || null
36
+ );
37
+ }
38
+
39
+ private applyEndpointFormat( endpoint: string, data: any = {} ): string {
40
+ // Replace query with `magic` placeholders.
41
+ if ( endpoint.includes( "{" ) ) {
42
+ endpoint = endpoint.split( "/" ).map( ( endpointPart ) => {
43
+ const match = endpointPart.match( "\\{(.*?)\\}" );
44
+
45
+ if ( match?.length && "undefined" !== typeof data[ match[ 1 ] ] ) {
46
+ return data[ match[ 1 ] ];
47
+ }
48
+
49
+ return endpointPart;
50
+ } ).join( "/" );
51
+ }
52
+
53
+ return endpoint;
54
+ }
55
+ }
@@ -1,7 +1,7 @@
1
1
  /**
2
- * @author: Leonid Vinikov <leonidvinikov@gmail.com>
2
+ * @author Leonid Vinikov <leonidvinikov@gmail.com>
3
3
  */
4
- export { CommandPublic } from "./command-public";
5
- export { CommandData } from "./command-data";
6
- export { CommandBase } from "./command-base";
7
- export { CommandInternal } from "./command-internal";
4
+ export { CommandPublic } from "@z-core/command-bases/command-public";
5
+ export { CommandRestful } from "@z-core/command-bases/command-restful";
6
+ export { CommandBase } from "@z-core/command-bases/command-base";
7
+ export { CommandInternal } from "@z-core/command-bases/command-internal";