[http-errors] upgrade to v1.6, enable strict null checks & linting

This commit is contained in:
Dimitri Benin 2017-08-15 18:54:01 +02:00
parent d4f42ab007
commit 9b9631b4df
4 changed files with 139 additions and 137 deletions

View File

@ -1,86 +1,74 @@
import * as createError from 'http-errors';
import * as express from 'express';
var app = express();
const app = express();
declare global {
namespace Express {
export interface Request {
user?: any
}
}
}
app.use(function (req, res, next) {
if (!req.user) return next(createError(401, 'Please login to view this page.'));
app.use((req, res, next) => {
if (!req) return next(createError('Please login to view this page.', 401));
next();
});
/* Examples taken from https://github.com/jshttp/http-errors/blob/1.3.1/test/test.js */
// createError(status)
var err = createError(404);
console.log(err.name);
console.log(err.message);
console.log(err.status);
console.log(err.statusCode);
console.log(err.expose);
console.log(err.headers);
let err = createError(404);
err; // $ExpectType HttpError
err.name; // $ExpectType string
err.message; // $ExpectType string
err.status; // $ExpectType number
err.statusCode; // $ExpectType number
err.expose; // $ExpectType boolean
err.headers; // $ExpectType { [key: string]: string; } | undefined
// createError(status, msg)
var err = createError(404, 'LOL');
err = createError(404, 'LOL');
// createError(status, props)
var err = createError(404, {id: 1});
err = createError(404, {id: 1});
// createError(props)
var err = createError({id: 1});
console.log((<any> err).id);
err = createError({id: 1});
// $ExpectType any
err.id;
// createError(msg, status)
var err = createError('LOL', 404);
err = createError('LOL', 404);
// createError(msg)
var err = createError('LOL');
err = createError('LOL');
// createError(msg, props)
var err = createError('LOL', {id: 1});
err = createError('LOL', {id: 1});
// createError(err)
var err = createError(new Error('LOL'));
err = createError(new Error('LOL'));
// createError(err, props)
var err = createError(new Error('LOL'), {id: 1});
err = createError(new Error('LOL'), {id: 1});
// createError(status, err, props)
var err = createError(404, new Error('LOL'), {id: 1});
err = createError(404, new Error('LOL'), {id: 1});
// createError(status, msg, props)
var err = createError(404, 'LOL', {id: 1});
err = createError(404, 'LOL', {id: 1});
// createError(status, msg, { expose: false })
var err = createError(404, 'LOL', {expose: false})
err = createError(404, 'LOL', {expose: false});
// new createError.NotFound()
var err = new createError.NotFound();
err = new createError.NotFound();
err = new createError.InternalServerError();
err = new createError[404]();
// new createError.InternalServerError()
var err = new createError.InternalServerError();
// new createError['404']()
var err = new createError['404']();
//createError['404'](); // TypeScript should fail with "Did you mean to include 'new'?"
//new createError(); // TypeScript should fail with "Only a void function can be called with the 'new' keyword"
createError['404'](); // $ExpectError
new createError(); // $ExpectError
// Error messages can have custom messages
var err = new createError.NotFound('This might be a problem');
var err = new createError['404']('This might be a problem');
err = new createError.NotFound('This might be a problem');
err = new createError[404]('This might be a problem');
// 1.5.0 supports 421 - Misdirected Request
var err = new createError.MisdirectedRequest();
var err = new createError.MisdirectedRequest('Where should this go?');
err = new createError.MisdirectedRequest();
err = new createError.MisdirectedRequest('Where should this go?');
let error: createError.HttpError;
console.log(error instanceof createError.HttpError);
// $ExpectType boolean
new Error() instanceof createError.HttpError;

View File

@ -1,97 +1,110 @@
// Type definitions for http-errors v1.5.0
// Type definitions for http-errors 1.6
// Project: https://github.com/jshttp/http-errors
// Definitions by: Tanguy Krotoff <https://github.com/tkrotoff>
// BendingBender <https://github.com/BendingBender>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
declare module 'http-errors' {
namespace createHttpError {
export = createHttpError;
// See https://github.com/jshttp/http-errors/blob/1.3.1/index.js#L42
interface HttpError extends Error {
status: number;
statusCode: number;
expose: boolean;
headers?: {
[key: string]: string
};
}
declare const createHttpError: createHttpError.CreateHttpError & createHttpError.NamedConstructors;
type HttpErrorConstructor = new(msg?: string) => HttpError;
interface CreateHttpError {
// See https://github.com/Microsoft/TypeScript/issues/227#issuecomment-50092674
[code: string]: new (msg?: string) => HttpError;
(...args: Array<Error | string | number | Object>): HttpError;
HttpError: HttpErrorConstructor;
Continue: HttpErrorConstructor;
SwitchingProtocols: HttpErrorConstructor;
Processing: HttpErrorConstructor;
OK: HttpErrorConstructor;
Created: HttpErrorConstructor;
Accepted: HttpErrorConstructor;
NonAuthoritativeInformation: HttpErrorConstructor;
NoContent: HttpErrorConstructor;
ResetContent: HttpErrorConstructor;
PartialContent: HttpErrorConstructor;
MultiStatus: HttpErrorConstructor;
AlreadyReported: HttpErrorConstructor;
IMUsed: HttpErrorConstructor;
MultipleChoices: HttpErrorConstructor;
MovedPermanently: HttpErrorConstructor;
Found: HttpErrorConstructor;
SeeOther: HttpErrorConstructor;
NotModified: HttpErrorConstructor;
UseProxy: HttpErrorConstructor;
Unused: HttpErrorConstructor;
TemporaryRedirect: HttpErrorConstructor;
PermanentRedirect: HttpErrorConstructor;
BadRequest: HttpErrorConstructor;
Unauthorized: HttpErrorConstructor;
PaymentRequired: HttpErrorConstructor;
Forbidden: HttpErrorConstructor;
NotFound: HttpErrorConstructor;
MethodNotAllowed: HttpErrorConstructor;
NotAcceptable: HttpErrorConstructor;
ProxyAuthenticationRequired: HttpErrorConstructor;
RequestTimeout: HttpErrorConstructor;
Conflict: HttpErrorConstructor;
Gone: HttpErrorConstructor;
LengthRequired: HttpErrorConstructor;
PreconditionFailed: HttpErrorConstructor;
PayloadTooLarge: HttpErrorConstructor;
URITooLong: HttpErrorConstructor;
UnsupportedMediaType: HttpErrorConstructor;
RangeNotSatisfiable: HttpErrorConstructor;
ExpectationFailed: HttpErrorConstructor;
ImATeapot: HttpErrorConstructor;
MisdirectedRequest: HttpErrorConstructor;
UnprocessableEntity: HttpErrorConstructor;
Locked: HttpErrorConstructor;
FailedDependency: HttpErrorConstructor;
UnorderedCollection: HttpErrorConstructor;
UpgradeRequired: HttpErrorConstructor;
PreconditionRequired: HttpErrorConstructor;
TooManyRequests: HttpErrorConstructor;
RequestHeaderFieldsTooLarge: HttpErrorConstructor;
UnavailableForLegalReasons: HttpErrorConstructor;
InternalServerError: HttpErrorConstructor;
NotImplemented: HttpErrorConstructor;
BadGateway: HttpErrorConstructor;
ServiceUnavailable: HttpErrorConstructor;
GatewayTimeout: HttpErrorConstructor;
HTTPVersionNotSupported: HttpErrorConstructor;
VariantAlsoNegotiates: HttpErrorConstructor;
InsufficientStorage: HttpErrorConstructor;
LoopDetected: HttpErrorConstructor;
BandwidthLimitExceeded: HttpErrorConstructor;
NotExtended: HttpErrorConstructor;
NetworkAuthenticationRequired: HttpErrorConstructor;
}
declare namespace createHttpError {
interface HttpError extends Error {
status: number;
statusCode: number;
expose: boolean;
headers?: {
[key: string]: string;
};
[key: string]: any;
}
var createHttpError: createHttpError.CreateHttpError;
export = createHttpError;
type HttpErrorConstructor = new (msg?: string) => HttpError;
type CreateHttpError = (...args: Array<Error | string | number | { [key: string]: any }>) => HttpError;
type NamedConstructors = { [code: string]: HttpErrorConstructor } & Record<'HttpError' |
'BadRequest' |
'Unauthorized' |
'PaymentRequired' |
'Forbidden' |
'NotFound' |
'MethodNotAllowed' |
'NotAcceptable' |
'ProxyAuthenticationRequired' |
'RequestTimeout' |
'Conflict' |
'Gone' |
'LengthRequired' |
'PreconditionFailed' |
'PayloadTooLarge' |
'URITooLong' |
'UnsupportedMediaType' |
'RangeNotSatisfiable' |
'ExpectationFailed' |
'ImATeapot' |
'MisdirectedRequest' |
'UnprocessableEntity' |
'Locked' |
'FailedDependency' |
'UnorderedCollection' |
'UpgradeRequired' |
'PreconditionRequired' |
'TooManyRequests' |
'RequestHeaderFieldsTooLarge' |
'UnavailableForLegalReasons' |
'InternalServerError' |
'NotImplemented' |
'BadGateway' |
'ServiceUnavailable' |
'GatewayTimeout' |
'HTTPVersionNotSupported' |
'VariantAlsoNegotiates' |
'InsufficientStorage' |
'LoopDetected' |
'BandwidthLimitExceeded' |
'NotExtended' |
'NetworkAuthenticationRequire' |
'400' |
'401' |
'402' |
'403' |
'404' |
'405' |
'406' |
'407' |
'408' |
'409' |
'410' |
'411' |
'412' |
'413' |
'414' |
'415' |
'416' |
'417' |
'418' |
'421' |
'422' |
'423' |
'424' |
'425' |
'426' |
'428' |
'429' |
'431' |
'451' |
'500' |
'501' |
'502' |
'503' |
'504' |
'505' |
'506' |
'507' |
'508' |
'509' |
'510' |
'511', HttpErrorConstructor>;
}

View File

@ -6,7 +6,7 @@
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": false,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
@ -19,4 +19,4 @@
"index.d.ts",
"http-errors-tests.ts"
]
}
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }