deprecate res.json(obj, status)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
closes #2106 closes #2107
authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent 0461e55 · Browse files · 4 files·+57 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+57 added−0 removed
- tests:+34 added−0 removed
- other:+23 added−0 removed
Provenance
- Commit
4e1e252- Tree
7fa143f- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
0461e55
4 files changed+57 added−0 removed
modified:lib/response.js+19 added
7 unmodified lines8 , utils = connect.utils9 , sign = require('cookie-signature').sign10 , normalizeType = require('./utils').normalizeType11 , normalizeTypes = require('./utils').normalizeTypes12+ , deprecate = require('./utils').deprecate13 , etag = require('./utils').etag14 , statusCodes = http.STATUS_CODES15 , cookie = require('cookie')16 , send = require('send')160 unmodified lines177 if (2 == arguments.length) {178 // res.json(body, status) backwards compat179 if ('number' == typeof arguments[1]) {180 this.statusCode = arguments[1];181+ return 'number' === typeof obj182+ ? jsonNumDeprecated.call(this, obj)183+ : jsonDeprecated.call(this, obj);184 } else {185 this.statusCode = obj;186 obj = arguments[1];187 }11 unmodified lines199 200 return this.send(body);201 };202 203+var jsonDeprecated = deprecate(res.json,204+ 'res.json(obj, status): Use res.json(status, obj) instead');205+206+var jsonNumDeprecated = deprecate(res.json,207+ 'res.json(num, status): Use res.status(status).json(num) instead');208+209 /**210 * Send JSON response with JSONP callback support.211 *212 * Examples:14 unmodified lines227 if (2 == arguments.length) {228 // res.json(body, status) backwards compat229 if ('number' == typeof arguments[1]) {230 this.statusCode = arguments[1];231+ return 'number' === typeof obj232+ ? jsonpNumDeprecated.call(this, obj)233+ : jsonpDeprecated.call(this, obj);234 } else {235 this.statusCode = obj;236 obj = arguments[1];237 }26 unmodified lines264 265 return this.send(body);266 };267 268+var jsonpDeprecated = deprecate(res.json,269+ 'res.jsonp(obj, status): Use res.jsonp(status, obj) instead');270+271+var jsonpNumDeprecated = deprecate(res.json,272+ 'res.jsonp(num, status): Use res.status(status).jsonp(num) instead');273+274 /**275 * Transfer the file at the given `path`.276 *277 * Automatically sets the _Content-Type_ response header field.545 unmodified linesmodified:test/res.json.js+17 added
160 unmodified linesExpand all16116216316416516616716816917017117217317417517617717817918018118218318418516 unmodified lines160 unmodified linesExpand all res.text.should.equal('{"id":1}'); done(); }) })
it('should use status as second number for backwards compat', function(done){ var app = express();
app.use(function(req, res){ res.json(200, 201); });
request(app) .get('/') .end(function(err, res){ res.statusCode.should.equal(201); res.headers.should.have.property('content-type', 'application/json; charset=utf-8'); res.text.should.equal('200'); done(); }) }) })
it('should not override previous Content-Types', function(done){ var app = express();16 unmodified linesmodified:test/res.jsonp.js+17 added
262 unmodified lines263 res.text.should.equal('{"id":1}');264 done();265 })266 })267+268+ it('should use status as second number for backwards compat', function(done){269+ var app = express();270+271+ app.use(function(req, res){272+ res.jsonp(200, 201);273+ });274+275+ request(app)276+ .get('/')277+ .end(function(err, res){278+ res.statusCode.should.equal(201);279+ res.headers.should.have.property('content-type', 'application/json; charset=utf-8');280+ res.text.should.equal('200');281+ done();282+ })283+ })284 })285 })modified:History.md+4 added
1 3.x2 ===3 4 * deprecate `app.del()` -- use `app.delete()` instead5+ * deprecate `res.json(obj, status)` -- use `res.json(status, obj)` instead6+ - the edge-case `res.json(status, num)` requires `res.status(status).json(num)`7+ * deprecate `res.jsonp(obj, status)` -- use `res.jsonp(status, obj)` instead8+ - the edge-case `res.jsonp(status, num)` requires `res.status(status).jsonp(num)`9 * update connect to 2.15.010 * Add `res.appendHeader`11 * Call error stack even when response has been sent12 * Patch `res.headerSent` to return Boolean1298 unmodified lines