@zenflux/core 0.0.0-alpha.10 → 0.0.0-alpha.12
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 +7 -5
- 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/exports.d.ts +1 -0
- package/dist/es/exports.d.ts.map +1 -1
- package/dist/es/initializer.d.ts +1 -0
- package/dist/es/initializer.d.ts.map +1 -1
- package/dist/es/interfaces/data.d.ts +18 -2
- package/dist/es/interfaces/data.d.ts.map +1 -1
- package/dist/es/interfaces/index.d.ts +9 -0
- package/dist/es/interfaces/index.d.ts.map +1 -0
- 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 +4 -11
- 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 +135 -88
- package/package.json +1 -1
- package/src/clients/http.ts +55 -36
- 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/exports.ts +1 -0
- package/src/interfaces/data.ts +21 -2
- package/src/interfaces/index.ts +8 -0
- package/src/manager-bases/commands.ts +6 -3
- package/src/manager-bases/data.ts +36 -24
- 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
package/src/clients/http.ts
CHANGED
|
@@ -6,12 +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
|
-
|
|
10
|
+
TErrorHandlerCallbackType,
|
|
11
|
+
TResponseFilterCallbackType,
|
|
12
|
+
TResponseHandlerCallbackType,
|
|
13
|
+
|
|
14
|
+
E_HTTP_METHOD_TYPE,
|
|
15
|
+
} from "../interfaces/";
|
|
15
16
|
|
|
16
17
|
// noinspection ExceptionCaughtLocallyJS
|
|
17
18
|
|
|
@@ -20,8 +21,9 @@ export class Http extends ObjectBase {
|
|
|
20
21
|
|
|
21
22
|
private readonly apiBaseUrl: string;
|
|
22
23
|
|
|
23
|
-
private errorHandler?:
|
|
24
|
-
private
|
|
24
|
+
private errorHandler?: TErrorHandlerCallbackType = undefined;
|
|
25
|
+
private responseFilter?: TResponseFilterCallbackType = undefined;
|
|
26
|
+
private responseHandler?: TResponseHandlerCallbackType = undefined;
|
|
25
27
|
|
|
26
28
|
static getName() {
|
|
27
29
|
return 'Core/Clients/Http';
|
|
@@ -40,35 +42,19 @@ export class Http extends ObjectBase {
|
|
|
40
42
|
this.apiBaseUrl = apiBaseUrl + '/';
|
|
41
43
|
}
|
|
42
44
|
|
|
43
|
-
public setResponseHandler( callback: TResponseHandlerCallback ) {
|
|
44
|
-
if ( this.responseHandler ) {
|
|
45
|
-
throw new Error( 'Data handler already set.' );
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
this.responseHandler = callback;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
public setErrorHandler( callback: TErrorHandlerCallback ) {
|
|
52
|
-
if ( this.errorHandler ) {
|
|
53
|
-
throw new Error( 'Error handler already set.' );
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
this.errorHandler = callback;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
45
|
/**
|
|
60
46
|
* Function fetch() : Fetch api.
|
|
61
47
|
*/
|
|
62
|
-
async fetch( path: string, method:
|
|
48
|
+
async fetch( path: string, method: E_HTTP_METHOD_TYPE, body: any = {} ) {
|
|
63
49
|
this.logger.startWith( { path, method, body } );
|
|
64
50
|
|
|
65
51
|
const params: RequestInit = { 'credentials': 'include' }, // Support cookies.
|
|
66
52
|
headers = {};
|
|
67
53
|
|
|
68
54
|
if ( [
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
55
|
+
E_HTTP_METHOD_TYPE.POST,
|
|
56
|
+
E_HTTP_METHOD_TYPE.PUT,
|
|
57
|
+
E_HTTP_METHOD_TYPE.PATCH,
|
|
72
58
|
].includes( method ) ) {
|
|
73
59
|
Object.assign( headers, { 'Content-Type': 'application/json' } );
|
|
74
60
|
Object.assign( params, {
|
|
@@ -86,25 +72,30 @@ export class Http extends ObjectBase {
|
|
|
86
72
|
let data = undefined;
|
|
87
73
|
|
|
88
74
|
try {
|
|
89
|
-
let responseText = await response.text();
|
|
90
|
-
|
|
91
75
|
if ( ! response.ok ) {
|
|
92
76
|
throw response;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
let responseText = await response.text();
|
|
80
|
+
|
|
81
|
+
responseText = this.applyResponseFilter( responseText );
|
|
82
|
+
|
|
83
|
+
// TODO: Currently support JSON and plain text.
|
|
84
|
+
if ( response.headers?.get( 'Content-Type' )?.includes( 'application/json' ) ) {
|
|
96
85
|
data = JSON.parse( responseText );
|
|
97
86
|
} else {
|
|
98
|
-
|
|
87
|
+
data = responseText;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if ( this.applyResponseHandler( data ) ) {
|
|
91
|
+
return false;
|
|
99
92
|
}
|
|
100
93
|
} catch ( e ) {
|
|
101
94
|
if ( this.applyErrorHandler( e ) ) {
|
|
102
95
|
return false;
|
|
103
96
|
}
|
|
104
97
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
return false;
|
|
98
|
+
await Promise.reject( e );
|
|
108
99
|
}
|
|
109
100
|
|
|
110
101
|
this.logger.drop( { path }, data );
|
|
@@ -112,10 +103,38 @@ export class Http extends ObjectBase {
|
|
|
112
103
|
return data;
|
|
113
104
|
}
|
|
114
105
|
|
|
106
|
+
public setErrorHandler( callback: TErrorHandlerCallbackType ) {
|
|
107
|
+
if ( this.errorHandler ) {
|
|
108
|
+
throw new Error( 'Error handler already set.' );
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
this.errorHandler = callback;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
public setResponseFilter( callback: TResponseFilterCallbackType ) {
|
|
115
|
+
if ( this.responseFilter ) {
|
|
116
|
+
throw new Error( 'Response filter already set.' );
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
this.responseFilter = callback;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
public setResponseHandler( callback: TResponseHandlerCallbackType ) {
|
|
123
|
+
if ( this.responseHandler ) {
|
|
124
|
+
throw new Error( 'Response handler already set.' );
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
this.responseHandler = callback;
|
|
128
|
+
}
|
|
129
|
+
|
|
115
130
|
private applyErrorHandler( error: any ) {
|
|
116
131
|
return this.errorHandler && this.errorHandler( error );
|
|
117
132
|
}
|
|
118
133
|
|
|
134
|
+
private applyResponseFilter( text: string ) {
|
|
135
|
+
return ( this.responseFilter && this.responseFilter( text ) ) || text;
|
|
136
|
+
}
|
|
137
|
+
|
|
119
138
|
private applyResponseHandler( text: string ) {
|
|
120
139
|
return this.responseHandler && this.responseHandler( text );
|
|
121
140
|
}
|
|
@@ -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/exports.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
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";
|
package/src/interfaces/data.ts
CHANGED
|
@@ -1,4 +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 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
|
+
};
|
|
@@ -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,16 +8,23 @@ import { CommandData } from '../command-bases/';
|
|
|
8
8
|
|
|
9
9
|
import Http from '../clients/http';
|
|
10
10
|
|
|
11
|
-
import {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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/";
|
|
16
23
|
|
|
17
24
|
export class Data extends Commands {
|
|
18
25
|
private static client: Http;
|
|
19
26
|
|
|
20
|
-
public currentHttpMethod:
|
|
27
|
+
public currentHttpMethod: E_HTTP_METHOD_TYPE;
|
|
21
28
|
|
|
22
29
|
static getName() {
|
|
23
30
|
return 'Core/Managers/Data';
|
|
@@ -26,7 +33,6 @@ export class Data extends Commands {
|
|
|
26
33
|
constructor( Config: IAPIConfig) {
|
|
27
34
|
super();
|
|
28
35
|
|
|
29
|
-
// @ts-ignore
|
|
30
36
|
Data.client = new Http( Config.baseURL );
|
|
31
37
|
}
|
|
32
38
|
|
|
@@ -35,25 +41,25 @@ export class Data extends Commands {
|
|
|
35
41
|
}
|
|
36
42
|
|
|
37
43
|
public get( command: string, args: ICommandArgsInterface = {}, options: ICommandOptionsInterface = {} ) {
|
|
38
|
-
this.currentHttpMethod =
|
|
44
|
+
this.currentHttpMethod = E_HTTP_METHOD_TYPE.GET;
|
|
39
45
|
|
|
40
46
|
return super.run( command, args, options );
|
|
41
47
|
}
|
|
42
48
|
|
|
43
49
|
public update( command: string, args: ICommandArgsInterface = {}, options: {} = {} ) {
|
|
44
|
-
this.currentHttpMethod =
|
|
50
|
+
this.currentHttpMethod = E_HTTP_METHOD_TYPE.PATCH;
|
|
45
51
|
|
|
46
52
|
return super.run( command, args, options );
|
|
47
53
|
}
|
|
48
54
|
|
|
49
55
|
public delete( command: string, args: ICommandArgsInterface = {}, options: {} = {} ) {
|
|
50
|
-
this.currentHttpMethod =
|
|
56
|
+
this.currentHttpMethod = E_HTTP_METHOD_TYPE.DELETE;
|
|
51
57
|
|
|
52
58
|
return super.run( command, args, options );
|
|
53
59
|
}
|
|
54
60
|
|
|
55
61
|
public create( command: string, args: ICommandArgsInterface = {}, options: {} = {} ) {
|
|
56
|
-
this.currentHttpMethod =
|
|
62
|
+
this.currentHttpMethod = E_HTTP_METHOD_TYPE.POST;
|
|
57
63
|
|
|
58
64
|
return super.run( command, args, options );
|
|
59
65
|
}
|
|
@@ -72,32 +78,38 @@ export class Data extends Commands {
|
|
|
72
78
|
},
|
|
73
79
|
};
|
|
74
80
|
|
|
75
|
-
if (
|
|
81
|
+
if ( E_HTTP_METHOD_TYPE.GET === this.currentHttpMethod ) {
|
|
76
82
|
newArgs.args.query = args;
|
|
77
83
|
} else {
|
|
78
84
|
newArgs.args.data = args;
|
|
79
85
|
}
|
|
80
86
|
|
|
81
|
-
args.result = await super.runInstance( command, newArgs, options )
|
|
87
|
+
args.result = await super.runInstance( command, newArgs, options );
|
|
82
88
|
|
|
83
89
|
// Clear method type.
|
|
84
|
-
this.currentHttpMethod =
|
|
90
|
+
this.currentHttpMethod = E_HTTP_METHOD_TYPE.__EMPTY__;
|
|
85
91
|
|
|
86
92
|
return args.result;
|
|
87
93
|
}
|
|
88
94
|
|
|
89
95
|
/**
|
|
90
|
-
*
|
|
91
|
-
*/
|
|
92
|
-
public setErrorHandler( callback: TErrorHandlerCallback ) {
|
|
93
|
-
this.getClient().setErrorHandler( callback );
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* On return true, the request will be swallowed.
|
|
96
|
+
* Handlers on return true will swallow the request.
|
|
98
97
|
*/
|
|
99
|
-
public
|
|
100
|
-
|
|
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
|
+
}
|
|
101
113
|
}
|
|
102
114
|
}
|
|
103
115
|
|
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
|
-
}
|