egg
Version:
A web application framework for Node.js
333 lines • 11.1 kB
TypeScript
import { MetaMiddlewareOptions } from "../app/middleware/meta.js";
import { NotFoundMiddlewareOptions } from "../app/middleware/notfound.js";
import { SiteFileMiddlewareOptions } from "../app/middleware/site_file.js";
import "./types.plugin.js";
import Context$1 from "../app/extend/context.js";
import "./egg.js";
import { Application } from "./application.js";
import { EggAppConfig as EggAppConfig$1, EggAppInfo, FileLoaderOptions } from "@eggjs/core";
import { EggLoggerOptions, EggLoggersOptions } from "egg-logger";
import { Dispatcher, RequestOptions } from "urllib";
import { LookupFunction, Socket } from "node:net";
import { PartialDeep } from "type-fest";
//#region src/lib/types.d.ts
type IgnoreItem = string | RegExp | ((ctx: Context$1) => boolean);
type IgnoreOrMatch = IgnoreItem | IgnoreItem[];
export interface ClientErrorResponse {
body: string | Buffer;
status: number;
headers: {
[key: string]: string;
};
}
/** egg env type */
export type EggEnvType = "local" | "unittest" | "prod" | string;
/** logger config of egg */
export interface EggLoggerConfig extends Omit<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;
/** [deprecated] Defaults to `true`. */
enablePerformanceTimer?: boolean;
/** using the app logger instead of EggContextLogger, defaults to `false` */
enableFastContextLogger?: boolean;
}
/** Custom Loader Configuration */
export interface CustomLoaderConfig extends Omit<FileLoaderOptions, "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 HttpClientConfig {
/** Request timeout */
timeout?: number;
/** Default request args for httpclient */
request?: RequestOptions;
/**
* @deprecated keep compatible with egg 3.x, no more used
*/
useHttpClientNext?: boolean;
/**
* Allow http2
*/
allowH2?: boolean;
lookup?: LookupFunction;
/**
* Interceptors for request composition, applied via `Dispatcher.compose()`.
* Each interceptor receives a `dispatch` function and returns a new `dispatch` function.
*
* @example
* ```ts
* // config.default.ts
* config.httpclient = {
* interceptors: [
* (dispatch) => (opts, handler) => {
* opts.headers = { ...opts.headers, 'x-trace-id': generateTraceId() };
* return dispatch(opts, handler);
* },
* ],
* };
* ```
*/
interceptors?: Dispatcher.DispatcherComposeInterceptor[];
}
/**
* 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>
*
* @deprecated use `PartialDeep` instead
*/
export type PowerPartial<T> = PartialDeep<T>;
export declare const EggAppConfig: ObjectConstructor;
export interface EggAppConfig extends EggAppConfig$1 {
workerStartTimeout: number;
baseDir: string;
middleware: string[];
coreMiddleware: 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";
onerror(err: any, ctx: Context$1): void;
};
/**
* logger options
* @member Config#logger
* @property {String} dir - directory of log files
* @property {String} encoding - log file encoding, 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, others is `NONE`
* @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} 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;
/**
* customLoader config
*/
/**
* It will ignore special keys when dumpConfig
*/
dump: {
ignore: Set<string | RegExp>;
timing: {
slowBootActionMinDuration: number;
};
};
/**
* The environment of egg
*/
env: EggEnvType;
/**
* The current HOME directory
*/
HOME: string;
hostHeaders: string;
/**
* Detect request' ip from specified headers, not case-sensitive. Only worked when config.proxy set to true.
*/
ipHeaders: string;
protocolHeaders: string;
maxProxyCount: number;
maxIpsCount: number;
proxy: boolean;
cookies: {
sameSite?: string;
httpOnly?: boolean;
};
/**
* The key that signing cookies. It can contain multiple keys separated by `.`
* @requires Cookie secret key to sign and encrypt, see https://eggjs.org/core/cookie-and-session#cookie-secret-key
*/
keys: string;
/**
* The name of the application
*/
name: string;
/**
* package.json
*/
pkg: Record<string, any>;
rundir: string;
/**
* siteFile middleware options
*/
siteFile: SiteFileMiddlewareOptions;
/**
* meta middleware options
*/
meta: MetaMiddlewareOptions;
/**
* notfound middleware options
*/
notfound: NotFoundMiddlewareOptions;
/**
* overrideMethod middleware options
*/
overrideMethod: {
enable: boolean;
allowedMethods: string[];
};
/**
* onClientError handler
*/
onClientError?(err: Error, socket: Socket, app: Application): 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;
cluster: {
listen: {
path: string;
port: number;
hostname: string;
/**
* enable SO_REUSEPORT socket option for server listen, default is `false`.
* Only available on Linux 3.9+, DragonFlyBSD 3.6+, FreeBSD 12.0+, Solaris 11.4, and AIX 7.2.5+.
* @see https://nodejs.org/api/net.html#serverlistenoptions-callback
*/
reusePort?: boolean;
};
};
clusterClient: {
maxWaitTime: number;
responseTimeout: number;
};
[prop: string]: any;
}
export type RequestObjectBody = Record<string, any>;
/**
* 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;
}
/**
* 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 Record<string, any> {}
/**
* The empty interface `IController` is a placeholder, for egg
* to auto injection controller to app.controller
*
* @example
*
* import { Controller } from 'egg';
* class HomeController extends Controller {
* async index() {}
* }
*
* declare module 'egg' {
* export interface IController {
* home: HomeController;
* }
* }
*
* Now I can get app.controller.home in the application.
*/
export interface IController extends Record<string, any> {}
//#endregion
export type { EggAppInfo, PartialDeep };