@eggjs/core 7.0.0-beta.20 → 7.0.0-beta.22

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,16 @@
1
+ import type { EggCore, Context } from './egg.ts';
2
+ /**
3
+ * BaseContextClass is a base class that can be extended,
4
+ * it's instantiated in context level,
5
+ * {@link Helper}, {@link Service} is extending it.
6
+ */
7
+ export declare class BaseContextClass {
8
+ ctx: Context;
9
+ app: EggCore;
10
+ config: Record<string, any>;
11
+ service: BaseContextClass;
12
+ /**
13
+ * @since 1.0.0
14
+ */
15
+ constructor(ctx: Context);
16
+ }
@@ -0,0 +1,37 @@
1
+ /**
2
+ * BaseContextClass is a base class that can be extended,
3
+ * it's instantiated in context level,
4
+ * {@link Helper}, {@link Service} is extending it.
5
+ */
6
+ export class BaseContextClass {
7
+ ctx;
8
+ app;
9
+ config;
10
+ service;
11
+ /**
12
+ * @since 1.0.0
13
+ */
14
+ constructor(ctx) {
15
+ /**
16
+ * @member {Context} BaseContextClass#ctx
17
+ * @since 1.0.0
18
+ */
19
+ this.ctx = ctx;
20
+ /**
21
+ * @member {Application} BaseContextClass#app
22
+ * @since 1.0.0
23
+ */
24
+ this.app = ctx.app;
25
+ /**
26
+ * @member {Config} BaseContextClass#config
27
+ * @since 1.0.0
28
+ */
29
+ this.config = ctx.app.config;
30
+ /**
31
+ * @member {Service} BaseContextClass#service
32
+ * @since 1.0.0
33
+ */
34
+ this.service = ctx.service;
35
+ }
36
+ }
37
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYmFzZV9jb250ZXh0X2NsYXNzLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vc3JjL2Jhc2VfY29udGV4dF9jbGFzcy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFFQTs7OztHQUlHO0FBQ0gsTUFBTSxPQUFPLGdCQUFnQjtJQUMzQixHQUFHLENBQVU7SUFDYixHQUFHLENBQVU7SUFDYixNQUFNLENBQXNCO0lBQzVCLE9BQU8sQ0FBbUI7SUFFMUI7O09BRUc7SUFDSCxZQUFZLEdBQVk7UUFDdEI7OztXQUdHO1FBQ0gsSUFBSSxDQUFDLEdBQUcsR0FBRyxHQUFHLENBQUM7UUFDZjs7O1dBR0c7UUFDSCxJQUFJLENBQUMsR0FBRyxHQUFHLEdBQUcsQ0FBQyxHQUFHLENBQUM7UUFDbkI7OztXQUdHO1FBQ0gsSUFBSSxDQUFDLE1BQU0sR0FBRyxHQUFHLENBQUMsR0FBRyxDQUFDLE1BQU0sQ0FBQztRQUM3Qjs7O1dBR0c7UUFDSCxJQUFJLENBQUMsT0FBTyxHQUFHLEdBQUcsQ0FBQyxPQUFPLENBQUM7SUFDN0IsQ0FBQztDQUNGIn0=
package/dist/egg.d.ts ADDED
@@ -0,0 +1,246 @@
1
+ import { Application as KoaApplication, Context as KoaContext, Request as KoaRequest, Response as KoaResponse, type MiddlewareFunc as KoaMiddlewareFunc, type Next } from '@eggjs/koa';
2
+ import { EggConsoleLogger, type Logger } from 'egg-logger';
3
+ import { EggRouter as Router, type RegisterOptions, type ResourcesController } from '@eggjs/router';
4
+ import type { ReadyFunctionArg } from 'get-ready';
5
+ import { BaseContextClass } from './base_context_class.ts';
6
+ import { Timing } from './utils/timing.ts';
7
+ import { Lifecycle } from './lifecycle.ts';
8
+ import { EggLoader } from './loader/egg_loader.ts';
9
+ import { type Fun } from './utils/index.ts';
10
+ import type { EggAppConfig } from './types.ts';
11
+ import { type SingletonCreateMethod } from './singleton.ts';
12
+ export declare const EGG_LOADER: unique symbol;
13
+ export interface EggCoreOptions {
14
+ baseDir: string;
15
+ type: 'application' | 'agent';
16
+ plugins?: any;
17
+ serverScope?: string;
18
+ env?: string;
19
+ }
20
+ export type EggCoreInitOptions = Partial<EggCoreOptions>;
21
+ export { KoaRequest, KoaResponse, KoaContext, KoaApplication, Router };
22
+ export type { Next, KoaMiddlewareFunc };
23
+ export declare class Request extends KoaRequest {
24
+ app: EggCore;
25
+ response: Response;
26
+ }
27
+ export declare class Response extends KoaResponse {
28
+ app: EggCore;
29
+ request: Request;
30
+ }
31
+ export declare class Context extends KoaContext {
32
+ app: EggCore;
33
+ request: Request;
34
+ response: Response;
35
+ service: BaseContextClass;
36
+ /**
37
+ * Returns map of URL parameters for given `path` and `paramNames`.
38
+ * @example
39
+ * ##### ctx.params.id {string}
40
+ *
41
+ * `GET /api/users/1` => `'1'`
42
+ *
43
+ * ##### ctx.params.per_page {string}
44
+ *
45
+ * The number of every page, `GET /api/users?per_page=20` => `20`
46
+ */
47
+ params?: Record<string, string>;
48
+ /**
49
+ * Returns array of router regexp url path captures.
50
+ */
51
+ captures?: string[];
52
+ /**
53
+ * Returns the name of the matched router.
54
+ */
55
+ routerName?: string;
56
+ /**
57
+ * Returns the path of the matched router.
58
+ */
59
+ routerPath?: string | RegExp;
60
+ }
61
+ export type MiddlewareFunc<T extends KoaContext = Context> = KoaMiddlewareFunc<T>;
62
+ export declare class EggCore extends KoaApplication {
63
+ #private;
64
+ options: EggCoreOptions;
65
+ timing: Timing;
66
+ console: EggConsoleLogger;
67
+ BaseContextClass: typeof BaseContextClass;
68
+ Controller: typeof BaseContextClass;
69
+ Service: typeof BaseContextClass;
70
+ Helper?: typeof BaseContextClass;
71
+ lifecycle: Lifecycle;
72
+ loader: EggLoader;
73
+ /** auto inject on loadService() */
74
+ readonly serviceClasses: Record<string, any>;
75
+ /** auto inject on loadController() */
76
+ readonly controller: Record<string, any>;
77
+ /** auto inject on loadMiddleware() */
78
+ readonly middlewares: Record<string, (opt: unknown, app: EggCore) => MiddlewareFunc>;
79
+ /**
80
+ * @class
81
+ * @param {Object} options - options
82
+ * @param {String} [options.baseDir] - the directory of application
83
+ * @param {String} [options.type] - whether it's running in app worker or agent worker
84
+ * @param {Object} [options.plugins] - custom plugins
85
+ * @since 1.0.0
86
+ */
87
+ constructor(options?: EggCoreInitOptions);
88
+ get logger(): Logger;
89
+ get coreLogger(): Logger;
90
+ /**
91
+ * create a singleton instance
92
+ * @param {String} name - unique name for singleton
93
+ * @param {Function|AsyncFunction} create - method will be invoked when singleton instance create
94
+ */
95
+ addSingleton(name: string, create: SingletonCreateMethod): void;
96
+ /**
97
+ * override koa's app.use, support generator function
98
+ * @since 1.0.0
99
+ */
100
+ use<T extends KoaContext = Context>(fn: MiddlewareFunc<T>): this;
101
+ /**
102
+ * Whether `application` or `agent`
103
+ * @member {String}
104
+ * @since 1.0.0
105
+ */
106
+ get type(): "application" | "agent";
107
+ /**
108
+ * The current directory of application
109
+ * @member {String}
110
+ * @see {@link AppInfo#baseDir}
111
+ * @since 1.0.0
112
+ */
113
+ get baseDir(): string;
114
+ /**
115
+ * Alias to {@link https://npmjs.com/package/depd}
116
+ * @member {Function}
117
+ * @since 1.0.0
118
+ */
119
+ get deprecate(): (message: string) => void;
120
+ /**
121
+ * The name of application
122
+ * @member {String}
123
+ * @see {@link AppInfo#name}
124
+ * @since 1.0.0
125
+ */
126
+ get name(): any;
127
+ /**
128
+ * Retrieve enabled plugins
129
+ * @member {Object}
130
+ * @since 1.0.0
131
+ */
132
+ get plugins(): Record<string, import("./types.ts").EggPluginInfo>;
133
+ /**
134
+ * The configuration of application
135
+ * @member {Config}
136
+ * @since 1.0.0
137
+ */
138
+ get config(): EggAppConfig;
139
+ /**
140
+ * Execute scope after loaded and before app start.
141
+ *
142
+ * Notice:
143
+ * This method is now NOT recommended and regarded as a deprecated one,
144
+ * For plugin development, we should use `didLoad` instead.
145
+ * For application development, we should use `willReady` instead.
146
+ *
147
+ * @see https://eggjs.org/en/advanced/loader.html#beforestart
148
+ *
149
+ * @param {Function} scope function will execute before app start
150
+ * @param {string} [name] scope name, default is empty string
151
+ */
152
+ beforeStart(scope: Fun, name?: string): void;
153
+ /**
154
+ * register an callback function that will be invoked when application is ready.
155
+ * @see https://github.com/node-modules/get-ready
156
+ * @since 1.0.0
157
+ * @example
158
+ * const app = new Application(...);
159
+ * app.ready(err => {
160
+ * if (err) throw err;
161
+ * console.log('done');
162
+ * });
163
+ */
164
+ ready(): Promise<void>;
165
+ ready(flagOrFunction: ReadyFunctionArg): void;
166
+ /**
167
+ * If a client starts asynchronously, you can register `readyCallback`,
168
+ * then the application will wait for the callback to ready
169
+ *
170
+ * It will log when the callback is not invoked after 10s
171
+ *
172
+ * Recommend to use {@link EggCore#beforeStart}
173
+ * @since 1.0.0
174
+ *
175
+ * @param {String} name - readyCallback task name
176
+ * @param {object} opts -
177
+ * - {Number} [timeout=10000] - emit `ready_timeout` when it doesn't finish but reach the timeout
178
+ * - {Boolean} [isWeakDep=false] - whether it's a weak dependency
179
+ * @returns {Function} - a callback
180
+ * @example
181
+ * const done = app.readyCallback('mysql');
182
+ * mysql.ready(done);
183
+ */
184
+ readyCallback(name: string, opts: object): (...args: unknown[]) => void;
185
+ /**
186
+ * Register a function that will be called when app close.
187
+ *
188
+ * Notice:
189
+ * This method is now NOT recommended directly used,
190
+ * Developers SHOULDN'T use app.beforeClose directly now,
191
+ * but in the form of class to implement beforeClose instead.
192
+ *
193
+ * @see https://eggjs.org/en/advanced/loader.html#beforeclose
194
+ *
195
+ * @param {Function} fn - the function that can be generator function or async function.
196
+ */
197
+ beforeClose(fn: Fun, name?: string): void;
198
+ /**
199
+ * Close all, it will close
200
+ * - callbacks registered by beforeClose
201
+ * - emit `close` event
202
+ * - remove add listeners
203
+ *
204
+ * If error is thrown when it's closing, the promise will reject.
205
+ * It will also reject after following call.
206
+ * @returns {Promise} promise
207
+ * @since 1.0.0
208
+ */
209
+ close(): Promise<void>;
210
+ /**
211
+ * get router
212
+ * @member {Router} EggCore#router
213
+ * @since 1.0.0
214
+ */
215
+ get router(): Router;
216
+ /**
217
+ * Alias to {@link Router#url}
218
+ * @param {String} name - Router name
219
+ * @param {Object} params - more parameters
220
+ * @returns {String} url
221
+ */
222
+ url(name: string, params?: Parameters<Router['url']>[1]): string;
223
+ head(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
224
+ head(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
225
+ get(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
226
+ get(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
227
+ put(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
228
+ put(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
229
+ patch(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
230
+ patch(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
231
+ post(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
232
+ post(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
233
+ delete(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
234
+ delete(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
235
+ del(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
236
+ del(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
237
+ all(path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
238
+ all(name: string, path: string | RegExp | (string | RegExp)[], ...middlewares: (MiddlewareFunc | string)[]): EggCore;
239
+ resources(prefix: string, controller: string | ResourcesController): EggCore;
240
+ resources(prefix: string, middleware: MiddlewareFunc, controller: string | ResourcesController): EggCore;
241
+ resources(name: string, prefix: string, controller: string | ResourcesController): EggCore;
242
+ resources(name: string, prefix: string, middleware: MiddlewareFunc, controller: string | ResourcesController): EggCore;
243
+ redirect(source: string, destination: string, status?: number): this;
244
+ register(path: string | RegExp | (string | RegExp)[], methods: string[], middleware: MiddlewareFunc | MiddlewareFunc[], opts?: RegisterOptions): this;
245
+ get [EGG_LOADER](): typeof EggLoader;
246
+ }