@webtypen/webframez-core 0.0.13 → 0.0.15

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.
@@ -0,0 +1,3 @@
1
+ export declare class BaseKernelConsole {
2
+ static commands: any;
3
+ }
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BaseKernelConsole = void 0;
4
+ class BaseKernelConsole {
5
+ }
6
+ exports.BaseKernelConsole = BaseKernelConsole;
7
+ BaseKernelConsole.commands = [];
@@ -0,0 +1,9 @@
1
+ import { Controller } from "./Controller/Controller";
2
+ export declare class BaseKernelWeb {
3
+ static controller: {
4
+ [key: string]: Controller;
5
+ };
6
+ static middleware: {
7
+ [key: string]: any;
8
+ };
9
+ }
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BaseKernelWeb = void 0;
4
+ class BaseKernelWeb {
5
+ }
6
+ exports.BaseKernelWeb = BaseKernelWeb;
7
+ BaseKernelWeb.controller = {};
@@ -0,0 +1,16 @@
1
+ type ConfigType = {
2
+ key: string;
3
+ component: any;
4
+ };
5
+ declare class ConfigFacade {
6
+ configs: {
7
+ [key: string]: any;
8
+ };
9
+ register(key: string | ConfigType[], component: {
10
+ [key: string]: any;
11
+ }): this;
12
+ get(key: string): any;
13
+ set(path: string, value: any | null | undefined): this;
14
+ }
15
+ export declare const Config: ConfigFacade;
16
+ export {};
package/lib/Config.js ADDED
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Config = void 0;
7
+ const lodash_1 = __importDefault(require("lodash"));
8
+ class ConfigFacade {
9
+ constructor() {
10
+ this.configs = {};
11
+ }
12
+ register(key, component) {
13
+ if (typeof key === "string") {
14
+ this.configs[key] = component;
15
+ }
16
+ else if (Array.isArray(key)) {
17
+ for (let conf of key) {
18
+ if (!conf.key || !conf.component) {
19
+ continue;
20
+ }
21
+ this.configs[conf.key] = conf.component;
22
+ }
23
+ }
24
+ return this;
25
+ }
26
+ get(key) {
27
+ return lodash_1.default.get(this.configs, key);
28
+ }
29
+ set(path, value) {
30
+ const newConfig = Object.assign({}, this.configs);
31
+ lodash_1.default.set(newConfig, path, value);
32
+ this.configs = newConfig;
33
+ return this;
34
+ }
35
+ }
36
+ exports.Config = new ConfigFacade();
@@ -8,29 +8,30 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
11
  Object.defineProperty(exports, "__esModule", { value: true });
15
12
  exports.DBConnection = void 0;
16
13
  // @ts-ignore
17
- const database_1 = __importDefault(require("../../../../../config/database"));
14
+ const Config_1 = require("../Config");
18
15
  const DBDrivers_1 = require("./DBDrivers");
