UNPKG

dino-express

Version:

DinO enabled REST framework based on express

170 lines 5.74 kB
"use strict"; // Copyright 2018 Quirino Brizi [quirino.brizi@gmail.com] // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. Object.defineProperty(exports, "__esModule", { value: true }); exports.ResponseImpl = void 0; const tslib_1 = require("tslib"); const http_exception_1 = require("./exception/http.exception"); const vary_1 = tslib_1.__importDefault(require("vary")); const http_1 = tslib_1.__importDefault(require("http")); const encodeurl_1 = tslib_1.__importDefault(require("encodeurl")); const ResponseAdaptorStrategySelector_1 = require("./response/ResponseAdaptorStrategySelector"); const send_1 = require("send"); const dino_core_1 = require("dino-core"); // eslint-disable-next-line @typescript-eslint/no-extraneous-class class EventEmitter { static captureRejectionSymbol; } class ResponseImpl extends http_1.default.ServerResponse { body; // eslint-disable-next-line @typescript-eslint/consistent-type-assertions app = {}; locals = {}; charset = 'UTF-8'; req; callback; runtimeContext; responseAdaptorStrategySelector = new ResponseAdaptorStrategySelector_1.ResponseAdaptorStrategySelector(); constructor(callback, req, runtimeContext) { super(req); this.req = req; this.callback = callback; this.runtimeContext = runtimeContext; } sendfile(_path, _options, _fn) { throw new Error('Method not implemented.'); } [EventEmitter.captureRejectionSymbol](_error, _event, ..._args) { throw new Error('Method not implemented.'); } status(code) { this.statusCode = code; return this; } sendStatus(_code) { throw new Error('Method not implemented.'); } links(_links) { throw new Error('Method not implemented.'); } send(_body) { this.callback(this.responseAdaptorStrategySelector.select(this.runtimeContext).adapt(this)); return this; } json(body) { return this.send(body); } jsonp(body) { return this.send(body); } sendFile(_path, _options, _fn) { throw new Error('Method not implemented.'); } download(_path, _filename, _options, _fn) { throw new Error('Method not implemented.'); } contentType(type) { this.header('Content-Type', type); return this; } type(type) { const ct = !type.includes('/') ? send_1.mime.lookup(type) : type; return this.set('Content-Type', ct); } format(obj) { const req = this.req; const next = req.next; const keys = Object.keys(obj).filter(function (v) { return v !== 'default'; }); const key = keys.length > 0 ? this.accepts(keys) : undefined; this.vary('Accept'); if (key !== undefined && typeof key === 'string') { this.set('Content-Type', ['json', 'default'].includes(key) ? 'application/json' : 'application/xml'); obj[key](req, this, next); } else if (obj.default !== undefined) { obj.default(req, this, next); } else { if (next !== undefined) { next(http_exception_1.HttpException.create(406, 'Not Acceptable')); } } return this; } accepts(types) { const headers = this.req.headers; if (dino_core_1.ObjectHelper.isDefined(headers)) { const accept = headers.accept; if (accept === 'application/json' && types.includes('json')) { return 'json'; } if (accept === 'application/xml' && types.includes('xml')) { return 'xml'; } } return 'json'; } attachment(_filename) { throw new Error('Method not implemented.'); } set(field, value) { if (value !== undefined) { this.setHeader(field, value); } return this; } header(field, value) { this.set(field, value); return this; } get(field) { return this.getHeader(field); } clearCookie(_name, _options) { throw new Error('Method not implemented.'); } cookie(_name, _val, _options) { throw new Error('Method not implemented.'); } location(url) { let loc = url; if (url === 'back') { loc = this.req.get('Referrer') ?? '/'; } // eslint-disable-next-line @typescript-eslint/no-unsafe-argument return this.set('Location', (0, encodeurl_1.default)(loc)); } redirect(_url, _status) { throw new Error('Method not implemented.'); } render(_view, _options, _callback) { throw new Error('Method not implemented.'); } vary(field) { (0, vary_1.default)(this, field); return this; } append(field, value) { const prev = this.get(field); let val = value; if (prev !== undefined) { val = Array.isArray(prev) ? prev.concat(value) : Array.isArray(value) ? [prev].concat(value) : [prev, value]; } return this.set(field, val); } } exports.ResponseImpl = ResponseImpl; //# sourceMappingURL=Response.js.map