Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom statuses #1119

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ module.exports = class Application extends Emitter {
this.context = Object.create(context);
this.request = Object.create(request);
this.response = Object.create(response);
this.customStatuses = {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Object.create(null)

}

/**
* Add custom status code

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All other comments have a period after the description text.

* @param {Number} code
* @param {String} description
* @api public
*/

addStatus(code, description) {
this.customStatuses[code] = description;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only issue is naming. i'd prefer this to be called customStatusCodes. what does everyone else think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

other than that, i'm good with this PR

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that my opinion matters, but personally I'd prefer addCustomStatusCode, erring on the side of a more descriptive name

}

/**
Expand Down Expand Up @@ -166,6 +178,7 @@ module.exports = class Application extends Emitter {
request.ctx = response.ctx = context;
request.response = response;
response.request = request;
response.customStatuses = this.customStatuses;
context.originalUrl = request.originalUrl = req.url;
context.cookies = new Cookies(req, res, {
keys: this.keys,
Expand Down
4 changes: 2 additions & 2 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ module.exports = {

set status(code) {
assert('number' == typeof code, 'status code must be a number');
assert(statuses[code], `invalid status code: ${code}`);
assert(statuses[code] || this.customStatuses[code], `invalid status code: ${code}`);
assert(!this.res.headersSent, 'headers have already been sent');
this._explicitStatus = true;
this.res.statusCode = code;
if (this.req.httpVersionMajor < 2) this.res.statusMessage = statuses[code];
if (this.req.httpVersionMajor < 2) this.res.statusMessage = statuses[code] || this.customStatuses[code];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I get you correctly, you meant to use the custom status codes instead of the default status codes.Is that right? So, actually the logic would be:

this.customStatuses[code] || statuses[code];

Then, if you have no custom status codes the default status will be used instead. Is that the expected behavior?

if (this.body && statuses.empty[code]) this.body = null;
},

Expand Down
3 changes: 1 addition & 2 deletions test/application/respond.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
'use strict';

const request = require('supertest');
const statuses = require('statuses');
const assert = require('assert');
const Koa = require('../..');
const fs = require('fs');
Expand Down Expand Up @@ -315,7 +314,7 @@ describe('app.respond', () => {
describe('with custom status=700', () => {
it('should respond with the associated status message', async () => {
const app = new Koa();
statuses['700'] = 'custom status';
app.addStatus(700, 'custom status');

app.use(ctx => {
ctx.status = 700;
Expand Down