Merge pull request #4808 from tkrotoff/http-errors

Add definitions for statuses, http-errors and api-error-handler
This commit is contained in:
Masahiro Wakame
2015-07-03 22:54:16 +09:00
6 changed files with 233 additions and 0 deletions
@@ -0,0 +1,11 @@
/// <reference path="api-error-handler.d.ts" />
import errorHandler = require('api-error-handler');
import express = require('express');
var api = express.Router();
api.get('/users/:userid', function (req, res, next) {
});
api.use(errorHandler());
+14
View File
@@ -0,0 +1,14 @@
// Type definitions for api-error-handler v1.0.0
// Project: https://github.com/expressjs/api-error-handler
// Definitions by: Tanguy Krotoff <https://github.com/tkrotoff>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
/// <reference path="../express/express.d.ts" />
declare module 'api-error-handler' {
import express = require('express');
function apiErrorHandler(options?: any): express.ErrorRequestHandler;
export = apiErrorHandler;
}
+69
View File
@@ -0,0 +1,69 @@
/// <reference path="http-errors.d.ts" />
/// <reference path="../express/express.d.ts" />
import createError = require('http-errors');
import express = require('express');
var app = express();
app.use(function (req, res, next) {
if (!req.user) return next(createError(401, 'Please login to view this page.'));
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);
// createError(status, msg)
var err = createError(404, 'LOL');
// createError(status, props)
var err = createError(404, {id: 1});
// createError(props)
var err = createError({id: 1});
console.log((<any> err).id);
// createError(msg, status)
var err = createError('LOL', 404);
// createError(msg)
var err = createError('LOL');
// createError(msg, props)
var err = createError('LOL', {id: 1});
// createError(err)
var err = createError(new Error('LOL'));
// createError(err, props)
var err = createError(new Error('LOL'), {id: 1});
// createError(status, err, props)
var err = createError(404, new Error('LOL'), {id: 1});
// createError(status, msg, props)
var err = createError(404, 'LOL', {id: 1});
// createError(status, msg, { expose: false })
var err = createError(404, 'LOL', {expose: false})
// new createError.NotFound()
var err = new createError.NotFound();
// 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"
+85
View File
@@ -0,0 +1,85 @@
// Type definitions for http-errors v1.3.1
// Project: https://github.com/jshttp/http-errors
// Definitions by: Tanguy Krotoff <https://github.com/tkrotoff>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
declare module 'http-errors' {
interface HttpError extends Error {
status: number;
statusCode: number;
expose: boolean;
}
interface CreateHttpError {
// See https://github.com/Microsoft/TypeScript/issues/227#issuecomment-50092674
[code: string]: new() => HttpError;
(...args: Array<Error | string | number | Object>): HttpError;
Continue: new() => HttpError;
SwitchingProtocols: new() => HttpError;
Processing: new() => HttpError;
OK: new() => HttpError;
Created: new() => HttpError;
Accepted: new() => HttpError;
NonAuthoritativeInformation: new() => HttpError;
NoContent: new() => HttpError;
ResetContent: new() => HttpError;
PartialContent: new() => HttpError;
MultiStatus: new() => HttpError;
AlreadyReported: new() => HttpError;
IMUsed: new() => HttpError;
MultipleChoices: new() => HttpError;
MovedPermanently: new() => HttpError;
Found: new() => HttpError;
SeeOther: new() => HttpError;
NotModified: new() => HttpError;
UseProxy: new() => HttpError;
Unused: new() => HttpError;
TemporaryRedirect: new() => HttpError;
PermanentRedirect: new() => HttpError;
BadRequest: new() => HttpError;
Unauthorized: new() => HttpError;
PaymentRequired: new() => HttpError;
Forbidden: new() => HttpError;
NotFound: new() => HttpError;
MethodNotAllowed: new() => HttpError;
NotAcceptable: new() => HttpError;
ProxyAuthenticationRequired: new() => HttpError;
RequestTimeout: new() => HttpError;
Conflict: new() => HttpError;
Gone: new() => HttpError;
LengthRequired: new() => HttpError;
PreconditionFailed: new() => HttpError;
PayloadTooLarge: new() => HttpError;
URITooLong: new() => HttpError;
UnsupportedMediaType: new() => HttpError;
RangeNotSatisfiable: new() => HttpError;
ExpectationFailed: new() => HttpError;
ImATeapot: new() => HttpError;
UnprocessableEntity: new() => HttpError;
Locked: new() => HttpError;
FailedDependency: new() => HttpError;
UnorderedCollection: new() => HttpError;
UpgradeRequired: new() => HttpError;
PreconditionRequired: new() => HttpError;
TooManyRequests: new() => HttpError;
RequestHeaderFieldsTooLarge: new() => HttpError;
UnavailableForLegalReasons: new() => HttpError;
InternalServerError: new() => HttpError;
NotImplemented: new() => HttpError;
BadGateway: new() => HttpError;
ServiceUnavailable: new() => HttpError;
GatewayTimeout: new() => HttpError;
HTTPVersionNotSupported: new() => HttpError;
VariantAlsoNegotiates: new() => HttpError;
InsufficientStorage: new() => HttpError;
LoopDetected: new() => HttpError;
BandwidthLimitExceeded: new() => HttpError;
NotExtended: new() => HttpError;
NetworkAuthenticationRequired: new() => HttpError;
}
var httpError: CreateHttpError;
export = httpError;
}
+33
View File
@@ -0,0 +1,33 @@
/// <reference path="statuses.d.ts" />
import status = require('statuses');
var code: number;
code = status(403) // => 403
code = status('403') // => 403
code = status('forbidden') // => 403
code = status('Forbidden') // => 403
code = status(306) // throws, as it's not supported by node.js
var codes: Array<number>;
codes = status.codes;
var msg: string;
msg = status[404] // => 'Not Found'
code = status['not found'] // => 404
code = status['Not Found'] // => 404
var isRedirect: boolean;
isRedirect = status.redirect[200] // => undefined
isRedirect = status.redirect[301] // => true
var isEmpty: boolean;
isEmpty = status.empty[200] // => undefined
isEmpty = status.empty[204] // => true
isEmpty = status.empty[304] // => true
var isRetry: boolean;
isRetry = status.retry[501] // => undefined
isRetry = status.retry[503] // => true
+21
View File
@@ -0,0 +1,21 @@
// Type definitions for http-errors v1.2.1
// Project: https://github.com/jshttp/statuses
// Definitions by: Tanguy Krotoff <https://github.com/tkrotoff>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
declare module 'statuses' {
interface Status {
[code: number]: string;
[msg: string]: any | number;
codes: Array<number>;
redirect: {[code: number]: boolean};
empty: {[code: number]: boolean};
retry: {[code: number]: boolean};
(code: number | string): number;
}
var status: Status;
export = status;
}