light-ning
Version:
(ALPHA) framework without dependecies...
47 lines (30 loc) • 1.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = undefined;
var _httpCodes = require('./httpCodes');
var _httpCodes2 = _interopRequireDefault(_httpCodes);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Response = function Response(response) {
var _this = this;
_classCallCheck(this, Response);
this.res = response;
return function (obj) {
var code = obj.code,
error = obj.error,
data = obj.data,
res = _this.res;
if (code) res.statusCode = code;
res.statusMessage = _httpCodes2.default['http_' + res.statusCode] || res.statusMessage;
if (error && code) return res.end(JSON.stringify({ error: error, code: res.statusCode }));else if (error && !code) {
throw new Error('response: "error" must sending with "code"');
}
if (res.statusCode >= 400 && res.statusCode <= 511) return res.end({ error: res.statusMessage, code: res.statusCode });
if (data) return res.end(JSON.stringify({ data: data, code: res.statusCode, message: res.statusMessage }));
res.end();
};
};
Response.displayName = 'Response';
exports.default = Response;