@gabliam/koa
Version:
Gabliam plugin for add koa
149 lines (148 loc) • 4.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.KoaResponse = void 0;
const tslib_1 = require("tslib");
const koa_send_1 = tslib_1.__importDefault(require("koa-send"));
const property_tunnel_1 = require("property-tunnel");
/* istanbul ignore next */
class KoaResponse {
constructor(context, response) {
this.context = context;
this.response = response;
}
get originalResponse() {
return this.response;
}
/**
* Redirect to the given `url` with optional response `status`
* defaulting to 302.
*
* The resulting `url` is determined by `res.location()`, so
* it will play nicely with mounted apps, relative paths,
* `"back"` etc.
*
* Examples:
*
* res.redirect('/foo/bar');
* res.redirect('http://example.com');
* res.redirect('http://example.com', 301);
* res.redirect('../login'); // /blog/post/1 -> /blog/login
*/
redirect(url, status) {
if (status) {
this.context.status = status;
}
this.response.redirect(url);
}
/**
* Transfer the file at the given `path`.
*
* Automatically sets the _Content-Type_ response header field.
* The callback `fn(err)` is invoked when the transfer is complete
* or when an error occurs. Be sure to check `res.sentHeader`
* if you wish to attempt responding, as the header and some data
* may have already been transferred.
*
* Options:
*
* - `maxAge` defaulting to 0 (can be string converted by `ms`)
* - `root` root directory for relative filenames
* - `headers` object of headers to serve with file
* - `dotfiles` serve dotfiles, defaulting to false; can be `"allow"` to send them
*
* Other options are passed along to `send`.
*
* Examples:
*
* The following example illustrates how `res.sendFile()` may
* be used as an alternative for the `static()` middleware for
* dynamic situations. The code backing `res.sendFile()` is actually
* the same code, so HTTP cache support etc is identical.
*
* app.get('/user/:uid/photos/:file', function(req, res){
* var uid = req.params.uid
* , file = req.params.file;
*
* req.user.mayViewFilesFrom(uid, function(yes){
* if (yes) {
* res.sendFile('/uploads/' + uid + '/' + file);
* } else {
* res.send(403, 'Sorry! you cant see that.');
* }
* });
* });
*
* @api public
*/
async sendFile(path, options) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore koa-router bad definition
await (0, koa_send_1.default)(this.context, path, options);
}
/**
* Append additional header `field` with value `val`.
*
* Examples:
*
* ```
* this.append('Link', ['<http://localhost/>', '<http://localhost:3000/>']);
* this.append('Set-Cookie', 'foo=bar; Path=/; HttpOnly');
* this.append('Warning', '199 Miscellaneous warning');
* ```
*/
append(field, val) {
this.response.append(field, val);
}
/**
* Set Content-Disposition header to "attachment" with optional `filename`.
*/
attachment(filename) {
this.response.attachment(filename);
}
/**
* Set header `field` to `val`, or pass
* an object of header fields.
*
* Examples:
*
* this.set('Foo', ['bar', 'baz']);
* this.set('Accept', 'application/json');
* this.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });
*/
set(field, val) {
if (typeof field === 'string') {
// cast to any (ts prob)
this.response.set(field, val);
}
else {
this.response.set(field);
}
}
/**
* Vary on `field`.
*/
vary(field) {
this.response.vary(field);
}
}
tslib_1.__decorate([
(0, property_tunnel_1.alias)(['response', 'headersSent'], { access: 'readonly' }),
tslib_1.__metadata("design:type", Boolean)
], KoaResponse.prototype, "headersSent", void 0);
tslib_1.__decorate([
(0, property_tunnel_1.alias)(['response', 'status']),
tslib_1.__metadata("design:type", Number)
], KoaResponse.prototype, "status", void 0);
tslib_1.__decorate([
(0, property_tunnel_1.alias)(['response', 'message']),
tslib_1.__metadata("design:type", String)
], KoaResponse.prototype, "message", void 0);
tslib_1.__decorate([
(0, property_tunnel_1.alias)(['response', 'body']),
tslib_1.__metadata("design:type", Object)
], KoaResponse.prototype, "body", void 0);
tslib_1.__decorate([
(0, property_tunnel_1.alias)(['response', 'type']),
tslib_1.__metadata("design:type", String)
], KoaResponse.prototype, "type", void 0);
exports.KoaResponse = KoaResponse;