@eggjs/core 6.2.0 → 6.2.2

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/src/egg.ts CHANGED
@@ -1,8 +1,15 @@
1
1
  /* eslint-disable prefer-spread */
2
2
  import assert from 'node:assert';
3
3
  import { debuglog } from 'node:util';
4
- import { Application as KoaApplication } from '@eggjs/koa';
5
- import type { ContextDelegation, MiddlewareFunc } from '@eggjs/koa';
4
+ import {
5
+ Application as KoaApplication, Context as KoaContext,
6
+ Request as KoaRequest, Response as KoaResponse,
7
+ } from '@eggjs/koa';
8
+ import type {
9
+ ContextDelegation as KoaContextDelegation,
10
+ MiddlewareFunc as KoaMiddlewareFunc,
11
+ Next,
12
+ } from '@eggjs/koa';
6
13
  import { EggConsoleLogger } from 'egg-logger';
7
14
  import { RegisterOptions, ResourcesController, EggRouter as Router } from '@eggjs/router';
8
15
  import type { ReadyFunctionArg } from 'get-ready';
@@ -15,7 +22,7 @@ import utils from './utils/index.js';
15
22
 
16
23
  const debug = debuglog('@eggjs/core:egg');
17
24
 
18
- const EGG_LOADER = Symbol.for('egg#loader');
25
+ export const EGG_LOADER = Symbol.for('egg#loader');
19
26
 
