diff --git a/types/trouter/index.d.ts b/types/trouter/index.d.ts new file mode 100644 index 0000000000..435a611971 --- /dev/null +++ b/types/trouter/index.d.ts @@ -0,0 +1,78 @@ +// Type definitions for trouter 3.0 +// Project: https://github.com/lukeed/trouter +// Definitions by: Markus Lanz +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +declare namespace Trouter { + interface FindResult { + params: { [k: string]: string; }; + handlers: T[]; + } + + type HTTPMethod = + | 'ACL' + | 'BIND' + | 'CHECKOUT' + | 'CONNECT' + | 'COPY' + | 'DELETE' + | 'GET' + | 'HEAD' + | 'LINK' + | 'LOCK' + | 'M-SEARCH' + | 'MERGE' + | 'MKACTIVITY' + | 'MKCALENDAR' + | 'MKCOL' + | 'MOVE' + | 'NOTIFY' + | 'OPTIONS' + | 'PATCH' + | 'POST' + | 'PROPFIND' + | 'PROPPATCH' + | 'PURGE' + | 'PUT' + | 'REBIND' + | 'REPORT' + | 'SEARCH' + | 'SOURCE' + | 'SUBSCRIBE' + | 'TRACE' + | 'UNBIND' + | 'UNLINK' + | 'UNLOCK' + | 'UNSUBSCRIBE'; +} + +declare class Trouter { + use(pattern: string, ...handlers: T[]): this; + + find(method: Trouter.HTTPMethod, url: string): Trouter.FindResult; + + add(method: Trouter.HTTPMethod, pattern: string, ...handlers: T[]): this; + + all(pattern: string, ...handlers: T[]): this; + + get(pattern: string, ...handlers: T[]): this; + + head(pattern: string, ...handlers: T[]): this; + + patch(pattern: string, ...handlers: T[]): this; + + options(pattern: string, ...handlers: T[]): this; + + connect(pattern: string, ...handlers: T[]): this; + + delete(pattern: string, ...handlers: T[]): this; + + trace(pattern: string, ...handlers: T[]): this; + + post(pattern: string, ...handlers: T[]): this; + + put(pattern: string, ...handlers: T[]): this; +} + +export = Trouter; diff --git a/types/trouter/trouter-tests.ts b/types/trouter/trouter-tests.ts new file mode 100644 index 0000000000..80346e3d79 --- /dev/null +++ b/types/trouter/trouter-tests.ts @@ -0,0 +1,52 @@ +import Trouter = require('trouter'); + +// Default type is "any" for handlers +const router = new Trouter(); +router.add('GET', '/user/:name/:id', 'test', 'test2', 'test3'); + +// Typed handler example +type Handler = (typedValue: number, couldBeAResponse: string) => void; + +const typed = new Trouter(); +typed.get('/user/:id', (t: number, s: string) => { +}); +const findResult = typed.find('GET', '/users/1'); +if (findResult.handlers.length > 0) { + findResult.handlers[0](42, "asdf"); +} +const notFound = typed.find('GET', '/not-existent'); +if (notFound.handlers.length === 0) { + // no match... +} + +// Find +const findTest = new Trouter(); +findTest.add('GET', '/user/:id', 'handler'); +const fresult = findTest.find('GET', '/user/1'); +if (fresult.params.id) { + // do something awesome! + fresult.handlers[0] === 'handler'; +} + +// Full API +const full = new Trouter(); +full.all('/somepattern', 'a handler'); +full.get('/somepattern', 'a handler'); +full.head('/somepattern', 'a handler'); +full.patch('/somepattern', 'a handler'); +full.options('/somepattern', 'a handler'); +full.connect('/somepattern', 'a handler'); +full.delete('/somepattern', 'a handler'); +full.trace('/somepattern', 'a handler'); +full.post('/somepattern', 'a handler'); +full.put('/somepattern', 'a handler'); + +full.add('GET', '/somepatternm', 'a handler'); +full.add('HEAD', '/somepatternm', 'a handler'); +full.add('PATCH', '/somepatternm', 'a handler'); +full.add('OPTIONS', '/somepatternm', 'a handler'); +full.add('CONNECT', '/somepatternm', 'a handler'); +full.add('DELETE', '/somepatternm', 'a handler'); +full.add('TRACE', '/somepatternm', 'a handler'); +full.add('POST', '/somepatternm', 'a handler'); +full.add('PUT', '/somepatternm', 'a handler'); diff --git a/types/trouter/tsconfig.json b/types/trouter/tsconfig.json new file mode 100644 index 0000000000..70dfaf7976 --- /dev/null +++ b/types/trouter/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "trouter-tests.ts" + ] +} diff --git a/types/trouter/tslint.json b/types/trouter/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/trouter/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }