@zenflux/core 0.0.1-dev.2 → 0.1.0

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 (121) hide show
  1. package/package.json +30 -48
  2. package/src/bases/controller-base.ts +96 -0
  3. package/src/bases/index.ts +2 -0
  4. package/src/bases/object-base.ts +83 -0
  5. package/src/clients/http.ts +48 -37
  6. package/src/command-bases/command-base.ts +35 -37
  7. package/src/command-bases/command-internal.ts +5 -7
  8. package/src/command-bases/command-public.ts +5 -6
  9. package/src/command-bases/command-restful.ts +55 -0
  10. package/src/command-bases/index.ts +5 -5
  11. package/src/errors/command-already-registered.ts +6 -5
  12. package/src/errors/command-not-found.ts +6 -4
  13. package/src/errors/controller-already-registered.ts +6 -5
  14. package/src/errors/controller-already-set.ts +8 -5
  15. package/src/errors/force-method-implementation.ts +23 -0
  16. package/src/errors/index-public.ts +1 -0
  17. package/src/errors/index.ts +5 -5
  18. package/src/exports-index.ts +13 -0
  19. package/src/exports.ts +67 -9
  20. package/src/index.ts +12 -3
  21. package/src/interfaces/commands.ts +3 -3
  22. package/src/interfaces/config.ts +10 -3
  23. package/src/interfaces/index.ts +5 -6
  24. package/src/interfaces/logger.ts +18 -0
  25. package/src/interfaces/{data.ts → restful.ts} +10 -10
  26. package/src/{manager-bases → managers}/commands.ts +52 -61
  27. package/src/managers/controllers.ts +40 -0
  28. package/src/managers/export.ts +30 -0
  29. package/src/managers/index.ts +5 -28
  30. package/src/managers/internal.ts +11 -0
  31. package/src/{manager-bases/data.ts → managers/restful.ts} +25 -24
  32. package/LICENSE +0 -21
  33. package/README.md +0 -2
  34. package/dist/es/clients/http.d.ts +0 -31
  35. package/dist/es/clients/http.d.ts.map +0 -1
  36. package/dist/es/command-bases/command-base.d.ts +0 -27
  37. package/dist/es/command-bases/command-base.d.ts.map +0 -1
  38. package/dist/es/command-bases/command-data.d.ts +0 -18
  39. package/dist/es/command-bases/command-data.d.ts.map +0 -1
  40. package/dist/es/command-bases/command-internal.d.ts +0 -10
  41. package/dist/es/command-bases/command-internal.d.ts.map +0 -1
  42. package/dist/es/command-bases/command-public.d.ts +0 -10
  43. package/dist/es/command-bases/command-public.d.ts.map +0 -1
  44. package/dist/es/command-bases/index.d.ts +0 -8
  45. package/dist/es/command-bases/index.d.ts.map +0 -1
  46. package/dist/es/core/controller.d.ts +0 -36
  47. package/dist/es/core/controller.d.ts.map +0 -1
  48. package/dist/es/core/index.d.ts +0 -3
  49. package/dist/es/core/index.d.ts.map +0 -1
  50. package/dist/es/core/object-base.d.ts +0 -15
  51. package/dist/es/core/object-base.d.ts.map +0 -1
  52. package/dist/es/errors/command-already-registered.d.ts +0 -9
  53. package/dist/es/errors/command-already-registered.d.ts.map +0 -1
  54. package/dist/es/errors/command-not-found.d.ts +0 -8
  55. package/dist/es/errors/command-not-found.d.ts.map +0 -1
  56. package/dist/es/errors/controller-already-registered.d.ts +0 -9
  57. package/dist/es/errors/controller-already-registered.d.ts.map +0 -1
  58. package/dist/es/errors/controller-already-set.d.ts +0 -8
  59. package/dist/es/errors/controller-already-set.d.ts.map +0 -1
  60. package/dist/es/errors/force-method.d.ts +0 -12
  61. package/dist/es/errors/force-method.d.ts.map +0 -1
  62. package/dist/es/errors/index.d.ts +0 -6
  63. package/dist/es/errors/index.d.ts.map +0 -1
  64. package/dist/es/exports.d.ts +0 -12
  65. package/dist/es/exports.d.ts.map +0 -1
  66. package/dist/es/index.d.ts +0 -6
  67. package/dist/es/index.d.ts.map +0 -1
  68. package/dist/es/initializer.d.ts +0 -22
  69. package/dist/es/initializer.d.ts.map +0 -1
  70. package/dist/es/interfaces/commands.d.ts +0 -14
  71. package/dist/es/interfaces/commands.d.ts.map +0 -1
  72. package/dist/es/interfaces/config.d.ts +0 -15
  73. package/dist/es/interfaces/config.d.ts.map +0 -1
  74. package/dist/es/interfaces/data.d.ts +0 -19
  75. package/dist/es/interfaces/data.d.ts.map +0 -1
  76. package/dist/es/interfaces/index.d.ts +0 -9
  77. package/dist/es/interfaces/index.d.ts.map +0 -1
  78. package/dist/es/interfaces/managers.d.ts +0 -14
  79. package/dist/es/interfaces/managers.d.ts.map +0 -1
  80. package/dist/es/interfaces/object-base.d.ts +0 -11
  81. package/dist/es/interfaces/object-base.d.ts.map +0 -1
  82. package/dist/es/manager-bases/commands.d.ts +0 -76
  83. package/dist/es/manager-bases/commands.d.ts.map +0 -1
  84. package/dist/es/manager-bases/controllers.d.ts +0 -19
  85. package/dist/es/manager-bases/controllers.d.ts.map +0 -1
  86. package/dist/es/manager-bases/data.d.ts +0 -26
  87. package/dist/es/manager-bases/data.d.ts.map +0 -1
  88. package/dist/es/manager-bases/index.d.ts +0 -8
  89. package/dist/es/manager-bases/index.d.ts.map +0 -1
  90. package/dist/es/manager-bases/internal.d.ts +0 -10
  91. package/dist/es/manager-bases/internal.d.ts.map +0 -1
  92. package/dist/es/managers/index.d.ts +0 -16
  93. package/dist/es/managers/index.d.ts.map +0 -1
  94. package/dist/es/modules/index.d.ts +0 -2
  95. package/dist/es/modules/index.d.ts.map +0 -1
  96. package/dist/es/modules/logger.d.ts +0 -118
  97. package/dist/es/modules/logger.d.ts.map +0 -1
  98. package/dist/es/utils/index.d.ts +0 -3
  99. package/dist/es/utils/index.d.ts.map +0 -1
  100. package/dist/es/utils/object.d.ts +0 -7
  101. package/dist/es/utils/object.d.ts.map +0 -1
  102. package/dist/es/utils/rgb.d.ts +0 -7
  103. package/dist/es/utils/rgb.d.ts.map +0 -1
  104. package/dist/es/zenflux-core.js +0 -2235
  105. package/src/command-bases/command-data.ts +0 -54
  106. package/src/core/controller.ts +0 -61
  107. package/src/core/index.ts +0 -2
  108. package/src/core/object-base.ts +0 -46
  109. package/src/errors/force-method.ts +0 -22
  110. package/src/initializer.ts +0 -56
  111. package/src/interfaces/managers.ts +0 -16
  112. package/src/interfaces/object-base.ts +0 -12
  113. package/src/manager-bases/controllers.ts +0 -37
  114. package/src/manager-bases/index.ts +0 -7
  115. package/src/manager-bases/internal.ts +0 -13
  116. package/src/modules/index.ts +0 -1
  117. package/src/modules/logger.ts +0 -459
  118. package/src/utils/index.ts +0 -2
  119. package/src/utils/object.ts +0 -18
  120. package/src/utils/rgb.ts +0 -33
  121. 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
