Skip to content
Myr

Invoke default with same arguments as types in res.format

Before adoption: this commit came in with the history the project adopted, so no landing records it.

closes #3587

authored by Nadav Ivgi · committed by Douglas Christopher Wilson · parent 10b9b50 · Browse files · 3 files+33 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+33 added−6 removed

  • tests:+28 added−1 removed
  • other:+5 added−5 removed

3 files+33 added−6 removed

modified:lib/response.js+4 added-5 removed
682 unmodified lines
683res.format = function(obj){684 var req = this.req;685 var next = req.next;686687 var fn = obj.default;688 if (fn) delete obj.default;689 var keys = Object.keys(obj);687 var keys = Object.keys(obj)688 .filter(function (v) { return v !== 'default' })689690 var key = keys.length > 0691 ? req.accepts(keys)692 : false;
2 unmodified lines
695696 if (key) {697 this.set('Content-Type', normalizeType(key).value);698 obj[key](req, this, next);700 } else if (fn) {701 fn();699 } else if (obj.default) {700 obj.default(req, this, next)701 } else {702 var err = new Error('Not Acceptable');703 err.status = err.statusCode = 406;704 err.types = normalizeTypes(keys).map(function(o){ return o.value });
461 unmodified lines
modified:test/res.format.js+28 added-1 removed
48 unmodified lines
4950app3.use(function(req, res, next){51 res.format({52 text: function(){ res.send('hey') },53 default: function(){ res.send('default') }53 default: function (a, b, c) {54 assert(req === a)55 assert(res === b)56 assert(next === c)57 res.send('default')58 }59 })60});6162var app4 = express();
59 unmodified lines
122 .get('/')123 .set('Accept', '*/*')124 .expect('hey', done);125 })126127 it('should be able to invoke other formatter', function (done) {128 var app = express()129130 app.use(function (req, res, next) {131 res.format({132 json: function () { res.send('json') },133 default: function () {134 res.header('x-default', '1')135 this.json()136 }137 })138 })139140 request(app)141 .get('/')142 .set('Accept', 'text/plain')143 .expect(200)144 .expect('x-default', '1')145 .expect('json')146 .end(done)147 })148 })149150 describe('in router', function(){151 test(app4);
92 unmodified lines
modified:History.md+1 added
3 unmodified lines
4 * Add "root" option to `res.download`5 * Allow `options` without `filename` in `res.download`6 * Deprecate string and non-integer arguments to `res.status`7 * Ignore `Object.prototype` values in settings through `app.set`/`app.get`8 * Invoke `default` with same arguments as types in `res.format`9 * Support proper 205 responses using `res.send`10 * deps: finalhandler@1.2.011 - Remove set content headers that break response12 - deps: on-finished@2.4.1
3537 unmodified lines