20
27
  export interface EggCoreOptions {
21
28
  baseDir: string;
@@ -27,12 +34,40 @@ export interface EggCoreOptions {
27
34
 
28
35
  export type EggCoreInitOptions = Partial<EggCoreOptions>;
29
36
 
30
- export type { ContextDelegation, MiddlewareFunc, Next } from '@eggjs/koa';
37
+ // export @eggjs/koa classes
38
+ export {
39
+ KoaRequest, KoaResponse, KoaContext, KoaApplication,
40
+ };
41
+
42
+ // export @eggjs/koa types
43
+ export type {
44
+ Next, KoaMiddlewareFunc, KoaContextDelegation,
45
+ };
46
+
47
+ // export @eggjs/core classes
48
+ export class Request extends KoaRequest {
49
+ declare app: EggCore;
50
+ declare response: Response;
51
+ declare ctx: ContextDelegation;
52
+ }
53
+
54
+ export class Response extends KoaResponse {
55
+ declare app: EggCore;
56
+ declare request: Request;
57
+ declare ctx: ContextDelegation;
58
+ }
31
59
 
32
- export interface EggCoreContext extends ContextDelegation {
33
- app: EggCore;
60
+ export class Context extends KoaContext {
61
+ declare app: EggCore;
62
+ declare request: Request;
63
+ declare response: Response;
64
+ declare service: BaseContextClass;
34
65
  }
35
66
 
67
+ // export @eggjs/core types
68
+ export type ContextDelegation = KoaContextDelegation & Context;
69
+ export type MiddlewareFunc<T extends ContextDelegation = ContextDelegation> = KoaMiddlewareFunc<T>;
70
+
36
71
  export class EggCore extends KoaApplication {
37
72
  options: EggCoreOptions;
38
73
  timing: Timing;
@@ -159,7 +194,7 @@ export class EggCore extends KoaApplication {
159
194
  use(fn: MiddlewareFunc) {
160
195
  assert(typeof fn === 'function', 'app.use() requires a function');
161
196
  debug('[use] add middleware: %o', fn._name || fn.name || '-');
162
- this.middleware.push(fn);
197
+ this.middleware.push(fn as unknown as KoaMiddlewareFunc);
163
198
  return this;
164
199
  }
165
200
 
@@ -229,11 +264,11 @@ export class EggCore extends KoaApplication {
229
264
  *
230
265
  * @see https://eggjs.org/en/advanced/loader.html#beforestart
231
266
  *
232
- * @param {Function|AsyncFunction} scope function will execute before app start
267
+ * @param {Function} scope function will execute before app start
233
268
  * @param {string} [name] scope name, default is empty string
234
269
  */
235
270
  beforeStart(scope: Fun, name?: string) {
236
- this.deprecate('Please use "Life Cycles" instead, see https://www.eggjs.org/advanced/loader#life-cycles');
271
+ this.deprecate('`beforeStart` was deprecated, please use "Life Cycles" instead, see https://www.eggjs.org/advanced/loader#life-cycles');
237
272
  this.lifecycle.registerBeforeStart(scope, name ?? '');
238
273
  }
239
274
 
@@ -276,7 +311,7 @@ export class EggCore extends KoaApplication {
276
311
  * mysql.ready(done);
277
312
  */
278
313
  readyCallback(name: string, opts: object) {
279
- this.deprecate('Please use "Life Cycles" instead, see https://www.eggjs.org/advanced/loader#life-cycles');
314
+ this.deprecate('`readyCallback` was deprecated, please use "Life Cycles" instead, see https://www.eggjs.org/advanced/loader#life-cycles');
280
315
  return this.lifecycle.legacyReadyCallback(name, opts);
281
316
  }
282
317
 
@@ -293,7 +328,7 @@ export class EggCore extends KoaApplication {
293
328
  * @param {Function} fn - the function that can be generator function or async function.
294
329
  */
295
330
  beforeClose(fn: Fun) {
296
- this.deprecate('Please use "Life Cycles" instead, see https://www.eggjs.org/advanced/loader#life-cycles');
331
+ this.deprecate('`beforeClose` was deprecated, please use "Life Cycles" instead, see https://www.eggjs.org/advanced/loader#life-cycles');
297
332
  this.lifecycle.registerBeforeClose(fn);
298
333
  }
299
334
 
@@ -1,7 +1,7 @@
1
1
  import assert from 'node:assert';
2
- import { type ContextDelegation } from '@eggjs/koa';
3
2
  import { isClass, isPrimitive } from 'is-type-of';
4
3
  import { FileLoader, EXPORTS, type FileLoaderOptions } from './file_loader.js';
4
+ import type { ContextDelegation } from '../egg.js';
5
5
 
6
6
  const CLASS_LOADER = Symbol('classLoader');
7
7
 
@@ -15,10 +15,10 @@ import { ContextLoader, ContextLoaderOptions } from './context_loader.js';
15
15
  import utils, { Fun } from '../utils/index.js';
16
16
  import sequencify from '../utils/sequencify.js';
17
17
  import { Timing } from '../utils/timing.js';
18
- import type { EggCoreContext, EggCore, MiddlewareFunc } from '../egg.js';
18
+ import type { ContextDelegation, EggCore, MiddlewareFunc } from '../egg.js';
19
19
  import { BaseContextClass } from '../base_context_class.js';
20
20
 
21
- const debug = debuglog('@eggjs/core:egg_loader');
21
+ const debug = debuglog('@eggjs/core/loader/egg_loader');
22
22
 
23
23
  const originalPrototypes: Record<string, any> = {
24
24
  request: Request.prototype,
@@ -1064,7 +1064,7 @@ export class EggLoader {
1064
1064
  for (const rawFilepath of filepaths) {
1065
1065
  const filepath = this.resolveModule(rawFilepath)!;
1066
1066
  if (!filepath) {
1067
- debug('loadExtend %o not found', rawFilepath);
1067
+ // debug('loadExtend %o not found', rawFilepath);
1068
1068
  continue;
1069
1069
  }
1070
1070
  if (filepath.endsWith('/index.js')) {
@@ -1073,9 +1073,14 @@ export class EggLoader {
1073
1073
  this.app.deprecate(`app/extend/${name}/index.ts is deprecated, use app/extend/${name}.ts instead`);
1074
1074
  }
1075
1075
 
1076
- const ext = await this.requireFile(filepath);
1076
+ let ext = await this.requireFile(filepath);
1077
+ // if extend object is Class, should use Class.prototype instead
1078
+ if (isClass(ext)) {
1079
+ ext = ext.prototype;
1080
+ }
1077
1081
  const properties = Object.getOwnPropertyNames(ext)
1078
- .concat(Object.getOwnPropertySymbols(ext) as any[]);
1082
+ .concat(Object.getOwnPropertySymbols(ext) as any[])
1083
+ .filter(name => name !== 'constructor'); // ignore class constructor for extend
1079
1084
 
1080
1085
  for (const property of properties) {
1081
1086
  if (mergeRecord.has(property)) {
@@ -1108,7 +1113,7 @@ export class EggLoader {
1108
1113
  Object.defineProperty(proto, property, descriptor!);
1109
1114
  mergeRecord.set(property, filepath);
1110
1115
  }
1111
- debug('merge %j to %s from %s', Object.keys(ext), name, filepath);
1116
+ debug('merge %j to %s from %s', properties, name, filepath);
1112
1117
  }
1113
1118
  this.timing.end(`Load extend/${name}.js`);
1114
1119
  }
@@ -1682,7 +1687,7 @@ function wrapControllerClass(Controller: typeof BaseContextClass, fullPath: stri
1682
1687
  }
1683
1688
 
1684
1689
  function controllerMethodToMiddleware(Controller: typeof BaseContextClass, key: string) {
1685
- return function classControllerMiddleware(this: EggCoreContext, ...args: any[]) {
1690
+ return function classControllerMiddleware(this: ContextDelegation, ...args: any[]) {
1686
1691
  const controller: any = new Controller(this);
1687
1692
  if (!this.app.config.controller?.supportParams) {
1688
1693
  args = [ this ];
@@ -1718,7 +1723,7 @@ function wrapObject(obj: Record<string, any>, fullPath: string, prefix?: string)
1718
1723
  }
1719
1724
 
1720
1725
  function objectFunctionToMiddleware(func: Fun) {
1721
- async function objectControllerMiddleware(this: EggCoreContext, ...args: any[]) {
1726
+ async function objectControllerMiddleware(this: ContextDelegation, ...args: any[]) {
1722
1727
  if (!this.app.config.controller?.supportParams) {
1723
1728
  args = [ this ];
1724
1729
  }