@zenflux/core 0.0.0-abc.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.
Files changed (73) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +2 -0
  3. package/dist/cjs/zenflux-core.js +1102 -0
  4. package/dist/es/rollup.config.d.ts +23 -0
  5. package/dist/es/src/clients/http.d.ts +20 -0
  6. package/dist/es/src/command-bases/command-base.d.ts +22 -0
  7. package/dist/es/src/command-bases/command-data.d.ts +17 -0
  8. package/dist/es/src/command-bases/command-internal.d.ts +9 -0
  9. package/dist/es/src/command-bases/command-public.d.ts +9 -0
  10. package/dist/es/src/command-bases/index.d.ts +7 -0
  11. package/dist/es/src/core/controller.d.ts +35 -0
  12. package/dist/es/src/core/index.d.ts +2 -0
  13. package/dist/es/src/core/object-base.d.ts +14 -0
  14. package/dist/es/src/enums/http.d.ts +12 -0
  15. package/dist/es/src/errors/command-already-registered.d.ts +8 -0
  16. package/dist/es/src/errors/command-not-found.d.ts +7 -0
  17. package/dist/es/src/errors/controller-already-registered.d.ts +8 -0
  18. package/dist/es/src/errors/force-method.d.ts +11 -0
  19. package/dist/es/src/errors/index.d.ts +4 -0
  20. package/dist/es/src/exports.d.ts +10 -0
  21. package/dist/es/src/index.d.ts +5 -0
  22. package/dist/es/src/initializer.d.ts +19 -0
  23. package/dist/es/src/interfaces/commands.d.ts +13 -0
  24. package/dist/es/src/interfaces/config.d.ts +10 -0
  25. package/dist/es/src/interfaces/object-base.d.ts +10 -0
  26. package/dist/es/src/manager-bases/commands.d.ts +74 -0
  27. package/dist/es/src/manager-bases/controllers.d.ts +17 -0
  28. package/dist/es/src/manager-bases/data.d.ts +23 -0
  29. package/dist/es/src/manager-bases/index.d.ts +7 -0
  30. package/dist/es/src/manager-bases/internal.d.ts +9 -0
  31. package/dist/es/src/managers/index.d.ts +8 -0
  32. package/dist/es/src/modules/index.d.ts +1 -0
  33. package/dist/es/src/modules/logger.d.ts +117 -0
  34. package/dist/es/src/utils/index.d.ts +2 -0
  35. package/dist/es/src/utils/object.d.ts +6 -0
  36. package/dist/es/src/utils/rgb.d.ts +6 -0
  37. package/dist/es/zenflux-core.js +1091 -0
  38. package/dist/es/zenflux-core.mjs +12 -0
  39. package/dist/umd/zenflux-core.js +12 -0
  40. package/package.json +55 -0
  41. package/src/clients/http.ts +81 -0
  42. package/src/command-bases/command-base.ts +67 -0
  43. package/src/command-bases/command-data.ts +54 -0
  44. package/src/command-bases/command-internal.ts +13 -0
  45. package/src/command-bases/command-public.ts +13 -0
  46. package/src/command-bases/index.ts +7 -0
  47. package/src/core/controller.ts +61 -0
  48. package/src/core/index.ts +2 -0
  49. package/src/core/object-base.ts +46 -0
  50. package/src/enums/http.ts +12 -0
  51. package/src/errors/command-already-registered.ts +12 -0
  52. package/src/errors/command-not-found.ts +10 -0
  53. package/src/errors/controller-already-registered.ts +12 -0
  54. package/src/errors/force-method.ts +22 -0
  55. package/src/errors/index.ts +4 -0
  56. package/src/exports.ts +11 -0
  57. package/src/index.ts +7 -0
  58. package/src/initializer.ts +63 -0
  59. package/src/interfaces/commands.ts +17 -0
  60. package/src/interfaces/config.ts +11 -0
  61. package/src/interfaces/object-base.ts +12 -0
  62. package/src/manager-bases/commands.ts +272 -0
  63. package/src/manager-bases/controllers.ts +36 -0
  64. package/src/manager-bases/data.ts +89 -0
  65. package/src/manager-bases/index.ts +7 -0
  66. package/src/manager-bases/internal.ts +13 -0
  67. package/src/managers/index.ts +9 -0
  68. package/src/modules/index.ts +1 -0
  69. package/src/modules/logger.ts +413 -0
  70. package/src/utils/index.ts +2 -0
  71. package/src/utils/object.ts +18 -0
  72. package/src/utils/rgb.ts +33 -0
  73. package/types/index.d.ts +2 -0
