From d404baa8b60fccb3ec4d0a4c2986f07faa9f7e6b Mon Sep 17 00:00:00 2001 From: Tanguy Krotoff Date: Fri, 3 Jul 2015 13:31:06 +0200 Subject: [PATCH 1/3] Add definitions for statuses (https://github.com/jshttp/statuses) --- statuses/statuses-tests.ts | 33 +++++++++++++++++++++++++++++++++ statuses/statuses.d.ts | 21 +++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 statuses/statuses-tests.ts create mode 100644 statuses/statuses.d.ts diff --git a/statuses/statuses-tests.ts b/statuses/statuses-tests.ts new file mode 100644 index 000000000..a55a706ff --- /dev/null +++ b/statuses/statuses-tests.ts @@ -0,0 +1,33 @@ +/// + +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; +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 diff --git a/statuses/statuses.d.ts b/statuses/statuses.d.ts new file mode 100644 index 000000000..7aacc2f61 --- /dev/null +++ b/statuses/statuses.d.ts @@ -0,0 +1,21 @@ +// Type definitions for http-errors v1.2.1 +// Project: https://github.com/jshttp/statuses +// Definitions by: Tanguy Krotoff +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module 'statuses' { + interface Status { + [code: number]: string; + [msg: string]: any | number; + + codes: Array; + redirect: {[code: number]: boolean}; + empty: {[code: number]: boolean}; + retry: {[code: number]: boolean}; + + (code: number | string): number; + } + + var status: Status; + export = status; +} From 715bd098cbcd439bd8d3e49e8a134fce0ec46009 Mon Sep 17 00:00:00 2001 From: Tanguy Krotoff Date: Fri, 3 Jul 2015 13:31:15 +0200 Subject: [PATCH 2/3] Add definitions for http-errors (https://github.com/jshttp/http-errors) --- http-errors/http-errors-tests.ts | 69 ++++++++++++++++++++++++++ http-errors/http-errors.d.ts | 85 ++++++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 http-errors/http-errors-tests.ts create mode 100644 http-errors/http-errors.d.ts diff --git a/http-errors/http-errors-tests.ts b/http-errors/http-errors-tests.ts new file mode 100644 index 000000000..06b91f13b --- /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 000000000..6f78ff6a7 --- /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; +} From 188cbfcb0ce61de02306a89e581a74aab0210172 Mon Sep 17 00:00:00 2001 From: Tanguy Krotoff Date: Fri, 3 Jul 2015 13:31:26 +0200 Subject: [PATCH 3/3] Add definitions for api-error-handler (https://github.com/expressjs/api-error-handler) --- api-error-handler/api-error-handler-tests.ts | 11 +++++++++++ api-error-handler/api-error-handler.d.ts | 14 ++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 api-error-handler/api-error-handler-tests.ts create mode 100644 api-error-handler/api-error-handler.d.ts diff --git a/api-error-handler/api-error-handler-tests.ts b/api-error-handler/api-error-handler-tests.ts new file mode 100644 index 000000000..83df91e86 --- /dev/null +++ b/api-error-handler/api-error-handler-tests.ts @@ -0,0 +1,11 @@ +/// + +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()); diff --git a/api-error-handler/api-error-handler.d.ts b/api-error-handler/api-error-handler.d.ts new file mode 100644 index 000000000..61a63825d --- /dev/null +++ b/api-error-handler/api-error-handler.d.ts @@ -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 +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare module 'api-error-handler' { + import express = require('express'); + + function apiErrorHandler(options?: any): express.ErrorRequestHandler; + + export = apiErrorHandler; +}