UNPKG

@tsed/common

Version:
118 lines 3.47 kB
"use strict"; var PlatformRequest_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.PlatformRequest = void 0; const tslib_1 = require("tslib"); const di_1 = require("@tsed/di"); /** * Platform Request abstraction layer. * @platform */ let PlatformRequest = PlatformRequest_1 = class PlatformRequest { constructor(raw) { this.raw = raw; } /** * Get the url of the request. * * Is equivalent of `express.response.originalUrl || express.response.url`. */ get url() { return this.raw.originalUrl || this.raw.url; } get headers() { return this.raw.headers; } get method() { return this.raw.method; } /** * Contains key-value pairs of data submitted in the request body. By default, it is `undefined`, and is populated when you use * `body-parsing` middleware such as `express.json()` or `express.urlencoded()`. */ get body() { return this.raw.body; } /** * When using `cookie-parser` middleware, this property is an object that contains cookies sent by the request. * If the request contains no cookies, it defaults to `{}`. */ get cookies() { return this.raw.cookies; } /** * This property is an object containing properties mapped to the named route `parameters`. * For example, if you have the route `/user/:name`, then the `name` property is available as `req.params.name`. * This object defaults to `{}`. */ get params() { return this.raw.params; } /** * This property is an object containing a property for each query string parameter in the route. * When query parser is set to disabled, it is an empty object `{}`, otherwise it is the result of the configured query parser. */ get query() { return this.raw.query; } /** * This property is an object containing a property for each session attributes set by any code. * It require to install a middleware like express-session to work. */ get session() { return this.raw.session; } /** * Create a new instance of PlatformRequest * @param injector * @param req */ static create(injector, req) { const locals = new Map(); locals.set(di_1.DI_PARAM_OPTIONS, req); return injector.invoke(PlatformRequest_1, locals); } /** * Returns the HTTP request header specified by field. The match is case-insensitive. * * ```typescript * request.get('Content-Type') // => "text/plain" * ``` * * @param name */ get(name) { return this.raw.get(name); } accepts(mime) { // @ts-ignore return this.raw.accepts(mime); } isAborted() { return this.raw.aborted; } destroy() { // @ts-ignore delete this.raw; } /** * Return the Framework response object (express, koa, etc...) */ getRequest() { return this.raw; } /** * Return the Node.js response object */ getReq() { return this.raw; } }; PlatformRequest = PlatformRequest_1 = tslib_1.__decorate([ di_1.Injectable(), di_1.Scope(di_1.ProviderScope.INSTANCE), tslib_1.__param(0, di_1.Opts), tslib_1.__metadata("design:paramtypes", [Object]) ], PlatformRequest); exports.PlatformRequest = PlatformRequest; //# sourceMappingURL=PlatformRequest.js.map