-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
51c56e6
commit 9a04177
Showing
5 changed files
with
231 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
export = Router; | ||
|
||
declare namespace Router { | ||
|
||
export interface RouterOptions { | ||
strict?: boolean; | ||
caseSensitive?: boolean; | ||
mergeParams?: boolean; | ||
} | ||
|
||
interface IncommingRequest { | ||
url: string, | ||
method: string, | ||
originalUrl?: string, | ||
params?: any; | ||
} | ||
|
||
interface RoutedRequest extends IncommingRequest { | ||
baseUrl: string, | ||
next?: NextFunction, | ||
route?: IRoute | ||
} | ||
|
||
type RequestParamHandler = (req: IncommingRequest, res: any, next: NextFunction, value: any, name: string) => any; | ||
|
||
interface RouteHandler { | ||
// tslint:disable-next-line callable-types (This is extended from and can't extend from a type alias in ts<2.2 | ||
(req: RoutedRequest, res: any, next: NextFunction): any; | ||
} | ||
|
||
interface RequestHandler { | ||
// tslint:disable-next-line callable-types (This is extended from and can't extend from a type alias in ts<2.2 | ||
(req: IncommingRequest, res: any, next: NextFunction): any; | ||
} | ||
|
||
|
||
interface NextFunction { | ||
// tslint:disable-next-line callable-types (In ts2.1 it thinks the type alias has no call signatures) | ||
(err?: any): void; | ||
} | ||
|
||
type ErrorRequestHandler = (err: any, req: IncommingRequest, res: any, next: NextFunction) => any; | ||
|
||
type PathParams = string | RegExp | Array<string | RegExp>; | ||
|
||
type RequestHandlerParams = RouteHandler | ErrorRequestHandler | Array<RouteHandler | ErrorRequestHandler>; | ||
|
||
interface IRouterMatcher<T> { | ||
(path: PathParams, ...handlers: RouteHandler[]): T; | ||
(path: PathParams, ...handlers: RequestHandlerParams[]): T; | ||
} | ||
|
||
interface IRouterHandler<T> { | ||
(...handlers: RouteHandler[]): T; | ||
(...handlers: RequestHandlerParams[]): T; | ||
} | ||
|
||
interface IRouter { | ||
/** | ||
* Map the given param placeholder `name`(s) to the given callback(s). | ||
* | ||
* Parameter mapping is used to provide pre-conditions to routes | ||
* which use normalized placeholders. For example a _:user_id_ parameter | ||
* could automatically load a user's information from the database without | ||
* any additional code, | ||
* | ||
* The callback uses the samesignature as middleware, the only differencing | ||
* being that the value of the placeholder is passed, in this case the _id_ | ||
* of the user. Once the `next()` function is invoked, just like middleware | ||
* it will continue on to execute the route, or subsequent parameter functions. | ||
* | ||
* app.param('user_id', function(req, res, next, id){ | ||
* User.find(id, function(err, user){ | ||
* if (err) { | ||
* next(err); | ||
* } else if (user) { | ||
* req.user = user; | ||
* next(); | ||
* } else { | ||
* next(new Error('failed to load user')); | ||
* } | ||
* }); | ||
* }); | ||
*/ | ||
param(name: string, handler: RequestParamHandler): this; | ||
|
||
/** | ||
* Alternatively, you can pass only a callback, in which case you have the opportunity to alter the app.param() | ||
* | ||
* @deprecated since version 4.11 | ||
*/ | ||
param(callback: (name: string, matcher: RegExp) => RequestParamHandler): this; | ||
|
||
/** | ||
* Special-cased "all" method, applying the given route `path`, | ||
* middleware, and callback to _every_ HTTP method. | ||
*/ | ||
all: IRouterMatcher<this>; | ||
|
||
get: IRouterMatcher<this>; | ||
post: IRouterMatcher<this>; | ||
put: IRouterMatcher<this>; | ||
delete: IRouterMatcher<this>; | ||
patch: IRouterMatcher<this>; | ||
options: IRouterMatcher<this>; | ||
head: IRouterMatcher<this>; | ||
|
||
use: IRouterHandler<this> & IRouterMatcher<this>; | ||
|
||
handle: RequestHandler; | ||
|
||
route(prefix: PathParams): IRoute; | ||
/** | ||
* Stack of configured routes | ||
*/ | ||
stack: any[]; | ||
} | ||
|
||
interface IRoute { | ||
path: string; | ||
stack: any; | ||
all: IRouterHandler<this>; | ||
get: IRouterHandler<this>; | ||
post: IRouterHandler<this>; | ||
put: IRouterHandler<this>; | ||
delete: IRouterHandler<this>; | ||
patch: IRouterHandler<this>; | ||
options: IRouterHandler<this>; | ||
head: IRouterHandler<this>; | ||
} | ||
|
||
interface RouterConstructor extends IRouter { | ||
new(options?: RouterOptions): IRouter & RequestHandler; | ||
} | ||
|
||
} | ||
|
||
declare var Router: Router.RouterConstructor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { createServer, IncomingMessage, ServerResponse } from 'http'; | ||
import { | ||
default as Router, | ||
RouterOptions, | ||
IncommingRequest, | ||
RouteHandler, | ||
IRoute, | ||
NextFunction, | ||
RoutedRequest | ||
} from '..'; | ||
|
||
|
||
const options: RouterOptions = { | ||
strict: false, | ||
caseSensitive: false, | ||
mergeParams: false | ||
}; | ||
|
||
const r = new Router(); | ||
const router = new Router(options); | ||
const routerHandler: RouteHandler = (req: IncommingRequest, res: any, next: NextFunction) => {}; | ||
|
||
router.get('/', routerHandler); | ||
router.post('/', routerHandler); | ||
router.delete('/', routerHandler); | ||
router.patch('/', routerHandler); | ||
router.options('/', routerHandler); | ||
router.head('/', routerHandler); | ||
|
||
// param | ||
router.param('user_id', (req, res, next, id) => {}); | ||
|
||
// middleware | ||
router.use((req, res, next) => { | ||
next(); | ||
}); | ||
|
||
const api: IRoute = router.route('/api/'); | ||
|
||
router.route('/') | ||
.all((req: RoutedRequest, res: any, next: NextFunction) => { | ||
next(); | ||
}) | ||
.get((req, res) => { | ||
// do nothing | ||
}); | ||
|
||
|
||
// test router handling | ||
|
||
var server = createServer(function(req: IncomingMessage, res: ServerResponse) { | ||
router(req as IncommingRequest, res, (err: any) => { | ||
// | ||
}) | ||
router.handle(req as Router.IncommingRequest, res, (err: any) => { | ||
// | ||
}) | ||
}) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"lib": [ | ||
"es6" | ||
], | ||
"esModuleInterop": true, | ||
"noImplicitAny": false, | ||
"noImplicitThis": true, | ||
"strictNullChecks": true, | ||
"strictFunctionTypes": true, | ||
"types": ["node"], | ||
"noEmit": true, | ||
"forceConsistentCasingInFileNames": true | ||
}, | ||
"include": [ | ||
"test/**/*" | ||
], | ||
"files": [ | ||
"index.d.ts" | ||
] | ||
} |