@zenflux/core 0.0.1-dev.1 → 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.
- package/dist/es/src/bases/controller-base.d.ts +55 -0
- package/dist/es/src/bases/controller-base.d.ts.map +1 -0
- package/dist/es/src/bases/index.d.ts +3 -0
- package/dist/es/src/bases/index.d.ts.map +1 -0
- package/dist/es/src/bases/object-base.d.ts +32 -0
- package/dist/es/src/bases/object-base.d.ts.map +1 -0
- package/dist/es/src/clients/http.d.ts +40 -0
- package/dist/es/src/clients/http.d.ts.map +1 -0
- package/dist/es/src/command-bases/command-base.d.ts +29 -0
- package/dist/es/src/command-bases/command-base.d.ts.map +1 -0
- package/dist/es/src/command-bases/command-internal.d.ts +9 -0
- package/dist/es/src/command-bases/command-internal.d.ts.map +1 -0
- package/dist/es/src/command-bases/command-public.d.ts +9 -0
- package/dist/es/src/command-bases/command-public.d.ts.map +1 -0
- package/dist/es/src/command-bases/command-restful.d.ts +22 -0
- package/dist/es/src/command-bases/command-restful.d.ts.map +1 -0
- package/dist/es/src/command-bases/index.d.ts +8 -0
- package/dist/es/src/command-bases/index.d.ts.map +1 -0
- package/dist/es/src/errors/command-already-registered.d.ts +11 -0
- package/dist/es/src/errors/command-already-registered.d.ts.map +1 -0
- package/dist/es/src/errors/command-not-found.d.ts +10 -0
- package/dist/es/src/errors/command-not-found.d.ts.map +1 -0
- package/dist/es/src/errors/controller-already-registered.d.ts +11 -0
- package/dist/es/src/errors/controller-already-registered.d.ts.map +1 -0
- package/dist/es/src/errors/controller-already-set.d.ts +11 -0
- package/dist/es/src/errors/controller-already-set.d.ts.map +1 -0
- package/dist/es/src/errors/force-method-implementation.d.ts +14 -0
- package/dist/es/src/errors/force-method-implementation.d.ts.map +1 -0
- package/dist/es/src/errors/index-public.d.ts +2 -0
- package/dist/es/src/errors/index-public.d.ts.map +1 -0
- package/dist/es/src/errors/index.d.ts +6 -0
- package/dist/es/src/errors/index.d.ts.map +1 -0
- package/dist/es/src/exports.d.ts +37 -0
- package/dist/es/src/exports.d.ts.map +1 -0
- package/dist/es/src/index.d.ts +6 -0
- package/dist/es/src/index.d.ts.map +1 -0
- package/dist/es/src/initializer.d.ts +36 -0
- package/dist/es/src/initializer.d.ts.map +1 -0
- package/dist/es/{interfaces → src/interfaces}/commands.d.ts +3 -3
- package/dist/es/src/interfaces/commands.d.ts.map +1 -0
- package/dist/es/src/interfaces/config.d.ts +21 -0
- package/dist/es/src/interfaces/config.d.ts.map +1 -0
- package/dist/es/src/interfaces/index.d.ts +8 -0
- package/dist/es/src/interfaces/index.d.ts.map +1 -0
- package/dist/es/src/interfaces/logger.d.ts +20 -0
- package/dist/es/src/interfaces/logger.d.ts.map +1 -0
- package/dist/es/{interfaces/data.d.ts → src/interfaces/restful.d.ts} +2 -2
- package/dist/es/src/interfaces/restful.d.ts.map +1 -0
- package/dist/es/src/managers/commands.d.ts +68 -0
- package/dist/es/src/managers/commands.d.ts.map +1 -0
- package/dist/es/src/managers/controllers.d.ts +17 -0
- package/dist/es/src/managers/controllers.d.ts.map +1 -0
- package/dist/es/src/managers/export.d.ts +16 -0
- package/dist/es/src/managers/export.d.ts.map +1 -0
- package/dist/es/src/managers/index.d.ts +8 -0
- package/dist/es/src/managers/index.d.ts.map +1 -0
- package/dist/es/src/managers/internal.d.ts +9 -0
- package/dist/es/src/managers/internal.d.ts.map +1 -0
- package/dist/es/{manager-bases/data.d.ts → src/managers/restful.d.ts} +9 -10
- package/dist/es/src/managers/restful.d.ts.map +1 -0
- package/dist/es/tsdoc-metadata.json +11 -0
- package/dist/es/zenflux-core.d.ts +470 -0
- package/dist/es/zenflux-core.js +709 -1025
- package/package.json +29 -47
- package/src/bases/controller-base.ts +96 -0
- package/src/bases/index.ts +2 -0
- package/src/bases/object-base.ts +83 -0
- package/src/clients/http.ts +51 -39
- package/src/command-bases/command-base.ts +34 -38
- package/src/command-bases/command-internal.ts +5 -7
- package/src/command-bases/command-public.ts +5 -6
- package/src/command-bases/command-restful.ts +55 -0
- package/src/command-bases/index.ts +5 -5
- package/src/errors/command-already-registered.ts +6 -5
- package/src/errors/command-not-found.ts +6 -4
- package/src/errors/controller-already-registered.ts +6 -5
- package/src/errors/controller-already-set.ts +8 -5
- package/src/errors/force-method-implementation.ts +23 -0
- package/src/errors/index-public.ts +1 -0
- package/src/errors/index.ts +5 -5
- package/src/exports.ts +47 -9
- package/src/index.ts +3 -4
- package/src/initializer.ts +13 -13
- package/src/interfaces/commands.ts +3 -3
- package/src/interfaces/config.ts +14 -2
- package/src/interfaces/index.ts +5 -6
- package/src/interfaces/logger.ts +18 -0
- package/src/interfaces/{data.ts → restful.ts} +10 -10
- package/src/{manager-bases → managers}/commands.ts +52 -60
- package/src/managers/controllers.ts +39 -0
- package/src/managers/export.ts +30 -0
- package/src/managers/index.ts +5 -28
- package/src/managers/internal.ts +11 -0
- package/src/{manager-bases/data.ts → managers/restful.ts} +18 -19
- package/LICENSE +0 -21
- package/README.md +0 -2
- package/dist/es/clients/http.d.ts +0 -32
- package/dist/es/clients/http.d.ts.map +0 -1
- package/dist/es/command-bases/command-base.d.ts +0 -27
- package/dist/es/command-bases/command-base.d.ts.map +0 -1
- package/dist/es/command-bases/command-data.d.ts +0 -18
- package/dist/es/command-bases/command-data.d.ts.map +0 -1
- package/dist/es/command-bases/command-internal.d.ts +0 -10
- package/dist/es/command-bases/command-internal.d.ts.map +0 -1
- package/dist/es/command-bases/command-public.d.ts +0 -10
- package/dist/es/command-bases/command-public.d.ts.map +0 -1
- package/dist/es/command-bases/index.d.ts +0 -8
- package/dist/es/command-bases/index.d.ts.map +0 -1
- package/dist/es/core/controller.d.ts +0 -36
- package/dist/es/core/controller.d.ts.map +0 -1
- package/dist/es/core/index.d.ts +0 -3
- package/dist/es/core/index.d.ts.map +0 -1
- package/dist/es/core/object-base.d.ts +0 -15
- package/dist/es/core/object-base.d.ts.map +0 -1
- package/dist/es/errors/command-already-registered.d.ts +0 -9
- package/dist/es/errors/command-already-registered.d.ts.map +0 -1
- package/dist/es/errors/command-not-found.d.ts +0 -8
- package/dist/es/errors/command-not-found.d.ts.map +0 -1
- package/dist/es/errors/controller-already-registered.d.ts +0 -9
- package/dist/es/errors/controller-already-registered.d.ts.map +0 -1
- package/dist/es/errors/controller-already-set.d.ts +0 -8
- package/dist/es/errors/controller-already-set.d.ts.map +0 -1
- package/dist/es/errors/force-method.d.ts +0 -12
- package/dist/es/errors/force-method.d.ts.map +0 -1
- package/dist/es/errors/index.d.ts +0 -6
- package/dist/es/errors/index.d.ts.map +0 -1
- package/dist/es/exports.d.ts +0 -12
- package/dist/es/exports.d.ts.map +0 -1
- package/dist/es/index.d.ts +0 -6
- package/dist/es/index.d.ts.map +0 -1
- package/dist/es/initializer.d.ts +0 -22
- package/dist/es/initializer.d.ts.map +0 -1
- package/dist/es/interfaces/commands.d.ts.map +0 -1
- package/dist/es/interfaces/config.d.ts +0 -11
- package/dist/es/interfaces/config.d.ts.map +0 -1
- package/dist/es/interfaces/data.d.ts.map +0 -1
- package/dist/es/interfaces/index.d.ts +0 -9
- package/dist/es/interfaces/index.d.ts.map +0 -1
- package/dist/es/interfaces/managers.d.ts +0 -14
- package/dist/es/interfaces/managers.d.ts.map +0 -1
- package/dist/es/interfaces/object-base.d.ts +0 -11
- package/dist/es/interfaces/object-base.d.ts.map +0 -1
- package/dist/es/manager-bases/commands.d.ts +0 -76
- package/dist/es/manager-bases/commands.d.ts.map +0 -1
- package/dist/es/manager-bases/controllers.d.ts +0 -19
- package/dist/es/manager-bases/controllers.d.ts.map +0 -1
- package/dist/es/manager-bases/data.d.ts.map +0 -1
- package/dist/es/manager-bases/index.d.ts +0 -8
- package/dist/es/manager-bases/index.d.ts.map +0 -1
- package/dist/es/manager-bases/internal.d.ts +0 -10
- package/dist/es/manager-bases/internal.d.ts.map +0 -1
- package/dist/es/managers/index.d.ts +0 -16
- package/dist/es/managers/index.d.ts.map +0 -1
- package/dist/es/modules/index.d.ts +0 -2
- package/dist/es/modules/index.d.ts.map +0 -1
- package/dist/es/modules/logger.d.ts +0 -118
- package/dist/es/modules/logger.d.ts.map +0 -1
- package/dist/es/utils/index.d.ts +0 -3
- package/dist/es/utils/index.d.ts.map +0 -1
- package/dist/es/utils/object.d.ts +0 -7
- package/dist/es/utils/object.d.ts.map +0 -1
- package/dist/es/utils/rgb.d.ts +0 -7
- package/dist/es/utils/rgb.d.ts.map +0 -1
- package/src/command-bases/command-data.ts +0 -54
- package/src/core/controller.ts +0 -61
- package/src/core/index.ts +0 -2
- package/src/core/object-base.ts +0 -46
- package/src/errors/force-method.ts +0 -22
- package/src/interfaces/managers.ts +0 -16
- package/src/interfaces/object-base.ts +0 -12
- package/src/manager-bases/controllers.ts +0 -37
- package/src/manager-bases/index.ts +0 -7
- package/src/manager-bases/internal.ts +0 -13
- package/src/modules/index.ts +0 -1
- package/src/modules/logger.ts +0 -459
- package/src/utils/index.ts +0 -2
- package/src/utils/object.ts +0 -18
- package/src/utils/rgb.ts +0 -33
- package/types/index.d.ts +0 -6
package/package.json
CHANGED
|
@@ -1,49 +1,31 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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,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
|
+
|
package/src/clients/http.ts
CHANGED
|
@@ -1,74 +1,83 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
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 "
|
|
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:
|
|
20
|
-
|
|
22
|
+
private readonly logger: ILogger;
|
|
21
23
|
private readonly apiBaseUrl: string;
|
|
22
|
-
|
|
23
|
-
private requestInit: RequestInit = { 'credentials': 'include' };
|
|
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
|
|
30
|
+
public static getName(): string {
|
|
31
|
+
return "ZenFlux/Core/Clients/Http";
|
|
31
32
|
}
|
|
32
33
|
|
|
33
34
|
/**
|
|
34
|
-
*
|
|
35
|
+
* Initializes the base class and sets up configuration parameters.
|
|
35
36
|
*/
|
|
36
|
-
constructor( apiBaseUrl =
|
|
37
|
+
public constructor( apiBaseUrl = "http://localhost", requestInit: RequestInit = { "credentials": "include" } ) {
|
|
37
38
|
super();
|
|
38
39
|
|
|
39
|
-
this.logger = new Logger( Http
|
|
40
|
+
this.logger = new ZenCore.classes.Logger( Http );
|
|
40
41
|
|
|
41
|
-
this.logger.
|
|
42
|
+
this.logger.startsWith( this.constructor, { apiBaseUrl } );
|
|
42
43
|
|
|
43
|
-
this.apiBaseUrl = apiBaseUrl +
|
|
44
|
+
this.apiBaseUrl = apiBaseUrl + "/";
|
|
45
|
+
this.requestInit = requestInit;
|
|
44
46
|
}
|
|
45
47
|
|
|
46
48
|
/**
|
|
47
|
-
*
|
|
49
|
+
* Fetches data from the specified path using the given HTTP method and optional request body.
|
|
48
50
|
*/
|
|
49
|
-
async fetch( path: string, method: E_HTTP_METHOD_TYPE, body: any = {} ) {
|
|
50
|
-
this.logger.
|
|
51
|
+
public async fetch( path: string, method: E_HTTP_METHOD_TYPE, body: any = {} ) {
|
|
52
|
+
this.logger.startsWith( this.constructor, { path, method, body } );
|
|
51
53
|
|
|
54
|
+
const params = Object.assign( {}, this.requestInit );
|
|
52
55
|
const headers = {};
|
|
53
56
|
|
|
54
57
|
if ( method === E_HTTP_METHOD_TYPE.GET ) {
|
|
55
|
-
Object.assign(
|
|
58
|
+
Object.assign( params, { headers } );
|
|
56
59
|
} else {
|
|
57
|
-
Object.assign( headers, {
|
|
58
|
-
Object.assign(
|
|
60
|
+
Object.assign( headers, { "Content-Type": "application/json" } );
|
|
61
|
+
Object.assign( params, {
|
|
59
62
|
method,
|
|
60
63
|
headers: headers,
|
|
61
|
-
body: JSON.stringify( body )
|
|
64
|
+
body: JSON.stringify( body ),
|
|
62
65
|
} );
|
|
63
66
|
}
|
|
64
67
|
|
|
65
|
-
const
|
|
68
|
+
const fetchPromise = globalThis.fetch( this.apiBaseUrl + path, params );
|
|
69
|
+
|
|
70
|
+
let response = await fetchPromise;
|
|
71
|
+
|
|
72
|
+
if ( ! response ) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
66
75
|
|
|
67
76
|
let data = undefined;
|
|
68
77
|
|
|
69
78
|
try {
|
|
70
79
|
if ( ! response.ok ) {
|
|
71
|
-
throw response;
|
|
80
|
+
throw new Error( response.statusText );
|
|
72
81
|
}
|
|
73
82
|
|
|
74
83
|
let responseText = await response.text();
|
|
@@ -76,7 +85,7 @@ export class Http extends ObjectBase {
|
|
|
76
85
|
responseText = this.applyResponseFilter( responseText );
|
|
77
86
|
|
|
78
87
|
// TODO: Currently support JSON and plain text.
|
|
79
|
-
if ( response.headers?.get(
|
|
88
|
+
if ( response.headers?.get( "Content-Type" )?.includes( "application/json" ) ) {
|
|
80
89
|
data = JSON.parse( responseText );
|
|
81
90
|
} else {
|
|
82
91
|
data = responseText;
|
|
@@ -93,41 +102,46 @@ export class Http extends ObjectBase {
|
|
|
93
102
|
await Promise.reject( e );
|
|
94
103
|
}
|
|
95
104
|
|
|
96
|
-
this.logger.drop( { path }, data );
|
|
105
|
+
this.logger.drop( this.fetch, { path }, data );
|
|
97
106
|
|
|
98
107
|
return data;
|
|
99
108
|
}
|
|
100
109
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
110
|
+
/**
|
|
111
|
+
* Sets the error handler callback for handling errors during fetch requests.
|
|
112
|
+
*/
|
|
105
113
|
public setErrorHandler( callback: TErrorHandlerCallbackType ) {
|
|
106
114
|
if ( this.errorHandler ) {
|
|
107
|
-
throw new Error(
|
|
115
|
+
throw new Error( "Error handler already set." );
|
|
108
116
|
}
|
|
109
117
|
|
|
110
118
|
this.errorHandler = callback;
|
|
111
119
|
}
|
|
112
120
|
|
|
121
|
+
/**
|
|
122
|
+
* Sets the response filter callback for filtering the response text.
|
|
123
|
+
*/
|
|
113
124
|
public setResponseFilter( callback: TResponseFilterCallbackType ) {
|
|
114
125
|
if ( this.responseFilter ) {
|
|
115
|
-
throw new Error(
|
|
126
|
+
throw new Error( "Response filter already set." );
|
|
116
127
|
}
|
|
117
128
|
|
|
118
129
|
this.responseFilter = callback;
|
|
119
130
|
}
|
|
120
131
|
|
|
132
|
+
/**
|
|
133
|
+
* Sets the response handler callback for handling the response data.
|
|
134
|
+
*/
|
|
121
135
|
public setResponseHandler( callback: TResponseHandlerCallbackType ) {
|
|
122
136
|
if ( this.responseHandler ) {
|
|
123
|
-
throw new Error(
|
|
137
|
+
throw new Error( "Response handler already set." );
|
|
124
138
|
}
|
|
125
139
|
|
|
126
140
|
this.responseHandler = callback;
|
|
127
141
|
}
|
|
128
142
|
|
|
129
143
|
private applyErrorHandler( error: any ) {
|
|
130
|
-
return this.errorHandler && this.errorHandler( error );
|
|
144
|
+
return !! this.errorHandler && this.errorHandler( error );
|
|
131
145
|
}
|
|
132
146
|
|
|
133
147
|
private applyResponseFilter( text: string ) {
|
|
@@ -135,8 +149,6 @@ export class Http extends ObjectBase {
|
|
|
135
149
|
}
|
|
136
150
|
|
|
137
151
|
private applyResponseHandler( text: string ) {
|
|
138
|
-
return this.responseHandler && this.responseHandler( text );
|
|
152
|
+
return !! this.responseHandler && this.responseHandler( text );
|
|
139
153
|
}
|
|
140
154
|
}
|
|
141
|
-
|
|
142
|
-
export default Http;
|
|
@@ -1,31 +1,30 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @author
|
|
3
|
-
* @description
|
|
4
|
-
*
|
|
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 "
|
|
8
|
-
import
|
|
8
|
+
import { ControllerBase, ObjectBase } from "@z-core/bases";
|
|
9
|
+
import { ControllerAlreadySet, ForceMethodImplementation } from "@z-core/errors";
|
|
9
10
|
|
|
10
|
-
import
|
|
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:
|
|
14
|
+
private static controller: ControllerBase;
|
|
20
15
|
|
|
21
16
|
protected args: ICommandArgsInterface = {};
|
|
22
17
|
protected options: ICommandOptionsInterface = {};
|
|
23
18
|
|
|
24
|
-
private readonly 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:
|
|
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
|
|
40
|
+
const type = ( this.constructor as typeof CommandBase );
|
|
42
41
|
|
|
43
|
-
this.logger = new Logger(
|
|
44
|
-
|
|
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.
|
|
47
|
+
this.logger.startsWith( this.constructor, { args, options } );
|
|
48
48
|
|
|
49
49
|
this.initialize( args, options );
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
|
|
52
|
+
protected initialize( args: ICommandArgsInterface, options: ICommandOptionsInterface ) {
|
|
53
53
|
this.args = args;
|
|
54
54
|
this.options = options;
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
-
|
|
58
|
-
|
|
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
|
|
3
|
-
* @description
|
|
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 "
|
|
5
|
+
import { CommandBase } from "@z-core/command-bases/command-base";
|
|
6
6
|
|
|
7
7
|
export class CommandInternal extends CommandBase {
|
|
8
|
-
static getName() {
|
|
9
|
-
return
|
|
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
|
|
3
|
-
* @description
|
|
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 "
|
|
5
|
+
import { CommandBase } from "@z-core/command-bases/command-base";
|
|
6
6
|
|
|
7
7
|
export class CommandPublic extends CommandBase {
|
|
8
|
-
static getName() {
|
|
9
|
-
return
|
|
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
|
|
2
|
+
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
3
|
*/
|
|
4
|
-
export { CommandPublic } from "
|
|
5
|
-
export {
|
|
6
|
-
export { CommandBase } from "
|
|
7
|
-
export { CommandInternal } from "
|
|
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";
|