@eggjs/core 6.2.1 → 6.2.3

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,41 @@ 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
+ Router,
41
+ };
42
+
43
+ // export @eggjs/koa types
44
+ export type {
45
+ Next, KoaMiddlewareFunc, KoaContextDelegation,
46
+ };
47
+
48
+ // export @eggjs/core classes
49
+ export class Request extends KoaRequest {
50
+ declare app: EggCore;
51
+ declare response: Response;
52
+ declare ctx: ContextDelegation;
53
+ }
54
+
55
+ export class Response extends KoaResponse {
56
+ declare app: EggCore;
57
+ declare request: Request;
58
+ declare ctx: ContextDelegation;
59
+ }
31
60
 
32
- export interface EggCoreContext extends ContextDelegation {
33
- app: EggCore;
61
+ export class Context extends KoaContext {
62
+ declare app: EggCore;
63
+ declare request: Request;
64
+ declare response: Response;
65
+ declare service: BaseContextClass;
34
66
  }
35
67
 
68
+ // export @eggjs/core types
69
+ export type ContextDelegation = KoaContextDelegation & Context;
70
+ export type MiddlewareFunc<T extends ContextDelegation = ContextDelegation> = KoaMiddlewareFunc<T>;
71
+
36
72
  export class EggCore extends KoaApplication {
37
73
  options: EggCoreOptions;
38
74
  timing: Timing;
@@ -159,7 +195,7 @@ export class EggCore extends KoaApplication {
159
195
  use(fn: MiddlewareFunc) {
160
196
  assert(typeof fn === 'function', 'app.use() requires a function');
161
197
  debug('[use] add middleware: %o', fn._name || fn.name || '-');
162
- this.middleware.push(fn);
198
+ this.middleware.push(fn as unknown as KoaMiddlewareFunc);
163
199
  return this;
164
200
  }
165
201
 
@@ -229,7 +265,7 @@ export class EggCore extends KoaApplication {
229
265
  *
230
266
  * @see https://eggjs.org/en/advanced/loader.html#beforestart
231
267
  *
232
- * @param {Function|AsyncFunction} scope function will execute before app start
268
+ * @param {Function} scope function will execute before app start
233
269
  * @param {string} [name] scope name, default is empty string
234
270
  */
235
271
  beforeStart(scope: Fun, name?: string) {
@@ -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
  }