egg
Version:
A web application framework for Node.js
181 lines • 4.95 kB
TypeScript
import { EggContextHttpClient as HttpClient, HttpClientRequestOptions, HttpClientRequestURL } from "../../lib/core/httpclient.js";
import { IService } from "../../lib/types.js";
import Response$1 from "./response.js";
import Request$1 from "./request.js";
import { Application } from "../../lib/application.js";
import Helper from "./helper.js";
import { Context as Context$1, Router } from "@eggjs/core";
import { Cookies } from "@eggjs/cookies";
import { EggLogger } from "egg-logger";
//#region src/app/extend/context.d.ts
interface Cookies$1 extends Cookies {
request: any;
response: any;
}
export default class Context extends Context$1 {
app: Application;
request: Request$1;
response: Response$1;
service: IService;
proxy: any;
/**
* Request start time
* @member {Number} Context#starttime
*/
starttime: number;
/**
* Request start timer using `performance.now()`
* @member {Number} Context#performanceStarttime
*/
performanceStarttime: number;
/**
* Get the current visitor's cookies.
*/
get cookies(): Cookies$1;
/**
* Get a wrapper httpclient instance contain ctx in the hold request process
*
* @return {HttpClient} the wrapper httpclient instance
*/
get httpclient(): HttpClient;
/**
* Alias to {@link Context#httpclient}
*/
get httpClient(): HttpClient;
/**
* Shortcut for httpclient.curl
*
* @function Context#curl
* @param {String|Object} url - request url address.
* @param {Object} [options] - options for request.
* @return {Object} see {@link ContextHttpClient#curl}
*/
curl(url: HttpClientRequestURL, options?: HttpClientRequestOptions): ReturnType<HttpClient["request"]>;
/**
* Alias to {@link Application#router}
*
* @member {Router} Context#router
* @since 1.0.0
* @example
* ```js
* this.router.pathFor('post', { id: 12 });
* ```
*/
get router(): Router;
/**
* Set router to Context, only use on EggRouter
* @param {Router} val router instance
*/
set router(val: Router);
/**
* Get helper instance from {@link Application#Helper}
*
* @member {Helper} Context#helper
* @since 1.0.0
*/
get helper(): Helper;
/**
* Wrap app.loggers with context information,
* if a custom logger is defined by naming aLogger, then you can `ctx.getLogger('aLogger')`
*
* @param {String} name - logger name
*/
getLogger(name: string): EggLogger;
/**
* Logger for Application
*
* @member {Logger} Context#logger
* @since 1.0.0
* @example
* ```js
* this.logger.info('some request data: %j', this.request.body);
* this.logger.warn('WARNING!!!!');
* ```
*/
get logger(): EggLogger;
/**
* Logger for frameworks and plugins
*
* @member {Logger} Context#coreLogger
* @since 1.0.0
*/
get coreLogger(): EggLogger;
/**
* 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
*/
get locals(): Record<string, any>;
set locals(val: Record<string, any>);
/**
* alias to {@link Context#locals}, compatible with koa that use this variable
* @member {Object} state
* @see Context#locals
*/
get state(): Record<string, any>;
set state(val: Record<string, any>);
/**
* Run async function in the background
* @param {Function} scope - the first args is ctx
* ```js
* this.body = 'hi';
*
* this.runInBackground(async ctx => {
* await ctx.mysql.query(sql);
* await ctx.curl(url);
* });
* ```
*/
runInBackground(scope: (ctx: Context) => Promise<void>, taskName?: string): void;
_runInBackground(scope: (ctx: Context) => Promise<void>, taskName: string): Promise<void>;
/**
* @member {Boolean} Context#acceptJSON
* @see Request#acceptJSON
* @since 1.0.0
*/
get acceptJSON(): boolean;
get query(): Record<string, string>;
/**
* @member {Array} Context#queries
* @see Request#queries
* @since 1.0.0
*/
get queries(): Record<string, string[]>;
/**
* @member {string} Context#ip
* @see Request#ip
* @since 1.0.0
*/
get ip(): string;
set ip(val: string);
/**
* @member {Number} Context#realStatus
* @see Response#realStatus
* @since 1.0.0
*/
get realStatus(): number;
set realStatus(val: number);
}
//#endregion