Add definitions for http-errors (https://github.com/jshttp/http-errors)

This commit is contained in:
Tanguy Krotoff
2015-07-03 13:31:15 +02:00
parent d404baa8b6
commit 715bd098cb
2 changed files with 154 additions and 0 deletions

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
http-errors/http-errors.d.ts vendored Normal file
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;
}