@zenflux/core 0.1.12 → 0.1.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/package.json +3 -4
- package/src/bases/controller-base.ts +0 -96
- package/src/bases/index.ts +0 -6
- package/src/bases/initialize-base.ts +0 -32
- package/src/bases/object-base.ts +0 -55
- package/src/clients/http.ts +0 -152
- package/src/command-bases/command-base.ts +0 -82
- package/src/command-bases/command-internal.ts +0 -11
- package/src/command-bases/command-public.ts +0 -12
- package/src/command-bases/command-restful.ts +0 -55
- package/src/command-bases/index.ts +0 -8
- package/src/errors/command-already-registered.ts +0 -13
- package/src/errors/command-not-found.ts +0 -12
- package/src/errors/controller-already-registered.ts +0 -13
- package/src/errors/controller-already-set.ts +0 -13
- package/src/errors/error-with-metadata.ts +0 -14
- package/src/errors/force-method-implementation.ts +0 -15
- package/src/errors/index-public.ts +0 -1
- package/src/errors/index.ts +0 -10
- package/src/exports-index.ts +0 -13
- package/src/exports.ts +0 -58
- package/src/index.ts +0 -18
- package/src/interfaces/commands.ts +0 -17
- package/src/interfaces/config.ts +0 -23
- package/src/interfaces/index.ts +0 -8
- package/src/interfaces/logger.ts +0 -19
- package/src/interfaces/restful.ts +0 -23
- package/src/managers/commands.ts +0 -268
- package/src/managers/controllers.ts +0 -40
- package/src/managers/export.ts +0 -30
- package/src/managers/index.ts +0 -8
- package/src/managers/internal.ts +0 -11
- package/src/managers/restful.ts +0 -117
- package/src/modules/debugger.ts +0 -89
- package/src/modules/event-bus/event-bus.ts +0 -204
- package/src/modules/logger/config.ts +0 -44
- package/src/modules/logger/implementation.ts +0 -287
- package/src/modules/logger/index.ts +0 -2
- package/src/modules/logger/logger-browser-infra.ts +0 -344
- package/src/modules/logger/types/picocolors.d.ts +0 -15
- package/src/modules/logger/utils/index.ts +0 -3
- package/src/modules/logger/utils/object.ts +0 -19
- package/src/modules/logger/utils/rgb.ts +0 -34
- package/src/modules/mcp-server/mcp-server.ts +0 -139
- package/src/modules/mcp-server/mcp-service.ts +0 -80
- package/src/modules/service/service-base.ts +0 -55
- package/src/modules/service/service-locator.ts +0 -205
- package/src/modules/service/service-with-dependencies-base.ts +0 -51
package/src/exports.ts
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
|
-
*/
|
|
4
|
-
// @ts-ignore - TODO handle error in jest
|
|
5
|
-
import { bases, commandBases, errors, interfaces, managers, } from "./exports-index";
|
|
6
|
-
|
|
7
|
-
import { Logger } from "./modules/logger";
|
|
8
|
-
|
|
9
|
-
import * as pkg from "../package.json" assert { type: "json" };
|
|
10
|
-
|
|
11
|
-
declare global {
|
|
12
|
-
var __ZEN_CORE__IS_INITIALIZED__: boolean;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export const classes = {
|
|
16
|
-
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
17
|
-
/* eslint-disable @typescript-eslint/explicit-member-accessibility */
|
|
18
|
-
|
|
19
|
-
Logger,
|
|
20
|
-
|
|
21
|
-
/* eslint-enable */
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
let exportedConfig: interfaces.IAPIConfig = {
|
|
25
|
-
version: pkg.version,
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
export function initialize( config?: Partial<interfaces.IAPIConfig> ) {
|
|
29
|
-
if ( "undefined" !== typeof __ZEN_CORE__IS_INITIALIZED__ && __ZEN_CORE__IS_INITIALIZED__ ) {
|
|
30
|
-
throw new Error( "ZenCore is already initialized." );
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
exportedConfig = ( config || exportedConfig ) as interfaces.IAPIConfig;
|
|
34
|
-
|
|
35
|
-
managers.initialize( exportedConfig );
|
|
36
|
-
|
|
37
|
-
globalThis.__ZEN_CORE__IS_INITIALIZED__ = true;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export function destroy() {
|
|
41
|
-
managers.destroy();
|
|
42
|
-
|
|
43
|
-
globalThis.__ZEN_CORE__IS_INITIALIZED__ = false;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export function onAfterInitialize( callback: () => void ) {
|
|
47
|
-
managers.afterInitializeCallbacks.push( callback );
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export const config = exportedConfig;
|
|
51
|
-
|
|
52
|
-
export {
|
|
53
|
-
bases,
|
|
54
|
-
commandBases,
|
|
55
|
-
errors,
|
|
56
|
-
interfaces,
|
|
57
|
-
managers
|
|
58
|
-
};
|
package/src/index.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import * as ZenCore from "./exports";
|
|
6
|
-
|
|
7
|
-
// TODO: Only in development mode
|
|
8
|
-
if ( ! globalThis?.zCore ) {
|
|
9
|
-
globalThis.zCore = ZenCore;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
declare global {
|
|
13
|
-
var zCore: typeof ZenCore;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export * from "./exports-index";
|
|
17
|
-
|
|
18
|
-
export default ZenCore;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
export type TCommandCallbackType = ( args?: any, options?: any ) => any;
|
|
2
|
-
|
|
3
|
-
export interface ICommandArgsInterface {
|
|
4
|
-
[ key: string ]: any;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export interface ICommandOptionsInterface {
|
|
8
|
-
[ key: string ]: any;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface IOnHookAffectInterface {
|
|
12
|
-
[ key: string ]: Array<string>;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export interface IOnHookInterface {
|
|
16
|
-
[ key: string ]: Array<TCommandCallbackType>;
|
|
17
|
-
}
|
package/src/interfaces/config.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
*
|
|
7
|
-
*/
|
|
8
|
-
export interface IAPIConfig {
|
|
9
|
-
/**
|
|
10
|
-
* @description API version, from `package.json`
|
|
11
|
-
*/
|
|
12
|
-
version: string;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* @description API base url for http requests.
|
|
16
|
-
*/
|
|
17
|
-
baseURL?: string;
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* @description Request Init for fetch API.
|
|
21
|
-
*/
|
|
22
|
-
requestInit?: RequestInit;
|
|
23
|
-
}
|
package/src/interfaces/index.ts
DELETED
package/src/interfaces/logger.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
|
-
*/
|
|
4
|
-
export type TCaller = string | Function;
|
|
5
|
-
|
|
6
|
-
export interface ILogger {
|
|
7
|
-
log( caller: TCaller, message: string, ...params: any[] ): void;
|
|
8
|
-
warn( caller: TCaller, message: string, ...params: any[] ): void;
|
|
9
|
-
error( caller: TCaller, message: string, ...params: any[] ): void;
|
|
10
|
-
info( caller: TCaller, message: string, ...params: any[] ): void;
|
|
11
|
-
debug( caller: TCaller, message: string, ...params: any[] ): void;
|
|
12
|
-
addMessagePrefix( prefix: string ): void;
|
|
13
|
-
|
|
14
|
-
startsEmpty( caller: TCaller ): void;
|
|
15
|
-
startsWith( caller: TCaller, params: object | string ): void;
|
|
16
|
-
|
|
17
|
-
dump( caller: TCaller, params: { [ key: string ]: object|string }, notice?: string ): void;
|
|
18
|
-
drop( caller: TCaller, according: { [ key: string ]: string }, data: any ): void;
|
|
19
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
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
|
-
};
|
package/src/managers/commands.ts
DELETED
|
@@ -1,268 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
|
-
* @description Responsible for manging/running/hooking commands.
|
|
4
|
-
*/
|
|
5
|
-
import { ObjectBase } from "../bases/object-base";
|
|
6
|
-
|
|
7
|
-
import { CommandNotFound } from "../errors/command-not-found";
|
|
8
|
-
import { CommandAlreadyRegistered } from "../errors/command-already-registered";
|
|
9
|
-
|
|
10
|
-
import type {
|
|
11
|
-
TCommandCallbackType,
|
|
12
|
-
|
|
13
|
-
IOnHookAffectInterface,
|
|
14
|
-
IOnHookInterface,
|
|
15
|
-
|
|
16
|
-
ICommandArgsInterface,
|
|
17
|
-
ICommandOptionsInterface,
|
|
18
|
-
|
|
19
|
-
ILogger,
|
|
20
|
-
} from "../interfaces";
|
|
21
|
-
|
|
22
|
-
import type { ControllerBase } from "../bases/controller-base";
|
|
23
|
-
import type { CommandBase } from "../command-bases/command-base";
|
|
24
|
-
|
|
25
|
-
export class Commands extends ObjectBase {
|
|
26
|
-
public static readonly trace:string[] = [];
|
|
27
|
-
|
|
28
|
-
private current: { [ key: string ]: CommandBase } = {};
|
|
29
|
-
private currentArgs: { [ key: string ]: ICommandArgsInterface } = {};
|
|
30
|
-
|
|
31
|
-
private trace:string[] = [];
|
|
32
|
-
|
|
33
|
-
private commands: { [ key: string ]: typeof CommandBase } = {};
|
|
34
|
-
|
|
35
|
-
private onBeforeHooks: IOnHookInterface = {};
|
|
36
|
-
private onBeforeUIHooks: IOnHookInterface = {};
|
|
37
|
-
|
|
38
|
-
private onAfterHooks: IOnHookInterface = {};
|
|
39
|
-
private onAfterOnceHooks: IOnHookInterface = {};
|
|
40
|
-
private onAfterUIHooks: IOnHookInterface = {};
|
|
41
|
-
private onAfterAffectHooks: IOnHookAffectInterface = {};
|
|
42
|
-
|
|
43
|
-
private readonly logger: ILogger;
|
|
44
|
-
|
|
45
|
-
public static getName() {
|
|
46
|
-
return "ZenFlux/Core/Managers/Commands";
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
private static async runCallbacks( callbacks: Array<Function>, args: ICommandArgsInterface = {}, options: ICommandOptionsInterface = {} ) {
|
|
50
|
-
const callbacksLength = callbacks?.length || 0;
|
|
51
|
-
|
|
52
|
-
for ( let i = 0; i < callbacksLength; ++i ) {
|
|
53
|
-
const callback = options.pop === true ? callbacks.pop() : callbacks[ i ];
|
|
54
|
-
|
|
55
|
-
if ( ! callback ) {
|
|
56
|
-
throw new Error( "Callback is not defined." );
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
await callback( args, options );
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
private static hookCommand( stack: any, hookCommand: string, context: TCommandCallbackType | string ) {
|
|
64
|
-
if ( ! stack[ hookCommand ] ) {
|
|
65
|
-
stack[ hookCommand ] = [];
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
stack[ hookCommand ].push( context );
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
public constructor() {
|
|
72
|
-
super();
|
|
73
|
-
|
|
74
|
-
const type = ( this.constructor as typeof Commands );
|
|
75
|
-
|
|
76
|
-
this.logger = new zCore.classes.Logger( type );
|
|
77
|
-
this.logger.startsEmpty( this.constructor );
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
public async run( command: string | CommandBase, args: ICommandArgsInterface = {}, options: any = {} ) {
|
|
81
|
-
if ( typeof command === "string" ) {
|
|
82
|
-
command = this.getCommandInstance( command, args, options );
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
this.attachCurrent( command, args );
|
|
86
|
-
|
|
87
|
-
const result = await this.runInstance( command, args, options );
|
|
88
|
-
|
|
89
|
-
this.detachCurrent( command );
|
|
90
|
-
|
|
91
|
-
return result;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
public register( commands: { [ key: string ]: typeof CommandBase }, controller: ControllerBase ) {
|
|
95
|
-
const result: { [ key: string ]: typeof CommandBase } = {};
|
|
96
|
-
|
|
97
|
-
Object.values( commands ).forEach( ( command ) => {
|
|
98
|
-
const commandName = command.getName();
|
|
99
|
-
|
|
100
|
-
if ( this.commands[ commandName ] ) {
|
|
101
|
-
throw new CommandAlreadyRegistered( command );
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
command.setController( controller );
|
|
105
|
-
|
|
106
|
-
this.commands[ commandName ] = command;
|
|
107
|
-
|
|
108
|
-
result[ commandName ] = command;
|
|
109
|
-
} );
|
|
110
|
-
|
|
111
|
-
return result;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
public getAll() {
|
|
115
|
-
return this.commands;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
public getByName( name: string ) {
|
|
119
|
-
return this.commands[ name ];
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
public getLogger() {
|
|
123
|
-
return this.logger;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
public getCommandInstance( name: string, args: ICommandArgsInterface = {}, options = {} ): CommandBase {
|
|
127
|
-
const CommandClass = this.commands[ name ];
|
|
128
|
-
|
|
129
|
-
if ( ! CommandClass ) {
|
|
130
|
-
throw new CommandNotFound( name );
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// @ts-ignore
|
|
134
|
-
return new CommandClass( args, options );
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Used to set hooks that effects only data and not UI.
|
|
139
|
-
*/
|
|
140
|
-
public onBefore( hookCommand: string, callback: TCommandCallbackType ) {
|
|
141
|
-
Commands.hookCommand( this.onBeforeHooks, hookCommand, callback );
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Used to set hooks that effects only UI.
|
|
146
|
-
*/
|
|
147
|
-
public onBeforeUI( hookCommand: string, callback: TCommandCallbackType ) {
|
|
148
|
-
Commands.hookCommand( this.onBeforeUIHooks, hookCommand, callback );
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Used to set hooks that effects only data and not UI.
|
|
153
|
-
*/
|
|
154
|
-
public onAfter( hookCommand: string, callback: TCommandCallbackType ) {
|
|
155
|
-
Commands.hookCommand( this.onAfterHooks, hookCommand, callback );
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Used to set hooks that effects only UI.
|
|
160
|
-
*/
|
|
161
|
-
public onAfterUI( command: string, callback: TCommandCallbackType ) {
|
|
162
|
-
Commands.hookCommand( this.onAfterUIHooks, command, callback );
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Used to set hooks that effects only data and not UI.
|
|
167
|
-
*/
|
|
168
|
-
public onAfterOnce( command: string, callback: TCommandCallbackType ) {
|
|
169
|
-
Commands.hookCommand( this.onAfterOnceHooks, command, callback );
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* Used to register a trigger that runs command after `hookCommand` run's.
|
|
174
|
-
*/
|
|
175
|
-
public onAfterAffect( hookCommand: string, affectCommand: string ) {
|
|
176
|
-
Commands.hookCommand( this.onAfterAffectHooks, hookCommand, affectCommand );
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
protected onBeforeRun( command: CommandBase, args: ICommandArgsInterface = {}, options = {} ) {
|
|
180
|
-
if ( this.onBeforeHooks[ command.getName() ] ) {
|
|
181
|
-
const callbacks = this.onBeforeHooks[ command.getName() ];
|
|
182
|
-
callbacks.forEach( ( callback ) => callback( args, options ) );
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
if ( this.onBeforeUIHooks[ command.getName() ] ) {
|
|
186
|
-
const callbacks = this.onBeforeUIHooks[ command.getName() ];
|
|
187
|
-
callbacks.forEach( ( callback ) => callback( args, options ) );
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
protected async runInstance( command: CommandBase, args: ICommandArgsInterface = {}, options = {} ) {
|
|
192
|
-
let result: any = null;
|
|
193
|
-
|
|
194
|
-
this.logger.startsWith( this.runInstance, {
|
|
195
|
-
command: command.getName(),
|
|
196
|
-
options,
|
|
197
|
-
args,
|
|
198
|
-
} );
|
|
199
|
-
|
|
200
|
-
this.onBeforeRun( command, args, options );
|
|
201
|
-
|
|
202
|
-
result = await command.run();
|
|
203
|
-
|
|
204
|
-
await this.onAfterRun( command, args, options, result );
|
|
205
|
-
|
|
206
|
-
return result;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
protected async onAfterRun( command: CommandBase, args: ICommandArgsInterface, options: {}, result: any ) {
|
|
210
|
-
options = Object.assign( {}, options );
|
|
211
|
-
if ( this.onAfterAffectHooks[ command.getName() ] ) {
|
|
212
|
-
this.onAfterAffectHooks[ command.getName() ].forEach( ( command ) => {
|
|
213
|
-
args.result = result;
|
|
214
|
-
|
|
215
|
-
result = this.run( command.toString(), args, options );
|
|
216
|
-
} );
|
|
217
|
-
|
|
218
|
-
result = await result;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
if ( this.onAfterHooks ) {
|
|
222
|
-
args.result = result;
|
|
223
|
-
|
|
224
|
-
await Commands.runCallbacks(
|
|
225
|
-
Object.assign( [], this.onAfterHooks[ command.getName() ] ),
|
|
226
|
-
args,
|
|
227
|
-
options
|
|
228
|
-
);
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
if ( this.onAfterOnceHooks ) {
|
|
232
|
-
await Commands.runCallbacks(
|
|
233
|
-
this.onAfterOnceHooks[ command.getName() ],
|
|
234
|
-
args,
|
|
235
|
-
{
|
|
236
|
-
... options,
|
|
237
|
-
pop: true, // Delete after run.
|
|
238
|
-
}
|
|
239
|
-
);
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
if ( this.onAfterUIHooks ) {
|
|
243
|
-
Commands.runCallbacks(
|
|
244
|
-
Object.assign( [], this.onAfterUIHooks[ command.getName() ] ),
|
|
245
|
-
args,
|
|
246
|
-
options
|
|
247
|
-
);
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
return result;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
protected attachCurrent( command: CommandBase, args:any = {} ) {
|
|
254
|
-
this.current[ command.getName() ] = command;
|
|
255
|
-
this.currentArgs[ command.getName() ] = args;
|
|
256
|
-
|
|
257
|
-
this.trace.push( command.getName() );
|
|
258
|
-
Commands.trace.push( command.getName() );
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
protected detachCurrent( command: CommandBase ) {
|
|
262
|
-
delete this.current[ command.getName() ];
|
|
263
|
-
delete this.currentArgs[ command.getName() ];
|
|
264
|
-
|
|
265
|
-
Commands.trace.pop();
|
|
266
|
-
this.trace.pop();
|
|
267
|
-
}
|
|
268
|
-
}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
|
-
* @description Responsible for manging controllers, each controller is global singleton instance.
|
|
4
|
-
*/
|
|
5
|
-
import { ControllerAlreadyRegistered } from "../errors/controller-already-registered";
|
|
6
|
-
|
|
7
|
-
import { ObjectBase } from "../bases/object-base";
|
|
8
|
-
|
|
9
|
-
import type { ControllerBase } from "../bases/controller-base";
|
|
10
|
-
|
|
11
|
-
export class Controllers extends ObjectBase {
|
|
12
|
-
private controllers: { [ key: string ]: ControllerBase } = {};
|
|
13
|
-
|
|
14
|
-
public constructor() {
|
|
15
|
-
super();
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
public static getName() {
|
|
19
|
-
return "ZenFlux/Core/Managers/Controllers";
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
public get( name: string ) {
|
|
23
|
-
return this.controllers[ name ];
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
public getAll() {
|
|
27
|
-
return this.controllers;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
public register( controller: ControllerBase ) {
|
|
31
|
-
if ( this.controllers[ controller.getName() ] ) {
|
|
32
|
-
throw new ControllerAlreadyRegistered( controller );
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// Register.
|
|
36
|
-
this.controllers[ controller.getName() ] = controller;
|
|
37
|
-
|
|
38
|
-
return controller;
|
|
39
|
-
}
|
|
40
|
-
}
|
package/src/managers/export.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
|
-
*/
|
|
4
|
-
import { Commands } from "./commands";
|
|
5
|
-
import { Controllers } from "./controllers";
|
|
6
|
-
import { Restful } from "./restful";
|
|
7
|
-
import { Internal } from "./internal";
|
|
8
|
-
|
|
9
|
-
import type { IAPIConfig } from "../interfaces";
|
|
10
|
-
|
|
11
|
-
export const afterInitializeCallbacks: ( () => void )[] = [];
|
|
12
|
-
|
|
13
|
-
export function initialize( config: IAPIConfig ) {
|
|
14
|
-
commands = new Commands();
|
|
15
|
-
controllers = new Controllers();
|
|
16
|
-
restful = new Restful( config );
|
|
17
|
-
internal = new Internal();
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function destroy() {
|
|
21
|
-
commands = {} as Commands;
|
|
22
|
-
controllers = {} as Controllers;
|
|
23
|
-
restful = {} as Restful;
|
|
24
|
-
internal = {} as Internal;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export let commands = {} as Commands;
|
|
28
|
-
export let controllers = {} as Controllers;
|
|
29
|
-
export let restful = {} as Restful;
|
|
30
|
-
export let internal = {} as Internal;
|
package/src/managers/index.ts
DELETED
package/src/managers/internal.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
|
-
* @description Responsible for manging internal commands, To serve commands that are not triggered by user.
|
|
4
|
-
*/
|
|
5
|
-
import { Commands } from "./commands";
|
|
6
|
-
|
|
7
|
-
export class Internal extends Commands {
|
|
8
|
-
public static getName() {
|
|
9
|
-
return "ZenFlux/Core/Managers/Internal";
|
|
10
|
-
}
|
|
11
|
-
}
|
package/src/managers/restful.ts
DELETED
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @author Leonid Vinikov <leonidvinikov@gmail.com>
|
|
3
|
-
* @description Responsible for manging restful commands which are wrappers for HTTP requests.
|
|
4
|
-
*/
|
|
5
|
-
import { Commands } from "./commands";
|
|
6
|
-
|
|
7
|
-
import { Http } from "../clients/http";
|
|
8
|
-
|
|
9
|
-
import {
|
|
10
|
-
E_RESPONSE_HANDLER_TYPE,
|
|
11
|
-
E_HTTP_METHOD_TYPE
|
|
12
|
-
} from "../interfaces";
|
|
13
|
-
|
|
14
|
-
import type { CommandRestful } from "../command-bases";
|
|
15
|
-
|
|
16
|
-
import type {
|
|
17
|
-
IAPIConfig,
|
|
18
|
-
ICommandArgsInterface,
|
|
19
|
-
ICommandOptionsInterface,
|
|
20
|
-
|
|
21
|
-
TErrorHandlerCallbackType,
|
|
22
|
-
TPossibleHandlersType,
|
|
23
|
-
TResponseFilterCallbackType,
|
|
24
|
-
TResponseHandlerCallbackType
|
|
25
|
-
} from "../interfaces";
|
|
26
|
-
|
|
27
|
-
export class Restful extends Commands {
|
|
28
|
-
private static client: Http;
|
|
29
|
-
|
|
30
|
-
public currentHttpMethod: E_HTTP_METHOD_TYPE;
|
|
31
|
-
|
|
32
|
-
public static getName() {
|
|
33
|
-
return "ZenFlux/Core/Managers/Restful";
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
public constructor( Config: IAPIConfig ) {
|
|
37
|
-
super();
|
|
38
|
-
|
|
39
|
-
Restful.client = new Http( Config.baseURL, Config.requestInit );
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
public getClient() {
|
|
43
|
-
return Restful.client;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
public get( command: string, args: ICommandArgsInterface = {}, options: ICommandOptionsInterface = {} ) {
|
|
47
|
-
this.currentHttpMethod = E_HTTP_METHOD_TYPE.GET;
|
|
48
|
-
|
|
49
|
-
return super.run( command, args, options );
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
public update( command: string, args: ICommandArgsInterface = {}, options: {} = {} ) {
|
|
53
|
-
this.currentHttpMethod = E_HTTP_METHOD_TYPE.PATCH;
|
|
54
|
-
|
|
55
|
-
return super.run( command, args, options );
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
public delete( command: string, args: ICommandArgsInterface = {}, options: {} = {} ) {
|
|
59
|
-
this.currentHttpMethod = E_HTTP_METHOD_TYPE.DELETE;
|
|
60
|
-
|
|
61
|
-
return super.run( command, args, options );
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
public create( command: string, args: ICommandArgsInterface = {}, options: {} = {} ) {
|
|
65
|
-
this.currentHttpMethod = E_HTTP_METHOD_TYPE.POST;
|
|
66
|
-
|
|
67
|
-
return super.run( command, args, options );
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
protected async runInstance( command: CommandRestful, args: ICommandArgsInterface = {}, options: {} = {} ) {
|
|
71
|
-
if ( ! this.currentHttpMethod ) {
|
|
72
|
-
throw new Error( "Cannot run directly use one of the http methods: \"get\", \"update\", \"delete, \"create" );
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// New args.
|
|
76
|
-
const newArgs = {
|
|
77
|
-
type: this.currentHttpMethod,
|
|
78
|
-
args: {
|
|
79
|
-
query: {},
|
|
80
|
-
data: {},
|
|
81
|
-
},
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
if ( E_HTTP_METHOD_TYPE.GET === this.currentHttpMethod ) {
|
|
85
|
-
newArgs.args.query = args;
|
|
86
|
-
} else {
|
|
87
|
-
newArgs.args.data = args;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
args.result = await super.runInstance( command, newArgs, options );
|
|
91
|
-
|
|
92
|
-
// Clear method type.
|
|
93
|
-
this.currentHttpMethod = E_HTTP_METHOD_TYPE.__EMPTY__;
|
|
94
|
-
|
|
95
|
-
return args.result;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Handlers on return true will swallow the request.
|
|
100
|
-
*/
|
|
101
|
-
public setHandler( type: E_RESPONSE_HANDLER_TYPE, callback: TPossibleHandlersType ) {
|
|
102
|
-
switch ( type ) {
|
|
103
|
-
case E_RESPONSE_HANDLER_TYPE.ERROR_HANDLER:
|
|
104
|
-
Restful.client.setErrorHandler( callback as TErrorHandlerCallbackType );
|
|
105
|
-
break;
|
|
106
|
-
case E_RESPONSE_HANDLER_TYPE.RESPONSE_FILTER:
|
|
107
|
-
Restful.client.setResponseFilter( callback as TResponseFilterCallbackType );
|
|
108
|
-
break;
|
|
109
|
-
case E_RESPONSE_HANDLER_TYPE.RESPONSE_HANDLER:
|
|
110
|
-
Restful.client.setResponseHandler( callback as TResponseHandlerCallbackType );
|
|
111
|
-
break;
|
|
112
|
-
|
|
113
|
-
default:
|
|
114
|
-
throw new Error( `Unknown handler type: '${ type }'` );
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
}
|