mirror of
https://github.com/FlipsideCrypto/DefinitelyTyped.git
synced 2026-02-06 10:56:53 +00:00
Added new types for trouter (#33557)
* Added types for trouter * Fixes * fix: include all valid HTTP methods * feat: add use(); bump to 3.0 * fix: change export Co-Authored-By: lukeed <luke.edwards05@gmail.com> * fix: use require in test * fix: incorrect import statement Co-Authored-By: lukeed <luke.edwards05@gmail.com> * fix: use namespace * fix: remove `false` return option; - part of 3.0 release
This commit is contained in:
parent
bfb70da14c
commit
08257fbe2b
78
types/trouter/index.d.ts
vendored
Normal file
78
types/trouter/index.d.ts
vendored
Normal file
@ -0,0 +1,78 @@
|
||||
// Type definitions for trouter 3.0
|
||||
// Project: https://github.com/lukeed/trouter
|
||||
// Definitions by: Markus Lanz <https://github.com/stahlstift>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.3
|
||||
|
||||
declare namespace Trouter {
|
||||
interface FindResult<T> {
|
||||
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<T = any> {
|
||||
use(pattern: string, ...handlers: T[]): this;
|
||||
|
||||
find(method: Trouter.HTTPMethod, url: string): Trouter.FindResult<T>;
|
||||
|
||||
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;
|
||||
52
types/trouter/trouter-tests.ts
Normal file
52
types/trouter/trouter-tests.ts
Normal file
@ -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<Handler>();
|
||||
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');
|
||||
23
types/trouter/tsconfig.json
Normal file
23
types/trouter/tsconfig.json
Normal file
@ -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"
|
||||
]
|
||||
}
|
||||
1
types/trouter/tslint.json
Normal file
1
types/trouter/tslint.json
Normal file
@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Loading…
Reference in New Issue
Block a user