@tsed/common
Version:
A TypeScript Framework on top of Express
102 lines (101 loc) • 3.14 kB
TypeScript
/// <reference types="node" />
import { InjectorService, LocalsContainer } from "@tsed/di";
import { IncomingMessage, ServerResponse } from "http";
import { EndpointMetadata } from "../../mvc/models/EndpointMetadata";
import { PlatformApplication } from "../services/PlatformApplication";
import { PlatformRequest } from "../services/PlatformRequest";
import { PlatformResponse } from "../services/PlatformResponse";
import { RequestLogger, RequestLoggerOptions } from "./RequestLogger";
export interface RequestContextOptions extends Omit<RequestLoggerOptions, "dateStart"> {
id: string;
logger: any;
injector?: InjectorService;
response?: PlatformResponse;
request?: PlatformRequest;
endpoint?: EndpointMetadata;
}
export declare class PlatformContext extends Map<any, any> {
/**
* Request id generated by @@contextMiddleware@@.
*
* ::: tip
* By default Ts.ED generate uuid like that `uuidv4().replace(/-/gi, ""))`.
* Dash are removed to simplify tracking logs in Kibana
* :::
*
* ::: tip
* Request id can by customized by changing the server configuration.
*
* ```typescript
* @Configuration({
* logger: {
* reqIdBuilder: createUniqId // give your own id generator function
* }
* })
* class Server {
*
* }
* ```
* :::
*
*/
readonly id: string;
/**
* Date when request have been handled by the server. @@RequestLogger@@ use this date to log request duration.
*/
readonly dateStart: Date;
/**
* The request container used by the Ts.ED DI. It contain all services annotated with `@Scope(ProviderScope.REQUEST)`
*/
container: LocalsContainer<any>;
/**
* The current @@EndpointMetadata@@ resolved by Ts.ED during the request.
*/
endpoint: EndpointMetadata;
/**
* The data return by the previous endpoint if you use multiple handler on the same route. By default data is empty.
*/
data: any;
/**
* Logger attached to the context request.
*/
logger: RequestLogger;
/**
* The current @@PlatformResponse@@.
*/
response: PlatformResponse;
/**
* The current @@PlatformRequest@@.
*/
request: PlatformRequest;
/**
*
*/
injector: InjectorService;
constructor({ id, injector, logger, response, request, endpoint, ...options }: RequestContextOptions);
get env(): import("@tsed/core").Env;
get app(): PlatformApplication<TsED.Application, TsED.Router>;
destroy(): Promise<void>;
isDone(): boolean;
emit(eventName: string, ...args: any[]): Promise<void>;
/**
* Return the framework request instance (Express, Koa, etc...)
*/
getRequest<Req = any>(): Req;
/**
* Return the framework response instance (Express, Koa, etc...)
*/
getResponse<Res = any>(): Res;
/**
* Get Node.js request
*/
getReq(): IncomingMessage;
/**
* Get Node.js response
*/
getRes(): ServerResponse;
/**
* Return the original application instance.
*/
getApp<T = any>(): T;
}