@zenflux/core 0.0.0-alpha.11 → 0.0.0-alpha.13
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 +5 -6
- package/dist/es/clients/http.d.ts.map +1 -1
- package/dist/es/command-bases/command-base.d.ts +4 -4
- package/dist/es/command-bases/command-base.d.ts.map +1 -1
- package/dist/es/command-bases/command-data.d.ts +1 -1
- package/dist/es/command-bases/command-data.d.ts.map +1 -1
- package/dist/es/core/object-base.d.ts +1 -1
- package/dist/es/core/object-base.d.ts.map +1 -1
- 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/errors/index.d.ts +1 -0
- package/dist/es/errors/index.d.ts.map +1 -1
- package/dist/es/interfaces/data.d.ts +14 -5
- package/dist/es/interfaces/data.d.ts.map +1 -1
- package/dist/es/interfaces/index.d.ts +4 -0
- package/dist/es/interfaces/index.d.ts.map +1 -1
- package/dist/es/manager-bases/commands.d.ts +1 -1
- package/dist/es/manager-bases/commands.d.ts.map +1 -1
- package/dist/es/manager-bases/data.d.ts +3 -6
- package/dist/es/manager-bases/data.d.ts.map +1 -1
- package/dist/es/managers/index.d.ts +1 -1
- package/dist/es/managers/index.d.ts.map +1 -1
- package/dist/es/zenflux-core.js +54 -42
- package/package.json +2 -2
- package/src/clients/http.ts +16 -16
- package/src/command-bases/command-base.ts +16 -10
- 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/interfaces/data.ts +16 -6
- package/src/interfaces/index.ts +4 -4
- package/src/manager-bases/commands.ts +6 -3
- package/src/manager-bases/data.ts +25 -25
- package/src/managers/index.ts +1 -1
- package/dist/es/enums/http.d.ts +0 -13
- package/dist/es/enums/http.d.ts.map +0 -1
- package/src/enums/http.ts +0 -12
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zenflux/core",
|
|
3
|
-
"version": "0.0.0-alpha.
|
|
3
|
+
"version": "0.0.0-alpha.13",
|
|
4
4
|
"description": "ZenFlux core",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"zenflux",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"@types/jest": "^29.2.5",
|
|
42
42
|
"@typescript-eslint/eslint-plugin": "^5.48.0",
|
|
43
43
|
"@typescript-eslint/parser": "^5.48.0",
|
|
44
|
-
"@zenflux/rollup-toolkit": "^0.0.0-alpha.
|
|
44
|
+
"@zenflux/rollup-toolkit": "^0.0.0-alpha.15",
|
|
45
45
|
"babel-jest": "^29.3.1",
|
|
46
46
|
"ts-jest": "^29.0.3",
|
|
47
47
|
"tslib": "^2.4.1"
|
package/src/clients/http.ts
CHANGED
|
@@ -6,13 +6,13 @@ import ObjectBase from "../core/object-base";
|
|
|
6
6
|
|
|
7
7
|
import Logger from '../modules/logger';
|
|
8
8
|
|
|
9
|
-
import { HTTPMethodEnum } from "../enums/http";
|
|
10
|
-
|
|
11
9
|
import {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
TErrorHandlerCallbackType,
|
|
11
|
+
TResponseFilterCallbackType,
|
|
12
|
+
TResponseHandlerCallbackType,
|
|
13
|
+
|
|
14
|
+
E_HTTP_METHOD_TYPE,
|
|
15
|
+
} from "../interfaces/";
|
|
16
16
|
|
|
17
17
|
// noinspection ExceptionCaughtLocallyJS
|
|
18
18
|
|
|
@@ -21,9 +21,9 @@ export class Http extends ObjectBase {
|
|
|
21
21
|
|
|
22
22
|
private readonly apiBaseUrl: string;
|
|
23
23
|
|
|
24
|
-
private errorHandler?:
|
|
25
|
-
private responseFilter?:
|
|
26
|
-
private responseHandler?:
|
|
24
|
+
private errorHandler?: TErrorHandlerCallbackType = undefined;
|
|
25
|
+
private responseFilter?: TResponseFilterCallbackType = undefined;
|
|
26
|
+
private responseHandler?: TResponseHandlerCallbackType = undefined;
|
|
27
27
|
|
|
28
28
|
static getName() {
|
|
29
29
|
return 'Core/Clients/Http';
|
|
@@ -45,16 +45,16 @@ export class Http extends ObjectBase {
|
|
|
45
45
|
/**
|
|
46
46
|
* Function fetch() : Fetch api.
|
|
47
47
|
*/
|
|
48
|
-
async fetch( path: string, method:
|
|
48
|
+
async fetch( path: string, method: E_HTTP_METHOD_TYPE, body: any = {} ) {
|
|
49
49
|
this.logger.startWith( { path, method, body } );
|
|
50
50
|
|
|
51
51
|
const params: RequestInit = { 'credentials': 'include' }, // Support cookies.
|
|
52
52
|
headers = {};
|
|
53
53
|
|
|
54
54
|
if ( [
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
55
|
+
E_HTTP_METHOD_TYPE.POST,
|
|
56
|
+
E_HTTP_METHOD_TYPE.PUT,
|
|
57
|
+
E_HTTP_METHOD_TYPE.PATCH,
|
|
58
58
|
].includes( method ) ) {
|
|
59
59
|
Object.assign( headers, { 'Content-Type': 'application/json' } );
|
|
60
60
|
Object.assign( params, {
|
|
@@ -103,7 +103,7 @@ export class Http extends ObjectBase {
|
|
|
103
103
|
return data;
|
|
104
104
|
}
|
|
105
105
|
|
|
106
|
-
public setErrorHandler( callback:
|
|
106
|
+
public setErrorHandler( callback: TErrorHandlerCallbackType ) {
|
|
107
107
|
if ( this.errorHandler ) {
|
|
108
108
|
throw new Error( 'Error handler already set.' );
|
|
109
109
|
}
|
|
@@ -111,7 +111,7 @@ export class Http extends ObjectBase {
|
|
|
111
111
|
this.errorHandler = callback;
|
|
112
112
|
}
|
|
113
113
|
|
|
114
|
-
public setResponseFilter( callback:
|
|
114
|
+
public setResponseFilter( callback: TResponseFilterCallbackType ) {
|
|
115
115
|
if ( this.responseFilter ) {
|
|
116
116
|
throw new Error( 'Response filter already set.' );
|
|
117
117
|
}
|
|
@@ -119,7 +119,7 @@ export class Http extends ObjectBase {
|
|
|
119
119
|
this.responseFilter = callback;
|
|
120
120
|
}
|
|
121
121
|
|
|
122
|
-
public setResponseHandler( callback:
|
|
122
|
+
public setResponseHandler( callback: TResponseHandlerCallbackType ) {
|
|
123
123
|
if ( this.responseHandler ) {
|
|
124
124
|
throw new Error( 'Response handler already set.' );
|
|
125
125
|
}
|
|
@@ -11,15 +11,29 @@ import ForceMethod from "../errors/force-method";
|
|
|
11
11
|
|
|
12
12
|
import Logger from "../modules/logger";
|
|
13
13
|
|
|
14
|
-
import {
|
|
14
|
+
import { ControllerAlreadySet } from "../errors";
|
|
15
|
+
|
|
16
|
+
import { ICommandArgsInterface, ICommandOptionsInterface } from "../interfaces/";
|
|
15
17
|
|
|
16
18
|
export abstract class CommandBase extends ObjectBase {
|
|
19
|
+
private static controller: Controller;
|
|
20
|
+
|
|
17
21
|
protected args: ICommandArgsInterface = {};
|
|
18
22
|
protected options: ICommandOptionsInterface = {};
|
|
19
23
|
|
|
20
24
|
private readonly logger: Logger;
|
|
21
25
|
|
|
22
|
-
|
|
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
|
+
}
|
|
23
37
|
|
|
24
38
|
constructor( args: ICommandArgsInterface = {}, options = {} ) {
|
|
25
39
|
super();
|
|
@@ -62,14 +76,6 @@ export abstract class CommandBase extends ObjectBase {
|
|
|
62
76
|
return this.options;
|
|
63
77
|
}
|
|
64
78
|
|
|
65
|
-
public setController( controller: Controller ) {
|
|
66
|
-
this.controller = controller;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
public getController() {
|
|
70
|
-
return this.controller;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
79
|
public onBeforeApply?():void;
|
|
74
80
|
|
|
75
81
|
public onAfterApply?():void;
|
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/interfaces/data.ts
CHANGED
|
@@ -1,13 +1,23 @@
|
|
|
1
|
-
export type
|
|
1
|
+
export type TErrorHandlerCallbackType = ( data: any ) => boolean;
|
|
2
2
|
|
|
3
|
-
export type
|
|
3
|
+
export type TResponseFilterCallbackType = ( text: string ) => string;
|
|
4
4
|
|
|
5
|
-
export type
|
|
5
|
+
export type TResponseHandlerCallbackType = ( text: string) => boolean;
|
|
6
6
|
|
|
7
|
-
export type
|
|
7
|
+
export type TPossibleHandlersType = TErrorHandlerCallbackType | TResponseFilterCallbackType | TResponseHandlerCallbackType;
|
|
8
8
|
|
|
9
|
-
export enum
|
|
9
|
+
export enum E_RESPONSE_HANDLER_TYPE {
|
|
10
10
|
ERROR_HANDLER = 'error_handler',
|
|
11
11
|
RESPONSE_FILTER = 'response_filter',
|
|
12
12
|
RESPONSE_HANDLER = 'response_handler',
|
|
13
|
-
}
|
|
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
|
+
};
|
package/src/interfaces/index.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @author: Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
3
|
*/
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
export * from './commands';
|
|
5
|
+
export * from './config';
|
|
6
6
|
export * from './data';
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
export * from './managers';
|
|
8
|
+
export * from './object-base';
|
|
@@ -10,11 +10,12 @@ import ObjectBase from "../core/object-base";
|
|
|
10
10
|
import Controller from "../core/controller";
|
|
11
11
|
|
|
12
12
|
import {
|
|
13
|
-
ICommandArgsInterface,
|
|
14
13
|
CommandCallbackType,
|
|
14
|
+
ICommandArgsInterface,
|
|
15
15
|
IOnHookAffectInterface,
|
|
16
|
-
IOnHookInterface,
|
|
17
|
-
|
|
16
|
+
IOnHookInterface,
|
|
17
|
+
ICommandOptionsInterface,
|
|
18
|
+
} from "../interfaces/";
|
|
18
19
|
|
|
19
20
|
export class Commands extends ObjectBase {
|
|
20
21
|
public static readonly trace:string[] = [];
|
|
@@ -94,6 +95,8 @@ export class Commands extends ObjectBase {
|
|
|
94
95
|
throw new CommandAlreadyRegistered( command );
|
|
95
96
|
}
|
|
96
97
|
|
|
98
|
+
command.setController( controller );
|
|
99
|
+
|
|
97
100
|
this.commands[ commandName ] = command;
|
|
98
101
|
|
|
99
102
|
result[ commandName ] = command;
|
|
@@ -8,22 +8,23 @@ import { CommandData } from '../command-bases/';
|
|
|
8
8
|
|
|
9
9
|
import Http from '../clients/http';
|
|
10
10
|
|
|
11
|
-
import { HTTPMethodEnum } from "../enums/http";
|
|
12
|
-
|
|
13
|
-
import { IAPIConfig } from "../interfaces/config";
|
|
14
|
-
import { ICommandArgsInterface, ICommandOptionsInterface } from '../interfaces/commands';
|
|
15
11
|
import {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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/";
|
|
22
23
|
|
|
23
24
|
export class Data extends Commands {
|
|
24
25
|
private static client: Http;
|
|
25
26
|
|
|
26
|
-
public currentHttpMethod:
|
|
27
|
+
public currentHttpMethod: E_HTTP_METHOD_TYPE;
|
|
27
28
|
|
|
28
29
|
static getName() {
|
|
29
30
|
return 'Core/Managers/Data';
|
|
@@ -32,7 +33,6 @@ export class Data extends Commands {
|
|
|
32
33
|
constructor( Config: IAPIConfig) {
|
|
33
34
|
super();
|
|
34
35
|
|
|
35
|
-
// @ts-ignore
|
|
36
36
|
Data.client = new Http( Config.baseURL );
|
|
37
37
|
}
|
|
38
38
|
|
|
@@ -41,25 +41,25 @@ export class Data extends Commands {
|
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
public get( command: string, args: ICommandArgsInterface = {}, options: ICommandOptionsInterface = {} ) {
|
|
44
|
-
this.currentHttpMethod =
|
|
44
|
+
this.currentHttpMethod = E_HTTP_METHOD_TYPE.GET;
|
|
45
45
|
|
|
46
46
|
return super.run( command, args, options );
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
public update( command: string, args: ICommandArgsInterface = {}, options: {} = {} ) {
|
|
50
|
-
this.currentHttpMethod =
|
|
50
|
+
this.currentHttpMethod = E_HTTP_METHOD_TYPE.PATCH;
|
|
51
51
|
|
|
52
52
|
return super.run( command, args, options );
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
public delete( command: string, args: ICommandArgsInterface = {}, options: {} = {} ) {
|
|
56
|
-
this.currentHttpMethod =
|
|
56
|
+
this.currentHttpMethod = E_HTTP_METHOD_TYPE.DELETE;
|
|
57
57
|
|
|
58
58
|
return super.run( command, args, options );
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
public create( command: string, args: ICommandArgsInterface = {}, options: {} = {} ) {
|
|
62
|
-
this.currentHttpMethod =
|
|
62
|
+
this.currentHttpMethod = E_HTTP_METHOD_TYPE.POST;
|
|
63
63
|
|
|
64
64
|
return super.run( command, args, options );
|
|
65
65
|
}
|
|
@@ -78,7 +78,7 @@ export class Data extends Commands {
|
|
|
78
78
|
},
|
|
79
79
|
};
|
|
80
80
|
|
|
81
|
-
if (
|
|
81
|
+
if ( E_HTTP_METHOD_TYPE.GET === this.currentHttpMethod ) {
|
|
82
82
|
newArgs.args.query = args;
|
|
83
83
|
} else {
|
|
84
84
|
newArgs.args.data = args;
|
|
@@ -87,7 +87,7 @@ export class Data extends Commands {
|
|
|
87
87
|
args.result = await super.runInstance( command, newArgs, options );
|
|
88
88
|
|
|
89
89
|
// Clear method type.
|
|
90
|
-
this.currentHttpMethod =
|
|
90
|
+
this.currentHttpMethod = E_HTTP_METHOD_TYPE.__EMPTY__;
|
|
91
91
|
|
|
92
92
|
return args.result;
|
|
93
93
|
}
|
|
@@ -95,16 +95,16 @@ export class Data extends Commands {
|
|
|
95
95
|
/**
|
|
96
96
|
* Handlers on return true will swallow the request.
|
|
97
97
|
*/
|
|
98
|
-
public setHandler( type:
|
|
98
|
+
public setHandler( type: E_RESPONSE_HANDLER_TYPE, callback: TPossibleHandlersType ) {
|
|
99
99
|
switch ( type ) {
|
|
100
|
-
case
|
|
101
|
-
Data.client.setErrorHandler( callback as
|
|
100
|
+
case E_RESPONSE_HANDLER_TYPE.ERROR_HANDLER:
|
|
101
|
+
Data.client.setErrorHandler( callback as TErrorHandlerCallbackType );
|
|
102
102
|
break;
|
|
103
|
-
case
|
|
104
|
-
Data.client.setResponseFilter( callback as
|
|
103
|
+
case E_RESPONSE_HANDLER_TYPE.RESPONSE_FILTER:
|
|
104
|
+
Data.client.setResponseFilter( callback as TResponseFilterCallbackType );
|
|
105
105
|
break;
|
|
106
|
-
case
|
|
107
|
-
Data.client.setResponseHandler( callback as
|
|
106
|
+
case E_RESPONSE_HANDLER_TYPE.RESPONSE_HANDLER:
|
|
107
|
+
Data.client.setResponseHandler( callback as TResponseHandlerCallbackType );
|
|
108
108
|
break;
|
|
109
109
|
|
|
110
110
|
default:
|
package/src/managers/index.ts
CHANGED
|
@@ -6,7 +6,7 @@ import Controllers from "../manager-bases/controllers";
|
|
|
6
6
|
import Data from "../manager-bases/data";
|
|
7
7
|
import Internal from "../manager-bases/internal";
|
|
8
8
|
|
|
9
|
-
import { IAPIConfig } from "../interfaces/
|
|
9
|
+
import { IAPIConfig } from "../interfaces/";
|
|
10
10
|
|
|
11
11
|
export const afterInitializeCallbacks: ( () => void )[] = [];
|
|
12
12
|
|
package/dist/es/enums/http.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @author: Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
|
-
*/
|
|
4
|
-
export declare enum HTTPMethodEnum {
|
|
5
|
-
DELETE = "DELETE",
|
|
6
|
-
GET = "GET",
|
|
7
|
-
OPTIONS = "OPTIONS",
|
|
8
|
-
PATCH = "PATCH",
|
|
9
|
-
POST = "POST",
|
|
10
|
-
PUT = "PUT",
|
|
11
|
-
'__EMPTY__' = ""
|
|
12
|
-
}
|
|
13
|
-
//# sourceMappingURL=http.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../src/enums/http.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,oBAAY,cAAc;IACtB,MAAM,WAAW;IACjB,GAAG,QAAQ;IACX,OAAO,YAAY;IACnB,KAAK,UAAU;IACf,IAAI,SAAS;IACb,GAAG,QAAQ;IACX,WAAW,KAAK;CACnB"}
|
package/src/enums/http.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @author: Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
|
-
*/
|
|
4
|
-
export enum HTTPMethodEnum {
|
|
5
|
-
DELETE = 'DELETE', // Delete.
|
|
6
|
-
GET = 'GET', // Read.
|
|
7
|
-
OPTIONS = 'OPTIONS',// Options about the request
|
|
8
|
-
PATCH = 'PATCH', // Update/Modify.
|
|
9
|
-
POST = 'POST', // Create.
|
|
10
|
-
PUT = 'PUT', // Update/Replace.
|
|
11
|
-
'__EMPTY__' = ''
|
|
12
|
-
}
|