- }
49
- }
2
+ "name": "@zenflux/core",
3
+ "version": "0.1.0",
4
+ "types": "dist/zenflux-core.d.ts",
5
+ "type": "module",
6
+ "files": [
7
+ "dist",
8
+ "src",
9
+ "types"
10
+ ],
11
+ "scripts": {
12
+ "@z-core--test": "jest",
13
+ "@z-core--build": "@z-cli @build",
14
+ "@z-core--dev": "@z-cli @watch --dev"
15
+ },
16
+ "exports": {
17
+ ".": {
18
+ "import": "./dist/zenflux-core.es.js",
19
+ "default": "./dist/zenflux-core.es.js",
20
+ "require": "./dist/zenflux-core.cjs.js",
21
+ "types": "./dist/zenflux-core.d.ts"
22
+ },
23
+ "./package.json": "./package.json"
24
+ },
25
+ "devDependencies": {
26
+ "@zenflux/cli": "0.1.0"
27
+ },
28
+ "publishConfig": {
29
+ "access": "public"
30
+ }
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 * as managers from "@z-core/managers/export";
10
+
11
+ import type { CommandPublic } from "@z-core/command-bases/command-public";
12
+ import type { CommandRestful } from "@z-core/command-bases/command-restful";
13
+ import type { CommandInternal } from "@z-core/command-bases/command-internal";
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,81 @@
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
+ import { E_HTTP_METHOD_TYPE } from "@z-core/interfaces";
6
7
 
