UNPKG

express-rendertype

Version:

Express middleware to (automatically) render content and errors to content-type

239 lines (197 loc) 6.82 kB
// Generated by IcedCoffeeScript 108.0.11 var Errors, Fancy, FancyErrors, RenderType, RenderTypedErrors, errify, iced, __iced_k, __iced_k_noop, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; iced = require('iced-runtime'); __iced_k = __iced_k_noop = function() {}; Errors = require("restify-errors"); errify = require("errify"); Fancy = require("./fancy"); RenderType = (function() { function RenderType() {} RenderType.text = function(path, obj) { return this.send(obj.toString()); }; RenderType.html = function(path, obj) { return this.render(path, obj); }; RenderType.json = function(path, obj) { return this.json(obj); }; RenderType.yaml = function(path, obj) { return this.send((require("js-yaml")).safeDump(obj, { skipInvalid: true })); }; RenderType.xml = function(path, obj) { return this.send((require("xml"))(obj, { indent: ' ' })); }; RenderType.auto = function(fallback, preference) { if (fallback == null) { fallback = false; } if (preference == null) { preference = ["yaml", "json", "html", "text"]; } return (function(_this) { return function(req, res, next) { res.rendr = function() { var type; type = req.accepts(preference); type || (type = fallback); if (!type) { next(new Errors.NotAcceptableError); } res.type(type); return _this[type].apply(res, arguments); }; return next(); }; })(this); }; return RenderType; })(); RenderTypedErrors = (function(_super) { __extends(RenderTypedErrors, _super); function RenderTypedErrors() { return RenderTypedErrors.__super__.constructor.apply(this, arguments); } RenderTypedErrors.Error404 = function(req, res, next) { return next(new Errors.NotFoundError); }; RenderTypedErrors.log = function() {}; RenderTypedErrors.text = function(err, req, res, next) { return RenderTypedErrors.__super__.constructor.text.call(this, "", err.message); }; RenderTypedErrors.html = function(err, req, res, next) { return RenderTypedErrors.__super__.constructor.html.call(this, "error", err); }; RenderTypedErrors.json = function(err, req, res, next) { return RenderTypedErrors.__super__.constructor.json.call(this, "", err); }; RenderTypedErrors.yaml = function(err, req, res, next) { return RenderTypedErrors.__super__.constructor.yaml.call(this, "", err); }; RenderTypedErrors.xml = function(err, req, res, next) { return RenderTypedErrors.__super__.constructor.xml.call(this, "", err); }; RenderTypedErrors.auto = function(fallback, preference, log) { if (fallback == null) { fallback = false; } if (preference == null) { preference = ["yaml", "json", "html", "text"]; } if (log == null) { log = this.log; } return (function(_this) { return function(err, req, res, next) { var i, line, message, stack, trimTo, type, _i, _len; type = req.accepts(preference); type || (type = fallback); if (err.status && !err.statusCode) { err.statusCode = err.status; } if (err.statusCode < 400 || !err.statusCode) { err.statusCode = 500; } if (err.statusCode && !(err instanceof Error)) { message = err.message; err = Errors.makeErrFromCode(err.statusCode); trimTo = 0; stack = (Fancy.stringify(err)).split("\n"); for (i = _i = 0, _len = stack.length; _i < _len; i = ++_i) { line = stack[i]; if (line.match(/at next/)) { trimTo = i; } } err.stack = stack.slice(trimTo).join("\n"); if (message) { err.message = message; } } log(err); if (res._header) { return req.socket.destroy(); } res.status(err.statusCode); res.setHeader("X-Content-Type-Options", "nosniff"); res.type(type); return _this[type].apply(res, arguments); }; })(this); }; return RenderTypedErrors; })(RenderType); FancyErrors = (function(_super) { __extends(FancyErrors, _super); function FancyErrors() { return FancyErrors.__super__.constructor.apply(this, arguments); } FancyErrors.text = function(err, req, res, next) { return FancyErrors.__super__.constructor.text.call(this, { message: Fancy.stringify(err) }); }; FancyErrors.html = function(err, req, res, next) { var html, ideally, ___iced_passed_deferral, __iced_deferrals, __iced_k; __iced_k = __iced_k_noop; ___iced_passed_deferral = iced.findDeferral(arguments); ideally = errify(next); (function(_this) { return (function(__iced_k) { __iced_deferrals = new iced.Deferrals(__iced_k, { parent: ___iced_passed_deferral, filename: "/home/charles/source/express-rendertype/src/rendertype.coffee", funcname: "FancyErrors.html" }); Fancy.html(err, ideally(__iced_deferrals.defer({ assign_fn: (function() { return function() { return html = arguments[0]; }; })(), lineno: 58 }))); __iced_deferrals._fulfill(); }); })(this)((function(_this) { return function() { res.type("html"); return res.send(html); }; })(this)); }; FancyErrors.json = function(err, req, res, next) { return FancyErrors.__super__.constructor.json.call(this, Fancy.json(err)); }; FancyErrors.yaml = function(err, req, res, next) { return FancyErrors.__super__.constructor.yaml.call(this, Fancy.json(err)); }; FancyErrors.xml = function(err, req, res, next) { return FancyErrors.__super__.constructor.xml.call(this, Fancy.json(err)); }; FancyErrors.auto = function(fallback, preference, log) { if (fallback == null) { fallback = false; } if (preference == null) { preference = ["yaml", "json", "html", "text"]; } if (log == null) { log = this.log; } return FancyErrors.__super__.constructor.auto.call(this, fallback, preference, Fancy.log(log)); }; return FancyErrors; })(RenderTypedErrors); RenderType.error = Errors; RenderType.error.fromCode = Errors.makeErrFromCode; RenderType.Errors = RenderTypedErrors; RenderType.FancyErrors = FancyErrors; module.exports = RenderType; //# sourceMappingURL=rendertype.js.map