@@ -0,0 +1,17 @@
1
+ export type CommandCallbackType = ( 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<CommandCallbackType>;
17
+ }
@@ -0,0 +1,11 @@
1
+ export interface IAPIConfig {
2
+ /**
3
+ * @description: API version, from `package.json`
4
+ */
5
+ version: string;
6
+
7
+ /**
8
+ * @description: API base url for http requests.
9
+ */
10
+ baseURL?: string;
11
+ }
@@ -0,0 +1,12 @@
1
+ export interface IObjectBaseInterface {
2
+
3
+ /**
4
+ * Virtual id is used for identification of object or for debugging proposes.
5
+ */
6
+ virtualId: number;
7
+
8
+ /**
9
+ *
10
+ */
11
+ getName(): string;
12
+ }
@@ -0,0 +1,272 @@
1
+ /**
2
+ * @author: Leonid Vinikov <leonidvinikov@gmail.com>
3
+ * @description: Responsible for manging/running/hooking commands.
4
+ */
5
+ import { Logger } from "../modules/";
6
+ import { CommandBase } from "../command-bases";
7
+ import { Controller, ObjectBase } from "../core";
8
+ import { CommandAlreadyRegistered, CommandNotFound } from "../errors/";
9
+
10
+ import {
11
+ ICommandArgsInterface,
12
+ CommandCallbackType,
13
+ IOnHookAffectInterface,
14
+ IOnHookInterface, ICommandOptionsInterface,
15
+ } from "../interfaces/commands";
16
+
17
+ export class Commands extends ObjectBase {
18
+ public static readonly trace:string[] = [];
19
+
20
+ current: { [ key: string ]: CommandBase } = {};
21
+ currentArgs: { [ key: string ]: ICommandArgsInterface } = {};
22
+
23
+ trace:string[] = [];
24
+
25
+ commands: { [ key: string ]: typeof CommandBase } = {};
26
+
27
+ onBeforeHooks: IOnHookInterface = {};
28
+ onBeforeUIHooks: IOnHookInterface = {};
29
+
30
+ onAfterHooks: IOnHookInterface = {};
31
+ onAfterOnceHooks: IOnHookInterface = {};
32
+ onAfterUIHooks: IOnHookInterface = {};
33
+ onAfterAffectHooks: IOnHookAffectInterface = {};
34
+
35
+ private readonly logger: Logger;
36
+
37
+ static getName() {
38
+ return 'Core/Managers/Commands';
39
+ }
40
+
41
+ private static async runCallbacks( callbacks: Array<Function>, args: ICommandArgsInterface = {}, options: ICommandOptionsInterface = {} ) {
42
+ const callbacksLength = callbacks?.length || 0;
43
+
44
+ for ( let i = 0; i < callbacksLength; ++i ) {
45
+ const callback = options.pop === true ? callbacks.pop() : callbacks[ i ];
46
+
47
+ if ( ! callback ) {
48
+ throw new Error( "Callback is not defined." );
49
+ }
50
+
51
+ await callback( args, options );
52
+ }
53
+ }
54
+
55
+ private static hookCommand( stack: any, hookCommand: string, context: CommandCallbackType | string ) {
56
+ if ( ! stack[ hookCommand ] ) {
57
+ stack[ hookCommand ] = [];
58
+ }
59
+
60
+ stack[ hookCommand ].push( context );
61
+ }
62
+
63
+ constructor() {
64
+ super();
65
+
66
+ this.logger = new Logger( this.getName(), true );
67
+
68
+ this.logger.startEmpty();
69
+ }
70
+
71
+ public async run( command: string | CommandBase, args: ICommandArgsInterface = {}, options: any = {} ) {
72
+ if ( typeof command === "string" ) {
73
+ command = this.getCommandInstance( command, args, options );
74
+ }
75
+
76
+ this.attachCurrent( command, args );
77
+
78
+ const result = await this.runInstance( command, args, options );
79
+
80
+ this.detachCurrent( command );
81
+
82
+ return result;
83
+ }
84
+
85
+ public register( commands: { [ key: string ]: typeof CommandBase }, controller: Controller ) {// eslint-disable-line @typescript-eslint/no-unused-vars
86
+ const result: { [ key: string ]: typeof CommandBase } = {};
87
+
88
+ Object.values( commands ).forEach( ( command ) => {
89
+ const commandName = command.getName();
90
+
91
+ if ( this.commands[ commandName ] ) {
92
+ throw new CommandAlreadyRegistered( command );
93
+ }
94
+
95
+ this.commands[ commandName ] = command;
96
+
97
+ result[ commandName ] = command;
98
+ } );
99
+
100
+ return result;
101
+ }
102
+
103
+ public getAll() {
104
+ return this.commands;
105
+ }
106
+
107
+ public getByName( name: string ) {
108
+ return this.commands[ name ];
109
+ }
110
+
111
+ public getLogger() {
112
+ return this.logger;
113
+ }
114
+
115
+ public getCommandInstance( name: string, args: ICommandArgsInterface = {}, options = {} ): CommandBase {
116
+ const CommandClass = this.commands[ name ];
117
+
118
+ if ( ! CommandClass ) {
119
+ throw new CommandNotFound( name );
120
+ }
121
+
122
+ // @ts-ignore
123
+ return new CommandClass( args, options );
124
+ }
125
+
126
+ /**
127
+ * Function onBefore() : Used to set hooks that effects only data and not UI.
128
+ */
129
+ public onBefore( hookCommand: string, callback: CommandCallbackType ) {
130
+ Commands.hookCommand( this.onBeforeHooks, hookCommand, callback );
131
+ }
132
+
133
+ /**
134
+ * Function onBeforeUI() : Used to set hooks that effects only UI.
135
+ */
136
+ public onBeforeUI( hookCommand: string, callback: CommandCallbackType ) {
137
+ Commands.hookCommand( this.onBeforeUIHooks, hookCommand, callback );
138
+ }
139
+
140
+ /**
141
+ * Function onAfter() : Used to set hooks that effects only data and not UI.
142
+ */
143
+ public onAfter( hookCommand: string, callback: CommandCallbackType ) {
144
+ Commands.hookCommand( this.onAfterHooks, hookCommand, callback );
145
+ }
146
+
147
+ /**
148
+ * Function onAfterUI() : Used to set hooks that effects only UI.
149
+ */
150
+ public onAfterUI( command: string, callback: CommandCallbackType ) {
151
+ Commands.hookCommand( this.onAfterUIHooks, command, callback );
152
+ }
153
+
154
+ /**
155
+ * Function onAfterOnce() : Used to set hooks that effects only data and not UI.
156
+ */
157
+ public onAfterOnce( command: string, callback: CommandCallbackType ) {
158
+ Commands.hookCommand( this.onAfterOnceHooks, command, callback );
159
+ }
160
+
161
+ /**
162
+ * Function onAfterAffect() : Used to register a trigger that runs command after `hookCommand` run's.
163
+ */
164
+ public onAfterAffect( hookCommand: string, affectCommand: string ) {
165
+ Commands.hookCommand( this.onAfterAffectHooks, hookCommand, affectCommand );
166
+ }
167
+
168
+ protected onBeforeRun( command: CommandBase, args: ICommandArgsInterface = {}, options = {} ) {
169
+ if ( this.onBeforeHooks[ command.getName() ] ) {
170
+ const callbacks = this.onBeforeHooks[ command.getName() ];
171
+ callbacks.forEach( ( callback ) => callback( args, options ) );
172
+ }
173
+
174
+ if ( this.onBeforeUIHooks[ command.getName() ] ) {
175
+ const callbacks = this.onBeforeUIHooks[ command.getName() ];
176
+ callbacks.forEach( ( callback ) => callback( args, options ) );
177
+ }
178
+ }
179
+
180
+ protected async runInstance( command: CommandBase, args: ICommandArgsInterface = {}, options = {} ) {
181
+ let result: any = null;
182
+
183
+ // TODO: Maybe a logger method to handle such cases.
184
+ if ( Object.keys( args ).length ) {
185
+ this.logger.startWith( {
186
+ command: command.getName(),
187
+ options,
188
+ CommandArgs: "->",
189
+ } );
190
+
191
+ this.logger.debug( "CommandArgs:" );
192
+ this.logger.object( args );
193
+ } else {
194
+ this.logger.startWith( {
195
+ command: command.getName(),
196
+ options,
197
+ args,
198
+ } );
199
+
200
+ }
201
+
202
+ this.onBeforeRun( command, args, options );
203
+
204
+ result = await command.run();
205
+
206
+ await this.onAfterRun( command, args, options, result );
207
+
208
+ return result;
209
+ }
210
+
211
+ protected async onAfterRun( command: CommandBase, args: ICommandArgsInterface, options: {}, result: any ) {
212
+ options = Object.assign( {}, options );
213
+ if ( this.onAfterAffectHooks[ command.getName() ] ) {
214
+ this.onAfterAffectHooks[ command.getName() ].forEach( ( command ) => {
215
+ args.result = result;
216
+
217
+ result = this.run( command.toString(), args, options );
218
+ } );
219
+
220
+ result = await result;
221
+ }
222
+
223
+ if ( this.onAfterHooks ) {
224
+ args.result = result;
225
+
226
+ await Commands.runCallbacks(
227
+ Object.assign( [], this.onAfterHooks[ command.getName() ] ),
228
+ args,
229
+ options
230
+ );
231
+ }
232
+
233
+ if ( this.onAfterOnceHooks ) {
234
+ await Commands.runCallbacks(
235
+ this.onAfterOnceHooks[ command.getName() ],
236
+ args,
237
+ {
238
+ ... options,
239
+ pop: true, // Delete after run.
240
+ }
241
+ );
242
+ }
243
+
244
+ if ( this.onAfterUIHooks ) {
245
+ Commands.runCallbacks(
246
+ Object.assign( [], this.onAfterUIHooks[ command.getName() ] ),
247
+ args,
248
+ options
249
+ )
250
+ }
251
+
252
+ return result;
253
+ }
254
+
255
+ protected attachCurrent( command: CommandBase, args:any = {} ) {
256
+ this.current[ command.getName() ] = command;
257
+ this.currentArgs[ command.getName() ] = args;
258
+
259
+ this.trace.push( command.getName() );
260
+ Commands.trace.push( command.getName() );
261
+ }
262
+
263
+ protected detachCurrent( command: CommandBase ) {
264
+ delete this.current[ command.getName() ];
265
+ delete this.currentArgs[ command.getName() ];
266
+
267
+ Commands.trace.pop();
268
+ this.trace.pop();
269
+ }
270
+ }
271
+
272
+ export default Commands;
@@ -0,0 +1,36 @@
1
+ /**
2
+ * @author: Leonid Vinikov <leonidvinikov@gmail.com>
3
+ * @description: Responsible for manging controllers, each controller is global singleton instance.
4
+ */
5
+ import { Controller, ObjectBase } from "../core";
6
+
7
+ import { ControllerAlreadyRegistered } from "../errors/controller-already-registered";
8
+
9
+ export class Controllers extends ObjectBase {
10
+ controllers: { [ key: string ]: Controller } = {};
11
+
12
+ static getName() {
13
+ return 'Core/Managers/Controllers';
14
+ }
15
+
16
+ get( name: string ) {
17
+ return this.controllers[ name ];
18
+ }
19
+
20
+ getAll() {
21
+ return this.controllers;
22
+ }
23
+
24
+ register( controller: Controller ) {
25
+ if ( this.controllers[ controller.getName() ] ) {
26
+ throw new ControllerAlreadyRegistered( controller );
27
+ }
28
+
29
+ // Register.
30
+ this.controllers[ controller.getName() ] = controller;
31
+
32
+ return controller;
33
+ }
34
+ }
35
+
36
+ export default Controllers;
@@ -0,0 +1,89 @@
1
+ /**
2
+ * @author: Leonid Vinikov <leonidvinikov@gmail.com>
3
+ * @description: Responsible for manging data commands which are wrappers for HTTP requests.
4
+ */
5
+ import Commands from "./commands";
6
+
7
+ import { CommandData } from '../command-bases/';
8
+
9
+ import Http from '../clients/http';
10
+
11
+ import { HTTPMethodEnum } from "../enums/http";
12
+
13
+ import { IAPIConfig } from "../interfaces/config";
14
+ import { ICommandArgsInterface, ICommandOptionsInterface } from '../interfaces/commands';
15
+
16
+ export class Data extends Commands {
17
+ private static client: Http;
18
+
19
+ public currentHttpMethod: HTTPMethodEnum;
20
+
21
+ static getName() {
22
+ return 'Core/Managers/Data';
23
+ }
24
+
25
+ constructor( Config: IAPIConfig) {
26
+ super();
27
+
28
+ // @ts-ignore
29
+ Data.client = new Http( Config.baseURL );
30
+ }
31
+
32
+ public getClient() {
33
+ return Data.client;
34
+ }
35
+
36
+ public get( command: string, args: ICommandArgsInterface = {}, options: ICommandOptionsInterface = {} ) {
37
+ this.currentHttpMethod = HTTPMethodEnum.GET;
38
+
39
+ return super.run( command, args, options );
40
+ }
41
+
42
+ public update( command: string, args: ICommandArgsInterface = {}, options: {} = {} ) {
43
+ this.currentHttpMethod = HTTPMethodEnum.PATCH;
44
+
45
+ return super.run( command, args, options );
46
+ }
47
+
48
+ public delete( command: string, args: ICommandArgsInterface = {}, options: {} = {} ) {
49
+ this.currentHttpMethod = HTTPMethodEnum.DELETE;
50
+
51
+ return super.run( command, args, options );
52
+ }
53
+
54
+ public create( command: string, args: ICommandArgsInterface = {}, options: {} = {} ) {
55
+ this.currentHttpMethod = HTTPMethodEnum.POST;
56
+
57
+ return super.run( command, args, options );
58
+ }
59
+
60
+ protected async runInstance( command: CommandData, args: ICommandArgsInterface = {}, options: {} = {} ) {
61
+ if ( ! this.currentHttpMethod ) {
62
+ throw new Error( 'Cannot run directly use one of the http methods: "get", "update", "delete, "create' );
63
+ }
64
+
65
+ // New args.
66
+ const newArgs = {
67
+ type: this.currentHttpMethod,
68
+ args: {
69
+ query: {},
70
+ data: {},
71
+ },
72
+ };
73
+
74
+ if ( HTTPMethodEnum.GET === this.currentHttpMethod ) {
75
+ newArgs.args.query = args;
76
+ } else {
77
+ newArgs.args.data = args;
78
+ }
79
+
80
+ args.result = await super.runInstance( command, newArgs, options )
81
+
82
+ // Clear method type.
83
+ this.currentHttpMethod = HTTPMethodEnum.__EMPTY__;
84
+
85
+ return args.result;
86
+ }
87
+ }
88
+
89
+ export default Data;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * @author: Leonid Vinikov <leonidvinikov@gmail.com>
3
+ */
4
+ export { Commands } from './commands';
5
+ export { Controllers } from './controllers';
6
+ export { Data } from './data';
7
+ export { Internal } from './internal';
@@ -0,0 +1,13 @@
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
+ static getName() {
9
+ return 'Core/Managers/Internal';
10
+ }
11
+ }
12
+
13
+ export default Internal;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * @author: Leonid Vinikov <leonidvinikov@gmail.com>
3
+ */
4
+ import * as managersBases from "../manager-bases";
5
+
6
+ export let commands = {} as managersBases.Commands;
7
+ export let controllers = {} as managersBases.Controllers;
8
+ export let data = {} as managersBases.Data;
9
+ export let internal = {} as managersBases.Internal;
@@ -0,0 +1 @@
1
+ export { Logger } from "./logger";