@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.
- package/package.json +30 -48
- 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 +48 -37
- package/src/command-bases/command-base.ts +35 -37
- 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-index.ts +13 -0
- package/src/exports.ts +67 -9
- package/src/index.ts +12 -3
- package/src/interfaces/commands.ts +3 -3
- package/src/interfaces/config.ts +10 -3
- 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 -61
- package/src/managers/controllers.ts +40 -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} +25 -24
- package/LICENSE +0 -21
- package/README.md +0 -2
- package/dist/es/clients/http.d.ts +0 -31
- 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 +0 -14
- package/dist/es/interfaces/commands.d.ts.map +0 -1
- package/dist/es/interfaces/config.d.ts +0 -15
- package/dist/es/interfaces/config.d.ts.map +0 -1
- package/dist/es/interfaces/data.d.ts +0 -19
- 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 +0 -26
- 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/dist/es/zenflux-core.js +0 -2235
- 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/initializer.ts +0 -56
- 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
|
-
|
|
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,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,77 +1,81 @@
|
|
|
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
|
|
6
|
+
import { E_HTTP_METHOD_TYPE } from "@z-core/interfaces";
|
|
6
7
|
|
|
7
|
-
import
|
|
8
|
+
import { ObjectBase } from "@z-core/bases/object-base";
|
|
8
9
|
|
|
9
|
-
import {
|
|
10
|
-
|
|
10
|
+
import type {
|
|
11
|
+
ILogger,
|
|
11
12
|
TErrorHandlerCallbackType,
|
|
12
13
|
TResponseFilterCallbackType,
|
|
13
|
-
TResponseHandlerCallbackType
|
|
14
|
-
} from "
|
|
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:
|
|
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
|
|
28
|
+
public static getName(): string {
|
|
29
|
+
return "ZenFlux/Core/Clients/Http";
|
|
31
30
|
}
|
|
32
31
|
|
|
33
32
|
/**
|
|
34
|
-
*
|
|
33
|
+
* Initializes the base class and sets up configuration parameters.
|
|
35
34
|
*/
|
|
36
|
-
constructor( apiBaseUrl =
|
|
35
|
+
public constructor( apiBaseUrl = "http://localhost", requestInit: RequestInit = { "credentials": "include" } ) {
|
|
37
36
|
super();
|
|
38
37
|
|
|
39
|
-
this.logger = new Logger( Http
|
|
38
|
+
this.logger = new zCore.classes.Logger( Http );
|
|
40
39
|
|
|
41
|
-
this.logger.
|
|
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
|
-
*
|
|
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.
|
|
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
|
-
|
|
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, {
|
|
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
|
|
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(
|
|
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(
|
|
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(
|
|
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(
|
|
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
|
|
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 { ObjectBase } from "@z-core/bases";
|
|
9
|
+
import { ControllerAlreadySet, ForceMethodImplementation } from "@z-core/errors";
|
|
9
10
|
|
|
10
|
-
import
|
|
11
|
+
import type { ControllerBase} from "@z-core/bases";
|
|
11
12
|
|
|
12
|
-
import
|
|
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:
|
|
16
|
+
private static controller: ControllerBase;
|
|
20
17
|
|
|
21
18
|
protected args: ICommandArgsInterface = {};
|
|
22
19
|
protected options: ICommandOptionsInterface = {};
|
|
23
20
|
|
|
24
|
-
private readonly 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:
|
|
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
|
|
42
|
+
const type = ( this.constructor as typeof CommandBase );
|
|
42
43
|
|
|
43
|
-
this.logger = new Logger(
|
|
44
|
-
|
|
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.
|
|
49
|
+
this.logger.startsWith( this.constructor, { args, options } );
|
|
48
50
|
|
|
49
51
|
this.initialize( args, options );
|
|
50
52
|
}
|
|
51
53
|
|
|
52
|
-
|
|
54
|
+
protected initialize( args: ICommandArgsInterface, options: ICommandOptionsInterface ) {
|
|
53
55
|
this.args = args;
|
|
54
56
|
this.options = options;
|
|
55
57
|
}
|
|
56
58
|
|
|
57
|
-
|
|
58
|
-
|
|
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
|
|
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";
|