Remove statuses and replace http-errors with http-errors-lite

master
Jonatan Nilsson 2019-10-11 16:40:06 +00:00
parent 28fee01c3f
commit dd35564df4
9 changed files with 87 additions and 72 deletions

View File

@ -12,7 +12,7 @@ const compose = require('koa-compose');
const isJSON = require('koa-is-json');
const context = require('./context');
const request = require('./request');
const statuses = require('statuses');
const statuses = require('./statuses');
const Emitter = require('events');
const util = require('util');
const Stream = require('stream');

View File

@ -6,10 +6,10 @@
*/
const util = require('util');
const createError = require('http-errors');
const createError = require('http-errors-lite');
const httpAssert = require('http-assert');
const delegate = require('./delegates');
const statuses = require('statuses');
const statuses = require('./statuses');
/**
* Context prototype.

View File

@ -11,7 +11,7 @@ const onFinish = require('on-finished');
const isJSON = require('koa-is-json');
const escape = require('escape-html');
const typeis = require('type-is').is;
const statuses = require('statuses');
const statuses = require('./statuses');
const destroy = require('destroy');
const assert = require('assert');
const extname = require('path').extname;

81
lib/statuses.js Normal file
View File

@ -0,0 +1,81 @@
module.exports = {
100: 'Continue',
101: 'Switching Protocols',
102: 'Processing',
103: 'Early Hints',
200: 'OK',
201: 'Created',
202: 'Accepted',
203: 'Non-Authoritative Information',
204: 'No Content',
205: 'Reset Content',
206: 'Partial Content',
207: 'Multi-Status',
208: 'Already Reported',
226: 'IM Used',
300: 'Multiple Choices',
301: 'Moved Permanently',
302: 'Found',
303: 'See Other',
304: 'Not Modified',
305: 'Use Proxy',
306: '(Unused)',
307: 'Temporary Redirect',
308: 'Permanent Redirect',
400: 'Bad Request',
401: 'Unauthorized',
402: 'Payment Required',
403: 'Forbidden',
404: 'Not Found',
405: 'Method Not Allowed',
406: 'Not Acceptable',
407: 'Proxy Authentication Required',
408: 'Request Timeout',
409: 'Conflict',
410: 'Gone',
411: 'Length Required',
412: 'Precondition Failed',
413: 'Payload Too Large',
414: 'URI Too Long',
415: 'Unsupported Media Type',
416: 'Range Not Satisfiable',
417: 'Expectation Failed',
418: 'I\'m a teapot',
421: 'Misdirected Request',
422: 'Unprocessable Entity',
423: 'Locked',
424: 'Failed Dependency',
425: 'Too Early',
426: 'Upgrade Required',
428: 'Precondition Required',
429: 'Too Many Requests',
431: 'Request Header Fields Too Large',
451: 'Unavailable For Legal Reasons',
500: 'Internal Server Error',
501: 'Not Implemented',
502: 'Bad Gateway',
503: 'Service Unavailable',
504: 'Gateway Timeout',
505: 'HTTP Version Not Supported',
506: 'Variant Also Negotiates',
507: 'Insufficient Storage',
508: 'Loop Detected',
509: 'Bandwidth Limit Exceeded',
510: 'Not Extended',
511: 'Network Authentication Required',
redirect: {
300: true,
301: true,
302: true,
303: true,
305: true,
307: true,
308: true
},
empty: {
204: true,
205: true,
304: true
}
};

View File

@ -30,12 +30,11 @@
"escape-html": "^1.0.3",
"fresh": "~0.5.2",
"http-assert": "^1.3.0",
"http-errors": "^1.6.3",
"http-errors-lite": "^2.0.2",
"koa-compose": "^4.1.0",
"koa-is-json": "^1.0.0",
"on-finished": "^2.3.0",
"parseurl": "^1.3.2",
"statuses": "^1.5.0",
"type-is": "^1.6.16",
"vary": "^1.1.2"
},

View File

@ -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');
@ -351,26 +350,6 @@ 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.use(ctx => {
ctx.status = 700;
});
const server = app.listen();
const res = await request(server)
.get('/')
.expect(700)
.expect('custom status');
assert.equal(res.res.statusMessage, 'custom status');
});
});
describe('with custom statusMessage=ok', () => {
it('should respond with the custom status message', async() => {
const app = new Koa();

View File

@ -44,7 +44,7 @@ describe('app.use(fn)', () => {
it('should catch thrown errors in non-async functions', () => {
const app = new Koa();
app.use(ctx => ctx.throw('Not Found', 404));
app.use(ctx => ctx.throw(404, 'Not Found'));
return request(app.callback())
.get('/')

View File

@ -32,21 +32,6 @@ describe('ctx.throw(err)', () => {
});
});
describe('ctx.throw(err, status)', () => {
it('should throw the error and set .status', () => {
const ctx = context();
const error = new Error('test');
try {
ctx.throw(error, 422);
} catch (err) {
assert.equal(err.status, 422);
assert.equal(err.message, 'test');
assert.equal(err.expose, true);
}
});
});
describe('ctx.throw(status, err)', () => {
it('should throw the error and set .status', () => {
const ctx = context();
@ -62,20 +47,6 @@ describe('ctx.throw(status, err)', () => {
});
});
describe('ctx.throw(msg, status)', () => {
it('should throw an error', () => {
const ctx = context();
try {
ctx.throw('name required', 400);
} catch (err) {
assert.equal(err.message, 'name required');
assert.equal(err.status, 400);
assert.equal(err.expose, true);
}
});
});
describe('ctx.throw(status, msg)', () => {
it('should throw an error', () => {
const ctx = context();

View File

@ -3,7 +3,6 @@
const response = require('../helpers/context').response;
const request = require('supertest');
const statuses = require('statuses');
const assert = require('assert');
const Koa = require('../..');
@ -29,20 +28,6 @@ describe('res.status=', () => {
});
});
describe('and custom status', () => {
beforeEach(() => statuses['700'] = 'custom status');
it('should set the status', () => {
const res = response();
res.status = 700;
assert.equal(res.status, 700);
});
it('should not throw', () => {
response().status = 700;
});
});
describe('and HTTP/2', () => {
it('should not set the status message', () => {
const res = response({