import {ApiRequest} from "@gongt/ts-stl-library/request/protocol"; import {Request, Response} from "express-serve-static-core"; import {ResponseInterface} from "./response-wrapper"; export interface RequestContextCreator { new(req: Request, res: Response): RequestContext } export abstract class RequestContext { protected req: Request; protected res: Response; public readonly response: ResponseInterface; public readonly params: ReqType&ApiRequest = {}; constructor(req: Request, res: Response) { this.req = req; this.res = res; this.response = this.createResponseWrapper(); } protected abstract createResponseWrapper(): ResponseInterface; get __dump() { return { req: this.req, res: this.res, } } /** @internal */ get _request_raw() { return this.req; } /** @internal */ get _response_raw() { return this.res; } getHeader(key: string) { return this.req.headers[key]; } get isXhr() { return this.req.xhr; } setSession(name: string, value: any) { if (!this.req['session']) { throw new Error('using session without start it.') } this.req['session'][name] = value; } }