@gabliam/koa
Version:
Gabliam plugin for add koa
251 lines (250 loc) • 8.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.KoaRequest = void 0;
const tslib_1 = require("tslib");
const lodash_1 = require("lodash");
const property_tunnel_1 = require("property-tunnel");
/* istanbul ignore next */
class KoaRequest {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore : context use with property-tunnel
constructor(context, request) {
this.context = context;
this.request = request;
/**
* Add private query.
* In koa, when you set value to query, koa update querystring with qs.stringify.
* When validate interceptor is used, if you set Joi.number, validate cast query to number.
*
* @see {@link https://github.com/koajs/koa/blob/master/src/request.js#L186}
*/
this._query = {};
}
/**
* Return the original request
*/
get originalRequest() {
return this.request;
}
/**
* Get parsed query-string.
* Set query-string as an object.
*/
set query(query) {
this._query = query;
this.request.query = query;
}
get query() {
return (0, lodash_1.merge)({}, this.request.query, this._query);
}
/**
* Check if the request was an _XMLHttpRequest_.
*/
get xhr() {
return this.request.get('X-Requested-With') === 'XMLHttpRequest';
}
/**
* 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) {
return this.request.is(type);
}
}
tslib_1.__decorate([
(0, property_tunnel_1.alias)(['request', 'body'])
// body
,
tslib_1.__metadata("design:type", Object)
], KoaRequest.prototype, "body", void 0);
tslib_1.__decorate([
(0, property_tunnel_1.alias)(['request', 'headers']),
tslib_1.__metadata("design:type", Object)
], KoaRequest.prototype, "headers", void 0);
tslib_1.__decorate([
(0, property_tunnel_1.alias)(['request', 'fresh'], { access: 'readonly' }),
tslib_1.__metadata("design:type", Boolean)
], KoaRequest.prototype, "fresh", void 0);
tslib_1.__decorate([
(0, property_tunnel_1.alias)(['request', 'hostname'], { access: 'readonly' }),
tslib_1.__metadata("design:type", String)
], KoaRequest.prototype, "hostname", void 0);
tslib_1.__decorate([
(0, property_tunnel_1.alias)(['request', 'ip'], { access: 'readonly' }),
tslib_1.__metadata("design:type", String)
], KoaRequest.prototype, "ip", void 0);
tslib_1.__decorate([
(0, property_tunnel_1.alias)(['request', 'ips'], { access: 'readonly' }),
tslib_1.__metadata("design:type", Array)
], KoaRequest.prototype, "ips", void 0);
tslib_1.__decorate([
(0, property_tunnel_1.alias)(['request', 'file'], { access: 'readonly' }),
tslib_1.__metadata("design:type", Object)
], KoaRequest.prototype, "file", void 0);
tslib_1.__decorate([
(0, property_tunnel_1.alias)(['request', 'files'], { access: 'readonly' }),
tslib_1.__metadata("design:type", Object)
], KoaRequest.prototype, "files", void 0);
tslib_1.__decorate([
(0, property_tunnel_1.alias)(['request', 'method']),
tslib_1.__metadata("design:type", String)
], KoaRequest.prototype, "method", void 0);
tslib_1.__decorate([
(0, property_tunnel_1.alias)(['request', 'originalUrl'], { access: 'readonly' }),
tslib_1.__metadata("design:type", String)
], KoaRequest.prototype, "originalUrl", void 0);
tslib_1.__decorate([
(0, property_tunnel_1.alias)(['request', 'url']),
tslib_1.__metadata("design:type", String)
], KoaRequest.prototype, "url", void 0);
tslib_1.__decorate([
(0, property_tunnel_1.alias)(['context', 'params']),
tslib_1.__metadata("design:type", Object)
], KoaRequest.prototype, "params", void 0);
tslib_1.__decorate([
(0, property_tunnel_1.alias)(['request', 'path']),
tslib_1.__metadata("design:type", String)
], KoaRequest.prototype, "path", void 0);
tslib_1.__decorate([
(0, property_tunnel_1.alias)(['request', 'protocol'], { access: 'readonly' }),
tslib_1.__metadata("design:type", String)
], KoaRequest.prototype, "protocol", void 0);
tslib_1.__decorate([
(0, property_tunnel_1.alias)(['request', 'querystring']),
tslib_1.__metadata("design:type", String)
], KoaRequest.prototype, "querystring", void 0);
tslib_1.__decorate([
(0, property_tunnel_1.alias)(['request', 'secure'], { access: 'readonly' }),
tslib_1.__metadata("design:type", Boolean)
], KoaRequest.prototype, "secure", void 0);
tslib_1.__decorate([
(0, property_tunnel_1.alias)(['request', 'stale'], { access: 'readonly' }),
tslib_1.__metadata("design:type", Boolean)
], KoaRequest.prototype, "stale", void 0);
tslib_1.__decorate([
(0, property_tunnel_1.alias)(['request', 'subdomains'], { access: 'readonly' }),
tslib_1.__metadata("design:type", Array)
], KoaRequest.prototype, "subdomains", void 0);
exports.KoaRequest = KoaRequest;