19
16
  class DBConnectionFacade {
20
17
  constructor() {
21
18
  this.connections = {};
22
19
  }
23
20
  getConnectionConfig(connection) {
21
+ const dbconfig = Config_1.Config.get("database");
22
+ if (!dbconfig) {
23
+ throw new Error("Missing database-config ...");
24
+ }
24
25
  if (!connection) {
25
- connection = database_1.default.defaultConnection;
26
+ connection = dbconfig.defaultConnection;
26
27
  }
27
28
  if (!connection ||
28
- !database_1.default ||
29
- !database_1.default.connections ||
30
- !database_1.default.connections[connection]) {
29
+ !dbconfig ||
30
+ !dbconfig.connections ||
31
+ !dbconfig.connections[connection]) {
31
32
  throw new Error("No connection given ...");
32
33
  }
33
- return database_1.default.connections[connection];
34
+ return dbconfig.connections[connection];
34
35
  }
35
36
  getConnectionDriver(connection) {
36
37
  const config = this.getConnectionConfig(connection);
@@ -44,8 +45,12 @@ class DBConnectionFacade {
44
45
  }
45
46
  getConnection(connectionName) {
46
47
  return __awaiter(this, void 0, void 0, function* () {
48
+ const dbconfig = Config_1.Config.get("database");
49
+ if (!dbconfig) {
50
+ throw new Error("Missing database-config ...");
51
+ }
47
52
  if (!connectionName) {
48
- connectionName = database_1.default.defaultConnection;
53
+ connectionName = dbconfig.defaultConnection;
49
54
  }
50
55
  if (!connectionName) {
51
56
  return null;
@@ -4,9 +4,9 @@ export declare class QueryBuilder {
4
4
  queryTable: string;
5
5
  mode: string;
6
6
  modelMapping: null;
7
- setModelMapping(model?: any | null): void;
8
- table(table: string): void;
9
- where(column: any, operator: any, value: any): void;
7
+ setModelMapping(model?: any | null): this;
8
+ table(table: string): this;
9
+ where(column: any, operator: any, value: any): this;
10
10
  get(options?: any): Promise<any>;
11
11
  first(options?: any): Promise<any>;
12
12
  delete(options?: {
@@ -21,9 +21,11 @@ class QueryBuilder {
21
21
  }
22
22
  setModelMapping(model) {
23
23
  this.modelMapping = model;
24
+ return this;
24
25
  }
25
26
  table(table) {
26
27
  this.queryTable = table;
28
+ return this;
27
29
  }
28
30
  where(column, operator, value) {
29
31
  this.query.push({
@@ -32,6 +34,7 @@ class QueryBuilder {
32
34
  operator: operator,
33
35
  value: value,
34
36
  });
37
+ return this;
35
38
  }
36
39
  get(options) {
37
40
  return __awaiter(this, void 0, void 0, function* () {
@@ -13,6 +13,10 @@ declare class RouterFacade {
13
13
  * Basename
14
14
  */
15
15
  basename: string | null;
16
+ /**
17
+ * Kernel-Class
18
+ */
19
+ kernel: any | null;
16
20
  /**
17
21
  * GET Route Store
18
22
  */
@@ -99,6 +103,7 @@ declare class RouterFacade {
99
103
  * @param res
100
104
  */
101
105
  handleRequest(req: IncomingMessage, res: ServerResponse): Promise<void>;
106
+ requestConsoleLog(req: IncomingMessage): void;
102
107
  }
103
108
  export declare const Router: RouterFacade;
104
109
  export {};
@@ -15,14 +15,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.Router = void 0;
16
16
  const qs_1 = __importDefault(require("qs"));
17
17
  const Response_1 = require("./Response");
18
- // @ts-ignore
19
- const Kernel_1 = require("../../../../../app/Kernel");
18
+ const Config_1 = require("../Config");
20
19
  class RouterFacade {
21
20
  constructor() {
22
21
  /**
23
22
  * Basename
24
23
  */
25
24
  this.basename = null;
25
+ /**
26
+ * Kernel-Class
27
+ */
28
+ this.kernel = null;
26
29
  /**
27
30
  * GET Route Store
28
31
  */
@@ -46,8 +49,11 @@ class RouterFacade {
46
49
  */
47
50
  init(options) {
48
51
  this.basename = options && options.basename ? options.basename : null;
52
+ this.kernel = options && options.kernel ? options.kernel : null;
49
53
  // Load routes
54
+ console.log("loadRoutes");
50
55
  require(process.cwd() + "/app/routes");
56
+ console.log("finishRoutes");
51
57
  }
52
58
  /**
53
59
  * Register a route
@@ -174,13 +180,16 @@ class RouterFacade {
174
180
  const route = this.dissolve(req);
175
181
  const response = new Response_1.Response();
176
182
  response.setServerResponse(res);
183
+ if (!this.kernel) {
184
+ throw new Error("Missing Kernel in Router ...");
185
+ }
177
186
  // Check-Middleware
178
187
  if (route &&
179
188
  route.options &&
180
189
  route.options.middleware &&
181
190
  route.options.middleware.length > 0) {
182
191
  for (let middlewareKey of route.options.middleware) {
183
- const middleware = Kernel_1.Kernel.middleware[middlewareKey];
192
+ const middleware = this.kernel.middleware[middlewareKey];
184
193
  if (!middleware) {
185
194
  return this.sendError(res, 500, "Unknown middleware `" + middlewareKey + "` ...");
186
195
  }
@@ -221,7 +230,7 @@ class RouterFacade {
221
230
  // Resolve route-component (handle controller definition)
222
231
  if (route && typeof route.component === "string") {
223
232
  const split = route.component.split("@");
224
- const controller = Kernel_1.Kernel.controller[split[0]];
233
+ const controller = this.kernel.controller[split[0]];
225
234
  if (!controller) {
226
235
  res.writeHead(500, { "Content-Type": "text/html" });
227
236
  res.end("Controller `" + split[0] + "` not found ...");
@@ -274,5 +283,10 @@ class RouterFacade {
274
283
  }
275
284
  });
276
285
  }
286
+ requestConsoleLog(req) {
287
+ if (Config_1.Config.get("router.requests.logConsole") !== true) {
288
+ return;
289
+ }
290
+ }
277
291
  }
278
292
  exports.Router = new RouterFacade();
@@ -5,20 +5,33 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.WebApplication = void 0;
7
7
  const http_1 = __importDefault(require("http"));
8
+ const Config_1 = require("./Config");
8
9
  const Router_1 = require("./Router/Router");
9
10
  class WebApplication {
10
11
  /**
11
12
  * Init the routes and start the http-server
12
13
  */
13
14
  boot(options) {
14
- Router_1.Router.init();
15
+ if (options && options.config) {
16
+ for (let key in options.config) {
17
+ Config_1.Config.register(key, options.config[key]);
18
+ }
19
+ }
20
+ Router_1.Router.init({
21
+ kernel: options && options.kernel ? options.kernel : null,
22
+ basename: options && options.basename ? options.basename : null,
23
+ });
15
24
  const port = options && options.port ? options.port : 3000;
16
25
  this.server = http_1.default
17
26
  .createServer((req, res) => {
18
27
  Router_1.Router.handleRequest(req, res);
19
28
  })
20
29
  .listen(port, () => {
21
- console.log("Server started and listening on port " + port);
30
+ console.log("Server started and listening on port " +
31
+ port +
32
+ (options && options.basename
33
+ ? " (Basename: " + options && options.basename + ")"
34
+ : ""));
22
35
  });
23
36
  }
24
37
  }
package/lib/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
- export * from "./BaseKernel";
1
+ export * from "./Config";
2
+ export * from "./BaseKernelWeb";
3
+ export * from "./BaseKernelConsole";
2
4
  export * from "./Controller/Controller";
3
5
  export * from "./Database/Model";
4
6
  export * from "./WebApplication";
package/lib/index.js CHANGED
@@ -14,7 +14,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./BaseKernel"), exports);
17
+ __exportStar(require("./Config"), exports);
18
+ __exportStar(require("./BaseKernelWeb"), exports);
19
+ __exportStar(require("./BaseKernelConsole"), exports);
18
20
  __exportStar(require("./Controller/Controller"), exports);
19
21
  __exportStar(require("./Database/Model"), exports);
20
22
  __exportStar(require("./WebApplication"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webtypen/webframez-core",
3
- "version": "0.0.13",
3
+ "version": "0.0.15",
4
4
  "description": "The core package of the typescript based webframez (web-framework) by webtypen.de",
5
5
  "homepage": "https://webtypen.de/",
6
6
  "license": "MIT",
@@ -22,6 +22,7 @@
22
22
  "build:check": "tsc --noEmit"
23
23
  },
24
24
  "devDependencies": {
25
+ "@types/lodash": "^4.14.190",
25
26
  "@types/node": "^18.11.9",
26
27
  "@types/qs": "^6.9.7",
27
28
  "ts-node": "^10.9.1",
@@ -0,0 +1,3 @@
1
+ export class BaseKernelConsole {
2
+ static commands: any = [];
3
+ }
@@ -1,7 +1,6 @@
1
1
  import { Controller } from "./Controller/Controller";
2
2
 
3
- export class BaseKernel {
4
- static commands = [];
3
+ export class BaseKernelWeb {
5
4
  static controller: { [key: string]: Controller } = {};
6
5
  static middleware: { [key: string]: any };
7
6
  }
package/src/Config.ts ADDED
@@ -0,0 +1,37 @@
1
+ import lodash from "lodash";
2
+
3
+ type ConfigType = {
4
+ key: string;
5
+ component: any;
6
+ };
7
+
8
+ class ConfigFacade {
9
+ configs: { [key: string]: any } = {};
10
+
11
+ register(key: string | ConfigType[], component: { [key: string]: any }) {
12
+ if (typeof key === "string") {
13
+ this.configs[key] = component;
14
+ } else if (Array.isArray(key)) {
15
+ for (let conf of key) {
16
+ if (!conf.key || !conf.component) {
17
+ continue;
18
+ }
19
+ this.configs[conf.key] = conf.component;
20
+ }
21
+ }
22
+ return this;
23
+ }
24
+
25
+ get(key: string) {
26
+ return lodash.get(this.configs, key);
27
+ }
28
+
29
+ set(path: string, value: any | null | undefined) {
30
+ const newConfig = { ...this.configs };
31
+ lodash.set(newConfig, path, value);
32
+ this.configs = newConfig;
33
+ return this;
34
+ }
35
+ }
36
+
37
+ export const Config = new ConfigFacade();
@@ -1,5 +1,5 @@
1
1
  // @ts-ignore
2
- import dbconfig from "../../../../../config/database";
2
+ import { Config } from "../Config";
3
3
  import { DBDrivers } from "./DBDrivers";
4
4
  import { QueryBuilder } from "./QueryBuilder";
5
5
 
@@ -7,6 +7,12 @@ class DBConnectionFacade {
7
7
  connections: { [key: string]: any } = {};
8
8
 
9
9
  getConnectionConfig(connection: string) {
10
+ const dbconfig = Config.get("database");
11
+
12
+ if (!dbconfig) {
13
+ throw new Error("Missing database-config ...");
14
+ }
15
+
10
16
  if (!connection) {
11
17
  connection = dbconfig.defaultConnection;
12
18
  }
@@ -36,6 +42,12 @@ class DBConnectionFacade {
36
42
  }
37
43
 
38
44
  async getConnection(connectionName?: string) {
45
+ const dbconfig = Config.get("database");
46
+
47
+ if (!dbconfig) {
48
+ throw new Error("Missing database-config ...");
49
+ }
50
+
39
51
  if (!connectionName) {
40
52
  connectionName = dbconfig.defaultConnection;
41
53
  }
@@ -10,10 +10,12 @@ export class QueryBuilder {
10
10
 
11
11
  setModelMapping(model?: any | null) {
12
12
  this.modelMapping = model;
13
+ return this;
13
14
  }
14
15
 
15
16
  table(table: string) {
16
17
  this.queryTable = table;
18
+ return this;
17
19
  }
18
20
 
19
21
  where(column: any, operator: any, value: any) {
@@ -23,6 +25,7 @@ export class QueryBuilder {
23
25
  operator: operator,
24
26
  value: value,
25
27
  });
28
+ return this;
26
29
  }
27
30
 
28
31
  async get(options?: any) {
@@ -1,8 +1,7 @@
1
1
  import qs from "qs";
2
2
  import { IncomingMessage, ServerResponse } from "http";
3
3
  import { Response } from "./Response";
4
- // @ts-ignore
5
- import { Kernel } from "../../../../../app/Kernel";
4
+ import { Config } from "../Config";
6
5
 
7
6
  type RouteObject = {
8
7
  path: string;
@@ -17,6 +16,11 @@ class RouterFacade {
17
16
  */
18
17
  basename: string | null = null;
19
18
 
19
+ /**
20
+ * Kernel-Class
21
+ */
22
+ kernel: any | null = null;
23
+
20
24
  /**
21
25
  * GET Route Store
22
26
  */
@@ -71,9 +75,12 @@ class RouterFacade {
71
75
  */
72
76
  init(options?: any) {
73
77
  this.basename = options && options.basename ? options.basename : null;
78
+ this.kernel = options && options.kernel ? options.kernel : null;
74
79
 
75
80
  // Load routes
81
+ console.log("loadRoutes");
76
82
  require(process.cwd() + "/app/routes");
83
+ console.log("finishRoutes");
77
84
  }
78
85
 
79
86
  /**
@@ -204,6 +211,10 @@ class RouterFacade {
204
211
  const response = new Response();
205
212
  response.setServerResponse(res);
206
213
 
214
+ if (!this.kernel) {
215
+ throw new Error("Missing Kernel in Router ...");
216
+ }
217
+
207
218
  // Check-Middleware
208
219
  if (
209
220
  route &&
@@ -212,7 +223,7 @@ class RouterFacade {
212
223
  route.options.middleware.length > 0
213
224
  ) {
214
225
  for (let middlewareKey of route.options.middleware) {
215
- const middleware = Kernel.middleware[middlewareKey];
226
+ const middleware = this.kernel.middleware[middlewareKey];
216
227
  if (!middleware) {
217
228
  return this.sendError(
218
229
  res,
@@ -273,7 +284,7 @@ class RouterFacade {
273
284
  // Resolve route-component (handle controller definition)
274
285
  if (route && typeof route.component === "string") {
275
286
  const split = route.component.split("@");
276
- const controller = Kernel.controller[split[0]];
287
+ const controller = this.kernel.controller[split[0]];
277
288
  if (!controller) {
278
289
  res.writeHead(500, { "Content-Type": "text/html" });
279
290
  res.end("Controller `" + split[0] + "` not found ...");
@@ -330,6 +341,12 @@ class RouterFacade {
330
341
  this.sendError(res, 404);
331
342
  }
332
343
  }
344
+
345
+ requestConsoleLog(req: IncomingMessage) {
346
+ if (Config.get("router.requests.logConsole") !== true) {
347
+ return;
348
+ }
349
+ }
333
350
  }
334
351
 
335
352
  export const Router = new RouterFacade();
@@ -1,4 +1,5 @@
1
1
  import http, { Server } from "http";
2
+ import { Config } from "./Config";
2
3
  import { Router } from "./Router/Router";
3
4
 
4
5
  export class WebApplication {
@@ -8,7 +9,16 @@ export class WebApplication {
8
9
  * Init the routes and start the http-server
9
10
  */
10
11
  boot(options?: any) {
11
- Router.init();
12
+ if (options && options.config) {
13
+ for (let key in options.config) {
14
+ Config.register(key, options.config[key]);
15
+ }
16
+ }
17
+
18
+ Router.init({
19
+ kernel: options && options.kernel ? options.kernel : null,
20
+ basename: options && options.basename ? options.basename : null,
21
+ });
12
22
 
13
23
  const port = options && options.port ? options.port : 3000;
14
24
  this.server = http
@@ -16,7 +26,13 @@ export class WebApplication {
16
26
  Router.handleRequest(req, res);
17
27
  })
18
28
  .listen(port, () => {
19
- console.log("Server started and listening on port " + port);
29
+ console.log(
30
+ "Server started and listening on port " +
31
+ port +
32
+ (options && options.basename
33
+ ? " (Basename: " + options && options.basename + ")"
34
+ : "")
35
+ );
20
36
  });
21
37
  }
22
38
  }
package/src/index.ts CHANGED
@@ -1,4 +1,6 @@
1
- export * from "./BaseKernel";
1
+ export * from "./Config";
2
+ export * from "./BaseKernelWeb";
3
+ export * from "./BaseKernelConsole";
2
4
  export * from "./Controller/Controller";
3
5
  export * from "./Database/Model";
4
6
  export * from "./WebApplication";