forked from eggjs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.d.ts
481 lines (432 loc) · 12.9 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
import KoaApplication = require('koa');
import { Logger, EggConsoleLogger } from 'egg-logger';
import depd = require('depd');
type EggType = 'application' | 'agent';
interface PlainObject<T = any> {
[key: string]: T;
}
export interface EggCoreOptions {
/** egg type, application or agent */
type?: EggType;
/** the directory of application */
baseDir?: EggAppInfo['baseDir'];
/** server scope */
serverScope?: string;
/** custom plugins */
plugins?: Plugins;
}
export interface EggLoaderOptions {
/** Application instance */
app: EggCore;
/** the directory of application */
baseDir: EggAppInfo['baseDir'];
/** egg logger */
logger: Logger;
/** server scope */
serverScope?: string;
/** custom plugins */
plugins?: Plugins;
}
export interface PluginInfo {
/** the plugin name, it can be used in `dep` */
name: string;
/** the package name of plugin */
package: string;
/** whether enabled */
enable: boolean;
/** the directory of the plugin package */
path: string;
/** the dependent plugins, you can use the plugin name */
dependencies: string[];
/** the optional dependent plugins. */
optionalDependencies: string[];
/** specify the serverEnv that only enable the plugin in it */
env: string[];
/** the file plugin config in. */
from: string;
}
export interface Plugins extends PlainObject<PluginInfo> { }
export interface EggCoreBase<Config> extends KoaApplication {
/**
* Whether `application` or `agent`
* @member {String}
* @since 1.0.0
*/
type: EggType;
/**
* The current directory of application
* @member {String}
* @see {@link EggAppInfo#baseDir}
* @since 1.0.0
*/
baseDir: EggAppInfo['baseDir'];
/**
* The name of application
* @member {String}
* @see {@link EggAppInfo#name}
* @since 1.0.0
*/
name: EggAppInfo['name'];
/**
* Convert a generator function to a promisable one.
*
* Notice: for other kinds of functions, it directly returns you what it is.
*
* @param {Function} fn The inputted function.
* @return {AsyncFunction} An async promise-based function.
* @example
* ```javascript
* const fn = function* (arg) {
return arg;
};
const wrapped = app.toAsyncFunction(fn);
wrapped(true).then((value) => console.log(value));
* ```
*/
toAsyncFunction<T = any>(fn: (...args: any[]) => IterableIterator<T>): (...args: any[]) => Promise<T>;
/**
* Convert an object with generator functions to a Promisable one.
* @param {Mixed} obj The inputted object.
* @return {Promise} A Promisable result.
* @example
* ```javascript
* const fn = function* (arg) {
return arg;
};
const arr = [ fn(1), fn(2) ];
const promise = app.toPromise(arr);
promise.then(res => console.log(res));
* ```
*/
toPromise<T = any>(obj: any): Promise<T>;
/**
* register an callback function that will be invoked when application is ready.
* @see https://github.com/node-modules/ready
* @since 1.0.0
* @param {boolean|Error|Function} flagOrFunction -
* @return {Promise|null} return promise when argument is undefined
* @example
* const app = new Application(...);
* app.ready(err => {
* if (err) throw err;
* console.log('done');
* });
*/
ready(fn?: (err?: Error) => void): any;
/**
* Close all, it wil close
* - callbacks registered by beforeClose
* - emit `close` event
* - remove add listeners
*
* If error is thrown when it's closing, the promise will reject.
* It will also reject after following call.
* @return {Promise} promise
* @since 1.0.0
*/
close(): Promise<any>;
/**
* If a client starts asynchronously, you can register `readyCallback`,
* then the application will wait for the callback to ready
*
* It will log when the callback is not invoked after 10s
*
* Recommend to use {@link EggCore#beforeStart}
* @since 1.0.0
*
* @param {String} name - readyCallback task name
* @param {object} opts -
* - {Number} [timeout=10000] - emit `ready_timeout` when it doesn't finish but reach the timeout
* - {Boolean} [isWeakDep=false] - whether it's a weak dependency
* @return {Function} - a callback
* @example
* const done = app.readyCallback('mysql');
* mysql.ready(done);
*/
readyCallback(name: string, opts?: { timeout?: number; isWeakDep?: boolean }): () => void;
/**
* The loader instance, the default class is {@link EggLoader}.
* If you want define
* @member {EggLoader} EggCore#loader
* @since 1.0.0
*/
loader: EggLoader<this, Config>;
/**
* The configuration of application
* @member {Config}
* @since 1.0.0
*/
config: Config;
/**
* Retrieve enabled plugins
* @member {Object}
* @since 1.0.0
*/
plugins: Plugins;
/**
* Register a function that will be called when app close
*/
beforeClose(fn: () => void): void;
/**
* Execute scope after loaded and before app start
*/
beforeStart(scope: () => void): void;
/**
* Alias to {@link https://npmjs.com/package/depd}
* @member {Function}
* @since 1.0.0
*/
deprecate: depd.Deprecate;
}
export interface EggCore<Config = PlainObject> extends EggCoreBase<Config> {
Controller: typeof BaseContextClass;
Service: typeof BaseContextClass;
}
export class EggCore {
/**
* @constructor
* @param {Object} options - options
* @param {String} [options.baseDir=process.cwd()] - the directory of application
* @param {String} [options.type=application|agent] - whether it's running in app worker or agent worker
* @param {Object} [options.plugins] - custom plugins
* @since 1.0.0
*/
constructor(options?: EggCoreOptions);
}
/**
* egg app info
* @example
* ```js
* // config/config.default.ts
* import { EggAppInfo } from 'egg';
*
* export default (appInfo: EggAppInfo) => {
* return {
* keys: appInfo.name + '123456',
* };
* }
* ```
*/
export interface EggAppInfo {
/** package.json */
pkg: PlainObject;
/** the application name from package.json */
name: string;
/** current directory of application */
baseDir: string;
/** equals to serverEnv */
env: string;
/** home directory of the OS */
HOME: string;
/** baseDir when local and unittest, HOME when other environment */
root: string;
}
/**
* BaseContextClass is a base class that can be extended,
* it's instantiated in context level,
* {@link Helper}, {@link Service} is extending it.
*/
export class BaseContextClass<
Context = any,
Application = any,
EggAppConfig = any,
Service = any
> {
constructor(ctx: Context);
/** request context */
protected ctx: Context;
/** Application */
protected app: Application;
/** Application config object */
protected config: EggAppConfig;
/** service */
protected service: Service;
}
export interface FileLoaderOption {
/** directories to be loaded */
directory: string | string[];
/** attach the target object from loaded files */
target: object;
/** match the files when load, support glob, default to all js files */
match?: string | string[];
/** ignore the files when load, support glob */
ignore?: string | string[];
/** custom file exports, receive two parameters, first is the inject object(if not js file, will be content buffer), second is an `options` object that contain `path` */
initializer?(obj: object, options: { path: string; pathName: string; }): any;
/** determine whether invoke when exports is function */
call?: boolean;
/** determine whether override the property when get the same name */
override?: boolean;
/** an object that be the argument when invoke the function */
inject?: object;
/** a function that filter the exports which can be loaded */
filter?(obj: object): boolean;
/** set property's case when converting a filepath to property list. */
caseStyle?: string | ((str: string) => string[]);
}
export interface ContextLoaderOption extends Partial<FileLoaderOption> {
/** directories to be loaded */
directory: string | string[];
/** required inject */
inject: object;
/** property name defined to target */
property: string;
/** determine the field name of inject object. */
fieldClass?: string;
}
declare interface FileLoaderBase {
/**
* attach items to target object. Mapping the directory to properties.
* `app/controller/group/repository.js` => `target.group.repository`
* @return {Object} target
* @since 1.0.0
*/
load(): object;
/**
* Parse files from given directories, then return an items list, each item contains properties and exports.
*
* For example, parse `app/controller/group/repository.js`
*
* ```js
* module.exports = app => {
* return class RepositoryController extends app.Controller {};
* }
* ```
*
* It returns a item
*
* ```js
* {
* properties: [ 'group', 'repository' ],
* exports: app => { ... },
* }
* ```
*
* `Properties` is an array that contains the directory of a filepath.
*
* `Exports` depends on type, if exports is a function, it will be called. if initializer is specified, it will be called with exports for customizing.
* @return {Array} items
* @since 1.0.0
*/
parse(): Array<{ fullpath: string; properties: string[]; exports: any; }>;
}
declare interface ContextLoaderBase extends FileLoaderBase {}
export interface FileLoader {
/**
* Load files from directory to target object.
* @since 1.0.0
*/
new (options: FileLoaderOption): FileLoaderBase;
}
export interface ContextLoader {
/**
* Same as {@link FileLoader}, but it will attach file to `inject[fieldClass]`. The exports will be lazy loaded, such as `ctx.group.repository`.
* @extends FileLoader
* @since 1.0.0
*/
new (options: ContextLoaderOption): ContextLoaderBase;
}
export class EggLoader<
T = EggCore,
Config = any,
Options extends EggLoaderOptions = EggLoaderOptions
> {
app: T;
eggPaths: string[];
pkg: PlainObject;
appInfo: EggAppInfo;
serverScope: string;
plugins: Plugins;
config: Config;
options: Options;
/**
* @constructor
* @param {Object} options - options
* @param {String} options.baseDir - the directory of application
* @param {EggCore} options.app - Application instance
* @param {Logger} options.logger - logger
* @param {Object} [options.plugins] - custom plugins
* @since 1.0.0
*/
constructor(options: EggLoaderOptions);
/**
* Get home directory
* @return {String} home directory
* @since 3.4.0
*/
getHomedir(): EggAppInfo['HOME'];
/**
* Get app info
* @return {EggAppInfo} appInfo
* @since 1.0.0
*/
getAppInfo(): EggAppInfo;
// Low Level API
/**
* Load single file, will invoke when export is function
*
* @param {String} filepath - fullpath
* @param {Array} arguments - pass rest arguments into the function when invoke
* @return {Object} exports
* @example
* ```js
* app.loader.loadFile(path.join(app.options.baseDir, 'config/router.js'));
* ```
* @since 1.0.0
*/
loadFile<T = any>(filepath: string, ...inject: any[]): T;
/**
* Get all loadUnit
*
* loadUnit is a directory that can be loaded by EggLoader, it has the same structure.
* loadUnit has a path and a type(app, framework, plugin).
*
* The order of the loadUnits:
*
* 1. plugin
* 2. framework
* 3. app
*
* @return {Array} loadUnits
* @since 1.0.0
*/
getLoadUnits(): Array<{ path: string; type: string; }>;
getEggPaths(): string[];
getServerEnv(): string;
/**
* Load files using {@link FileLoader}, inject to {@link Application}
* @param {String|Array} directory - see {@link FileLoader}
* @param {String} property - see {@link FileLoader}
* @param {Object} opt - see {@link FileLoader}
* @since 1.0.0
*/
loadToApp(directory: string | string[], property: string, opt?: Partial<FileLoaderOption>): void;
/**
* Load files using {@link ContextLoader}
* @param {String|Array} directory - see {@link ContextLoader}
* @param {String} property - see {@link ContextLoader}
* @param {Object} opt - see {@link ContextLoader}
* @since 1.0.0
*/
loadToContext(directory: string | string[], property: string, opt?: Partial<ContextLoaderOption>): void;
getTypeFiles(filename: string): string[];
resolveModule(filepath: string): string | undefined;
FileLoader: FileLoader;
ContextLoader: ContextLoader;
// load methods
protected loadConfig(): void;
protected loadController(opt?: Partial<FileLoaderOption>): void;
protected loadCustomLoader(): void;
protected loadCustomApp(): void;
protected loadCustomAgent(): void;
protected loadAgentExtend(): void;
protected loadApplicationExtend(): void;
protected loadRequestExtend(): void;
protected loadResponseExtend(): void;
protected loadContextExtend(): void;
protected loadHelperExtend(): void;
protected loadMiddleware(opt?: Partial<FileLoaderOption>): void;
protected loadPlugin(): void;
protected loadRouter(): void;
protected loadService(opt?: Partial<ContextLoaderOption>): void;
}