UNPKG

@tsed/platform-http

Version:
70 lines (69 loc) 2.02 kB
import { DIContext, injector } from "@tsed/di"; import { $asyncEmit } from "@tsed/hooks"; import { PlatformApplication } from "../services/PlatformApplication.js"; import { PlatformRequest } from "../services/PlatformRequest.js"; import { PlatformResponse } from "../services/PlatformResponse.js"; export class PlatformContext extends DIContext { #isFinished; constructor(options) { super(options); this.PLATFORM = "WWW"; this.#isFinished = false; options.endpoint && (this.endpoint = options.endpoint); this.event = options.event; this.response = new (options.ResponseKlass || PlatformResponse)(this); this.request = new (options.RequestKlass || PlatformRequest)(this); this.request.request.$ctx = this; this.request.request.id = this.id; this.response.setHeader("x-request-id", this.id); } get url() { return this.request.url; } get app() { return injector().get(PlatformApplication); } start() { return $asyncEmit("$onRequest", [this]); } async finish() { await Promise.all([$asyncEmit("$onResponse", [this]), this.destroy()]); this.#isFinished = true; } isFinished() { return this.#isFinished; } isDone() { return this.request?.isAborted() || this.response?.isDone() || this.isFinished(); } /** * Return the framework request instance (Express, Koa, etc...) */ getRequest() { return this.request.getRequest(); } /** * Return the framework response instance (Express, Koa, etc...) */ getResponse() { return this.response.getResponse(); } /** * Get Node.js request */ getReq() { return this.request.getReq(); } /** * Get Node.js response */ getRes() { return this.response.getRes(); } /** * Return the original application instance. */ getApp() { return this.app.getApp(); } }