@zenflux/core 0.0.0-devlopment.0 → 0.0.0-devlopment.2
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/clients/http.d.ts +30 -0
- package/dist/es/clients/http.d.ts.map +1 -0
- package/dist/es/{src/command-bases → command-bases}/command-base.d.ts +7 -2
- package/dist/es/command-bases/command-base.d.ts.map +1 -0
- package/dist/es/{src/command-bases → command-bases}/command-data.d.ts +2 -1
- package/dist/es/command-bases/command-data.d.ts.map +1 -0
- package/dist/es/{src/command-bases → command-bases}/command-internal.d.ts +2 -1
- package/dist/es/command-bases/command-internal.d.ts.map +1 -0
- package/dist/es/{src/command-bases → command-bases}/command-public.d.ts +2 -1
- package/dist/es/command-bases/command-public.d.ts.map +1 -0
- package/dist/es/{src/command-bases → command-bases}/index.d.ts +4 -0
- package/dist/es/command-bases/index.d.ts.map +1 -0
- package/dist/es/{src/core → core}/controller.d.ts +2 -1
- package/dist/es/core/controller.d.ts.map +1 -0
- package/dist/es/{src/core → core}/index.d.ts +1 -0
- package/dist/es/core/index.d.ts.map +1 -0
- package/dist/es/{src/core → core}/object-base.d.ts +2 -1
- package/dist/es/core/object-base.d.ts.map +1 -0
- package/dist/es/{src/errors → errors}/command-already-registered.d.ts +2 -1
- package/dist/es/errors/command-already-registered.d.ts.map +1 -0
- package/dist/es/{src/errors → errors}/command-not-found.d.ts +2 -1
- package/dist/es/errors/command-not-found.d.ts.map +1 -0
- package/dist/es/{src/errors → errors}/controller-already-registered.d.ts +2 -1
- package/dist/es/errors/controller-already-registered.d.ts.map +1 -0
- package/dist/es/errors/controller-already-set.d.ts +8 -0
- package/dist/es/errors/controller-already-set.d.ts.map +1 -0
- package/dist/es/{src/errors → errors}/force-method.d.ts +2 -1
- package/dist/es/errors/force-method.d.ts.map +1 -0
- package/dist/es/{src/errors → errors}/index.d.ts +2 -0
- package/dist/es/errors/index.d.ts.map +1 -0
- package/dist/es/{src/exports.d.ts → exports.d.ts} +5 -0
- package/dist/es/exports.d.ts.map +1 -0
- package/dist/es/index.d.ts +6 -0
- package/dist/es/index.d.ts.map +1 -0
- package/dist/es/{src/initializer.d.ts → initializer.d.ts} +4 -1
- package/dist/es/initializer.d.ts.map +1 -0
- package/dist/es/{src/interfaces → interfaces}/commands.d.ts +1 -0
- package/dist/es/interfaces/commands.d.ts.map +1 -0
- package/dist/es/{src/interfaces → interfaces}/config.d.ts +1 -0
- package/dist/es/interfaces/config.d.ts.map +1 -0
- package/dist/es/interfaces/data.d.ts +19 -0
- package/dist/es/interfaces/data.d.ts.map +1 -0
- package/dist/es/interfaces/index.d.ts +9 -0
- package/dist/es/interfaces/index.d.ts.map +1 -0
- package/dist/es/interfaces/managers.d.ts +14 -0
- package/dist/es/interfaces/managers.d.ts.map +1 -0
- package/dist/es/{src/interfaces → interfaces}/object-base.d.ts +1 -0
- package/dist/es/interfaces/object-base.d.ts.map +1 -0
- package/dist/es/{src/manager-bases → manager-bases}/commands.d.ts +5 -3
- package/dist/es/manager-bases/commands.d.ts.map +1 -0
- package/dist/es/{src/manager-bases → manager-bases}/controllers.d.ts +4 -2
- package/dist/es/manager-bases/controllers.d.ts.map +1 -0
- package/dist/es/{src/manager-bases → manager-bases}/data.d.ts +8 -5
- package/dist/es/manager-bases/data.d.ts.map +1 -0
- package/dist/es/{src/manager-bases → manager-bases}/index.d.ts +4 -0
- package/dist/es/manager-bases/index.d.ts.map +1 -0
- package/dist/es/{src/manager-bases → manager-bases}/internal.d.ts +2 -1
- package/dist/es/manager-bases/internal.d.ts.map +1 -0
- package/dist/es/managers/index.d.ts +16 -0
- package/dist/es/managers/index.d.ts.map +1 -0
- package/dist/es/{src/modules → modules}/index.d.ts +1 -0
- package/dist/es/modules/index.d.ts.map +1 -0
- package/dist/es/{src/modules → modules}/logger.d.ts +1 -0
- package/dist/es/modules/logger.d.ts.map +1 -0
- package/dist/es/{src/utils → utils}/index.d.ts +1 -0
- package/dist/es/utils/index.d.ts.map +1 -0
- package/dist/es/{src/utils → utils}/object.d.ts +1 -0
- package/dist/es/utils/object.d.ts.map +1 -0
- package/dist/es/{src/utils → utils}/rgb.d.ts +1 -0
- package/dist/es/utils/rgb.d.ts.map +1 -0
- package/dist/es/zenflux-core.js +2053 -890
- package/package.json +29 -35
- package/src/clients/http.ts +74 -18
- package/src/command-bases/command-base.ts +25 -8
- package/src/command-bases/command-data.ts +3 -3
- package/src/core/object-base.ts +1 -1
- package/src/errors/controller-already-set.ts +10 -0
- package/src/errors/index.ts +1 -0
- package/src/exports.ts +1 -1
- package/src/index.ts +2 -1
- package/src/initializer.ts +14 -21
- package/src/interfaces/data.ts +23 -0
- package/src/interfaces/index.ts +8 -0
- package/src/interfaces/managers.ts +16 -0
- package/src/manager-bases/commands.ts +9 -4
- package/src/manager-bases/controllers.ts +2 -1
- package/src/manager-bases/data.ts +40 -13
- package/src/managers/index.ts +26 -5
- package/src/modules/logger.ts +53 -7
- package/types/index.d.ts +6 -2
- package/dist/cjs/zenflux-core.js +0 -1082
- package/dist/es/rollup.config.d.ts +0 -23
- package/dist/es/src/clients/http.d.ts +0 -20
- package/dist/es/src/enums/http.d.ts +0 -9
- package/dist/es/src/index.d.ts +0 -2
- package/dist/es/src/managers/index.d.ts +0 -5
- package/dist/es/zenflux-core.mjs +0 -12
- package/dist/umd/zenflux-core.js +0 -12
- package/src/enums/http.ts +0 -12
package/package.json
CHANGED
|
@@ -1,55 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zenflux/core",
|
|
3
|
-
"version": "0.0.0-devlopment.
|
|
3
|
+
"version": "0.0.0-devlopment.2",
|
|
4
4
|
"description": "ZenFlux core",
|
|
5
|
-
"author": "Leonid Vinikov <leonidvinikov@gmail.com> (https://github.com/iNewLegend)",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"test": "export flow_modules_logger=off && jest",
|
|
8
|
-
"build": "rollup --config rollup.config.ts --configPlugin typescript"
|
|
9
|
-
},
|
|
10
|
-
"repository": {
|
|
11
|
-
"type": "git",
|
|
12
|
-
"url": "git+https://github.com/zenflux/core.git"
|
|
13
|
-
},
|
|
14
5
|
"keywords": [
|
|
15
6
|
"zenflux",
|
|
16
7
|
"zen",
|
|
17
8
|
"flux",
|
|
18
9
|
"commands",
|
|
19
10
|
"manager",
|
|
20
|
-
"commands manager"
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
"dist",
|
|
24
|
-
"src",
|
|
25
|
-
"types"
|
|
11
|
+
"commands manager",
|
|
12
|
+
"core",
|
|
13
|
+
"zenflux-core"
|
|
26
14
|
],
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/zenflux/core.git"
|
|
18
|
+
},
|
|
27
19
|
"license": "MIT",
|
|
20
|
+
"author": "Leonid Vinikov <leonidvinikov@gmail.com> (https://github.com/iNewLegend)",
|
|
28
21
|
"main": "dist/cjs/zenflux-core.js",
|
|
29
22
|
"unpkg": "dist/umd/zenflux-core.js",
|
|
30
23
|
"module": "dist/es/zenflux-core.js",
|
|
31
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
|
+
},
|
|
32
37
|
"devDependencies": {
|
|
33
|
-
"@babel/
|
|
34
|
-
"@babel/plugin-transform-runtime": "^7.19.6",
|
|
38
|
+
"@babel/plugin-syntax-import-assertions": "^7.20.0",
|
|
35
39
|
"@babel/preset-env": "^7.20.2",
|
|
36
40
|
"@babel/preset-typescript": "^7.18.6",
|
|
37
|
-
"@
|
|
38
|
-
"@
|
|
39
|
-
"@
|
|
40
|
-
"@rollup
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"@types/jest": "^29.2.5",
|
|
45
|
-
"@typescript-eslint/eslint-plugin": "^5.48.0",
|
|
46
|
-
"@typescript-eslint/parser": "^5.48.0",
|
|
47
|
-
"babel-jest": "^29.3.1",
|
|
48
|
-
"rollup": "^3.9.1",
|
|
49
|
-
"ts-jest": "^29.0.3",
|
|
50
|
-
"tslib": "^2.4.1"
|
|
51
|
-
},
|
|
52
|
-
"dependencies": {
|
|
53
|
-
"@babel/runtime": "^7.19.0"
|
|
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"
|
|
54
48
|
}
|
|
55
49
|
}
|
package/src/clients/http.ts
CHANGED
|
@@ -6,13 +6,24 @@ import ObjectBase from "../core/object-base";
|
|
|
6
6
|
|
|
7
7
|
import Logger from '../modules/logger';
|
|
8
8
|
|
|
9
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
E_HTTP_METHOD_TYPE,
|
|
11
|
+
TErrorHandlerCallbackType,
|
|
12
|
+
TResponseFilterCallbackType,
|
|
13
|
+
TResponseHandlerCallbackType,
|
|
14
|
+
} from "../interfaces/";
|
|
15
|
+
|
|
16
|
+
// noinspection ExceptionCaughtLocallyJS
|
|
10
17
|
|
|
11
18
|
export class Http extends ObjectBase {
|
|
12
19
|
private readonly logger: Logger;
|
|
13
20
|
|
|
14
21
|
private readonly apiBaseUrl: string;
|
|
15
22
|
|
|
23
|
+
private errorHandler?: TErrorHandlerCallbackType = undefined;
|
|
24
|
+
private responseFilter?: TResponseFilterCallbackType = undefined;
|
|
25
|
+
private responseHandler?: TResponseHandlerCallbackType = undefined;
|
|
26
|
+
|
|
16
27
|
static getName() {
|
|
17
28
|
return 'Core/Clients/Http';
|
|
18
29
|
}
|
|
@@ -33,26 +44,21 @@ export class Http extends ObjectBase {
|
|
|
33
44
|
/**
|
|
34
45
|
* Function fetch() : Fetch api.
|
|
35
46
|
*/
|
|
36
|
-
async fetch( path: string, method:
|
|
47
|
+
async fetch( path: string, method: E_HTTP_METHOD_TYPE, body: any = {} ) {
|
|
37
48
|
this.logger.startWith( { path, method, body } );
|
|
38
49
|
|
|
39
50
|
const params: RequestInit = { 'credentials': 'include' }, // Support cookies.
|
|
40
51
|
headers = {};
|
|
41
52
|
|
|
42
|
-
if (
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
HTTPMethodEnum.PATCH,
|
|
46
|
-
].includes( method ) ) {
|
|
53
|
+
if ( method === E_HTTP_METHOD_TYPE.GET ) {
|
|
54
|
+
Object.assign( params, { headers } );
|
|
55
|
+
} else {
|
|
47
56
|
Object.assign( headers, { 'Content-Type': 'application/json' } );
|
|
48
57
|
Object.assign( params, {
|
|
49
58
|
method,
|
|
50
59
|
headers: headers,
|
|
51
60
|
body: JSON.stringify( body )
|
|
52
61
|
} );
|
|
53
|
-
|
|
54
|
-
} else {
|
|
55
|
-
Object.assign( params, { headers } );
|
|
56
62
|
}
|
|
57
63
|
|
|
58
64
|
const response = await globalThis.fetch( this.apiBaseUrl + path, params );
|
|
@@ -60,22 +66,72 @@ export class Http extends ObjectBase {
|
|
|
60
66
|
let data = undefined;
|
|
61
67
|
|
|
62
68
|
try {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
69
|
+
if ( ! response.ok ) {
|
|
70
|
+
throw response;
|
|
71
|
+
}
|
|
66
72
|
|
|
67
|
-
|
|
68
|
-
|
|
73
|
+
let responseText = await response.text();
|
|
74
|
+
|
|
75
|
+
responseText = this.applyResponseFilter( responseText );
|
|
76
|
+
|
|
77
|
+
// TODO: Currently support JSON and plain text.
|
|
78
|
+
if ( response.headers?.get( 'Content-Type' )?.includes( 'application/json' ) ) {
|
|
79
|
+
data = JSON.parse( responseText );
|
|
80
|
+
} else {
|
|
81
|
+
data = responseText;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if ( this.applyResponseHandler( data ) ) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
} catch ( e ) {
|
|
88
|
+
if ( this.applyErrorHandler( e ) ) {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
69
91
|
|
|
70
|
-
|
|
71
|
-
if ( data?.error && data?.global && data?.message ) {
|
|
72
|
-
throw new Error( data.message );
|
|
92
|
+
await Promise.reject( e );
|
|
73
93
|
}
|
|
74
94
|
|
|
75
95
|
this.logger.drop( { path }, data );
|
|
76
96
|
|
|
77
97
|
return data;
|
|
78
98
|
}
|
|
99
|
+
|
|
100
|
+
public setErrorHandler( callback: TErrorHandlerCallbackType ) {
|
|
101
|
+
if ( this.errorHandler ) {
|
|
102
|
+
throw new Error( 'Error handler already set.' );
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
this.errorHandler = callback;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
public setResponseFilter( callback: TResponseFilterCallbackType ) {
|
|
109
|
+
if ( this.responseFilter ) {
|
|
110
|
+
throw new Error( 'Response filter already set.' );
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
this.responseFilter = callback;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
public setResponseHandler( callback: TResponseHandlerCallbackType ) {
|
|
117
|
+
if ( this.responseHandler ) {
|
|
118
|
+
throw new Error( 'Response handler already set.' );
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
this.responseHandler = callback;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
private applyErrorHandler( error: any ) {
|
|
125
|
+
return this.errorHandler && this.errorHandler( error );
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
private applyResponseFilter( text: string ) {
|
|
129
|
+
return ( this.responseFilter && this.responseFilter( text ) ) || text;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
private applyResponseHandler( text: string ) {
|
|
133
|
+
return this.responseHandler && this.responseHandler( text );
|
|
134
|
+
}
|
|
79
135
|
}
|
|
80
136
|
|
|
81
137
|
export default Http;
|
|
@@ -5,19 +5,36 @@
|
|
|
5
5
|
* - Add validateArgs method or find something better.
|
|
6
6
|
*/
|
|
7
7
|
import ObjectBase from "../core/object-base";
|
|
8
|
+
import Controller from "../core/controller";
|
|
8
9
|
|
|
9
10
|
import ForceMethod from "../errors/force-method";
|
|
10
11
|
|
|
11
12
|
import Logger from "../modules/logger";
|
|
12
13
|
|
|
13
|
-
import {
|
|
14
|
+
import { ControllerAlreadySet } from "../errors";
|
|
15
|
+
|
|
16
|
+
import { ICommandArgsInterface, ICommandOptionsInterface } from "../interfaces/";
|
|
14
17
|
|
|
15
18
|
export abstract class CommandBase extends ObjectBase {
|
|
19
|
+
private static controller: Controller;
|
|
20
|
+
|
|
16
21
|
protected args: ICommandArgsInterface = {};
|
|
17
22
|
protected options: ICommandOptionsInterface = {};
|
|
18
23
|
|
|
19
24
|
private readonly logger: Logger;
|
|
20
25
|
|
|
26
|
+
public static setController( controller: Controller ) {
|
|
27
|
+
if ( this.controller ) {
|
|
28
|
+
throw new ControllerAlreadySet();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
this.controller = controller;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public static getController() {
|
|
35
|
+
return this.controller;
|
|
36
|
+
}
|
|
37
|
+
|
|
21
38
|
constructor( args: ICommandArgsInterface = {}, options = {} ) {
|
|
22
39
|
super();
|
|
23
40
|
|
|
@@ -32,16 +49,16 @@ export abstract class CommandBase extends ObjectBase {
|
|
|
32
49
|
this.initialize( args, options );
|
|
33
50
|
}
|
|
34
51
|
|
|
35
|
-
initialize( args: ICommandArgsInterface, options: ICommandOptionsInterface ) {
|
|
52
|
+
public initialize( args: ICommandArgsInterface, options: ICommandOptionsInterface ) {
|
|
36
53
|
this.args = args;
|
|
37
54
|
this.options = options;
|
|
38
55
|
}
|
|
39
56
|
|
|
40
|
-
apply( args = this.args, options = this.options ): any {// eslint-disable-line @typescript-eslint/no-unused-vars
|
|
57
|
+
public apply( args = this.args, options = this.options ): any {// eslint-disable-line @typescript-eslint/no-unused-vars
|
|
41
58
|
throw new ForceMethod( this, "apply" );
|
|
42
59
|
}
|
|
43
60
|
|
|
44
|
-
async run() {
|
|
61
|
+
public async run() {
|
|
45
62
|
this.onBeforeApply && this.onBeforeApply();
|
|
46
63
|
|
|
47
64
|
const result = await this.apply( this.args, this.options );
|
|
@@ -51,17 +68,17 @@ export abstract class CommandBase extends ObjectBase {
|
|
|
51
68
|
return result;
|
|
52
69
|
}
|
|
53
70
|
|
|
54
|
-
getArgs() {
|
|
71
|
+
public getArgs() {
|
|
55
72
|
return this.args;
|
|
56
73
|
}
|
|
57
74
|
|
|
58
|
-
getOptions() {
|
|
75
|
+
public getOptions() {
|
|
59
76
|
return this.options;
|
|
60
77
|
}
|
|
61
78
|
|
|
62
|
-
onBeforeApply?():void;
|
|
79
|
+
public onBeforeApply?():void;
|
|
63
80
|
|
|
64
|
-
onAfterApply?():void;
|
|
81
|
+
public onAfterApply?():void;
|
|
65
82
|
}
|
|
66
83
|
|
|
67
84
|
export default CommandBase;
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import { ForceMethod } from '../errors/force-method';
|
|
6
6
|
import { CommandBase } from './command-base';
|
|
7
7
|
|
|
8
|
-
import
|
|
8
|
+
import * as managers from '../managers';
|
|
9
9
|
|
|
10
10
|
export abstract class CommandData extends CommandBase {
|
|
11
11
|
static getName() {
|
|
@@ -28,8 +28,8 @@ export abstract class CommandData extends CommandBase {
|
|
|
28
28
|
apply( args = this.args, options = this.options ) {// eslint-disable-line @typescript-eslint/no-unused-vars
|
|
29
29
|
const endpoint = this.applyEndpointFormat( this.getEndpoint(), args );
|
|
30
30
|
|
|
31
|
-
return data.getClient().fetch(
|
|
32
|
-
endpoint, data.currentHttpMethod, args || null
|
|
31
|
+
return managers.data.getClient().fetch(
|
|
32
|
+
endpoint, managers.data.currentHttpMethod, args || null
|
|
33
33
|
);
|
|
34
34
|
}
|
|
35
35
|
|
package/src/core/object-base.ts
CHANGED
package/src/errors/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { CommandAlreadyRegistered } from "./command-already-registered";
|
|
2
2
|
export { CommandNotFound } from "./command-not-found";
|
|
3
3
|
export { ControllerAlreadyRegistered } from "./controller-already-registered";
|
|
4
|
+
export { ControllerAlreadySet } from './controller-already-set';
|
|
4
5
|
export { ForceMethod } from "./force-method";
|
package/src/exports.ts
CHANGED
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
export * as commandBases from "./command-bases";
|
|
5
5
|
export * as core from "./core";
|
|
6
6
|
export * as errors from "./errors";
|
|
7
|
+
export * as interfaces from "./interfaces";
|
|
7
8
|
export * as managers from "./managers";
|
|
8
9
|
export * as managerBases from "./manager-bases";
|
|
9
10
|
export * as modules from "./modules";
|
|
10
11
|
export * as utils from "./utils";
|
|
11
|
-
|
package/src/index.ts
CHANGED
package/src/initializer.ts
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import * as exported from './exports';
|
|
1
|
+
// @ts-ignore
|
|
2
|
+
import * as pkg from "../package.json" assert { type: "json" };
|
|
4
3
|
|
|
5
4
|
import { IAPIConfig } from "./interfaces/config";
|
|
6
5
|
|
|
6
|
+
import { destroy, initialize, afterInitializeCallbacks } from "./managers";
|
|
7
|
+
|
|
8
|
+
import * as exported from './exports';
|
|
9
|
+
|
|
7
10
|
declare global {
|
|
8
11
|
var ZenCore: any;
|
|
12
|
+
var __ZEN_CORE__IS_INITIALIZED__: boolean;
|
|
9
13
|
}
|
|
10
14
|
|
|
11
|
-
let isInitialized = false;
|
|
12
|
-
|
|
13
15
|
function errorInitTwice() {
|
|
14
|
-
if (
|
|
16
|
+
if ( 'undefined' !== typeof __ZEN_CORE__IS_INITIALIZED__ && __ZEN_CORE__IS_INITIALIZED__) {
|
|
15
17
|
throw new Error( 'ZenCore is already initialized.' );
|
|
16
18
|
}
|
|
17
19
|
}
|
|
@@ -20,9 +22,7 @@ errorInitTwice();
|
|
|
20
22
|
|
|
21
23
|
export let config: IAPIConfig = {
|
|
22
24
|
version: pkg.version,
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const afterInitializeCallbacks: ( () => void )[] = [];
|
|
25
|
+
};
|
|
26
26
|
|
|
27
27
|
export const CoreAPI = {
|
|
28
28
|
initialize: ( configuration?: IAPIConfig ) => {
|
|
@@ -30,23 +30,16 @@ export const CoreAPI = {
|
|
|
30
30
|
|
|
31
31
|
config = configuration || config;
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
exported.managers.controllers = new exported.managerBases.Controllers();
|
|
35
|
-
exported.managers.data = new exported.managerBases.Data( config );
|
|
36
|
-
exported.managers.internal = new exported.managerBases.Internal();
|
|
33
|
+
initialize( config );
|
|
37
34
|
|
|
38
|
-
|
|
35
|
+
globalThis.__ZEN_CORE__IS_INITIALIZED__ = true;
|
|
39
36
|
|
|
40
|
-
afterInitializeCallbacks.forEach( ( callback ) => callback() );
|
|
41
37
|
},
|
|
42
38
|
|
|
43
39
|
destroy: () => {
|
|
44
|
-
|
|
40
|
+
destroy();
|
|
45
41
|
|
|
46
|
-
|
|
47
|
-
exported.managers.controllers = {} as exported.managerBases.Controllers
|
|
48
|
-
exported.managers.data = {} as exported.managerBases.Data;
|
|
49
|
-
exported.managers.internal = {} as exported.managerBases.Internal;
|
|
42
|
+
globalThis.__ZEN_CORE__IS_INITIALIZED__ = false;
|
|
50
43
|
},
|
|
51
44
|
|
|
52
45
|
onAfterInitialize: ( callback: () => void ) => {
|
|
@@ -56,7 +49,7 @@ export const CoreAPI = {
|
|
|
56
49
|
config,
|
|
57
50
|
|
|
58
51
|
...exported,
|
|
59
|
-
}
|
|
52
|
+
};
|
|
60
53
|
|
|
61
54
|
// TODO: Make it available only for development.
|
|
62
55
|
if ( ! globalThis?.ZenCore ) globalThis.ZenCore = CoreAPI;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export type TErrorHandlerCallbackType = ( data: any ) => boolean;
|
|
2
|
+
|
|
3
|
+
export type TResponseFilterCallbackType = ( text: string ) => string;
|
|
4
|
+
|
|
5
|
+
export type TResponseHandlerCallbackType = ( text: string) => boolean;
|
|
6
|
+
|
|
7
|
+
export type TPossibleHandlersType = TErrorHandlerCallbackType | TResponseFilterCallbackType | TResponseHandlerCallbackType;
|
|
8
|
+
|
|
9
|
+
export enum E_RESPONSE_HANDLER_TYPE {
|
|
10
|
+
ERROR_HANDLER = 'error_handler',
|
|
11
|
+
RESPONSE_FILTER = 'response_filter',
|
|
12
|
+
RESPONSE_HANDLER = 'response_handler',
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export enum E_HTTP_METHOD_TYPE {
|
|
16
|
+
DELETE = 'DELETE', // Delete.
|
|
17
|
+
GET = 'GET', // Read.
|
|
18
|
+
OPTIONS = 'OPTIONS',// Options about the request
|
|
19
|
+
PATCH = 'PATCH', // Update/Modify.
|
|
20
|
+
POST = 'POST', // Create.
|
|
21
|
+
PUT = 'PUT', // Update/Replace.
|
|
22
|
+
'__EMPTY__' = ''
|
|
23
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import Commands from "../manager-bases/commands";
|
|
2
|
+
import Controllers from "../manager-bases/controllers";
|
|
3
|
+
import Data from "../manager-bases/data";
|
|
4
|
+
import Internal from "../manager-bases/internal";
|
|
5
|
+
|
|
6
|
+
import { IAPIConfig } from "./config";
|
|
7
|
+
|
|
8
|
+
export interface IManagers {
|
|
9
|
+
initialize: ( config: IAPIConfig ) => void;
|
|
10
|
+
destroy: () => void;
|
|
11
|
+
|
|
12
|
+
commands: Commands;
|
|
13
|
+
controllers: Controllers;
|
|
14
|
+
data: Data;
|
|
15
|
+
internal: Internal;
|
|
16
|
+
}
|
|
@@ -4,15 +4,18 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import { Logger } from "../modules/";
|
|
6
6
|
import { CommandBase } from "../command-bases";
|
|
7
|
-
import { Controller, ObjectBase } from "../core";
|
|
8
7
|
import { CommandAlreadyRegistered, CommandNotFound } from "../errors/";
|
|
9
8
|
|
|
9
|
+
import ObjectBase from "../core/object-base";
|
|
10
|
+
import Controller from "../core/controller";
|
|
11
|
+
|
|
10
12
|
import {
|
|
11
|
-
ICommandArgsInterface,
|
|
12
13
|
CommandCallbackType,
|
|
14
|
+
ICommandArgsInterface,
|
|
13
15
|
IOnHookAffectInterface,
|
|
14
|
-
IOnHookInterface,
|
|
15
|
-
|
|
16
|
+
IOnHookInterface,
|
|
17
|
+
ICommandOptionsInterface,
|
|
18
|
+
} from "../interfaces/";
|
|
16
19
|
|
|
17
20
|
export class Commands extends ObjectBase {
|
|
18
21
|
public static readonly trace:string[] = [];
|
|
@@ -92,6 +95,8 @@ export class Commands extends ObjectBase {
|
|
|
92
95
|
throw new CommandAlreadyRegistered( command );
|
|
93
96
|
}
|
|
94
97
|
|
|
98
|
+
command.setController( controller );
|
|
99
|
+
|
|
95
100
|
this.commands[ commandName ] = command;
|
|
96
101
|
|
|
97
102
|
result[ commandName ] = command;
|
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
* @author: Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
3
|
* @description: Responsible for manging controllers, each controller is global singleton instance.
|
|
4
4
|
*/
|
|
5
|
-
import
|
|
5
|
+
import ObjectBase from "../core/object-base";
|
|
6
|
+
import Controller from "../core/controller";
|
|
6
7
|
|
|
7
8
|
import { ControllerAlreadyRegistered } from "../errors/controller-already-registered";
|
|
8
9
|
|
|
@@ -8,15 +8,23 @@ import { CommandData } from '../command-bases/';
|
|
|
8
8
|
|
|
9
9
|
import Http from '../clients/http';
|
|
10
10
|
|
|
11
|
-
import {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
import {
|
|
12
|
+
IAPIConfig,
|
|
13
|
+
ICommandArgsInterface,
|
|
14
|
+
ICommandOptionsInterface,
|
|
15
|
+
TErrorHandlerCallbackType,
|
|
16
|
+
TPossibleHandlersType,
|
|
17
|
+
TResponseFilterCallbackType,
|
|
18
|
+
TResponseHandlerCallbackType,
|
|
19
|
+
|
|
20
|
+
E_RESPONSE_HANDLER_TYPE,
|
|
21
|
+
E_HTTP_METHOD_TYPE
|
|
22
|
+
} from "../interfaces/";
|
|
15
23
|
|
|
16
24
|
export class Data extends Commands {
|
|
17
25
|
private static client: Http;
|
|
18
26
|
|
|
19
|
-
public currentHttpMethod:
|
|
27
|
+
public currentHttpMethod: E_HTTP_METHOD_TYPE;
|
|
20
28
|
|
|
21
29
|
static getName() {
|
|
22
30
|
return 'Core/Managers/Data';
|
|
@@ -25,7 +33,6 @@ export class Data extends Commands {
|
|
|
25
33
|
constructor( Config: IAPIConfig) {
|
|
26
34
|
super();
|
|
27
35
|
|
|
28
|
-
// @ts-ignore
|
|
29
36
|
Data.client = new Http( Config.baseURL );
|
|
30
37
|
}
|
|
31
38
|
|
|
@@ -34,25 +41,25 @@ export class Data extends Commands {
|
|
|
34
41
|
}
|
|
35
42
|
|
|
36
43
|
public get( command: string, args: ICommandArgsInterface = {}, options: ICommandOptionsInterface = {} ) {
|
|
37
|
-
this.currentHttpMethod =
|
|
44
|
+
this.currentHttpMethod = E_HTTP_METHOD_TYPE.GET;
|
|
38
45
|
|
|
39
46
|
return super.run( command, args, options );
|
|
40
47
|
}
|
|
41
48
|
|
|
42
49
|
public update( command: string, args: ICommandArgsInterface = {}, options: {} = {} ) {
|
|
43
|
-
this.currentHttpMethod =
|
|
50
|
+
this.currentHttpMethod = E_HTTP_METHOD_TYPE.PATCH;
|
|
44
51
|
|
|
45
52
|
return super.run( command, args, options );
|
|
46
53
|
}
|
|
47
54
|
|
|
48
55
|
public delete( command: string, args: ICommandArgsInterface = {}, options: {} = {} ) {
|
|
49
|
-
this.currentHttpMethod =
|
|
56
|
+
this.currentHttpMethod = E_HTTP_METHOD_TYPE.DELETE;
|
|
50
57
|
|
|
51
58
|
return super.run( command, args, options );
|
|
52
59
|
}
|
|
53
60
|
|
|
54
61
|
public create( command: string, args: ICommandArgsInterface = {}, options: {} = {} ) {
|
|
55
|
-
this.currentHttpMethod =
|
|
62
|
+
this.currentHttpMethod = E_HTTP_METHOD_TYPE.POST;
|
|
56
63
|
|
|
57
64
|
return super.run( command, args, options );
|
|
58
65
|
}
|
|
@@ -71,19 +78,39 @@ export class Data extends Commands {
|
|
|
71
78
|
},
|
|
72
79
|
};
|
|
73
80
|
|
|
74
|
-
if (
|
|
81
|
+
if ( E_HTTP_METHOD_TYPE.GET === this.currentHttpMethod ) {
|
|
75
82
|
newArgs.args.query = args;
|
|
76
83
|
} else {
|
|
77
84
|
newArgs.args.data = args;
|
|
78
85
|
}
|
|
79
86
|
|
|
80
|
-
args.result = await super.runInstance( command, newArgs, options )
|
|
87
|
+
args.result = await super.runInstance( command, newArgs, options );
|
|
81
88
|
|
|
82
89
|
// Clear method type.
|
|
83
|
-
this.currentHttpMethod =
|
|
90
|
+
this.currentHttpMethod = E_HTTP_METHOD_TYPE.__EMPTY__;
|
|
84
91
|
|
|
85
92
|
return args.result;
|
|
86
93
|
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Handlers on return true will swallow the request.
|
|
97
|
+
*/
|
|
98
|
+
public setHandler( type: E_RESPONSE_HANDLER_TYPE, callback: TPossibleHandlersType ) {
|
|
99
|
+
switch ( type ) {
|
|
100
|
+
case E_RESPONSE_HANDLER_TYPE.ERROR_HANDLER:
|
|
101
|
+
Data.client.setErrorHandler( callback as TErrorHandlerCallbackType );
|
|
102
|
+
break;
|
|
103
|
+
case E_RESPONSE_HANDLER_TYPE.RESPONSE_FILTER:
|
|
104
|
+
Data.client.setResponseFilter( callback as TResponseFilterCallbackType );
|
|
105
|
+
break;
|
|
106
|
+
case E_RESPONSE_HANDLER_TYPE.RESPONSE_HANDLER:
|
|
107
|
+
Data.client.setResponseHandler( callback as TResponseHandlerCallbackType );
|
|
108
|
+
break;
|
|
109
|
+
|
|
110
|
+
default:
|
|
111
|
+
throw new Error( `Unknown handler type: '${ type }'` );
|
|
112
|
+
}
|
|
113
|
+
}
|
|
87
114
|
}
|
|
88
115
|
|
|
89
116
|
export default Data;
|