@tsed/platform-http
Version:
A TypeScript Framework on top of Express
135 lines (134 loc) • 3.55 kB
JavaScript
import { injectable, ProviderScope } from "@tsed/di";
/**
* Platform Request abstraction layer.
* @platform
*/
export class PlatformRequest {
constructor($ctx) {
this.$ctx = $ctx;
}
/**
* The current @@PlatformResponse@@.
*/
get response() {
return this.$ctx.response;
}
get raw() {
return this.$ctx.event.request;
}
get secure() {
return this.raw.secure;
}
get host() {
return this.get("host");
}
get protocol() {
return this.raw.protocol;
}
/**
* 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;
}
get rawBody() {
return this.raw.rawBody || 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 requires to install a middleware like express-session to work.
*/
get session() {
return this.raw.session;
}
get files() {
return this.raw.files;
}
get route() {
return this.$ctx.handlerMetadata?.path;
}
/**
* Return the original request framework instance
*/
get request() {
return this.getRequest();
}
/**
* Return the original request node.js instance
*/
get req() {
return this.getReq();
}
/**
* 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);
}
getHeader(name) {
return this.get(name);
}
accepts(mime) {
// @ts-ignore
return this.raw.accepts(mime);
}
isAborted() {
return this.raw.aborted;
}
/**
* Return the Framework response object (express, koa, etc...)
*/
getRequest() {
return this.raw;
}
/**
* Return the Node.js response object
*/
getReq() {
return this.$ctx.event.request;
}
}
injectable(PlatformRequest).scope(ProviderScope.INSTANCE);