mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-05-30 18:43:21 +08:00
47
types/named-routes/index.d.ts
vendored
Normal file
47
types/named-routes/index.d.ts
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
// Type definitions for named-routes 2.0
|
||||
// Project: https://github.com/alubbe/named-routes#readme
|
||||
// Definitions by: Philipp Katz <https://github.com/qqilihq>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.2
|
||||
|
||||
import * as express from 'express';
|
||||
|
||||
declare module 'express-serve-static-core' {
|
||||
interface Application {
|
||||
namedRoutes: NamedRouter;
|
||||
}
|
||||
// tslint:disable-next-line interface-name
|
||||
interface IRouterMatcher<T> {
|
||||
(path: PathParams, name: string, ...handlers: RequestHandler[]): T;
|
||||
(path: PathParams, name: string, ...handlers: RequestHandlerParams[]): T;
|
||||
}
|
||||
}
|
||||
|
||||
interface RouterOptions {
|
||||
caseSensitive: boolean;
|
||||
}
|
||||
|
||||
interface RouteOptions {
|
||||
name: string;
|
||||
recursiveWildcard: boolean;
|
||||
caseSensitive: boolean;
|
||||
wildcardInPairs: boolean;
|
||||
}
|
||||
|
||||
interface RouteParams {
|
||||
[ key: string ]: string | string[] | number | number[] | boolean | boolean[] | null;
|
||||
}
|
||||
|
||||
declare class NamedRouter {
|
||||
constructor(options?: Partial<RouterOptions>);
|
||||
match(req: express.Request): boolean | object;
|
||||
add(method: string, path: string, callbacks: express.RequestHandler | express.RequestHandler[], options?: Partial<RouteOptions>): void;
|
||||
build(name: string, params?: RouteParams, method?: string): string;
|
||||
registerAppHelpers(app: express.Express): NamedRouter;
|
||||
param(name: string, callback: express.RequestHandler): NamedRouter;
|
||||
param(callback: express.RequestHandler): NamedRouter;
|
||||
dispatch(req: express.Request, res?: express.Response, next?: express.NextFunction): void;
|
||||
extendExpress(app: express.Express | express.Router): NamedRouter;
|
||||
}
|
||||
|
||||
export = NamedRouter;
|
||||
35
types/named-routes/named-routes-tests.ts
Normal file
35
types/named-routes/named-routes-tests.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import NamedRouter = require('named-routes');
|
||||
import * as express from 'express';
|
||||
const app = express();
|
||||
|
||||
// constructor and `RouterOptions`
|
||||
let router = new NamedRouter();
|
||||
router = new NamedRouter({ caseSensitive: true });
|
||||
|
||||
router.extendExpress(app); // $ExpectType NamedRouter
|
||||
router.registerAppHelpers(app); // $ExpectType NamedRouter
|
||||
app.get('/path/:id', 'foo', () => {}); // $ExpectType Express
|
||||
app.namedRoutes.build('foo', { id: 1 }); // $ExpectType string
|
||||
|
||||
const expressRouter = express.Router();
|
||||
router.extendExpress(expressRouter); // $ExpectType NamedRouter
|
||||
expressRouter.post('/path/:id', 'foo', () => {}); // $ExpectType Router
|
||||
|
||||
// `RouteOptions`
|
||||
router.add('get', '/path/:id', () => {}); // $ExpectType void
|
||||
router.add('get', '/path/:id', () => {}, { name: 'foo' }); // $ExpectType void
|
||||
router.add('get', '/path/:id', () => {}, { name: 'foo', recursiveWildcard: true }); // $ExpectType void
|
||||
router.add('get', '/path/:id', () => {}, { name: 'foo', caseSensitive: true }); // $ExpectType void
|
||||
router.add('get', '/path/:id', () => {}, { name: 'foo', wildcardInPairs: true }); // $ExpectType void
|
||||
|
||||
// `RouteParams`
|
||||
router.build('foo', { string: 'a' }); // $ExpectType string
|
||||
router.build('foo', { stringArray: [ 'a', 'b' ] }); // $ExpectType string
|
||||
router.build('foo', { number: 1 }); // $ExpectType string
|
||||
router.build('foo', { numberArray: [ 1, 2 ] }); // $ExpectType string
|
||||
router.build('foo', { boolean: true }); // $ExpectType string
|
||||
router.build('foo', { booleanArray: [ true, false ] }); // $ExpectType string
|
||||
router.build('foo', { null: null }); // $ExpectType string
|
||||
|
||||
const req: express.Request = {} as any;
|
||||
router.dispatch(req); // $ExpectType void
|
||||
23
types/named-routes/tsconfig.json
Normal file
23
types/named-routes/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",
|
||||
"named-routes-tests.ts"
|
||||
]
|
||||
}
|
||||
1
types/named-routes/tslint.json
Normal file
1
types/named-routes/tslint.json
Normal file
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Reference in New Issue
Block a user