7
- import Logger from '../modules/logger';
8
+ import { ObjectBase } from "@z-core/bases/object-base";
8
9
 
9
- import {
10
- E_HTTP_METHOD_TYPE,
10
+ import type {
11
+ ILogger,
11
12
  TErrorHandlerCallbackType,
12
13
  TResponseFilterCallbackType,
13
- TResponseHandlerCallbackType,
14
- } from "../interfaces/";
14
+ TResponseHandlerCallbackType
15
+ } from "@z-core/interfaces";
15
16
 
16
17
  // noinspection ExceptionCaughtLocallyJS
17
18
 
18
19
  export class Http extends ObjectBase {
19
- private readonly logger: Logger;
20
-
20
+ private readonly logger: ILogger;
21
21
  private readonly apiBaseUrl: string;
22
-
23
22
  private readonly requestInit: RequestInit;
24
23
 
25
24
  private errorHandler?: TErrorHandlerCallbackType = undefined;
26
25
  private responseFilter?: TResponseFilterCallbackType = undefined;
27
26
  private responseHandler?: TResponseHandlerCallbackType = undefined;
28
27
 
29
- static getName() {
30
- return 'Core/Clients/Http';
28
+ public static getName(): string {
29
+ return "ZenFlux/Core/Clients/Http";
31
30
  }
32
31
 
33
32
  /**
34
- * Function constructor() : Create the http.
33
+ * Initializes the base class and sets up configuration parameters.
35
34
  */
36
- constructor( apiBaseUrl = 'http://localhost', requestInit: RequestInit = { 'credentials': 'include' } ) {
35
+ public constructor( apiBaseUrl = "http://localhost", requestInit: RequestInit = { "credentials": "include" } ) {
37
36
  super();
38
37
 
39
- this.logger = new Logger( Http.getName(), true );
38
+ this.logger = new zCore.classes.Logger( Http );
40
39
 
41
- this.logger.startWith( { apiBaseUrl } );
42
-
43
- this.apiBaseUrl = apiBaseUrl + '/';
40
+ this.logger.startsWith( this.constructor, { apiBaseUrl } );
44
41
 
42
+ this.apiBaseUrl = apiBaseUrl + "/";
45
43
  this.requestInit = requestInit;
46
44
  }
47
45
 
48
46
  /**
49
- * Function fetch() : Fetch api.
47
+ * Fetches data from the specified path using the given HTTP method and optional request body.
50
48
  */
51
- async fetch( path: string, method: E_HTTP_METHOD_TYPE, body: any = {} ) {
52
- this.logger.startWith( { path, method, body } );
49
+ public async fetch( path: string, method: E_HTTP_METHOD_TYPE, body: any = {} ) {
50
+ this.logger.startsWith( this.constructor, { path, method, body } );
53
51
 
54
- const params = Object.assign( {}, this.requestInit ),
55
- headers = {};
52
+ const params = Object.assign( {}, this.requestInit );
53
+ const headers = {};
56
54
 
57
55
  if ( method === E_HTTP_METHOD_TYPE.GET ) {
58
56
  Object.assign( params, { headers } );
59
57
  } else {
60
- Object.assign( headers, { 'Content-Type': 'application/json' } );
58
+ Object.assign( headers, { "Content-Type": "application/json" } );
61
59
  Object.assign( params, {
62
60
  method,
63
61
  headers: headers,
64
- body: JSON.stringify( body )
62
+ body: JSON.stringify( body ),
65
63
  } );
66
64
  }
67
65
 
68
- const response = await globalThis.fetch( this.apiBaseUrl + path, params );
66
+ const fetchPromise = globalThis.fetch( this.apiBaseUrl + path, params );
67
+
68
+ let response = await fetchPromise;
69
+
70
+ if ( ! response ) {
71
+ return false;
72
+ }
69
73
 
70
74
  let data = undefined;
71
75
 
72
76
  try {
73
77
  if ( ! response.ok ) {
74
- throw response;
78
+ throw new Error( response.statusText );
75
79
  }
76
80
 
77
81
  let responseText = await response.text();
@@ -79,7 +83,7 @@ export class Http extends ObjectBase {
79
83
  responseText = this.applyResponseFilter( responseText );
80
84
 
81
85
  // TODO: Currently support JSON and plain text.
82
- if ( response.headers?.get( 'Content-Type' )?.includes( 'application/json' ) ) {
86
+ if ( response.headers?.get( "Content-Type" )?.includes( "application/json" ) ) {
83
87
  data = JSON.parse( responseText );
84
88
  } else {
85
89
  data = responseText;
@@ -96,37 +100,46 @@ export class Http extends ObjectBase {
96
100
  await Promise.reject( e );
97
101
  }
98
102
 
99
- this.logger.drop( { path }, data );
103
+ this.logger.drop( this.fetch, { path }, data );
100
104
 
101
105
  return data;
102
106
  }
103
107
 
108
+ /**
109
+ * Sets the error handler callback for handling errors during fetch requests.
110
+ */
104
111
  public setErrorHandler( callback: TErrorHandlerCallbackType ) {
105
112
  if ( this.errorHandler ) {
106
- throw new Error( 'Error handler already set.' );
113
+ throw new Error( "Error handler already set." );
107
114
  }
108
115
 
109
116
  this.errorHandler = callback;
110
117
  }
111
118
 
119
+ /**
120
+ * Sets the response filter callback for filtering the response text.
121
+ */
112
122
  public setResponseFilter( callback: TResponseFilterCallbackType ) {
113
123
  if ( this.responseFilter ) {
114
- throw new Error( 'Response filter already set.' );
124
+ throw new Error( "Response filter already set." );
115
125
  }
116
126
 
117
127
  this.responseFilter = callback;
118
128
  }
119
129
 
130
+ /**
131
+ * Sets the response handler callback for handling the response data.
132
+ */
120
133
  public setResponseHandler( callback: TResponseHandlerCallbackType ) {
121
134
  if ( this.responseHandler ) {
122
- throw new Error( 'Response handler already set.' );
135
+ throw new Error( "Response handler already set." );
123
136
  }
124
137
 
125
138
  this.responseHandler = callback;
126
139
  }
127
140
 
128
141
  private applyErrorHandler( error: any ) {
129
- return this.errorHandler && this.errorHandler( error );
142
+ return !! this.errorHandler && this.errorHandler( error );
130
143
  }
131
144
 
132
145
  private applyResponseFilter( text: string ) {
@@ -134,8 +147,6 @@ export class Http extends ObjectBase {
134
147
  }
135
148
 
136
149
  private applyResponseHandler( text: string ) {
137
- return this.responseHandler && this.responseHandler( text );
150
+ return !! this.responseHandler && this.responseHandler( text );
138
151
  }
139
152
  }
140
-
141
- export default Http;
@@ -1,31 +1,32 @@
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 { ObjectBase } from "@z-core/bases";
9
+ import { ControllerAlreadySet, ForceMethodImplementation } from "@z-core/errors";
9
10
 
10
- import ForceMethod from "../errors/force-method";
11
+ import type { ControllerBase} from "@z-core/bases";
11
12
 
12
- import Logger from "../modules/logger";
13
-
14
- import { ControllerAlreadySet } from "../errors";
15
-
16
- import { ICommandArgsInterface, ICommandOptionsInterface } from "../interfaces/";
13
+ import type { ICommandArgsInterface, ICommandOptionsInterface, ILogger } from "@z-core/interfaces";
17
14
 
18
15
  export abstract class CommandBase extends ObjectBase {
19
- private static controller: Controller;
16
+ private static controller: ControllerBase;
20
17
 
21
18
  protected args: ICommandArgsInterface = {};
22
19
  protected options: ICommandOptionsInterface = {};
23
20
 
24
- private readonly logger: Logger;
21
+ private readonly logger: ILogger;
22
+
23
+ public static getName() {
24
+ return "ZenFlux/Core/CommandBases/CommandBase";
25
+ }
25
26
 
26
- public static setController( controller: Controller ) {
27
+ public static setController( controller: ControllerBase ) {
27
28
  if ( this.controller ) {
28
- throw new ControllerAlreadySet();
29
+ throw new ControllerAlreadySet( this.controller );
29
30
  }
30
31
 
31
32
  this.controller = controller;
@@ -35,30 +36,41 @@ export abstract class CommandBase extends ObjectBase {
35
36
  return this.controller;
36
37
  }
37
38
 
38
- constructor( args: ICommandArgsInterface = {}, options = {} ) {
39
+ public constructor( args: ICommandArgsInterface = {}, options = {} ) {
39
40
  super();
40
41
 
41
- const name = ( this.constructor as typeof CommandBase ).getName();
42
+ const type = ( this.constructor as typeof CommandBase );
42
43
 
43
- this.logger = new Logger( name, true, {
44
- sameColor: true,
44
+ this.logger = new zCore.classes.Logger( type, {
45
+ // Happens or occurs many times, often in a similar or identical manner.
46
+ repeatedly: true,
45
47
  } );
46
48
 
47
- this.logger.startWith( { args, options } );
49
+ this.logger.startsWith( this.constructor, { args, options } );
48
50
 
49
51
  this.initialize( args, options );
50
52
  }
51
53
 
52
- public initialize( args: ICommandArgsInterface, options: ICommandOptionsInterface ) {
54
+ protected initialize( args: ICommandArgsInterface, options: ICommandOptionsInterface ) {
53
55
  this.args = args;
54
56
  this.options = options;
55
57
  }
56
58
 
57
- public apply( args = this.args, options = this.options ): any {// eslint-disable-line @typescript-eslint/no-unused-vars
58
- throw new ForceMethod( this, "apply" );
59
+ /**
60
+ * TODO: Maybe abstract, currently not sure about parameters initialization.
61
+ */
62
+ protected apply( args = this.args, options = this.options ): any {// eslint-disable-line @typescript-eslint/no-unused-vars
63
+ throw new ForceMethodImplementation( this, "apply" );
59
64
  }
60
65
 
66
+ protected onBeforeApply?():void;
67
+ protected onAfterApply?():void;
68
+
61
69
  public async run() {
70
+ return this.runInternal();
71
+ }
72
+
73
+ private async runInternal() {
62
74
  this.onBeforeApply && this.onBeforeApply();
63
75
 
64
76
  const result = await this.apply( this.args, this.options );
@@ -67,18 +79,4 @@ export abstract class CommandBase extends ObjectBase {
67
79
 
68
80
  return result;
69
81
  }
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
82
  }
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";