diff --git a/http-errors/http-errors-tests.ts b/http-errors/http-errors-tests.ts new file mode 100644 index 0000000000..06b91f13b6 --- /dev/null +++ b/http-errors/http-errors-tests.ts @@ -0,0 +1,69 @@ +/// +/// + +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(( 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" diff --git a/http-errors/http-errors.d.ts b/http-errors/http-errors.d.ts new file mode 100644 index 0000000000..6f78ff6a73 --- /dev/null +++ b/http-errors/http-errors.d.ts @@ -0,0 +1,85 @@ +// Type definitions for http-errors v1.3.1 +// Project: https://github.com/jshttp/http-errors +// Definitions by: Tanguy Krotoff +// 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): 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; +}