1 В избранное 0 Ответвления 0

OSCHINA-MIRROR/mirrors-eggjs

Присоединиться к Gitlife
Откройте для себя и примите участие в публичных проектах с открытым исходным кодом с участием более 10 миллионов разработчиков. Приватные репозитории также полностью бесплатны :)
Присоединиться бесплатно
Клонировать/Скачать
index.d.ts 36 КБ
Копировать Редактировать Web IDE Исходные данные Просмотреть построчно История
fengmk2 Отправлено 19.01.2025 15:12 02d9fdb
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
import accepts = require('accepts');
import { AsyncLocalStorage } from 'async_hooks';
import { EventEmitter } from 'events'
import { Readable } from 'stream';
import { Socket } from 'net';
import { IncomingMessage, ServerResponse } from 'http';
import KoaApplication = require('koa');
import KoaRouter = require('koa-router');
import {
EggLogger as Logger,
EggLoggers,
LoggerLevel as EggLoggerLevel,
EggLoggersOptions,
EggLoggerOptions,
EggContextLogger,
} from 'egg-logger';
import {
RequestOptions2 as RequestOptionsOld,
HttpClientResponse as HttpClientResponseOld,
} from 'urllib';
import {
RequestURL,
RequestOptions,
HttpClientResponse as HttpClientResponseNext,
} from 'urllib-next';
import {
EggCoreBase,
FileLoaderOption,
EggLoader as CoreLoader,
EggCoreOptions as CoreOptions,
EggLoaderOptions as CoreLoaderOptions,
BaseContextClass as CoreBaseContextClass,
} from 'egg-core';
import EggCookies = require('egg-cookies');
import 'egg-onerror';
import 'egg-session';
import 'egg-i18n';
import 'egg-watcher';
import 'egg-multipart';
import 'egg-security';
import 'egg-development';
import 'egg-logrotator';
import 'egg-schedule';
import 'egg-static';
import 'egg-jsonp';
import 'egg-view';
declare module 'egg' {
export type EggLogger = Logger;
// plain object
type PlainObject<T = any> = { [key: string]: T };
// Remove specific property from the specific class
type RemoveSpecProp<T, P> = Pick<T, Exclude<keyof T, P>>;
// Usage:
// ```ts
// import { HttpClientRequestURL, HttpClientRequestOptions, HttpClientResponse } from 'egg';
// async function request(url: HttpClientRequestURL, options: HttpClientRequestOptions): Promise<HttpClientResponse> {
// return await app.httpclient.request(url, options);
// }
// ```
export type HttpClientRequestURL = RequestURL;
export type HttpClientRequestOptions = RequestOptions;
export type HttpClientResponse<T = any> = HttpClientResponseNext<T>;
// Compatible with both urllib@2 and urllib@3 RequestOptions to request
export interface EggHttpClient extends EventEmitter {
request<T = any>(url: HttpClientRequestURL): Promise<HttpClientResponseOld<T> | HttpClientResponse<T>>;
request<T = any>(url: HttpClientRequestURL, options: RequestOptionsOld | HttpClientRequestOptions):
Promise<HttpClientResponseOld<T> | HttpClientResponse<T>>;
curl<T = any>(url: HttpClientRequestURL): Promise<HttpClientResponseOld<T> | HttpClientResponse<T>>;
curl<T = any>(url: HttpClientRequestURL, options: RequestOptionsOld | HttpClientRequestOptions):
Promise<HttpClientResponseOld<T> | HttpClientResponse<T>>;
safeCurl<T = any>(url: HttpClientRequestURL): Promise<HttpClientResponseOld<T> | HttpClientResponse<T>>;
safeCurl<T = any>(url: HttpClientRequestURL, options: RequestOptionsOld | HttpClientRequestOptions):
Promise<HttpClientResponseOld<T> | HttpClientResponse<T>>;
}
interface EggHttpConstructor {
new(app: Application): EggHttpClient;
}
export interface EggContextHttpClient extends EggHttpClient { }
interface EggContextHttpClientConstructor {
new(ctx: Context): EggContextHttpClient;
}
/**
* 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 extends CoreBaseContextClass<Context, Application, EggAppConfig, IService> { // tslint:disable-line
/**
* logger
*/
protected logger: EggLogger;
}
export class Boot {
/**
* logger
* @member {EggLogger}
*/
protected logger: EggLogger;
/**
* The configuration of application
* @member {EggAppConfig}
*/
protected config: EggAppConfig;
/**
* The instance of agent
* @member {Agent}
*/
protected agent: Agent;
/**
* The instance of app
* @member {Application}
*/
protected app: Application;
}
export type RequestArrayBody = any[];
export type RequestObjectBody = PlainObject;
export interface Request extends KoaApplication.Request { // tslint:disable-line
/**
* detect if response should be json
* 1. url path ends with `.json`
* 2. response type is set to json
* 3. detect by request accept header
*
* @member {Boolean} Request#acceptJSON
* @since 1.0.0
*/
acceptJSON: boolean;
/**
* Request remote IPv4 address
* @member {String} Request#ip
* @example
* ```js
* this.request.ip
* => '127.0.0.1'
* => '111.10.2.1'
* ```
*/
ip: string;
/**
* Get all pass through ip addresses from the request.
* Enable only on `app.config.proxy = true`
*
* @member {Array} Request#ips
* @example
* ```js
* this.request.ips
* => ['100.23.1.2', '201.10.10.2']
* ```
*/
ips: string[];
protocol: string;
/**
* get params pass by querystring, all value are Array type. {@link Request#query}
* @member {Array} Request#queries
* @example
* ```js
* GET http://127.0.0.1:7001?a=b&a=c&o[foo]=bar&b[]=1&b[]=2&e=val
* this.queries
* =>
* {
* "a": ["b", "c"],
* "o[foo]": ["bar"],
* "b[]": ["1", "2"],
* "e": ["val"]
* }
* ```
*/
queries: PlainObject<string[]>;
/**
* get params pass by querystring, all value are String type.
* @member {Object} Request#query
* @example
* ```js
* GET http://127.0.0.1:7001?name=Foo&age=20&age=21
* this.query
* => { 'name': 'Foo', 'age': 20 }
*
* GET http://127.0.0.1:7001?a=b&a=c&o[foo]=bar&b[]=1&b[]=2&e=val
* this.query
* =>
* {
* "a": "b",
* "o[foo]": "bar",
* "b[]": "1",
* "e": "val"
* }
* ```
*/
query: PlainObject<string>;
body: any;
}
export interface Response<ResponseBodyT = any> extends KoaApplication.Response { // tslint:disable-line
/**
* read response real status code.
*
* e.g.: Using 302 status redirect to the global error page
* instead of show current 500 status page.
* And access log should save 500 not 302,
* then the `realStatus` can help us find out the real status code.
* @member {Number} Context#realStatus
*/
realStatus: number;
body: ResponseBodyT;
}
export type LoggerLevel = EggLoggerLevel;
/**
* 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 {
pkg: any; // package.json
name: string; // the application name from package.json
baseDir: string; // current directory of application
env: EggEnvType; // equals to serverEnv
HOME: string; // home directory of the OS
root: string; // baseDir when local and unittest, HOME when other environment
}
type IgnoreItem = string | RegExp | ((ctx: Context) => boolean);
type IgnoreOrMatch = IgnoreItem | IgnoreItem[];
/** logger config of egg */
export interface EggLoggerConfig extends RemoveSpecProp<EggLoggersOptions, 'type'> {
/** custom config of coreLogger */
coreLogger?: Partial<EggLoggerOptions>;
/** allow debug log at prod, defaults to `false` */
allowDebugAtProd?: boolean;
/** disable logger console after app ready. defaults to `false` on local and unittest env, others is `true`. */
disableConsoleAfterReady?: boolean;
/** using performance.now() timer instead of Date.now() for more more precise milliseconds, defaults to `false`. e.g.: logger will set 1.456ms instead of 1ms. */
enablePerformanceTimer?: boolean;
/** using the app logger instead of EggContextLogger, defaults to `false` */
enableFastContextLogger?: boolean;
}
/** Custom Loader Configuration */
export interface CustomLoaderConfig extends RemoveSpecProp<FileLoaderOption, 'inject' | 'target'> {
/**
* an object you wanner load to, value can only be 'ctx' or 'app'. default to app
*/
inject?: 'ctx' | 'app';
/**
* whether need to load files in plugins or framework, default to false
*/
loadunit?: boolean;
}
export interface HttpClientBaseConfig {
/** Whether use http keepalive */
keepAlive?: boolean;
/** Free socket after keepalive timeout */
freeSocketKeepAliveTimeout?: number;
/** Free socket after request timeout */
freeSocketTimeout?: number;
/** Request timeout */
timeout?: number;
/** Determines how many concurrent sockets the agent can have open per origin */
maxSockets?: number;
/** The maximum number of sockets that will be left open in the free state */
maxFreeSockets?: number;
}
/** HttpClient config */
export interface HttpClientConfig extends HttpClientBaseConfig {
/** http.Agent */
httpAgent?: HttpClientBaseConfig;
/** https.Agent */
httpsAgent?: HttpClientBaseConfig;
/** Default request args for httpclient */
request?: HttpClientRequestOptions | RequestOptionsOld;
/** Whether enable dns cache */
enableDNSCache?: boolean;
/** Enable proxy request. Default is `false`. */
enableProxy?: boolean;
/** proxy agent uri or options. Default is `null`. */
proxy?: string | { [key: string]: any };
/** DNS cache lookup interval */
dnsCacheLookupInterval?: number;
/** DNS cache max age */
dnsCacheMaxLength?: number;
/** use urllib@3 HttpClient. Default is `false` */
useHttpClientNext?: boolean;
/** Allow to use HTTP2 first, only work on `useHttpClientNext = true`. Default is `false` */
allowH2?: boolean;
}
export interface EggAppConfig {
workerStartTimeout: number;
baseDir: string;
middleware: string[];
/**
* The option of `bodyParser` middleware
*
* @member Config#bodyParser
* @property {Boolean} enable - enable bodyParser or not, default to true
* @property {String | RegExp | Function | Array} ignore - won't parse request body when url path hit ignore pattern, can not set `ignore` when `match` presented
* @property {String | RegExp | Function | Array} match - will parse request body only when url path hit match pattern
* @property {String} encoding - body encoding config, default utf8
* @property {String} formLimit - form body size limit, default 1mb
* @property {String} jsonLimit - json body size limit, default 1mb
* @property {String} textLimit - json body size limit, default 1mb
* @property {Boolean} strict - json body strict mode, if set strict value true, then only receive object and array json body
* @property {Number} queryString.arrayLimit - from item array length limit, default 100
* @property {Number} queryString.depth - json value deep length, default 5
* @property {Number} queryString.parameterLimit - parameter number limit, default 1000
* @property {String[]} enableTypes - parser will only parse when request type hits enableTypes, default is ['json', 'form']
* @property {Object} extendTypes - support extend types
* @property {String} onProtoPoisoning - Defines what action must take when parsing a JSON object with `__proto__`. Possible values are `'error'`, `'remove'` and `'ignore'`. Default is `'error'`, it will return `400` response when `Prototype-Poisoning` happen.
*/
bodyParser: {
enable: boolean;
encoding: string;
formLimit: string;
jsonLimit: string;
textLimit: string;
strict: boolean;
queryString: {
arrayLimit: number;
depth: number;
parameterLimit: number;
};
ignore: IgnoreOrMatch;
match: IgnoreOrMatch;
enableTypes: string[];
extendTypes: {
json: string[];
form: string[];
text: string[];
};
/** Default is `'error'`, it will return `400` response when `Prototype-Poisoning` happen. */
onProtoPoisoning: 'error' | 'remove' | 'ignore';
};
/**
* logger options
* @member Config#logger
* @property {String} dir - directory of log files
* @property {String} encoding - log file encloding, defaults to utf8
* @property {String} level - default log level, could be: DEBUG, INFO, WARN, ERROR or NONE, defaults to INFO in production
* @property {String} consoleLevel - log level of stdout, defaults to INFO in local serverEnv, defaults to WARN in unittest, defaults to NONE elsewise
* @property {Boolean} disableConsoleAfterReady - disable logger console after app ready. defaults to `false` on local and unittest env, others is `true`.
* @property {Boolean} outputJSON - log as JSON or not, defaults to false
* @property {Boolean} buffer - if enabled, flush logs to disk at a certain frequency to improve performance, defaults to true
* @property {String} errorLogName - file name of errorLogger
* @property {String} coreLogName - file name of coreLogger
* @property {String} agentLogName - file name of agent worker log
* @property {Object} coreLogger - custom config of coreLogger
* @property {Boolean} allowDebugAtProd - allow debug log at prod, defaults to false
* @property {Boolean} enablePerformanceTimer - using performance.now() timer instead of Date.now() for more more precise milliseconds, defaults to false. e.g.: logger will set 1.456ms instead of 1ms.
* @property {Boolean} enableFastContextLogger - using the app logger instead of EggContextLogger, defaults to false
*/
logger: EggLoggerConfig;
/** custom logger of egg */
customLogger: {
[key: string]: EggLoggerOptions;
};
/** Configuration of httpclient in egg. */
httpclient: HttpClientConfig;
development: {
/**
* dirs needed watch, when files under these change, application will reload, use relative path
*/
watchDirs: string[];
/**
* dirs don't need watch, including subdirectories, use relative path
*/
ignoreDirs: string[];
/**
* don't wait all plugins ready, default is true.
*/
fastReady: boolean;
/**
* whether reload on debug, default is true.
*/
reloadOnDebug: boolean;
/**
* whether override default watchDirs, default is false.
*/
overrideDefault: boolean;
/**
* whether override default ignoreDirs, default is false.
*/
overrideIgnore: boolean;
/**
* whether to reload, use https://github.com/sindresorhus/multimatch
*/
reloadPattern: string[] | string;
};
/**
* customLoader config
*/
customLoader: {
[key: string]: CustomLoaderConfig;
};
/**
* It will ignore special keys when dumpConfig
*/
dump: {
ignore: Set<string>;
};
/**
* The environment of egg
*/
env: EggEnvType;
/**
* The current HOME directory
*/
HOME: string;
hostHeaders: string;
/**
* I18n options
*/
i18n: {
/**
* default value EN_US
*/
defaultLocale: string;
/**
* i18n resource file dir, not recommend to change default value
*/
dirs: string[];
/**
* custom the locale value field, default `query.locale`, you can modify this config, such as `query.lang`
*/
queryField: string;
/**
* The locale value key in the cookie, default is locale.
*/
cookieField: string;
/**
* Locale cookie expire time, default `1y`, If pass number value, the unit will be ms
*/
cookieMaxAge: string | number;
};
/**
* Detect request' ip from specified headers, not case-sensitive. Only worked when config.proxy set to true.
*/
ipHeaders: string;
/**
* jsonp options
* @member Config#jsonp
* @property {String} callback - jsonp callback method key, default to `_callback`
* @property {Number} limit - callback method name's max length, default to `50`
* @property {Boolean} csrf - enable csrf check or not. default to false
* @property {String|RegExp|Array} whiteList - referrer white list
*/
jsonp: {
limit: number;
callback: string;
csrf: boolean;
whiteList: string | RegExp | Array<string | RegExp>;
};
/**
* The key that signing cookies. It can contain multiple keys seperated by .
*/
keys: string;
/**
* The name of the application
*/
name: string;
/**
* package.json
*/
pkg: any;
rundir: string;
security: {
domainWhiteList: string[];
protocolWhiteList: string[];
defaultMiddleware: string;
csrf: any;
ssrf: {
ipBlackList: string[];
ipExceptionList: string[];
checkAddress?(ip: string): boolean;
};
xframe: {
enable: boolean;
value: 'SAMEORIGIN' | 'DENY' | string;
};
hsts: any;
methodnoallow: { enable: boolean };
noopen: { enable: boolean; }
xssProtection: any;
csp: any;
};
siteFile: PlainObject<string | Buffer>;
watcher: PlainObject;
onClientError(err: Error, socket: Socket, app: EggApplication): ClientErrorResponse | Promise<ClientErrorResponse>;
/**
* server timeout in milliseconds, default to 0 (no timeout).
*
* for special request, just use `ctx.req.setTimeout(ms)`
*
* @see https://nodejs.org/api/http.html#http_server_timeout
*/
serverTimeout: number | null;
[prop: string]: any;
}
export interface ClientErrorResponse {
body: string | Buffer;
status: number;
headers: { [key: string]: string };
}
export interface Router extends Omit<KoaRouter<any, Context>, 'url'> {
/**
* restful router api
*/
resources(name: string, prefix: string, ...middleware: any[]): Router;
/**
* @param {String} name - Router name
* @param {Object} [params] - more parameters
* @example
* ```js
* router.url('edit_post', { id: 1, name: 'foo', page: 2 })
* => /posts/1/edit?name=foo&page=2
* router.url('posts', { name: 'foo&1', page: 2 })
* => /posts?name=foo%261&page=2
* ```
* @return {String} url by path name and query params.
* @since 1.0.0
*/
url(name: string, params?: any): string;
/**
* Alias for the url method
*/
pathFor(name: string, params?: any): string;
methods: string[];
}
export interface EggApplication extends Omit<EggCoreBase<EggAppConfig>, 'ctxStorage' | 'currentContext'> {
/**
* HttpClient instance
*/
httpclient: EggHttpClient;
/**
* Logger for Application, wrapping app.coreLogger with context infomation
*
* @member {ContextLogger} Context#logger
* @since 1.0.0
* @example
* ```js
* this.logger.info('some request data: %j', this.request.body);
* this.logger.warn('WARNING!!!!');
* ```
*/
logger: EggLogger;
/**
* core logger for framework and plugins, log file is $HOME/logs/{appname}/egg-web
*/
coreLogger: EggLogger;
/**
* All loggers contain logger, coreLogger and customLogger
*/
loggers: EggLoggers;
/**
* messenger instance
*/
messenger: Messenger;
/**
* get router
*/
router: Router;
/**
* create a singleton instance
*/
addSingleton(name: string, create: any): void;
runSchedule(schedulePath: string, ...args: any[]): Promise<any>;
/**
* http request helper base on httpclient, it will auto save httpclient log.
* Keep the same api with httpclient.request(url, args).
* See https://github.com/node-modules/urllib#api-doc for more details.
*/
curl: EggHttpClient['request'];
/**
* Get logger by name, it's equal to app.loggers['name'], but you can extend it with your own logical
*/
getLogger(name: string): EggLogger;
/**
* print the infomation when console.log(app)
*/
inspect(): any;
/**
* Alias to Router#url
*/
url(name: string, params: any): any;
/**
* Create an anonymous context, the context isn't request level, so the request is mocked.
* then you can use context level API like `ctx.service`
* @member {String} EggApplication#createAnonymousContext
* @param {Request} req - if you want to mock request like querystring, you can pass an object to this function.
* @return {Context} context
*/
createAnonymousContext(req?: Request): Context;
/**
* export context base classes, let framework can impl sub class and over context extend easily.
*/
ContextCookies: typeof EggCookies;
ContextLogger: typeof EggContextLogger;
ContextHttpClient: EggContextHttpClientConstructor;
HttpClient: EggHttpConstructor;
Subscription: typeof Subscription;
Controller: typeof Controller;
Service: typeof Service;
}
// compatible
export class EggApplication {
constructor(options?: CoreOptions);
}
export type RouterPath = string | RegExp;
export class Application extends EggApplication {
/**
* global locals for view
* @see Context#locals
*/
locals: IApplicationLocals;
/**
* HTTP get method
*/
get(path: RouterPath, fn: string): void;
get(path: RouterPath, ...middleware: any[]): void;
/**
* HTTP post method
*/
post(path: RouterPath, fn: string): void;
post(path: RouterPath, ...middleware: any[]): void;
/**
* HTTP put method
*/
put(path: RouterPath, fn: string): void;
put(path: RouterPath, ...middleware: any[]): void;
/**
* HTTP patch method
*/
patch(path: RouterPath, fn: string): void;
patch(path: RouterPath, ...middleware: any[]): void;
/**
* HTTP delete method
*/
delete(path: RouterPath, fn: string): void;
delete(path: RouterPath, ...middleware: any[]): void;
/**
* restful router api
*/
resources(name: string, prefix: string, fn: string): Router;
resources(path: string, prefix: string, ...middleware: any[]): Router;
redirect(path: string, redirectPath: string): void;
controller: IController;
middleware: KoaApplication.Middleware[] & IMiddleware;
/**
* Run async function in the background
* @see Context#runInBackground
* @param {Function} scope - the first args is an anonymous ctx
*/
runInBackground(scope: (ctx: Context) => void): void;
/**
* Run async function in the anonymous context scope
* @see Context#runInAnonymousContextScope
* @param {Function} scope - the first args is an anonymous ctx, scope should be async function
* @param {Request} req - if you want to mock request like querystring, you can pass an object to this function.
*/
runInAnonymousContextScope<R>(scope: (ctx: Context) => Promise<R>, req?: Request): Promise<R>;
/**
* Get current execute ctx async local storage
* @returns {AsyncLocalStorage} localStorage - store current execute Context
*/
get ctxStorage(): AsyncLocalStorage<Context>;
/**
* Get current execute ctx, maybe undefined
* @returns {Context} ctx - current execute Context
*/
get currentContext(): Context;
}
export interface IApplicationLocals extends PlainObject { }
export interface FileStream extends Readable { // tslint:disable-line
fields: any;
filename: string;
fieldname: string;
mime: string;
mimeType: string;
transferEncoding: string;
encoding: string;
truncated: boolean;
}
interface GetFileStreamOptions {
requireFile?: boolean; // required file submit, default is true
defCharset?: string;
limits?: {
fieldNameSize?: number;
fieldSize?: number;
fields?: number;
fileSize?: number;
files?: number;
parts?: number;
headerPairs?: number;
};
checkFile?(
fieldname: string,
file: any,
filename: string,
encoding: string,
mimetype: string
): void | Error;
}
/**
* KoaApplication's Context will carry the default 'cookie' property in
* the egg's Context interface, which is wrong here because we have our own
* special properties (e.g: encrypted). So we must remove this property and
* create our own with the same name.
* @see https://github.com/eggjs/egg/pull/2958
*
* However, the latest version of Koa has "[key: string]: any" on the
* context, and there'll be a type error for "keyof koa.Context".
* So we have to directly inherit from "KoaApplication.BaseContext" and
* rewrite all the properties to be compatible with types in Koa.
* @see https://github.com/eggjs/egg/pull/3329
*/
export interface Context<ResponseBodyT = any> extends KoaApplication.BaseContext {
[key: string]: any;
body: ResponseBodyT;
app: Application;
// properties of koa.Context
req: IncomingMessage;
res: ServerResponse;
originalUrl: string;
respond?: boolean;
service: IService;
request: Request;
response: Response<ResponseBodyT>;
// The new 'cookies' instead of Koa's.
cookies: EggCookies;
helper: IHelper;
/**
* Resource Parameters
* @example
* ##### ctx.params.id {string}
*
* `GET /api/users/1` => `'1'`
*
* ##### ctx.params.ids {Array<String>}
*
* `GET /api/users/1,2,3` => `['1', '2', '3']`
*
* ##### ctx.params.fields {Array<String>}
*
* Expect request return data fields, for example
* `GET /api/users/1?fields=name,title` => `['name', 'title']`.
*
* ##### ctx.params.data {Object}
*
* Tht request data object
*
* ##### ctx.params.page {Number}
*
* Page number, `GET /api/users?page=10` => `10`
*
* ##### ctx.params.per_page {Number}
*
* The number of every page, `GET /api/users?per_page=20` => `20`
*/
params: any;
/**
* @see Request#query
*/
query: PlainObject<string>;
/**
* @see Request#queries
*/
queries: PlainObject<string[]>;
/**
* @see Request#accept
*/
accept: accepts.Accepts;
/**
* @see Request#acceptJSON
*/
acceptJSON: boolean;
/**
* @see Request#ip
*/
ip: string;
/**
* @see Response#realStatus
*/
realStatus: number;
/**
* Set the ctx.body.data value
*
* @member {Object} Context#data=
* @example
* ```js
* ctx.data = {
* id: 1,
* name: 'fengmk2'
* };
* ```
*
* will get responce
*
* ```js
* HTTP/1.1 200 OK
*
* {
* "data": {
* "id": 1,
* "name": "fengmk2"
* }
* }
* ```
*/
data: any;
/**
* set ctx.body.meta value
*
* @example
* ```js
* ctx.meta = {
* count: 100
* };
* ```
* will get responce
*
* ```js
* HTTP/1.1 200 OK
*
* {
* "meta": {
* "count": 100
* }
* }
* ```
*/
meta: any;
/**
* locals is an object for view, you can use `app.locals` and `ctx.locals` to set variables,
* which will be used as data when view is rendering.
* The difference between `app.locals` and `ctx.locals` is the context level, `app.locals` is global level, and `ctx.locals` is request level. when you get `ctx.locals`, it will merge `app.locals`.
*
* when you set locals, only object is available
*
* ```js
* this.locals = {
* a: 1
* };
* this.locals = {
* b: 1
* };
* this.locals.c = 1;
* console.log(this.locals);
* {
* a: 1,
* b: 1,
* c: 1,
* };
* ```
*
* `ctx.locals` has cache, it only merges `app.locals` once in one request.
*
* @member {Object} Context#locals
*/
locals: IApplicationLocals & IContextLocals;
/**
* alias to {@link locals}, compatible with koa that use this variable
*/
state: any;
/**
* Logger for Application, wrapping app.coreLogger with context infomation
*
* @member {ContextLogger} Context#logger
* @since 1.0.0
* @example
* ```js
* this.logger.info('some request data: %j', this.request.body);
* this.logger.warn('WARNING!!!!');
* ```
*/
logger: EggLogger;
/**
* Get logger by name, it's equal to app.loggers['name'], but you can extend it with your own logical
*/
getLogger(name: string): EggLogger;
/**
* Request start time
*/
starttime: number;
/**
* Request start timer using `performance.now()`
*/
performanceStarttime?: number;
/**
* http request helper base on httpclient, it will auto save httpclient log.
* Keep the same api with httpclient.request(url, args).
* See https://github.com/node-modules/urllib#api-doc for more details.
*/
curl: EggHttpClient['request'];
__(key: string, ...values: string[]): string;
gettext(key: string, ...values: string[]): string;
/**
* get upload file stream
* @example
* ```js
* const stream = await this.getFileStream();
* // get other fields
* console.log(stream.fields);
* ```
* @method Context#getFileStream
* @param {Object} options
* @return {ReadStream} stream
* @since 1.0.0
*/
getFileStream(options?: GetFileStreamOptions): Promise<FileStream>;
/**
* @see Responce.redirect
*/
redirect(url: string, alt?: string): void;
httpclient: EggContextHttpClient;
}
export interface IContextLocals extends PlainObject { }
export class Controller extends BaseContextClass { }
export class Service extends BaseContextClass { }
export class Subscription extends BaseContextClass { }
/**
* The empty interface `IService` is a placeholder, for egg
* to auto injection service to ctx.service
*
* @example
*
* import { Service } from 'egg';
* class FooService extends Service {
* async bar() {}
* }
*
* declare module 'egg' {
* export interface IService {
* foo: FooService;
* }
* }
*
* Now I can get ctx.service.foo at controller and other service file.
*/
export interface IService extends PlainObject { } // tslint:disable-line
export interface IController extends PlainObject { } // tslint:disable-line
export interface IMiddleware extends PlainObject { } // tslint:disable-line
export interface IHelper extends PlainObject, BaseContextClass {
/**
* Generate URL path(without host) for route. Takes the route name and a map of named params.
* @method Helper#pathFor
* @param {String} name - Router Name
* @param {Object} params - Other params
*
* @example
* ```js
* app.get('home', '/index.htm', 'home.index');
* ctx.helper.pathFor('home', { by: 'recent', limit: 20 })
* => /index.htm?by=recent&limit=20
* ```
* @return {String} url path(without host)
*/
pathFor(name: string, params?: PlainObject): string;
/**
* Generate full URL(with host) for route. Takes the route name and a map of named params.
* @method Helper#urlFor
* @param {String} name - Router name
* @param {Object} params - Other params
* @example
* ```js
* app.get('home', '/index.htm', 'home.index');
* ctx.helper.urlFor('home', { by: 'recent', limit: 20 })
* => http://127.0.0.1:7001/index.htm?by=recent&limit=20
* ```
* @return {String} full url(with host)
*/
urlFor(name: string, params?: PlainObject): string;
}
// egg env type
export type EggEnvType = 'local' | 'unittest' | 'prod' | string;
/**
* plugin config item interface
*/
export interface IEggPluginItem {
env?: EggEnvType[];
path?: string;
package?: string;
enable?: boolean;
}
export type EggPluginItem = IEggPluginItem | boolean;
/**
* build-in plugin list
*/
export interface EggPlugin {
[key: string]: EggPluginItem | undefined;
onerror?: EggPluginItem;
session?: EggPluginItem;
i18n?: EggPluginItem;
watcher?: EggPluginItem;
multipart?: EggPluginItem;
security?: EggPluginItem;
development?: EggPluginItem;
logrotator?: EggPluginItem;
schedule?: EggPluginItem;
static?: EggPluginItem;
jsonp?: EggPluginItem;
view?: EggPluginItem;
}
/**
* Singleton instance in Agent Worker, extend {@link EggApplication}
*/
export class Agent extends EggApplication {
}
export interface ClusterOptions {
/** specify framework that can be absolute path or npm package */
framework?: string;
/** directory of application, default to `process.cwd()` */
baseDir?: string;
/** customized plugins, for unittest */
plugins?: object | null;
/** numbers of app workers, default to `os.cpus().length` */
workers?: number;
/** listening port, default to 7001(http) or 8443(https) */
port?: number;
/** https or not */
https?: boolean;
/** ssl key */
key?: string;
/** ssl cert */
cert?: string;
[prop: string]: any;
}
export function startCluster(options: ClusterOptions, callback: (...args: any[]) => any): void;
export interface StartOptions {
/** specify framework that can be absolute path or npm package */
framework?: string;
/** directory of application, default to `process.cwd()` */
baseDir?: string;
/** ignore single process mode warning */
ignoreWarning?: boolean;
}
export function start(options?: StartOptions): Promise<Application>
/**
* Powerful Partial, Support adding ? modifier to a mapped property in deep level
* @example
* import { PowerPartial, EggAppConfig } from 'egg';
*
* // { view: { defaultEngines: string } } => { view?: { defaultEngines?: string } }
* type EggConfig = PowerPartial<EggAppConfig>
*/
export type PowerPartial<T> = {
[U in keyof T]?: T[U] extends object
? PowerPartial<T[U]>
: T[U]
};
// send data can be number|string|boolean|object but not Set|Map
export interface Messenger extends EventEmitter {
/**
* broadcast to all agent/app processes including itself
*/
broadcast(action: string, data: any): void;
/**
* send to agent from the app,
* send to an random app from the agent
*/
sendRandom(action: string, data: any): void;
/**
* send to specified process
*/
sendTo(pid: number, action: string, data: any): void;
/**
* send to agent from the app,
* send to itself from the agent
*/
sendToAgent(action: string, data: any): void;
/**
* send to all app including itself from the app,
* send to all app from the agent
*/
sendToApp(action: string, data: any): void;
}
// compatible
export interface EggLoaderOptions extends CoreLoaderOptions { }
export interface EggLoader extends CoreLoader { }
/**
* App worker process Loader, will load plugins
* @see https://github.com/eggjs/egg-core
*/
export class AppWorkerLoader extends CoreLoader {
loadConfig(): void;
load(): void;
}
/**
* Agent worker process loader
* @see https://github.com/eggjs/egg-loader
*/
export class AgentWorkerLoader extends CoreLoader {
loadConfig(): void;
load(): void;
}
export interface IBoot {
/**
* Ready to call configDidLoad,
* Config, plugin files are referred,
* this is the last chance to modify the config.
*/
configWillLoad?(): void;
/**
* Config, plugin files have loaded
*/
configDidLoad?(): void;
/**
* All files have loaded, start plugin here
*/
didLoad?(): Promise<void>;
/**
* All plugins have started, can do some thing before app ready
*/
willReady?(): Promise<void>;
/**
* Worker is ready, can do some things,
* don't need to block the app boot
*/
didReady?(): Promise<void>;
/**
* Server is listening
*/
serverDidReady?(): Promise<void>;
/**
* Do some thing before app close
*/
beforeClose?(): Promise<void>;
}
export interface Singleton<T> {
get(id: string): T;
}
}

Опубликовать ( 0 )

Вы можете оставить комментарий после Вход в систему

1
https://api.gitlife.ru/oschina-mirror/mirrors-eggjs.git
git@api.gitlife.ru:oschina-mirror/mirrors-eggjs.git
oschina-mirror
mirrors-eggjs
mirrors-eggjs
master