koatty 3.6.10 → 3.7.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.
@@ -4,7 +4,7 @@
4
4
  {
5
5
  "type": "node",
6
6
  "request": "launch",
7
- "name": "TS Program",
7
+ "name": "Koatty Test Program",
8
8
  "args": [
9
9
  "${workspaceRoot}/demo/src/App.ts"
10
10
  ],
package/CHANGELOG.md CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ## [3.7.0](https://github.com/thinkkoa/koatty/compare/v3.6.10...v3.7.0) (2022-11-01)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * ctx.body未赋值导致的404 ([ea0a248](https://github.com/thinkkoa/koatty/commit/ea0a248a71031b2d8a26b0b4280410dcdeb841af))
11
+
5
12
  ### [3.6.10](https://github.com/thinkkoa/koatty/compare/v3.6.10-1...v3.6.10) (2022-09-05)
6
13
 
7
14
 
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2022-09-05 18:42:36
3
+ * @Date: 2022-11-01 17:00:46
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
@@ -175,113 +175,6 @@ export declare function ExecBootStrap(bootFunc?: Function): (target: any) => Pro
175
175
 
176
176
  export { Helper }
177
177
 
178
- /**
179
- * HTTP controller
180
- *
181
- * @export
182
- * @class HttpController
183
- * @implements {IController}
184
- */
185
- export declare class HttpController extends BaseController {
186
- /**
187
- * Whether it is a GET request
188
- *
189
- * @public
190
- * @returns {boolean}
191
- * @memberof HttpController
192
- */
193
- isGet(): boolean;
194
- /**
195
- * Whether it is a POST request
196
- *
197
- * @public
198
- * @returns {boolean}
199
- * @memberof HttpController
200
- */
201
- isPost(): boolean;
202
- /**
203
- * Determines whether the METHOD request is specified
204
- *
205
- * @public
206
- * @param {string} method
207
- * @returns {boolean}
208
- * @memberof HttpController
209
- */
210
- isMethod(method: string): boolean;
211
- /**
212
- * Get/Set headers.
213
- *
214
- * @public
215
- * @param {string} [name]
216
- * @param {*} [value]
217
- * @returns {*}
218
- * @memberof HttpController
219
- */
220
- header(name?: string, value?: any): any;
221
- /**
222
- * Get POST/GET parameters, the POST value is priority.
223
- *
224
- * @param {string} [name]
225
- * @returns
226
- * @memberof HttpController
227
- */
228
- param(name?: string): Promise<any>;
229
- /**
230
- * Set response content-type
231
- *
232
- * @public
233
- * @param {string} contentType
234
- * @param {(string | boolean)} [encoding]
235
- * @returns {string}
236
- * @memberof HttpController
237
- */
238
- type(contentType: string, encoding?: string | boolean): string;
239
- /**
240
- * set cache-control and expires header
241
- *
242
- * @public
243
- * @param {number} [timeout=30]
244
- * @returns {void}
245
- * @memberof HttpController
246
- */
247
- expires(timeout?: number): void;
248
- /**
249
- * Url redirect
250
- *
251
- * @param {string} urls
252
- * @param {string} [alt]
253
- * @returns {void}
254
- * @memberof HttpController
255
- */
256
- redirect(urls: string, alt?: string): void;
257
- /**
258
- * Block access
259
- *
260
- * @param {number} [code=403]
261
- * @returns {Promise<any>}
262
- * @memberof HttpController
263
- */
264
- deny(code?: number): Promise<any>;
265
- /**
266
- * Set response Body content
267
- *
268
- * @param {*} data
269
- * @param {string} [contentType]
270
- * @param {string} [encoding]
271
- * @returns void
272
- * @memberof HttpController
273
- */
274
- body(data: any, contentType?: string, encoding?: string): void;
275
- /**
276
- * Respond to json formatted content
277
- *
278
- * @param {*} data
279
- * @returns {Promise<any>}
280
- * @memberof HttpController
281
- */
282
- json(data: any): void;
283
- }
284
-
285
178
  /**
286
179
  * Interface for Controller
287
180
  */
@@ -290,8 +183,6 @@ export declare interface IController {
290
183
  readonly ctx: KoattyContext;
291
184
  __befor?: () => Promise<any>;
292
185
  __after?: () => Promise<any>;
293
- readonly ok: (msg?: string | ApiInput, data?: any, ret?: number) => Promise<any>;
294
- readonly fail: (msg?: Error | string | ApiInput, data?: any, ret?: number) => void;
295
186
  }
296
187
 
297
188
  /**
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2022-09-05 18:42:21
3
+ * @Date: 2022-11-01 17:00:27
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
@@ -9,7 +9,6 @@
9
9
 
10
10
  Object.defineProperty(exports, '__esModule', { value: true });
11
11
 
12
- var koatty_lib = require('koatty_lib');
13
12
  require('reflect-metadata');
14
13
  var fs = require('fs');
15
14
  var koatty_core = require('koatty_core');
@@ -21,6 +20,7 @@ var koatty_loader = require('koatty_loader');
21
20
  var koatty_config = require('koatty_config');
22
21
  var koatty_logger = require('koatty_logger');
23
22
  var koatty_exception = require('koatty_exception');
23
+ var koatty_lib = require('koatty_lib');
24
24
  var koatty_trace = require('koatty_trace');
25
25
  var koatty_payload = require('koatty_payload');
26
26
 
@@ -47,6 +47,42 @@ function _interopNamespace(e) {
47
47
  var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
48
48
  var path__namespace = /*#__PURE__*/_interopNamespace(path);
49
49
 
50
+ /**
51
+ * @ author: richen
52
+ * @ copyright: Copyright (c) - <richenlin(at)gmail.com>
53
+ * @ license: BSD (3-Clause)
54
+ * @ version: 2020-05-10 11:49:15
55
+ */
56
+ // Logger
57
+ const Logger = koatty_logger.DefaultLogger;
58
+ /**
59
+ * SetLogger
60
+ *
61
+ * @export
62
+ * @param {{
63
+ * logLevel?: LogLevelType;
64
+ * logConsole?: boolean;
65
+ * logFile?: boolean;
66
+ * logFileLevel?: LogLevelType;
67
+ * logFilePath?: string;
68
+ * }} config
69
+ */
70
+ function SetLogger(app, config) {
71
+ if (config.logLevel !== undefined) {
72
+ koatty_logger.DefaultLogger.setLevel(config.logLevel);
73
+ }
74
+ if (config.logConsole !== undefined && !app.silent) {
75
+ koatty_logger.DefaultLogger.setLogConsole(config.logConsole);
76
+ }
77
+ if (config.logFile !== undefined && !app.silent) {
78
+ koatty_logger.DefaultLogger.setLogFile(config.logFile);
79
+ koatty_logger.DefaultLogger.setLogFilePath(config.logFilePath);
80
+ }
81
+ if (config.logFileLevel !== undefined) {
82
+ koatty_logger.DefaultLogger.setLogFileLevel(config.logFileLevel);
83
+ }
84
+ }
85
+
50
86
  /**
51
87
  * @ author: richen
52
88
  * @ copyright: Copyright (c) - <richenlin(at)gmail.com>
@@ -124,8 +160,22 @@ function formatApiData(msg, data, defaultCode) {
124
160
  * @ author: richen
125
161
  * @ copyright: Copyright (c) - <richenlin(at)gmail.com>
126
162
  * @ license: BSD (3-Clause)
127
- * @ version: 2020-05-20 15:45:24
163
+ * @ version: 2020-05-10 11:49:15
128
164
  */
165
+ const COMPONENT_SCAN = 'COMPONENT_SCAN';
166
+ const CONFIGURATION_SCAN = 'CONFIGURATION_SCAN';
167
+ const APP_READY_HOOK = "APP_READY_HOOK";
168
+ // tslint:disable: no-irregular-whitespace
169
+ const LOGO = `
170
+
171
+ ██  ██  ██████  █████  ████████ ████████ ██  ██ 
172
+ ██  ██  ██    ██ ██   ██    ██       ██     ██  ██  
173
+ █████   ██  ██ ███████  ██  ██   ████  
174
+ ██  ██  ██  ██ ██   ██  ██  ██   ██  
175
+ ██  ██  ██████  ██  ██  ██  ██  ██ 
176
+ https://github.com/koatty
177
+ `;
178
+
129
179
  /**
130
180
  * Base controller
131
181
  *
@@ -152,25 +202,6 @@ class BaseController {
152
202
  */
153
203
  init(...arg) {
154
204
  }
155
- // /**
156
- // * Class pre-execution method (except constructor, init, __after).
157
- // *
158
- // * @returns {Promise<any>}
159
- // * @memberof BaseController
160
- // */
161
- // public __before(): Promise<any> {
162
- // return Promise.resolve();
163
- // }
164
- // /**
165
- // * Class after-execution method (except constructor, init, __before).
166
- // *
167
- // * @public
168
- // * @returns {*}
169
- // * @memberof BaseController
170
- // */
171
- // public __after(): Promise<any> {
172
- // return Promise.resolve();
173
- // }
174
205
  /**
175
206
  * Response to normalize json format content for success
176
207
  *
@@ -223,244 +254,6 @@ class BaseController {
223
254
  // }
224
255
  // });
225
256
 
226
- /**
227
- * @ author: richen
228
- * @ copyright: Copyright (c) - <richenlin(at)gmail.com>
229
- * @ license: BSD (3-Clause)
230
- * @ version: 2020-05-20 15:45:24
231
- */
232
- /**
233
- * HTTP controller
234
- *
235
- * @export
236
- * @class HttpController
237
- * @implements {IController}
238
- */
239
- class HttpController extends BaseController {
240
- /**
241
- * Whether it is a GET request
242
- *
243
- * @public
244
- * @returns {boolean}
245
- * @memberof HttpController
246
- */
247
- isGet() {
248
- return this.ctx.method === "GET";
249
- }
250
- /**
251
- * Whether it is a POST request
252
- *
253
- * @public
254
- * @returns {boolean}
255
- * @memberof HttpController
256
- */
257
- isPost() {
258
- return this.ctx.method === "POST";
259
- }
260
- /**
261
- * Determines whether the METHOD request is specified
262
- *
263
- * @public
264
- * @param {string} method
265
- * @returns {boolean}
266
- * @memberof HttpController
267
- */
268
- isMethod(method) {
269
- return this.ctx.method === method.toUpperCase();
270
- }
271
- /**
272
- * Get/Set headers.
273
- *
274
- * @public
275
- * @param {string} [name]
276
- * @param {*} [value]
277
- * @returns {*}
278
- * @memberof HttpController
279
- */
280
- header(name, value) {
281
- if (name === undefined) {
282
- return this.ctx.headers;
283
- }
284
- if (value === undefined) {
285
- return this.ctx.get(name);
286
- }
287
- return this.ctx.set(name, value);
288
- }
289
- /**
290
- * Get POST/GET parameters, the POST value is priority.
291
- *
292
- * @param {string} [name]
293
- * @returns
294
- * @memberof HttpController
295
- */
296
- param(name) {
297
- return this.ctx.bodyParser().then((body) => {
298
- const getParams = this.ctx.queryParser() || {};
299
- const postParams = (body.post ? body.post : body) || {};
300
- if (name !== undefined) {
301
- return postParams[name] === undefined ? getParams[name] : postParams[name];
302
- }
303
- return { ...getParams, ...postParams };
304
- });
305
- }
306
- /**
307
- * Set response content-type
308
- *
309
- * @public
310
- * @param {string} contentType
311
- * @param {(string | boolean)} [encoding]
312
- * @returns {string}
313
- * @memberof HttpController
314
- */
315
- type(contentType, encoding) {
316
- if (encoding !== false && !contentType.includes("charset")) {
317
- contentType = `${contentType}; charset=${encoding || this.app.config("encoding")}`;
318
- }
319
- this.ctx.type = contentType;
320
- return contentType;
321
- }
322
- /**
323
- * set cache-control and expires header
324
- *
325
- * @public
326
- * @param {number} [timeout=30]
327
- * @returns {void}
328
- * @memberof HttpController
329
- */
330
- expires(timeout = 30) {
331
- timeout = koatty_lib.Helper.toNumber(timeout) * 1000;
332
- const date = new Date(Date.now() + timeout);
333
- this.ctx.set("Cache-Control", `max-age=${timeout}`);
334
- return this.ctx.set("Expires", date.toUTCString());
335
- }
336
- /**
337
- * Url redirect
338
- *
339
- * @param {string} urls
340
- * @param {string} [alt]
341
- * @returns {void}
342
- * @memberof HttpController
343
- */
344
- redirect(urls, alt) {
345
- return this.ctx.redirect(urls, alt);
346
- }
347
- /**
348
- * Block access
349
- *
350
- * @param {number} [code=403]
351
- * @returns {Promise<any>}
352
- * @memberof HttpController
353
- */
354
- deny(code = 403) {
355
- return this.ctx.throw(code);
356
- }
357
- /**
358
- * Set response Body content
359
- *
360
- * @param {*} data
361
- * @param {string} [contentType]
362
- * @param {string} [encoding]
363
- * @returns void
364
- * @memberof HttpController
365
- */
366
- body(data, contentType, encoding) {
367
- contentType = contentType || "text/plain";
368
- encoding = encoding || this.app.config("encoding") || "utf-8";
369
- this.type(contentType, encoding);
370
- this.ctx.body = data;
371
- return null;
372
- }
373
- /**
374
- * Respond to json formatted content
375
- *
376
- * @param {*} data
377
- * @returns {Promise<any>}
378
- * @memberof HttpController
379
- */
380
- json(data) {
381
- return this.body(data, "application/json");
382
- }
383
- }
384
- // const properties = ["constructor", "init"];
385
- // export const HttpController = new Proxy(Base, {
386
- // set(target, key, value, receiver) {
387
- // if (Reflect.get(target, key, receiver) === undefined) {
388
- // return Reflect.set(target, key, value, receiver);
389
- // } else if (key === "init") {
390
- // return Reflect.set(target, key, value, receiver);
391
- // } else {
392
- // throw Error("Cannot redefine getter-only property");
393
- // }
394
- // },
395
- // deleteProperty(target, key) {
396
- // throw Error("Cannot delete getter-only property");
397
- // },
398
- // construct(target, args, newTarget) {
399
- // Reflect.ownKeys(target.prototype).map((n) => {
400
- // if (newTarget.prototype.hasOwnProperty(n) && !properties.includes(Helper.toString(n))) {
401
- // throw Error(`Cannot override the final method "${Helper.toString(n)}"`);
402
- // }
403
- // });
404
- // return Reflect.construct(target, args, newTarget);
405
- // }
406
- // });
407
-
408
- /**
409
- * @ author: richen
410
- * @ copyright: Copyright (c) - <richenlin(at)gmail.com>
411
- * @ license: BSD (3-Clause)
412
- * @ version: 2020-05-10 11:49:15
413
- */
414
- // Logger
415
- const Logger = koatty_logger.DefaultLogger;
416
- /**
417
- * SetLogger
418
- *
419
- * @export
420
- * @param {{
421
- * logLevel?: LogLevelType;
422
- * logConsole?: boolean;
423
- * logFile?: boolean;
424
- * logFileLevel?: LogLevelType;
425
- * logFilePath?: string;
426
- * }} config
427
- */
428
- function SetLogger(app, config) {
429
- if (config.logLevel !== undefined) {
430
- koatty_logger.DefaultLogger.setLevel(config.logLevel);
431
- }
432
- if (config.logConsole !== undefined && !app.silent) {
433
- koatty_logger.DefaultLogger.setLogConsole(config.logConsole);
434
- }
435
- if (config.logFile !== undefined && !app.silent) {
436
- koatty_logger.DefaultLogger.setLogFile(config.logFile);
437
- koatty_logger.DefaultLogger.setLogFilePath(config.logFilePath);
438
- }
439
- if (config.logFileLevel !== undefined) {
440
- koatty_logger.DefaultLogger.setLogFileLevel(config.logFileLevel);
441
- }
442
- }
443
-
444
- /**
445
- * @ author: richen
446
- * @ copyright: Copyright (c) - <richenlin(at)gmail.com>
447
- * @ license: BSD (3-Clause)
448
- * @ version: 2020-05-10 11:49:15
449
- */
450
- const COMPONENT_SCAN = 'COMPONENT_SCAN';
451
- const CONFIGURATION_SCAN = 'CONFIGURATION_SCAN';
452
- const APP_READY_HOOK = "APP_READY_HOOK";
453
- // tslint:disable: no-irregular-whitespace
454
- const LOGO = `
455
-
456
- ██  ██  ██████  █████  ████████ ████████ ██  ██ 
457
- ██  ██  ██    ██ ██   ██    ██       ██     ██  ██  
458
- █████   ██  ██ ███████  ██  ██   ████  
459
- ██  ██  ██  ██ ██   ██  ██  ██   ██  
460
- ██  ██  ██████  ██  ██  ██  ██  ██ 
461
- https://github.com/koatty
462
- `;
463
-
464
257
  class TraceMiddleware {
465
258
  run(options, app) {
466
259
  return koatty_trace.Trace(options, app);
@@ -838,7 +631,7 @@ class Loader {
838
631
  }
839
632
  }
840
633
 
841
- var version = "3.6.10";
634
+ var version = "3.7.0";
842
635
  var engines = {
843
636
  node: ">12.0.0"
844
637
  };
@@ -947,12 +740,6 @@ const executeBootstrap = async function (target, bootFunc, isInitiative = false)
947
740
  await asyncEvent(app, 'appBoot');
948
741
  // Load App ready hooks
949
742
  Loader.LoadAppReadyHooks(app, target);
950
- // New router
951
- const KoattyRouter = newRouter(app);
952
- koatty_lib.Helper.define(app, "router", KoattyRouter);
953
- // Create Server
954
- app.server = newServe(app);
955
- koatty_lib.Helper.define(app, "listenCallback", listenCallback(app));
956
743
  // Load Middleware
957
744
  Logger.Log('think', '', 'Load Middlewares ...');
958
745
  await Loader.LoadMiddlewares(app);
@@ -965,14 +752,18 @@ const executeBootstrap = async function (target, bootFunc, isInitiative = false)
965
752
  // Load Controllers
966
753
  Logger.Log('think', '', 'Load Controllers ...');
967
754
  const controllers = Loader.LoadControllers(app);
755
+ // Create Server
756
+ app.server = newServe(app);
757
+ // Create router
758
+ app.router = newRouter(app);
968
759
  // Load Routers
969
760
  Logger.Log('think', '', 'Load Routers ...');
970
- KoattyRouter.LoadRouter(controllers);
761
+ app.router.LoadRouter(controllers);
971
762
  // Emit app ready event
972
763
  Logger.Log('think', '', 'Emit App Ready ...');
973
764
  await asyncEvent(app, 'appReady');
974
765
  if (!isUTRuntime) {
975
- app.listen();
766
+ app.listen(app.server, listenCallback(app));
976
767
  }
977
768
  return app;
978
769
  }
@@ -1001,8 +792,8 @@ const newRouter = function (app) {
1001
792
  * @returns {*}
1002
793
  */
1003
794
  const listenCallback = (app) => {
1004
- const options = app.server.options;
1005
795
  return function () {
796
+ const options = app.server.options;
1006
797
  // Emit app started event
1007
798
  Logger.Log('think', '', 'Emit App Start ...');
1008
799
  asyncEvent(app, 'appStart');
@@ -1033,9 +824,7 @@ const newServe = function (app) {
1033
824
  const hostname = process.env.IP ||
1034
825
  process.env.HOSTNAME?.replace(/-/g, '.') || app.config('app_host') || '127.0.0.1';
1035
826
  const options = {
1036
- hostname,
1037
- port,
1038
- protocol,
827
+ hostname, port, protocol,
1039
828
  ext: {
1040
829
  key: "",
1041
830
  cert: "",
@@ -1056,7 +845,11 @@ const newServe = function (app) {
1056
845
  const proto = app.config("protoFile", "router");
1057
846
  options.ext.protoFile = proto;
1058
847
  }
1059
- return koatty_serve.Serve(app, options);
848
+ const server = koatty_serve.Serve(app, options);
849
+ process.on('SIGINT', () => {
850
+ server.Stop();
851
+ });
852
+ return server;
1060
853
  };
1061
854
  /**
1062
855
  * Execute event as async
@@ -1259,10 +1052,6 @@ class BaseService {
1259
1052
  }
1260
1053
  }
1261
1054
 
1262
- Object.defineProperty(exports, 'Helper', {
1263
- enumerable: true,
1264
- get: function () { return koatty_lib.Helper; }
1265
- });
1266
1055
  Object.defineProperty(exports, 'Config', {
1267
1056
  enumerable: true,
1268
1057
  get: function () { return koatty_config.Config; }
@@ -1271,6 +1060,10 @@ Object.defineProperty(exports, 'Value', {
1271
1060
  enumerable: true,
1272
1061
  get: function () { return koatty_config.Value; }
1273
1062
  });
1063
+ Object.defineProperty(exports, 'Helper', {
1064
+ enumerable: true,
1065
+ get: function () { return koatty_lib.Helper; }
1066
+ });
1274
1067
  exports.BaseController = BaseController;
1275
1068
  exports.BaseService = BaseService;
1276
1069
  exports.BindAppReadyHook = BindAppReadyHook;
@@ -1280,7 +1073,6 @@ exports.ComponentScan = ComponentScan;
1280
1073
  exports.ConfigurationScan = ConfigurationScan;
1281
1074
  exports.Controller = Controller;
1282
1075
  exports.ExecBootStrap = ExecBootStrap;
1283
- exports.HttpController = HttpController;
1284
1076
  exports.Logger = Logger;
1285
1077
  exports.Middleware = Middleware;
1286
1078
  exports.Plugin = Plugin;
package/dist/index.mjs CHANGED
@@ -1,12 +1,10 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2022-09-05 18:42:21
3
+ * @Date: 2022-11-01 17:00:27
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
7
7
  */
8
- import { Helper } from 'koatty_lib';
9
- export { Helper } from 'koatty_lib';
10
8
  import 'reflect-metadata';
11
9
  import fs from 'fs';
12
10
  import { Koatty } from 'koatty_core';
@@ -15,7 +13,7 @@ import { NewRouter, CONTROLLER_ROUTER } from 'koatty_router';
15
13
  export * from 'koatty_router';
16
14
  import { IOCContainer, TAGGED_CLS } from 'koatty_container';
17
15
  export * from 'koatty_container';
18
- import { Serve, BindProcessEvent } from 'koatty_serve';
16
+ import { BindProcessEvent, Serve } from 'koatty_serve';
19
17
  export * from 'koatty_serve';
20
18
  import * as path from 'path';
21
19
  import { Load } from 'koatty_loader';
@@ -24,9 +22,47 @@ export { Config, Value } from 'koatty_config';
24
22
  import { DefaultLogger } from 'koatty_logger';
25
23
  import { prevent } from 'koatty_exception';
26
24
  export * from 'koatty_exception';
25
+ import { Helper } from 'koatty_lib';
26
+ export { Helper } from 'koatty_lib';
27
27
  import { Trace } from 'koatty_trace';
28
28
  import { Payload } from 'koatty_payload';
29
29
 
30
+ /**
31
+ * @ author: richen
32
+ * @ copyright: Copyright (c) - <richenlin(at)gmail.com>
33
+ * @ license: BSD (3-Clause)
34
+ * @ version: 2020-05-10 11:49:15
35
+ */
36
+ // Logger
37
+ const Logger = DefaultLogger;
38
+ /**
39
+ * SetLogger
40
+ *
41
+ * @export
42
+ * @param {{
43
+ * logLevel?: LogLevelType;
44
+ * logConsole?: boolean;
45
+ * logFile?: boolean;
46
+ * logFileLevel?: LogLevelType;
47
+ * logFilePath?: string;
48
+ * }} config
49
+ */
50
+ function SetLogger(app, config) {
51
+ if (config.logLevel !== undefined) {
52
+ DefaultLogger.setLevel(config.logLevel);
53
+ }
54
+ if (config.logConsole !== undefined && !app.silent) {
55
+ DefaultLogger.setLogConsole(config.logConsole);
56
+ }
57
+ if (config.logFile !== undefined && !app.silent) {
58
+ DefaultLogger.setLogFile(config.logFile);
59
+ DefaultLogger.setLogFilePath(config.logFilePath);
60
+ }
61
+ if (config.logFileLevel !== undefined) {
62
+ DefaultLogger.setLogFileLevel(config.logFileLevel);
63
+ }
64
+ }
65
+
30
66
  /**
31
67
  * @ author: richen
32
68
  * @ copyright: Copyright (c) - <richenlin(at)gmail.com>
@@ -104,8 +140,22 @@ function formatApiData(msg, data, defaultCode) {
104
140
  * @ author: richen
105
141
  * @ copyright: Copyright (c) - <richenlin(at)gmail.com>
106
142
  * @ license: BSD (3-Clause)
107
- * @ version: 2020-05-20 15:45:24
143
+ * @ version: 2020-05-10 11:49:15
108
144
  */
145
+ const COMPONENT_SCAN = 'COMPONENT_SCAN';
146
+ const CONFIGURATION_SCAN = 'CONFIGURATION_SCAN';
147
+ const APP_READY_HOOK = "APP_READY_HOOK";
148
+ // tslint:disable: no-irregular-whitespace
149
+ const LOGO = `
150
+
151
+ ██  ██  ██████  █████  ████████ ████████ ██  ██ 
152
+ ██  ██  ██    ██ ██   ██    ██       ██     ██  ██  
153
+ █████   ██  ██ ███████  ██  ██   ████  
154
+ ██  ██  ██  ██ ██   ██  ██  ██   ██  
155
+ ██  ██  ██████  ██  ██  ██  ██  ██ 
156
+ https://github.com/koatty
157
+ `;
158
+
109
159
  /**
110
160
  * Base controller
111
161
  *
@@ -132,25 +182,6 @@ class BaseController {
132
182
  */
133
183
  init(...arg) {
134
184
  }
135
- // /**
136
- // * Class pre-execution method (except constructor, init, __after).
137
- // *
138
- // * @returns {Promise<any>}
139
- // * @memberof BaseController
140
- // */
141
- // public __before(): Promise<any> {
142
- // return Promise.resolve();
143
- // }
144
- // /**
145
- // * Class after-execution method (except constructor, init, __before).
146
- // *
147
- // * @public
148
- // * @returns {*}
149
- // * @memberof BaseController
150
- // */
151
- // public __after(): Promise<any> {
152
- // return Promise.resolve();
153
- // }
154
185
  /**
155
186
  * Response to normalize json format content for success
156
187
  *
@@ -203,244 +234,6 @@ class BaseController {
203
234
  // }
204
235
  // });
205
236
 
206
- /**
207
- * @ author: richen
208
- * @ copyright: Copyright (c) - <richenlin(at)gmail.com>
209
- * @ license: BSD (3-Clause)
210
- * @ version: 2020-05-20 15:45:24
211
- */
212
- /**
213
- * HTTP controller
214
- *
215
- * @export
216
- * @class HttpController
217
- * @implements {IController}
218
- */
219
- class HttpController extends BaseController {
220
- /**
221
- * Whether it is a GET request
222
- *
223
- * @public
224
- * @returns {boolean}
225
- * @memberof HttpController
226
- */
227
- isGet() {
228
- return this.ctx.method === "GET";
229
- }
230
- /**
231
- * Whether it is a POST request
232
- *
233
- * @public
234
- * @returns {boolean}
235
- * @memberof HttpController
236
- */
237
- isPost() {
238
- return this.ctx.method === "POST";
239
- }
240
- /**
241
- * Determines whether the METHOD request is specified
242
- *
243
- * @public
244
- * @param {string} method
245
- * @returns {boolean}
246
- * @memberof HttpController
247
- */
248
- isMethod(method) {
249
- return this.ctx.method === method.toUpperCase();
250
- }
251
- /**
252
- * Get/Set headers.
253
- *
254
- * @public
255
- * @param {string} [name]
256
- * @param {*} [value]
257
- * @returns {*}
258
- * @memberof HttpController
259
- */
260
- header(name, value) {
261
- if (name === undefined) {
262
- return this.ctx.headers;
263
- }
264
- if (value === undefined) {
265
- return this.ctx.get(name);
266
- }
267
- return this.ctx.set(name, value);
268
- }
269
- /**
270
- * Get POST/GET parameters, the POST value is priority.
271
- *
272
- * @param {string} [name]
273
- * @returns
274
- * @memberof HttpController
275
- */
276
- param(name) {
277
- return this.ctx.bodyParser().then((body) => {
278
- const getParams = this.ctx.queryParser() || {};
279
- const postParams = (body.post ? body.post : body) || {};
280
- if (name !== undefined) {
281
- return postParams[name] === undefined ? getParams[name] : postParams[name];
282
- }
283
- return { ...getParams, ...postParams };
284
- });
285
- }
286
- /**
287
- * Set response content-type
288
- *
289
- * @public
290
- * @param {string} contentType
291
- * @param {(string | boolean)} [encoding]
292
- * @returns {string}
293
- * @memberof HttpController
294
- */
295
- type(contentType, encoding) {
296
- if (encoding !== false && !contentType.includes("charset")) {
297
- contentType = `${contentType}; charset=${encoding || this.app.config("encoding")}`;
298
- }
299
- this.ctx.type = contentType;
300
- return contentType;
301
- }
302
- /**
303
- * set cache-control and expires header
304
- *
305
- * @public
306
- * @param {number} [timeout=30]
307
- * @returns {void}
308
- * @memberof HttpController
309
- */
310
- expires(timeout = 30) {
311
- timeout = Helper.toNumber(timeout) * 1000;
312
- const date = new Date(Date.now() + timeout);
313
- this.ctx.set("Cache-Control", `max-age=${timeout}`);
314
- return this.ctx.set("Expires", date.toUTCString());
315
- }
316
- /**
317
- * Url redirect
318
- *
319
- * @param {string} urls
320
- * @param {string} [alt]
321
- * @returns {void}
322
- * @memberof HttpController
323
- */
324
- redirect(urls, alt) {
325
- return this.ctx.redirect(urls, alt);
326
- }
327
- /**
328
- * Block access
329
- *
330
- * @param {number} [code=403]
331
- * @returns {Promise<any>}
332
- * @memberof HttpController
333
- */
334
- deny(code = 403) {
335
- return this.ctx.throw(code);
336
- }
337
- /**
338
- * Set response Body content
339
- *
340
- * @param {*} data
341
- * @param {string} [contentType]
342
- * @param {string} [encoding]
343
- * @returns void
344
- * @memberof HttpController
345
- */
346
- body(data, contentType, encoding) {
347
- contentType = contentType || "text/plain";
348
- encoding = encoding || this.app.config("encoding") || "utf-8";
349
- this.type(contentType, encoding);
350
- this.ctx.body = data;
351
- return null;
352
- }
353
- /**
354
- * Respond to json formatted content
355
- *
356
- * @param {*} data
357
- * @returns {Promise<any>}
358
- * @memberof HttpController
359
- */
360
- json(data) {
361
- return this.body(data, "application/json");
362
- }
363
- }
364
- // const properties = ["constructor", "init"];
365
- // export const HttpController = new Proxy(Base, {
366
- // set(target, key, value, receiver) {
367
- // if (Reflect.get(target, key, receiver) === undefined) {
368
- // return Reflect.set(target, key, value, receiver);
369
- // } else if (key === "init") {
370
- // return Reflect.set(target, key, value, receiver);
371
- // } else {
372
- // throw Error("Cannot redefine getter-only property");
373
- // }
374
- // },
375
- // deleteProperty(target, key) {
376
- // throw Error("Cannot delete getter-only property");
377
- // },
378
- // construct(target, args, newTarget) {
379
- // Reflect.ownKeys(target.prototype).map((n) => {
380
- // if (newTarget.prototype.hasOwnProperty(n) && !properties.includes(Helper.toString(n))) {
381
- // throw Error(`Cannot override the final method "${Helper.toString(n)}"`);
382
- // }
383
- // });
384
- // return Reflect.construct(target, args, newTarget);
385
- // }
386
- // });
387
-
388
- /**
389
- * @ author: richen
390
- * @ copyright: Copyright (c) - <richenlin(at)gmail.com>
391
- * @ license: BSD (3-Clause)
392
- * @ version: 2020-05-10 11:49:15
393
- */
394
- // Logger
395
- const Logger = DefaultLogger;
396
- /**
397
- * SetLogger
398
- *
399
- * @export
400
- * @param {{
401
- * logLevel?: LogLevelType;
402
- * logConsole?: boolean;
403
- * logFile?: boolean;
404
- * logFileLevel?: LogLevelType;
405
- * logFilePath?: string;
406
- * }} config
407
- */
408
- function SetLogger(app, config) {
409
- if (config.logLevel !== undefined) {
410
- DefaultLogger.setLevel(config.logLevel);
411
- }
412
- if (config.logConsole !== undefined && !app.silent) {
413
- DefaultLogger.setLogConsole(config.logConsole);
414
- }
415
- if (config.logFile !== undefined && !app.silent) {
416
- DefaultLogger.setLogFile(config.logFile);
417
- DefaultLogger.setLogFilePath(config.logFilePath);
418
- }
419
- if (config.logFileLevel !== undefined) {
420
- DefaultLogger.setLogFileLevel(config.logFileLevel);
421
- }
422
- }
423
-
424
- /**
425
- * @ author: richen
426
- * @ copyright: Copyright (c) - <richenlin(at)gmail.com>
427
- * @ license: BSD (3-Clause)
428
- * @ version: 2020-05-10 11:49:15
429
- */
430
- const COMPONENT_SCAN = 'COMPONENT_SCAN';
431
- const CONFIGURATION_SCAN = 'CONFIGURATION_SCAN';
432
- const APP_READY_HOOK = "APP_READY_HOOK";
433
- // tslint:disable: no-irregular-whitespace
434
- const LOGO = `
435
-
436
- ██  ██  ██████  █████  ████████ ████████ ██  ██ 
437
- ██  ██  ██    ██ ██   ██    ██       ██     ██  ██  
438
- █████   ██  ██ ███████  ██  ██   ████  
439
- ██  ██  ██  ██ ██   ██  ██  ██   ██  
440
- ██  ██  ██████  ██  ██  ██  ██  ██ 
441
- https://github.com/koatty
442
- `;
443
-
444
237
  class TraceMiddleware {
445
238
  run(options, app) {
446
239
  return Trace(options, app);
@@ -818,7 +611,7 @@ class Loader {
818
611
  }
819
612
  }
820
613
 
821
- var version = "3.6.10";
614
+ var version = "3.7.0";
822
615
  var engines = {
823
616
  node: ">12.0.0"
824
617
  };
@@ -927,12 +720,6 @@ const executeBootstrap = async function (target, bootFunc, isInitiative = false)
927
720
  await asyncEvent(app, 'appBoot');
928
721
  // Load App ready hooks
929
722
  Loader.LoadAppReadyHooks(app, target);
930
- // New router
931
- const KoattyRouter = newRouter(app);
932
- Helper.define(app, "router", KoattyRouter);
933
- // Create Server
934
- app.server = newServe(app);
935
- Helper.define(app, "listenCallback", listenCallback(app));
936
723
  // Load Middleware
937
724
  Logger.Log('think', '', 'Load Middlewares ...');
938
725
  await Loader.LoadMiddlewares(app);
@@ -945,14 +732,18 @@ const executeBootstrap = async function (target, bootFunc, isInitiative = false)
945
732
  // Load Controllers
946
733
  Logger.Log('think', '', 'Load Controllers ...');
947
734
  const controllers = Loader.LoadControllers(app);
735
+ // Create Server
736
+ app.server = newServe(app);
737
+ // Create router
738
+ app.router = newRouter(app);
948
739
  // Load Routers
949
740
  Logger.Log('think', '', 'Load Routers ...');
950
- KoattyRouter.LoadRouter(controllers);
741
+ app.router.LoadRouter(controllers);
951
742
  // Emit app ready event
952
743
  Logger.Log('think', '', 'Emit App Ready ...');
953
744
  await asyncEvent(app, 'appReady');
954
745
  if (!isUTRuntime) {
955
- app.listen();
746
+ app.listen(app.server, listenCallback(app));
956
747
  }
957
748
  return app;
958
749
  }
@@ -981,8 +772,8 @@ const newRouter = function (app) {
981
772
  * @returns {*}
982
773
  */
983
774
  const listenCallback = (app) => {
984
- const options = app.server.options;
985
775
  return function () {
776
+ const options = app.server.options;
986
777
  // Emit app started event
987
778
  Logger.Log('think', '', 'Emit App Start ...');
988
779
  asyncEvent(app, 'appStart');
@@ -1013,9 +804,7 @@ const newServe = function (app) {
1013
804
  const hostname = process.env.IP ||
1014
805
  process.env.HOSTNAME?.replace(/-/g, '.') || app.config('app_host') || '127.0.0.1';
1015
806
  const options = {
1016
- hostname,
1017
- port,
1018
- protocol,
807
+ hostname, port, protocol,
1019
808
  ext: {
1020
809
  key: "",
1021
810
  cert: "",
@@ -1036,7 +825,11 @@ const newServe = function (app) {
1036
825
  const proto = app.config("protoFile", "router");
1037
826
  options.ext.protoFile = proto;
1038
827
  }
1039
- return Serve(app, options);
828
+ const server = Serve(app, options);
829
+ process.on('SIGINT', () => {
830
+ server.Stop();
831
+ });
832
+ return server;
1040
833
  };
1041
834
  /**
1042
835
  * Execute event as async
@@ -1239,4 +1032,4 @@ class BaseService {
1239
1032
  }
1240
1033
  }
1241
1034
 
1242
- export { BaseController, BaseService, BindAppReadyHook, Bootstrap, Component, ComponentScan, ConfigurationScan, Controller, ExecBootStrap, HttpController, Logger, Middleware, Plugin, Service };
1035
+ export { BaseController, BaseService, BindAppReadyHook, Bootstrap, Component, ComponentScan, ConfigurationScan, Controller, ExecBootStrap, Logger, Middleware, Plugin, Service };
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koatty",
3
- "version": "3.6.10",
3
+ "version": "3.7.0",
4
4
  "description": "Koa2 + Typescript = koatty. Use Typescript's decorator implement auto injection.",
5
5
  "scripts": {
6
6
  "build": "npm run build:js && npm run build:dts && npm run build:doc && npm run build:cp",
@@ -58,9 +58,9 @@
58
58
  "@rollup/plugin-json": "^4.x.x",
59
59
  "@types/jest": "^27.x.x",
60
60
  "@types/koa": "^2.x.x",
61
- "@types/koa__router": "^8.0.11",
61
+ "@types/koa__router": "^8.x.x",
62
62
  "@types/node": "^16.x.x",
63
- "@types/ws": "^8.5.3",
63
+ "@types/ws": "^8.x.x",
64
64
  "@typescript-eslint/eslint-plugin": "^5.x.x",
65
65
  "@typescript-eslint/parser": "^5.x.x",
66
66
  "conventional-changelog-cli": "^2.x.x",
@@ -71,6 +71,7 @@
71
71
  "husky": "^4.x.x",
72
72
  "jest": "^28.x.x",
73
73
  "jest-html-reporters": "^3.x.x",
74
+ "koa-session": "^6.2.0",
74
75
  "rollup": "^2.x.x",
75
76
  "rollup-plugin-typescript2": "^0.x.x",
76
77
  "standard-version": "^9.x.x",
@@ -82,16 +83,16 @@
82
83
  "koa": "2.13.4",
83
84
  "koatty_config": "1.1.5",
84
85
  "koatty_container": "1.7.10",
85
- "koatty_core": "1.6.8",
86
+ "koatty_core": "1.6.9",
86
87
  "koatty_exception": "1.2.7",
87
88
  "koatty_lib": "1.2.12",
88
89
  "koatty_loader": "1.0.3",
89
90
  "koatty_logger": "1.3.14",
90
91
  "koatty_payload": "1.3.18",
91
- "koatty_proto": "1.1.8",
92
- "koatty_router": "1.7.9",
93
- "koatty_serve": "1.4.11",
94
- "koatty_trace": "1.6.9",
92
+ "koatty_proto": "^1.1.9",
93
+ "koatty_router": "1.7.12",
94
+ "koatty_serve": "1.4.12",
95
+ "koatty_trace": "1.6.10",
95
96
  "koatty_validation": "1.2.5",
96
97
  "reflect-metadata": "0.1.13",
97
98
  "tslib": "2.4.0"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koatty",
3
- "version": "3.6.10",
3
+ "version": "3.7.0",
4
4
  "description": "Koa2 + Typescript = koatty. Use Typescript's decorator implement auto injection.",
5
5
  "scripts": {
6
6
  "build": "npm run build:js && npm run build:dts && npm run build:doc && npm run build:cp",
@@ -58,9 +58,9 @@
58
58
  "@rollup/plugin-json": "^4.x.x",
59
59
  "@types/jest": "^27.x.x",
60
60
  "@types/koa": "^2.x.x",
61
- "@types/koa__router": "^8.0.11",
61
+ "@types/koa__router": "^8.x.x",
62
62
  "@types/node": "^16.x.x",
63
- "@types/ws": "^8.5.3",
63
+ "@types/ws": "^8.x.x",
64
64
  "@typescript-eslint/eslint-plugin": "^5.x.x",
65
65
  "@typescript-eslint/parser": "^5.x.x",
66
66
  "conventional-changelog-cli": "^2.x.x",
@@ -71,6 +71,7 @@
71
71
  "husky": "^4.x.x",
72
72
  "jest": "^28.x.x",
73
73
  "jest-html-reporters": "^3.x.x",
74
+ "koa-session": "^6.2.0",
74
75
  "rollup": "^2.x.x",
75
76
  "rollup-plugin-typescript2": "^0.x.x",
76
77
  "standard-version": "^9.x.x",
@@ -82,16 +83,16 @@
82
83
  "koa": "2.13.4",
83
84
  "koatty_config": "1.1.5",
84
85
  "koatty_container": "1.7.10",
85
- "koatty_core": "1.6.8",
86
+ "koatty_core": "1.6.9",
86
87
  "koatty_exception": "1.2.7",
87
88
  "koatty_lib": "1.2.12",
88
89
  "koatty_loader": "1.0.3",
89
90
  "koatty_logger": "1.3.14",
90
91
  "koatty_payload": "1.3.18",
91
- "koatty_proto": "1.1.8",
92
- "koatty_router": "1.7.9",
93
- "koatty_serve": "1.4.11",
94
- "koatty_trace": "1.6.9",
92
+ "koatty_proto": "^1.1.9",
93
+ "koatty_router": "1.7.12",
94
+ "koatty_serve": "1.4.12",
95
+ "koatty_trace": "1.6.10",
95
96
  "koatty_validation": "1.2.5",
96
97
  "reflect-metadata": "0.1.13",
97
98
  "tslib": "2.4.0"