@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.
- package/LICENSE +21 -0
- package/README.md +2 -0
- package/dist/cjs/zenflux-core.js +1102 -0
- package/dist/es/rollup.config.d.ts +23 -0
- package/dist/es/src/clients/http.d.ts +20 -0
- package/dist/es/src/command-bases/command-base.d.ts +22 -0
- package/dist/es/src/command-bases/command-data.d.ts +17 -0
- package/dist/es/src/command-bases/command-internal.d.ts +9 -0
- package/dist/es/src/command-bases/command-public.d.ts +9 -0
- package/dist/es/src/command-bases/index.d.ts +7 -0
- package/dist/es/src/core/controller.d.ts +35 -0
- package/dist/es/src/core/index.d.ts +2 -0
- package/dist/es/src/core/object-base.d.ts +14 -0
- package/dist/es/src/enums/http.d.ts +12 -0
- package/dist/es/src/errors/command-already-registered.d.ts +8 -0
- package/dist/es/src/errors/command-not-found.d.ts +7 -0
- package/dist/es/src/errors/controller-already-registered.d.ts +8 -0
- package/dist/es/src/errors/force-method.d.ts +11 -0
- package/dist/es/src/errors/index.d.ts +4 -0
- package/dist/es/src/exports.d.ts +10 -0
- package/dist/es/src/index.d.ts +5 -0
- package/dist/es/src/initializer.d.ts +19 -0
- package/dist/es/src/interfaces/commands.d.ts +13 -0
- package/dist/es/src/interfaces/config.d.ts +10 -0
- package/dist/es/src/interfaces/object-base.d.ts +10 -0
- package/dist/es/src/manager-bases/commands.d.ts +74 -0
- package/dist/es/src/manager-bases/controllers.d.ts +17 -0
- package/dist/es/src/manager-bases/data.d.ts +23 -0
- package/dist/es/src/manager-bases/index.d.ts +7 -0
- package/dist/es/src/manager-bases/internal.d.ts +9 -0
- package/dist/es/src/managers/index.d.ts +8 -0
- package/dist/es/src/modules/index.d.ts +1 -0
- package/dist/es/src/modules/logger.d.ts +117 -0
- package/dist/es/src/utils/index.d.ts +2 -0
- package/dist/es/src/utils/object.d.ts +6 -0
- package/dist/es/src/utils/rgb.d.ts +6 -0
- package/dist/es/zenflux-core.js +1091 -0
- package/dist/es/zenflux-core.mjs +12 -0
- package/dist/umd/zenflux-core.js +12 -0
- package/package.json +55 -0
- package/src/clients/http.ts +81 -0
- package/src/command-bases/command-base.ts +67 -0
- package/src/command-bases/command-data.ts +54 -0
- package/src/command-bases/command-internal.ts +13 -0
- package/src/command-bases/command-public.ts +13 -0
- package/src/command-bases/index.ts +7 -0
- package/src/core/controller.ts +61 -0
- package/src/core/index.ts +2 -0
- package/src/core/object-base.ts +46 -0
- package/src/enums/http.ts +12 -0
- package/src/errors/command-already-registered.ts +12 -0
- package/src/errors/command-not-found.ts +10 -0
- package/src/errors/controller-already-registered.ts +12 -0
- package/src/errors/force-method.ts +22 -0
- package/src/errors/index.ts +4 -0
- package/src/exports.ts +11 -0
- package/src/index.ts +7 -0
- package/src/initializer.ts +63 -0
- package/src/interfaces/commands.ts +17 -0
- package/src/interfaces/config.ts +11 -0
- package/src/interfaces/object-base.ts +12 -0
- package/src/manager-bases/commands.ts +272 -0
- package/src/manager-bases/controllers.ts +36 -0
- package/src/manager-bases/data.ts +89 -0
- package/src/manager-bases/index.ts +7 -0
- package/src/manager-bases/internal.ts +13 -0
- package/src/managers/index.ts +9 -0
- package/src/modules/index.ts +1 -0
- package/src/modules/logger.ts +413 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/object.ts +18 -0
- package/src/utils/rgb.ts +33 -0
- package/types/index.d.ts +2 -0
|
@@ -0,0 +1,1091 @@
|
|
|
1
|
+
class ForceMethodBase extends Error {
|
|
2
|
+
constructor(className, methodName) {
|
|
3
|
+
super(`ForeMethod implementation: at '${className}' method: '${methodName}'`);
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
class ForceMethod extends Error {
|
|
7
|
+
constructor(context, methodName) {
|
|
8
|
+
super(`ForeMethod implementation: at '${context.getName()}' method: '${methodName}'`);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @author: Leonid Vinikov <leonidvinikov@gmail.com>
|
|
14
|
+
* @description: is base class/object for all classes, responsible for: 'getName', implementation.
|
|
15
|
+
*/
|
|
16
|
+
let IdCounter = 0;
|
|
17
|
+
/**
|
|
18
|
+
* Base class 4 *
|
|
19
|
+
*/
|
|
20
|
+
class ObjectBase {
|
|
21
|
+
virtualId = 0;
|
|
22
|
+
static getName() {
|
|
23
|
+
throw new ForceMethodBase("ObjectBase", "getName");
|
|
24
|
+
}
|
|
25
|
+
constructor() {
|
|
26
|
+
this.virtualId = IdCounter;
|
|
27
|
+
APIIncreaseIdCounter();
|
|
28
|
+
}
|
|
29
|
+
getName() {
|
|
30
|
+
return this.constructor.getName();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function APIIncreaseIdCounter() {
|
|
34
|
+
return IdCounter++;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Function getCircularReplacer() : Reduce circular references.
|
|
39
|
+
*
|
|
40
|
+
* @copyright https://stackoverflow.com/a/53731154
|
|
41
|
+
*/
|
|
42
|
+
const getCircularReplacer = () => {
|
|
43
|
+
const seen = new WeakSet();
|
|
44
|
+
return (key, value) => {
|
|
45
|
+
if (typeof value === "object" && value !== null) {
|
|
46
|
+
if (seen.has(value)) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
seen.add(value);
|
|
50
|
+
}
|
|
51
|
+
return value;
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Function hexColorDelta() : Return color difference in ratio decimal point.
|
|
57
|
+
*
|
|
58
|
+
* @copyright http://jsfiddle.net/96sME/
|
|
59
|
+
*/
|
|
60
|
+
const hexColorDelta = function (hex1, hex2) {
|
|
61
|
+
hex1 = hex1.replace("#", "");
|
|
62
|
+
hex2 = hex2.replace("#", "");
|
|
63
|
+
// Get red/green/blue int values of hex1
|
|
64
|
+
const r1 = parseInt(hex1.substring(0, 2), 16);
|
|
65
|
+
const g1 = parseInt(hex1.substring(2, 4), 16);
|
|
66
|
+
const b1 = parseInt(hex1.substring(4, 6), 16);
|
|
67
|
+
// Get red/green/blue int values of hex2
|
|
68
|
+
const r2 = parseInt(hex2.substring(0, 2), 16);
|
|
69
|
+
const g2 = parseInt(hex2.substring(2, 4), 16);
|
|
70
|
+
const b2 = parseInt(hex2.substring(4, 6), 16);
|
|
71
|
+
// Calculate differences between reds, greens and blues
|
|
72
|
+
let r = 255 - Math.abs(r1 - r2);
|
|
73
|
+
let g = 255 - Math.abs(g1 - g2);
|
|
74
|
+
let b = 255 - Math.abs(b1 - b2);
|
|
75
|
+
// Limit differences between 0 and 1
|
|
76
|
+
r /= 255;
|
|
77
|
+
g /= 255;
|
|
78
|
+
b /= 255;
|
|
79
|
+
// 0 means opposite colors, 1 means same colors
|
|
80
|
+
return (r + g + b) / 3;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
var index$6 = /*#__PURE__*/Object.freeze({
|
|
84
|
+
__proto__: null,
|
|
85
|
+
getCircularReplacer: getCircularReplacer,
|
|
86
|
+
hexColorDelta: hexColorDelta
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @author: Leonid Vinikov <leonidvinikov@gmail.com>
|
|
91
|
+
* @description: Module for logging instances.
|
|
92
|
+
* @TODO:
|
|
93
|
+
* - Remove `@ts-ignore`, requires a refactor.
|
|
94
|
+
* - Add night/white mode.
|
|
95
|
+
*/
|
|
96
|
+
// TODO: Should by dynamic/configure-able.
|
|
97
|
+
const MAX_WRAPPING_RECURSIVE_DEPTH = 4;
|
|
98
|
+
class Logger {
|
|
99
|
+
static wrappers = {};
|
|
100
|
+
static wrapperDepth = 0;
|
|
101
|
+
static sharedData = {};
|
|
102
|
+
static colorsInUse = [];
|
|
103
|
+
defaultStyle;
|
|
104
|
+
color;
|
|
105
|
+
outputHandler;
|
|
106
|
+
args = {};
|
|
107
|
+
name;
|
|
108
|
+
state = false;
|
|
109
|
+
static getName() {
|
|
110
|
+
return 'Core/Modules/Logger';
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Function reset() : Reset logger globals.
|
|
114
|
+
*/
|
|
115
|
+
static reset() {
|
|
116
|
+
Logger.wrappers = {};
|
|
117
|
+
Logger.wrapperDepth = 0;
|
|
118
|
+
Logger.colorsInUse = [];
|
|
119
|
+
Logger.sharedData = {};
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Function createCustomWrapper() : Creates custom wrapper for log entities.
|
|
123
|
+
*/
|
|
124
|
+
static createCustomWrapper(classType, callback) {
|
|
125
|
+
// @ts-ignore
|
|
126
|
+
if (!Logger.wrappers[classType]) {
|
|
127
|
+
// @ts-ignore
|
|
128
|
+
Logger.wrappers[classType.name] = [];
|
|
129
|
+
}
|
|
130
|
+
// @ts-ignore
|
|
131
|
+
Logger.wrappers[classType.name].push({
|
|
132
|
+
classType,
|
|
133
|
+
callback
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Function getCallerName() : Return caller name.
|
|
138
|
+
*/
|
|
139
|
+
static getCallerName() {
|
|
140
|
+
const error = new Error(),
|
|
141
|
+
caller = error.stack ? error.stack.split("\n")[3].trim() : '_UNKNOWN_CALLER_NAME_';
|
|
142
|
+
if (caller.startsWith("at new")) {
|
|
143
|
+
return "constructor";
|
|
144
|
+
}
|
|
145
|
+
return caller.split(".")[1].split(" ")[0];
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Function functionView() : Return function preview.
|
|
149
|
+
*/
|
|
150
|
+
static getFunctionView(fn) {
|
|
151
|
+
let fReturn = "anonymous function()";
|
|
152
|
+
// TODO: Check if works.
|
|
153
|
+
if (typeof fn !== "string" && fn.name.length !== 0) {
|
|
154
|
+
fReturn = fn.name.split(" ")[1] + "()";
|
|
155
|
+
}
|
|
156
|
+
return fReturn;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Function constructor() : Create logger class.
|
|
160
|
+
*/
|
|
161
|
+
constructor(owner, isActive = false, args = {}) {
|
|
162
|
+
if ('off' === globalThis.process?.env['flow_modules_logger']) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
this.state = isActive;
|
|
166
|
+
this.args = args;
|
|
167
|
+
this.name = "";
|
|
168
|
+
if (typeof owner == "string") {
|
|
169
|
+
this.name = owner;
|
|
170
|
+
} else {
|
|
171
|
+
this.name = owner.constructor.name;
|
|
172
|
+
}
|
|
173
|
+
if (isActive) {
|
|
174
|
+
this.initialize();
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Function initialize() : Initialize logger class.
|
|
179
|
+
*/
|
|
180
|
+
initialize() {
|
|
181
|
+
// @ts-ignore
|
|
182
|
+
if (this.args.sameColor) {
|
|
183
|
+
if (!Logger.sharedData[this.name]) {
|
|
184
|
+
Logger.sharedData[this.name] = this.getRandomColor();
|
|
185
|
+
}
|
|
186
|
+
this.color = Logger.sharedData[this.name];
|
|
187
|
+
} else {
|
|
188
|
+
this.color = this.getRandomColor();
|
|
189
|
+
Logger.colorsInUse.push(this.color);
|
|
190
|
+
}
|
|
191
|
+
this.outputHandler = console.log;
|
|
192
|
+
this.defaultStyle = ["color: grey;font-size:7px", "display: block", `color: ${this.color}`, "color: grey", "font-weight: bold", "color: #607D8B", "font-size: 16px;color: red;font-weight:800"];
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Function setOutputHandler() : Set custom output handler.
|
|
196
|
+
*/
|
|
197
|
+
setOutputHandler(outputHandler) {
|
|
198
|
+
this.outputHandler = outputHandler;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Function useWrapper() : Search for customized log view, each instance,
|
|
202
|
+
* that typeof the instance was registered wrapper will have unique output
|
|
203
|
+
* according to the registered callback.
|
|
204
|
+
*/
|
|
205
|
+
useWrapper(obj, shouldHandleChildren = true) {
|
|
206
|
+
if (!obj || 'object' !== typeof obj || Array.isArray(obj)) {
|
|
207
|
+
return obj;
|
|
208
|
+
}
|
|
209
|
+
Logger.wrapperDepth++;
|
|
210
|
+
// Result will lose classType (instanceOf will not work), now based on obj.
|
|
211
|
+
let result = {
|
|
212
|
+
...obj
|
|
213
|
+
};
|
|
214
|
+
// @ts-ignore
|
|
215
|
+
Object.values(Logger.wrappers).forEach(([wrapper]) => {
|
|
216
|
+
if (obj instanceof wrapper.classType) {
|
|
217
|
+
result = wrapper.callback(obj) || result;
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
if (Logger.wrapperDepth >= MAX_WRAPPING_RECURSIVE_DEPTH) {
|
|
221
|
+
Logger.wrapperDepth--;
|
|
222
|
+
return result;
|
|
223
|
+
}
|
|
224
|
+
if (shouldHandleChildren && result) {
|
|
225
|
+
Object.entries(result).forEach(([key, value]) => {
|
|
226
|
+
result[key] = this.useWrapper(value, true);
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
Logger.wrapperDepth--;
|
|
230
|
+
return result;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Function startEmpty() : Notify function start without args.
|
|
234
|
+
*
|
|
235
|
+
* @param {string} output
|
|
236
|
+
*/
|
|
237
|
+
startEmpty(output = "") {
|
|
238
|
+
if (!this.state) return;
|
|
239
|
+
this.printFunctionNotify("se", Logger.getCallerName(), output);
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Function startWith() : Notify function start with args.
|
|
243
|
+
*
|
|
244
|
+
* TODO: Rename to startsWith
|
|
245
|
+
*/
|
|
246
|
+
startWith(params) {
|
|
247
|
+
if (!this.state) return;
|
|
248
|
+
params = Object.assign({}, params);
|
|
249
|
+
const type = "se";
|
|
250
|
+
const source = Logger.getCallerName();
|
|
251
|
+
if (typeof params == "string") {
|
|
252
|
+
this.printInLineString(type, source, params);
|
|
253
|
+
} else if (Object.keys(params).length === 1) {
|
|
254
|
+
const key = Object.keys(params)[0];
|
|
255
|
+
let value = Object.values(params)[0];
|
|
256
|
+
// TODO: Check is repeated logic, handle it.
|
|
257
|
+
if (typeof value === "object") {
|
|
258
|
+
this.printNextLineObject(type, source, key, value || {});
|
|
259
|
+
} else if (typeof value == "function") {
|
|
260
|
+
this.printInLineFunction(type, source, key, value);
|
|
261
|
+
} else {
|
|
262
|
+
this.printInLineElement(type, source, key, value);
|
|
263
|
+
}
|
|
264
|
+
} else {
|
|
265
|
+
this.printMultiLineObject(type, source, params);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Function object() : Prints object.
|
|
270
|
+
**/
|
|
271
|
+
object(params = {}, notice = "") {
|
|
272
|
+
if (!this.state) return;
|
|
273
|
+
const source = Logger.getCallerName();
|
|
274
|
+
params = Object.create(params);
|
|
275
|
+
for (let key in params) {
|
|
276
|
+
if (typeof params[key] === "object") {
|
|
277
|
+
params[key] = JSON.stringify(this.useWrapper(params[key]), getCircularReplacer());
|
|
278
|
+
}
|
|
279
|
+
this.out.apply(this, [`%c(ob)-> %c%c${this.name}%c::%c${source}%c() [${notice}] ->> ${key}: '${params[key]}'%c`].concat(this.defaultStyle));
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Function debug() : Notify debug.
|
|
284
|
+
*/
|
|
285
|
+
debug(output) {
|
|
286
|
+
if (!this.state) return;
|
|
287
|
+
this.printFunctionNotify("db", Logger.getCallerName(), output);
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Function drop : Preview as continue of log which drop ite.
|
|
291
|
+
*/
|
|
292
|
+
drop(according, data) {
|
|
293
|
+
if (!this.state) return;
|
|
294
|
+
const source = Logger.getCallerName();
|
|
295
|
+
for (let key in according) {
|
|
296
|
+
this.out.apply(this, [`%c(dr)-> %c%c${this.name}%c::%c${source}%c() ->> corresponding ${key}: '${according[key]}' RESULT: %c↓`].concat(this.defaultStyle));
|
|
297
|
+
}
|
|
298
|
+
this.out(data);
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Function throw() : Throws logged error.
|
|
302
|
+
*/
|
|
303
|
+
throw(output, name = 'null', params = {}) {
|
|
304
|
+
this.printFunctionNotify("tw", Logger.getCallerName(), output);
|
|
305
|
+
if (params) this.printNextLineObject("tw", Logger.getCallerName(), name, params);
|
|
306
|
+
throw new Error().stack;
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Function clone() : Create clone instance.
|
|
310
|
+
*/
|
|
311
|
+
clone() {
|
|
312
|
+
return Object.assign(Object.create(Object.getPrototypeOf(this)), this);
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Function printFunctionNotify() : Print simple log for notify source (function).
|
|
316
|
+
*
|
|
317
|
+
*/
|
|
318
|
+
printFunctionNotify(type, source, output) {
|
|
319
|
+
this.out.apply(this, [`%c(${type})-> %c%c${this.name}%c::%c${source}%c() ${output}%c`].concat(this.defaultStyle));
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Function printInLineElement() : Print in line element.
|
|
323
|
+
*/
|
|
324
|
+
printInLineElement(type, source, key, value) {
|
|
325
|
+
this.out.apply(this, [`%c(${type})-> %c%c${this.name}%c::%c${source}%c() ->> ${key}: '${value}'%c`].concat(this.defaultStyle));
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Function printInLineFunction() : Print in line function.
|
|
329
|
+
*/
|
|
330
|
+
printInLineFunction(type, source, key, fn) {
|
|
331
|
+
fn = Logger.getFunctionView(fn);
|
|
332
|
+
this.printInLineElement(type, source, key, fn);
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Function printInLineString() : Print in line string.
|
|
336
|
+
*/
|
|
337
|
+
printInLineString(type, source, string) {
|
|
338
|
+
this.printInLineElement(type, source, "(string)", string);
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Function printNextLineObject() : Print object in next line.
|
|
342
|
+
*/
|
|
343
|
+
printNextLineObject(type, source, key, obj) {
|
|
344
|
+
obj = this.useWrapper(obj);
|
|
345
|
+
this.out.apply(this, [`%c(${type})-> %c%c${this.name}%c::%c${source}%c() ->> ${key} %c↓`].concat(this.defaultStyle));
|
|
346
|
+
// print in next line
|
|
347
|
+
this.out(obj);
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Function printMultiLineObject() : Print object in multiline format.
|
|
351
|
+
*/
|
|
352
|
+
printMultiLineObject(type, source, obj) {
|
|
353
|
+
this.out.apply(this, [`%c(${type})-> %c%c${this.name}%c::%c${source}%c(${Object.keys(obj).join(", ")}) %c↓`].concat(this.defaultStyle));
|
|
354
|
+
for (let key in obj) {
|
|
355
|
+
let value = obj[key];
|
|
356
|
+
if (typeof value === "object") {
|
|
357
|
+
value = JSON.stringify(this.useWrapper(value), getCircularReplacer());
|
|
358
|
+
} else if (typeof obj[key] == "function") {
|
|
359
|
+
value = Logger.getFunctionView(value);
|
|
360
|
+
}
|
|
361
|
+
this.out.apply(this, ["%c" + key + ": `" + this.useWrapper(value) + "`", "color: #a3a3a3"]);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Function getRandomColor() : Return random color.
|
|
366
|
+
*/
|
|
367
|
+
getRandomColor() {
|
|
368
|
+
const letters = "0123456789ABCDEF";
|
|
369
|
+
let color = "#";
|
|
370
|
+
for (let i = 0; i < 6; i++) {
|
|
371
|
+
color += letters[Math.floor(Math.random() * 16)];
|
|
372
|
+
}
|
|
373
|
+
let similar = Logger.colorsInUse.some(value => {
|
|
374
|
+
// it returns the ratio of difference... closer to 1.0 is less difference.
|
|
375
|
+
return hexColorDelta(color, value) >= 0.8;
|
|
376
|
+
});
|
|
377
|
+
// if the color is similar, try again.
|
|
378
|
+
if (similar) {
|
|
379
|
+
return this.getRandomColor();
|
|
380
|
+
}
|
|
381
|
+
return color;
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Function out() : Print console log with style
|
|
385
|
+
*/
|
|
386
|
+
out(...args) {
|
|
387
|
+
this.outputHandler.apply(this, args);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* @author: Leonid Vinikov <leonidvinikov@gmail.com>
|
|
393
|
+
* @description: Base for all commands, they are all will be managed by the `Commands` managers.
|
|
394
|
+
* @TODO
|
|
395
|
+
* - Add validateArgs method or find something better.
|
|
396
|
+
*/
|
|
397
|
+
class CommandBase extends ObjectBase {
|
|
398
|
+
args = {};
|
|
399
|
+
options = {};
|
|
400
|
+
logger;
|
|
401
|
+
constructor(args = {}, options = {}) {
|
|
402
|
+
super();
|
|
403
|
+
const name = this.constructor.getName();
|
|
404
|
+
this.logger = new Logger(name, true, {
|
|
405
|
+
sameColor: true
|
|
406
|
+
});
|
|
407
|
+
this.logger.startWith({
|
|
408
|
+
args,
|
|
409
|
+
options
|
|
410
|
+
});
|
|
411
|
+
this.initialize(args, options);
|
|
412
|
+
}
|
|
413
|
+
initialize(args, options) {
|
|
414
|
+
this.args = args;
|
|
415
|
+
this.options = options;
|
|
416
|
+
}
|
|
417
|
+
apply(args = this.args, options = this.options) {
|
|
418
|
+
throw new ForceMethod(this, "apply");
|
|
419
|
+
}
|
|
420
|
+
async run() {
|
|
421
|
+
this.onBeforeApply && this.onBeforeApply();
|
|
422
|
+
const result = await this.apply(this.args, this.options);
|
|
423
|
+
this.onAfterApply && this.onAfterApply();
|
|
424
|
+
return result;
|
|
425
|
+
}
|
|
426
|
+
getArgs() {
|
|
427
|
+
return this.args;
|
|
428
|
+
}
|
|
429
|
+
getOptions() {
|
|
430
|
+
return this.options;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* @author: Leonid Vinikov <leonidvinikov@gmail.com>
|
|
436
|
+
* @description: CommandPublic represents a USER action, every class which inherit from this class will USER action.
|
|
437
|
+
* */
|
|
438
|
+
class CommandPublic extends CommandBase {
|
|
439
|
+
static getName() {
|
|
440
|
+
return 'Core/CommandBases/CommandPublic';
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
let commands = {};
|
|
445
|
+
let controllers = {};
|
|
446
|
+
let data = {};
|
|
447
|
+
let internal = {};
|
|
448
|
+
|
|
449
|
+
var index$5 = /*#__PURE__*/Object.freeze({
|
|
450
|
+
__proto__: null,
|
|
451
|
+
get commands () { return commands; },
|
|
452
|
+
get controllers () { return controllers; },
|
|
453
|
+
get data () { return data; },
|
|
454
|
+
get internal () { return internal; }
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* @author: Leonid Vinikov <leonidvinikov@gmail.com>
|
|
459
|
+
* @description: Each data command should represent a final REST endpoint.
|
|
460
|
+
*/
|
|
461
|
+
class CommandData extends CommandBase {
|
|
462
|
+
static getName() {
|
|
463
|
+
return 'Core/CommandBases/CommandData';
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Function getEndpoint() : Override this method is required to determine endpoint and magic query params.
|
|
467
|
+
* @example
|
|
468
|
+
* ```
|
|
469
|
+
* args = { query: { id: 1 } };
|
|
470
|
+
* getEndpoint() = '/api/v1/users/{id}';
|
|
471
|
+
* result = '/api/v1/users/1';
|
|
472
|
+
* ```
|
|
473
|
+
*/
|
|
474
|
+
getEndpoint() {
|
|
475
|
+
throw new ForceMethod(this, 'getEndpoint');
|
|
476
|
+
}
|
|
477
|
+
apply(args = this.args, options = this.options) {
|
|
478
|
+
const endpoint = this.applyEndpointFormat(this.getEndpoint(), args);
|
|
479
|
+
return data.getClient().fetch(endpoint, data.currentHttpMethod, args || null);
|
|
480
|
+
}
|
|
481
|
+
applyEndpointFormat(endpoint, data = {}) {
|
|
482
|
+
// Replace query with `magic` placeholders.
|
|
483
|
+
if (endpoint.includes('{')) {
|
|
484
|
+
endpoint = endpoint.split('/').map(endpointPart => {
|
|
485
|
+
const match = endpointPart.match('\\{(.*?)\\}');
|
|
486
|
+
if (match?.length && 'undefined' !== typeof data[match[1]]) {
|
|
487
|
+
return data[match[1]];
|
|
488
|
+
}
|
|
489
|
+
return endpointPart;
|
|
490
|
+
}).join('/');
|
|
491
|
+
}
|
|
492
|
+
return endpoint;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* @author: Leonid Vinikov <leonidvinikov@gmail.com>
|
|
498
|
+
* @description: CommandInternal, is used when part of the logic needed to be in the command but not represent a user action.
|
|
499
|
+
*/
|
|
500
|
+
class CommandInternal extends CommandBase {
|
|
501
|
+
static getName() {
|
|
502
|
+
return 'Core/CommandBases/CommandInternal';
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/**
|
|
507
|
+
* @author: Leonid Vinikov <leonidvinikov@gmail.com>
|
|
508
|
+
*/
|
|
509
|
+
|
|
510
|
+
var index$4 = /*#__PURE__*/Object.freeze({
|
|
511
|
+
__proto__: null,
|
|
512
|
+
CommandBase: CommandBase,
|
|
513
|
+
CommandData: CommandData,
|
|
514
|
+
CommandInternal: CommandInternal,
|
|
515
|
+
CommandPublic: CommandPublic
|
|
516
|
+
});
|
|
517
|
+
|
|
518
|
+
/**
|
|
519
|
+
* @author: Leonid Vinikov <leonidvinikov@gmail.com>
|
|
520
|
+
* @description: Responsible for commands, Aka ( CommandsSpace ).
|
|
521
|
+
*/
|
|
522
|
+
class Controller extends ObjectBase {
|
|
523
|
+
commands;
|
|
524
|
+
data;
|
|
525
|
+
internal;
|
|
526
|
+
static getName() {
|
|
527
|
+
return 'Core/Core/Controller';
|
|
528
|
+
}
|
|
529
|
+
constructor() {
|
|
530
|
+
super();
|
|
531
|
+
this.initialize();
|
|
532
|
+
}
|
|
533
|
+
initialize() {
|
|
534
|
+
this.register();
|
|
535
|
+
this.setupHooks && this.setupHooks();
|
|
536
|
+
}
|
|
537
|
+
register() {
|
|
538
|
+
this.commands = commands.register(this.getCommands(), this);
|
|
539
|
+
this.data = data.register(this.getData(), this);
|
|
540
|
+
this.internal = internal.register(this.getInternal(), this);
|
|
541
|
+
}
|
|
542
|
+
getCommands() {
|
|
543
|
+
return {};
|
|
544
|
+
}
|
|
545
|
+
getData() {
|
|
546
|
+
return {};
|
|
547
|
+
}
|
|
548
|
+
getInternal() {
|
|
549
|
+
return {};
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
var index$3 = /*#__PURE__*/Object.freeze({
|
|
554
|
+
__proto__: null,
|
|
555
|
+
Controller: Controller,
|
|
556
|
+
ObjectBase: ObjectBase
|
|
557
|
+
});
|
|
558
|
+
|
|
559
|
+
class CommandAlreadyRegistered extends Error {
|
|
560
|
+
constructor(command) {
|
|
561
|
+
super(`Command: '${command.getName()}' is already registered`);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/**
|
|
566
|
+
* @author: Leonid Vinikov <leonidvinikov@gmail.com>
|
|
567
|
+
*/
|
|
568
|
+
class CommandNotFound extends Error {
|
|
569
|
+
constructor(command) {
|
|
570
|
+
super(`Command: '${command}' is not found`);
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
class ControllerAlreadyRegistered extends Error {
|
|
575
|
+
constructor(controller) {
|
|
576
|
+
super(`Controller: '${controller.getName()}' is already registered`);
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
var index$2 = /*#__PURE__*/Object.freeze({
|
|
581
|
+
__proto__: null,
|
|
582
|
+
CommandAlreadyRegistered: CommandAlreadyRegistered,
|
|
583
|
+
CommandNotFound: CommandNotFound,
|
|
584
|
+
ControllerAlreadyRegistered: ControllerAlreadyRegistered,
|
|
585
|
+
ForceMethod: ForceMethod
|
|
586
|
+
});
|
|
587
|
+
|
|
588
|
+
var index$1 = /*#__PURE__*/Object.freeze({
|
|
589
|
+
__proto__: null,
|
|
590
|
+
Logger: Logger
|
|
591
|
+
});
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* @author: Leonid Vinikov <leonidvinikov@gmail.com>
|
|
595
|
+
* @description: Responsible for manging/running/hooking commands.
|
|
596
|
+
*/
|
|
597
|
+
class Commands extends ObjectBase {
|
|
598
|
+
static trace = [];
|
|
599
|
+
current = {};
|
|
600
|
+
currentArgs = {};
|
|
601
|
+
trace = [];
|
|
602
|
+
commands = {};
|
|
603
|
+
onBeforeHooks = {};
|
|
604
|
+
onBeforeUIHooks = {};
|
|
605
|
+
onAfterHooks = {};
|
|
606
|
+
onAfterOnceHooks = {};
|
|
607
|
+
onAfterUIHooks = {};
|
|
608
|
+
onAfterAffectHooks = {};
|
|
609
|
+
logger;
|
|
610
|
+
static getName() {
|
|
611
|
+
return 'Core/Managers/Commands';
|
|
612
|
+
}
|
|
613
|
+
static async runCallbacks(callbacks, args = {}, options = {}) {
|
|
614
|
+
const callbacksLength = callbacks?.length || 0;
|
|
615
|
+
for (let i = 0; i < callbacksLength; ++i) {
|
|
616
|
+
const callback = options.pop === true ? callbacks.pop() : callbacks[i];
|
|
617
|
+
if (!callback) {
|
|
618
|
+
throw new Error("Callback is not defined.");
|
|
619
|
+
}
|
|
620
|
+
await callback(args, options);
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
static hookCommand(stack, hookCommand, context) {
|
|
624
|
+
if (!stack[hookCommand]) {
|
|
625
|
+
stack[hookCommand] = [];
|
|
626
|
+
}
|
|
627
|
+
stack[hookCommand].push(context);
|
|
628
|
+
}
|
|
629
|
+
constructor() {
|
|
630
|
+
super();
|
|
631
|
+
this.logger = new Logger(this.getName(), true);
|
|
632
|
+
this.logger.startEmpty();
|
|
633
|
+
}
|
|
634
|
+
async run(command, args = {}, options = {}) {
|
|
635
|
+
if (typeof command === "string") {
|
|
636
|
+
command = this.getCommandInstance(command, args, options);
|
|
637
|
+
}
|
|
638
|
+
this.attachCurrent(command, args);
|
|
639
|
+
const result = await this.runInstance(command, args, options);
|
|
640
|
+
this.detachCurrent(command);
|
|
641
|
+
return result;
|
|
642
|
+
}
|
|
643
|
+
register(commands, controller) {
|
|
644
|
+
const result = {};
|
|
645
|
+
Object.values(commands).forEach(command => {
|
|
646
|
+
const commandName = command.getName();
|
|
647
|
+
if (this.commands[commandName]) {
|
|
648
|
+
throw new CommandAlreadyRegistered(command);
|
|
649
|
+
}
|
|
650
|
+
this.commands[commandName] = command;
|
|
651
|
+
result[commandName] = command;
|
|
652
|
+
});
|
|
653
|
+
return result;
|
|
654
|
+
}
|
|
655
|
+
getAll() {
|
|
656
|
+
return this.commands;
|
|
657
|
+
}
|
|
658
|
+
getByName(name) {
|
|
659
|
+
return this.commands[name];
|
|
660
|
+
}
|
|
661
|
+
getLogger() {
|
|
662
|
+
return this.logger;
|
|
663
|
+
}
|
|
664
|
+
getCommandInstance(name, args = {}, options = {}) {
|
|
665
|
+
const CommandClass = this.commands[name];
|
|
666
|
+
if (!CommandClass) {
|
|
667
|
+
throw new CommandNotFound(name);
|
|
668
|
+
}
|
|
669
|
+
// @ts-ignore
|
|
670
|
+
return new CommandClass(args, options);
|
|
671
|
+
}
|
|
672
|
+
/**
|
|
673
|
+
* Function onBefore() : Used to set hooks that effects only data and not UI.
|
|
674
|
+
*/
|
|
675
|
+
onBefore(hookCommand, callback) {
|
|
676
|
+
Commands.hookCommand(this.onBeforeHooks, hookCommand, callback);
|
|
677
|
+
}
|
|
678
|
+
/**
|
|
679
|
+
* Function onBeforeUI() : Used to set hooks that effects only UI.
|
|
680
|
+
*/
|
|
681
|
+
onBeforeUI(hookCommand, callback) {
|
|
682
|
+
Commands.hookCommand(this.onBeforeUIHooks, hookCommand, callback);
|
|
683
|
+
}
|
|
684
|
+
/**
|
|
685
|
+
* Function onAfter() : Used to set hooks that effects only data and not UI.
|
|
686
|
+
*/
|
|
687
|
+
onAfter(hookCommand, callback) {
|
|
688
|
+
Commands.hookCommand(this.onAfterHooks, hookCommand, callback);
|
|
689
|
+
}
|
|
690
|
+
/**
|
|
691
|
+
* Function onAfterUI() : Used to set hooks that effects only UI.
|
|
692
|
+
*/
|
|
693
|
+
onAfterUI(command, callback) {
|
|
694
|
+
Commands.hookCommand(this.onAfterUIHooks, command, callback);
|
|
695
|
+
}
|
|
696
|
+
/**
|
|
697
|
+
* Function onAfterOnce() : Used to set hooks that effects only data and not UI.
|
|
698
|
+
*/
|
|
699
|
+
onAfterOnce(command, callback) {
|
|
700
|
+
Commands.hookCommand(this.onAfterOnceHooks, command, callback);
|
|
701
|
+
}
|
|
702
|
+
/**
|
|
703
|
+
* Function onAfterAffect() : Used to register a trigger that runs command after `hookCommand` run's.
|
|
704
|
+
*/
|
|
705
|
+
onAfterAffect(hookCommand, affectCommand) {
|
|
706
|
+
Commands.hookCommand(this.onAfterAffectHooks, hookCommand, affectCommand);
|
|
707
|
+
}
|
|
708
|
+
onBeforeRun(command, args = {}, options = {}) {
|
|
709
|
+
if (this.onBeforeHooks[command.getName()]) {
|
|
710
|
+
const callbacks = this.onBeforeHooks[command.getName()];
|
|
711
|
+
callbacks.forEach(callback => callback(args, options));
|
|
712
|
+
}
|
|
713
|
+
if (this.onBeforeUIHooks[command.getName()]) {
|
|
714
|
+
const callbacks = this.onBeforeUIHooks[command.getName()];
|
|
715
|
+
callbacks.forEach(callback => callback(args, options));
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
async runInstance(command, args = {}, options = {}) {
|
|
719
|
+
let result = null;
|
|
720
|
+
// TODO: Maybe a logger method to handle such cases.
|
|
721
|
+
if (Object.keys(args).length) {
|
|
722
|
+
this.logger.startWith({
|
|
723
|
+
command: command.getName(),
|
|
724
|
+
options,
|
|
725
|
+
CommandArgs: "->"
|
|
726
|
+
});
|
|
727
|
+
this.logger.debug("CommandArgs:");
|
|
728
|
+
this.logger.object(args);
|
|
729
|
+
} else {
|
|
730
|
+
this.logger.startWith({
|
|
731
|
+
command: command.getName(),
|
|
732
|
+
options,
|
|
733
|
+
args
|
|
734
|
+
});
|
|
735
|
+
}
|
|
736
|
+
this.onBeforeRun(command, args, options);
|
|
737
|
+
result = await command.run();
|
|
738
|
+
await this.onAfterRun(command, args, options, result);
|
|
739
|
+
return result;
|
|
740
|
+
}
|
|
741
|
+
async onAfterRun(command, args, options, result) {
|
|
742
|
+
options = Object.assign({}, options);
|
|
743
|
+
if (this.onAfterAffectHooks[command.getName()]) {
|
|
744
|
+
this.onAfterAffectHooks[command.getName()].forEach(command => {
|
|
745
|
+
args.result = result;
|
|
746
|
+
result = this.run(command.toString(), args, options);
|
|
747
|
+
});
|
|
748
|
+
result = await result;
|
|
749
|
+
}
|
|
750
|
+
if (this.onAfterHooks) {
|
|
751
|
+
args.result = result;
|
|
752
|
+
await Commands.runCallbacks(Object.assign([], this.onAfterHooks[command.getName()]), args, options);
|
|
753
|
+
}
|
|
754
|
+
if (this.onAfterOnceHooks) {
|
|
755
|
+
await Commands.runCallbacks(this.onAfterOnceHooks[command.getName()], args, {
|
|
756
|
+
...options,
|
|
757
|
+
pop: true // Delete after run.
|
|
758
|
+
});
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
if (this.onAfterUIHooks) {
|
|
762
|
+
Commands.runCallbacks(Object.assign([], this.onAfterUIHooks[command.getName()]), args, options);
|
|
763
|
+
}
|
|
764
|
+
return result;
|
|
765
|
+
}
|
|
766
|
+
attachCurrent(command, args = {}) {
|
|
767
|
+
this.current[command.getName()] = command;
|
|
768
|
+
this.currentArgs[command.getName()] = args;
|
|
769
|
+
this.trace.push(command.getName());
|
|
770
|
+
Commands.trace.push(command.getName());
|
|
771
|
+
}
|
|
772
|
+
detachCurrent(command) {
|
|
773
|
+
delete this.current[command.getName()];
|
|
774
|
+
delete this.currentArgs[command.getName()];
|
|
775
|
+
Commands.trace.pop();
|
|
776
|
+
this.trace.pop();
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
/**
|
|
781
|
+
* @author: Leonid Vinikov <leonidvinikov@gmail.com>
|
|
782
|
+
* @description: Responsible for manging controllers, each controller is global singleton instance.
|
|
783
|
+
*/
|
|
784
|
+
class Controllers extends ObjectBase {
|
|
785
|
+
controllers = {};
|
|
786
|
+
static getName() {
|
|
787
|
+
return 'Core/Managers/Controllers';
|
|
788
|
+
}
|
|
789
|
+
get(name) {
|
|
790
|
+
return this.controllers[name];
|
|
791
|
+
}
|
|
792
|
+
getAll() {
|
|
793
|
+
return this.controllers;
|
|
794
|
+
}
|
|
795
|
+
register(controller) {
|
|
796
|
+
if (this.controllers[controller.getName()]) {
|
|
797
|
+
throw new ControllerAlreadyRegistered(controller);
|
|
798
|
+
}
|
|
799
|
+
// Register.
|
|
800
|
+
this.controllers[controller.getName()] = controller;
|
|
801
|
+
return controller;
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
/**
|
|
806
|
+
* @author: Leonid Vinikov <leonidvinikov@gmail.com>
|
|
807
|
+
*/
|
|
808
|
+
var HTTPMethodEnum;
|
|
809
|
+
(function (HTTPMethodEnum) {
|
|
810
|
+
HTTPMethodEnum["DELETE"] = "DELETE";
|
|
811
|
+
HTTPMethodEnum["GET"] = "GET";
|
|
812
|
+
HTTPMethodEnum["OPTIONS"] = "OPTIONS";
|
|
813
|
+
HTTPMethodEnum["PATCH"] = "PATCH";
|
|
814
|
+
HTTPMethodEnum["POST"] = "POST";
|
|
815
|
+
HTTPMethodEnum["PUT"] = "PUT";
|
|
816
|
+
HTTPMethodEnum["__EMPTY__"] = "";
|
|
817
|
+
})(HTTPMethodEnum || (HTTPMethodEnum = {}));
|
|
818
|
+
|
|
819
|
+
/**
|
|
820
|
+
* @author: Leonid Vinikov <leonidvinikov@gmail.com>
|
|
821
|
+
* @description: Providing a simple wrapper for fetch API.
|
|
822
|
+
*/
|
|
823
|
+
class Http extends ObjectBase {
|
|
824
|
+
logger;
|
|
825
|
+
apiBaseUrl;
|
|
826
|
+
static getName() {
|
|
827
|
+
return 'Core/Clients/Http';
|
|
828
|
+
}
|
|
829
|
+
/**
|
|
830
|
+
* Function constructor() : Create the http.
|
|
831
|
+
*/
|
|
832
|
+
constructor(apiBaseUrl = 'http://localhost') {
|
|
833
|
+
super();
|
|
834
|
+
this.logger = new Logger(Http.getName(), true);
|
|
835
|
+
this.logger.startWith({
|
|
836
|
+
apiBaseUrl
|
|
837
|
+
});
|
|
838
|
+
this.apiBaseUrl = apiBaseUrl + '/';
|
|
839
|
+
}
|
|
840
|
+
/**
|
|
841
|
+
* Function fetch() : Fetch api.
|
|
842
|
+
*/
|
|
843
|
+
async fetch(path, method, body = null) {
|
|
844
|
+
this.logger.startWith({
|
|
845
|
+
path,
|
|
846
|
+
method,
|
|
847
|
+
body
|
|
848
|
+
});
|
|
849
|
+
const params = {
|
|
850
|
+
'credentials': 'include'
|
|
851
|
+
},
|
|
852
|
+
// Support cookies.
|
|
853
|
+
headers = {};
|
|
854
|
+
if ([HTTPMethodEnum.POST, HTTPMethodEnum.PUT, HTTPMethodEnum.PATCH].includes(method)) {
|
|
855
|
+
Object.assign(headers, {
|
|
856
|
+
'Content-Type': 'application/json'
|
|
857
|
+
});
|
|
858
|
+
Object.assign(params, {
|
|
859
|
+
method,
|
|
860
|
+
headers: headers,
|
|
861
|
+
body: JSON.stringify(body)
|
|
862
|
+
});
|
|
863
|
+
} else {
|
|
864
|
+
Object.assign(params, {
|
|
865
|
+
headers
|
|
866
|
+
});
|
|
867
|
+
}
|
|
868
|
+
const response = await globalThis.fetch(this.apiBaseUrl + path, params);
|
|
869
|
+
let data = undefined;
|
|
870
|
+
try {
|
|
871
|
+
data = await response.json();
|
|
872
|
+
} catch (e) {
|
|
873
|
+
console.error(e);
|
|
874
|
+
return false;
|
|
875
|
+
}
|
|
876
|
+
// TODO: This is part should be modular and not hard coded.
|
|
877
|
+
if (data?.error && data?.global && data?.message) {
|
|
878
|
+
throw new Error(data.message);
|
|
879
|
+
}
|
|
880
|
+
this.logger.drop({
|
|
881
|
+
path
|
|
882
|
+
}, data);
|
|
883
|
+
return data;
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
/**
|
|
888
|
+
* @author: Leonid Vinikov <leonidvinikov@gmail.com>
|
|
889
|
+
* @description: Responsible for manging data commands which are wrappers for HTTP requests.
|
|
890
|
+
*/
|
|
891
|
+
class Data extends Commands {
|
|
892
|
+
static client;
|
|
893
|
+
currentHttpMethod;
|
|
894
|
+
static getName() {
|
|
895
|
+
return 'Core/Managers/Data';
|
|
896
|
+
}
|
|
897
|
+
constructor(Config) {
|
|
898
|
+
super();
|
|
899
|
+
// @ts-ignore
|
|
900
|
+
Data.client = new Http(Config.baseURL);
|
|
901
|
+
}
|
|
902
|
+
getClient() {
|
|
903
|
+
return Data.client;
|
|
904
|
+
}
|
|
905
|
+
get(command, args = {}, options = {}) {
|
|
906
|
+
this.currentHttpMethod = HTTPMethodEnum.GET;
|
|
907
|
+
return super.run(command, args, options);
|
|
908
|
+
}
|
|
909
|
+
update(command, args = {}, options = {}) {
|
|
910
|
+
this.currentHttpMethod = HTTPMethodEnum.PATCH;
|
|
911
|
+
return super.run(command, args, options);
|
|
912
|
+
}
|
|
913
|
+
delete(command, args = {}, options = {}) {
|
|
914
|
+
this.currentHttpMethod = HTTPMethodEnum.DELETE;
|
|
915
|
+
return super.run(command, args, options);
|
|
916
|
+
}
|
|
917
|
+
create(command, args = {}, options = {}) {
|
|
918
|
+
this.currentHttpMethod = HTTPMethodEnum.POST;
|
|
919
|
+
return super.run(command, args, options);
|
|
920
|
+
}
|
|
921
|
+
async runInstance(command, args = {}, options = {}) {
|
|
922
|
+
if (!this.currentHttpMethod) {
|
|
923
|
+
throw new Error('Cannot run directly use one of the http methods: "get", "update", "delete, "create');
|
|
924
|
+
}
|
|
925
|
+
// New args.
|
|
926
|
+
const newArgs = {
|
|
927
|
+
type: this.currentHttpMethod,
|
|
928
|
+
args: {
|
|
929
|
+
query: {},
|
|
930
|
+
data: {}
|
|
931
|
+
}
|
|
932
|
+
};
|
|
933
|
+
if (HTTPMethodEnum.GET === this.currentHttpMethod) {
|
|
934
|
+
newArgs.args.query = args;
|
|
935
|
+
} else {
|
|
936
|
+
newArgs.args.data = args;
|
|
937
|
+
}
|
|
938
|
+
args.result = await super.runInstance(command, newArgs, options);
|
|
939
|
+
// Clear method type.
|
|
940
|
+
this.currentHttpMethod = HTTPMethodEnum.__EMPTY__;
|
|
941
|
+
return args.result;
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
/**
|
|
946
|
+
* @author: Leonid Vinikov <leonidvinikov@gmail.com>
|
|
947
|
+
* @description: Responsible for manging internal commands, To serve commands that are not triggered by user.
|
|
948
|
+
*/
|
|
949
|
+
class Internal extends Commands {
|
|
950
|
+
static getName() {
|
|
951
|
+
return 'Core/Managers/Internal';
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
/**
|
|
956
|
+
* @author: Leonid Vinikov <leonidvinikov@gmail.com>
|
|
957
|
+
*/
|
|
958
|
+
|
|
959
|
+
var index = /*#__PURE__*/Object.freeze({
|
|
960
|
+
__proto__: null,
|
|
961
|
+
Commands: Commands,
|
|
962
|
+
Controllers: Controllers,
|
|
963
|
+
Data: Data,
|
|
964
|
+
Internal: Internal
|
|
965
|
+
});
|
|
966
|
+
|
|
967
|
+
/**
|
|
968
|
+
* @author: Leonid Vinikov <leonidvinikov@gmail.com>
|
|
969
|
+
*/
|
|
970
|
+
|
|
971
|
+
var exported = /*#__PURE__*/Object.freeze({
|
|
972
|
+
__proto__: null,
|
|
973
|
+
commandBases: index$4,
|
|
974
|
+
core: index$3,
|
|
975
|
+
errors: index$2,
|
|
976
|
+
managerBases: index,
|
|
977
|
+
managers: index$5,
|
|
978
|
+
modules: index$1,
|
|
979
|
+
utils: index$6
|
|
980
|
+
});
|
|
981
|
+
|
|
982
|
+
var name = "@zenflux/core";
|
|
983
|
+
var version = "0.0.0-abc.0";
|
|
984
|
+
var description = "ZenFlux core";
|
|
985
|
+
var author = "Leonid Vinikov <leonidvinikov@gmail.com> (https://github.com/iNewLegend)";
|
|
986
|
+
var scripts = {
|
|
987
|
+
test: "export flow_modules_logger=off && jest",
|
|
988
|
+
build: "rollup --config rollup.config.ts --configPlugin typescript"
|
|
989
|
+
};
|
|
990
|
+
var repository = {
|
|
991
|
+
type: "git",
|
|
992
|
+
url: "git+https://github.com/zenflux/core.git"
|
|
993
|
+
};
|
|
994
|
+
var keywords = [
|
|
995
|
+
"zenflux",
|
|
996
|
+
"zen",
|
|
997
|
+
"flux",
|
|
998
|
+
"commands",
|
|
999
|
+
"manager",
|
|
1000
|
+
"commands manager"
|
|
1001
|
+
];
|
|
1002
|
+
var files = [
|
|
1003
|
+
"dist",
|
|
1004
|
+
"src",
|
|
1005
|
+
"types"
|
|
1006
|
+
];
|
|
1007
|
+
var license = "MIT";
|
|
1008
|
+
var main = "dist/cjs/zenflux-core.js";
|
|
1009
|
+
var unpkg = "dist/umd/zenflux-core.js";
|
|
1010
|
+
var module = "dist/es/zenflux-core.js";
|
|
1011
|
+
var types = "types/index.d.ts";
|
|
1012
|
+
var devDependencies = {
|
|
1013
|
+
"@babel/core": "^7.20.7",
|
|
1014
|
+
"@babel/plugin-transform-runtime": "^7.19.6",
|
|
1015
|
+
"@babel/preset-env": "^7.20.2",
|
|
1016
|
+
"@babel/preset-typescript": "^7.18.6",
|
|
1017
|
+
"@babel/runtime": "^7.20.7",
|
|
1018
|
+
"@rollup/plugin-babel": "^6.0.3",
|
|
1019
|
+
"@rollup/plugin-json": "^6.0.0",
|
|
1020
|
+
"@rollup/plugin-node-resolve": "^15.0.1",
|
|
1021
|
+
"@rollup/plugin-replace": "^5.0.2",
|
|
1022
|
+
"@rollup/plugin-terser": "^0.2.1",
|
|
1023
|
+
"@rollup/plugin-typescript": "^10.0.1",
|
|
1024
|
+
"@types/jest": "^29.2.5",
|
|
1025
|
+
"@typescript-eslint/eslint-plugin": "^5.48.0",
|
|
1026
|
+
"@typescript-eslint/parser": "^5.48.0",
|
|
1027
|
+
"babel-jest": "^29.3.1",
|
|
1028
|
+
rollup: "^3.9.1",
|
|
1029
|
+
"ts-jest": "^29.0.3",
|
|
1030
|
+
tslib: "^2.4.1"
|
|
1031
|
+
};
|
|
1032
|
+
var dependencies = {
|
|
1033
|
+
"@babel/runtime": "^7.19.0"
|
|
1034
|
+
};
|
|
1035
|
+
var pkg = {
|
|
1036
|
+
name: name,
|
|
1037
|
+
version: version,
|
|
1038
|
+
description: description,
|
|
1039
|
+
author: author,
|
|
1040
|
+
scripts: scripts,
|
|
1041
|
+
repository: repository,
|
|
1042
|
+
keywords: keywords,
|
|
1043
|
+
files: files,
|
|
1044
|
+
license: license,
|
|
1045
|
+
main: main,
|
|
1046
|
+
unpkg: unpkg,
|
|
1047
|
+
module: module,
|
|
1048
|
+
types: types,
|
|
1049
|
+
devDependencies: devDependencies,
|
|
1050
|
+
dependencies: dependencies
|
|
1051
|
+
};
|
|
1052
|
+
|
|
1053
|
+
let isInitialized = false;
|
|
1054
|
+
function errorInitTwice() {
|
|
1055
|
+
if (isInitialized) {
|
|
1056
|
+
throw new Error('ZenCore is already initialized.');
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
errorInitTwice();
|
|
1060
|
+
let config = {
|
|
1061
|
+
version: pkg.version
|
|
1062
|
+
};
|
|
1063
|
+
const afterInitializeCallbacks = [];
|
|
1064
|
+
const CoreAPI = {
|
|
1065
|
+
initialize: configuration => {
|
|
1066
|
+
errorInitTwice();
|
|
1067
|
+
config = configuration || config;
|
|
1068
|
+
commands = new Commands();
|
|
1069
|
+
controllers = new Controllers();
|
|
1070
|
+
data = new Data(config);
|
|
1071
|
+
internal = new Internal();
|
|
1072
|
+
isInitialized = true;
|
|
1073
|
+
afterInitializeCallbacks.forEach(callback => callback());
|
|
1074
|
+
},
|
|
1075
|
+
destroy: () => {
|
|
1076
|
+
isInitialized = false;
|
|
1077
|
+
commands = {};
|
|
1078
|
+
controllers = {};
|
|
1079
|
+
data = {};
|
|
1080
|
+
internal = {};
|
|
1081
|
+
},
|
|
1082
|
+
onAfterInitialize: callback => {
|
|
1083
|
+
afterInitializeCallbacks.push(callback);
|
|
1084
|
+
},
|
|
1085
|
+
config,
|
|
1086
|
+
...exported
|
|
1087
|
+
};
|
|
1088
|
+
// TODO: Make it available only for development.
|
|
1089
|
+
// if ( ! globalThis?.ZenCore ) globalThis.ZenCore = CoreAPI;
|
|
1090
|
+
|
|
1091
|
+
export { index$4 as commandBases, index$3 as core, CoreAPI as default, index$2 as errors, index as managerBases, index$5 as managers, index$1 as modules, index$6 as utils };
|