UNPKG

@gabliam/express

Version:
244 lines (243 loc) 8.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ExpressRequest = void 0; const tslib_1 = require("tslib"); const property_tunnel_1 = require("property-tunnel"); const qs_1 = tslib_1.__importDefault(require("qs")); const url_1 = tslib_1.__importDefault(require("url")); /* istanbul ignore next */ class ExpressRequest { constructor(request) { this.request = request; } /** * Return the original request */ get originalRequest() { return this.request; } /** * Get/Set query string. */ get querystring() { let search = url_1.default.parse(this.request.url, true).search || ''; if (search && search[0] === '?') { search = search.slice(1); } return search; } set querystring(querystring) { if (querystring) { this.request.query = qs_1.default.parse(querystring); } } /** * Check if the given `type(s)` is acceptable, returning * the best match when true, otherwise `undefined`, in which * case you should respond with 406 "Not Acceptable". * * The `type` value may be a single mime type string * such as "application/json", the extension name * such as "json", a comma-delimted list such as "json, html, text/plain", * or an array `["json", "html", "text/plain"]`. When a list * or array is given the _best_ match, if any is returned. * * Examples: * * // Accept: text/html * req.accepts('html'); * // => "html" * * // Accept: text/*, application/json * req.accepts('html'); * // => "html" * req.accepts('text/html'); * // => "text/html" * req.accepts('json, text'); * // => "json" * req.accepts('application/json'); * // => "application/json" * * // Accept: text/*, application/json * req.accepts('image/png'); * req.accepts('png'); * // => undefined * * // Accept: text/*;q=.5, application/json * req.accepts(['html', 'json']); * req.accepts('html, json'); * // => "json" */ accepts(...type) { if (type.length) { return this.request.accepts(...type); } return this.request.accepts(); } /** * Return accepted charsets or best fit based on `charsets`. * * Given `Accept-Charset: utf-8, iso-8859-1;q=0.2, utf-7;q=0.5` * an array sorted by quality is returned: * * ['utf-8', 'utf-7', 'iso-8859-1'] */ acceptsCharsets(...charsets) { if (charsets.length) { return this.request.acceptsCharsets(...charsets); } return this.request.acceptsCharsets(); } /** * Return accepted encodings or best fit based on `encodings`. * * Given `Accept-Encoding: gzip, deflate` * an array sorted by quality is returned: * * ['gzip', 'deflate'] */ acceptsEncodings(...encodings) { if (encodings.length) { return this.request.acceptsEncodings(...encodings); } return this.request.acceptsEncodings(); } /** * Return accepted languages or best fit based on `langs`. * * Given `Accept-Language: en;q=0.8, es, pt` * an array sorted by quality is returned: * * ['es', 'pt', 'en'] */ acceptsLanguages(...langs) { if (langs.length) { return this.request.acceptsLanguages(...langs); } return this.request.acceptsLanguages(); } /** * Return request header. * * The `Referrer` header field is special-cased, * both `Referrer` and `Referer` are interchangeable. * * Examples: * * this.get('Content-Type'); * // => "text/plain" * * this.get('content-type'); * // => "text/plain" * * this.get('Something'); * // => undefined */ get(field) { return this.request.get(field); } /** * Check if the incoming request contains the "Content-Type" * header field, and it contains the give mime `type`. * * Examples: * * // With Content-Type: text/html; charset=utf-8 * req.is('html'); * req.is('text/html'); * req.is('text/*'); * // => true * * // When Content-Type is application/json * req.is('json'); * req.is('application/json'); * req.is('application/*'); * // => true * * req.is('html'); * // => false */ is(type) { const is = this.request.is(type); return is === null ? false : is; } } tslib_1.__decorate([ (0, property_tunnel_1.alias)(['request', 'body']) // body , tslib_1.__metadata("design:type", Object) ], ExpressRequest.prototype, "body", void 0); tslib_1.__decorate([ (0, property_tunnel_1.alias)(['request', 'headers']), tslib_1.__metadata("design:type", Object) ], ExpressRequest.prototype, "headers", void 0); tslib_1.__decorate([ (0, property_tunnel_1.alias)(['request', 'fresh'], { access: 'readonly' }), tslib_1.__metadata("design:type", Boolean) ], ExpressRequest.prototype, "fresh", void 0); tslib_1.__decorate([ (0, property_tunnel_1.alias)(['request', 'hostname'], { access: 'readonly' }), tslib_1.__metadata("design:type", String) ], ExpressRequest.prototype, "hostname", void 0); tslib_1.__decorate([ (0, property_tunnel_1.alias)(['request', 'ip'], { access: 'readonly' }), tslib_1.__metadata("design:type", String) ], ExpressRequest.prototype, "ip", void 0); tslib_1.__decorate([ (0, property_tunnel_1.alias)(['request', 'ips'], { access: 'readonly' }), tslib_1.__metadata("design:type", Array) ], ExpressRequest.prototype, "ips", void 0); tslib_1.__decorate([ (0, property_tunnel_1.alias)(['request', 'file'], { access: 'readonly' }), tslib_1.__metadata("design:type", Object) ], ExpressRequest.prototype, "file", void 0); tslib_1.__decorate([ (0, property_tunnel_1.alias)(['request', 'files'], { access: 'readonly' }), tslib_1.__metadata("design:type", Object) ], ExpressRequest.prototype, "files", void 0); tslib_1.__decorate([ (0, property_tunnel_1.alias)(['request', 'method']), tslib_1.__metadata("design:type", String) ], ExpressRequest.prototype, "method", void 0); tslib_1.__decorate([ (0, property_tunnel_1.alias)(['request', 'originalUrl'], { access: 'readonly' }), tslib_1.__metadata("design:type", String) ], ExpressRequest.prototype, "originalUrl", void 0); tslib_1.__decorate([ (0, property_tunnel_1.alias)(['request', 'url']), tslib_1.__metadata("design:type", String) ], ExpressRequest.prototype, "url", void 0); tslib_1.__decorate([ (0, property_tunnel_1.alias)(['request', 'params']), tslib_1.__metadata("design:type", Object) ], ExpressRequest.prototype, "params", void 0); tslib_1.__decorate([ (0, property_tunnel_1.alias)(['request', 'path']), tslib_1.__metadata("design:type", String) ], ExpressRequest.prototype, "path", void 0); tslib_1.__decorate([ (0, property_tunnel_1.alias)(['request', 'protocol'], { access: 'readonly' }), tslib_1.__metadata("design:type", String) ], ExpressRequest.prototype, "protocol", void 0); tslib_1.__decorate([ (0, property_tunnel_1.alias)(['request', 'query']), tslib_1.__metadata("design:type", Object) ], ExpressRequest.prototype, "query", void 0); tslib_1.__decorate([ (0, property_tunnel_1.alias)(['request', 'secure'], { access: 'readonly' }), tslib_1.__metadata("design:type", Boolean) ], ExpressRequest.prototype, "secure", void 0); tslib_1.__decorate([ (0, property_tunnel_1.alias)(['request', 'stale'], { access: 'readonly' }), tslib_1.__metadata("design:type", Boolean) ], ExpressRequest.prototype, "stale", void 0); tslib_1.__decorate([ (0, property_tunnel_1.alias)(['request', 'subdomains'], { access: 'readonly' }), tslib_1.__metadata("design:type", Array) ], ExpressRequest.prototype, "subdomains", void 0); tslib_1.__decorate([ (0, property_tunnel_1.alias)(['request', 'xhr'], { access: 'readonly' }), tslib_1.__metadata("design:type", Boolean) ], ExpressRequest.prototype, "xhr", void 0); exports.ExpressRequest = ExpressRequest;