Skip to content

Commit

Permalink
Added typescript definition files
Browse files Browse the repository at this point in the history
  • Loading branch information
ilikejames committed Jan 8, 2019
1 parent 51c56e6 commit 9a04177
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 5 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
language: node_js
node_js:
- "0.8"
- "0.10"
- "0.12"
- "1.8"
- "2.5"
Expand Down Expand Up @@ -31,5 +29,6 @@ script:
- "test ! -z $(npm -ps ls istanbul) || npm test"
- "test -z $(npm -ps ls istanbul) || npm run-script test-travis"
- "test -z $(npm -ps ls eslint) || npm run-script lint"
- "npm run-script test-types"
after_script:
- "test -e ./coverage/lcov.info && npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"
138 changes: 138 additions & 0 deletions index.d.ts
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;
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,34 @@
"utils-merge": "1.0.1"
},
"devDependencies": {
"@types/node": "^10.12.18",
"after": "0.8.2",
"eslint": "3.19.0",
"eslint-plugin-markdown": "1.0.0-beta.6",
"finalhandler": "1.1.1",
"istanbul": "0.4.5",
"mocha": "2.5.3",
"supertest": "1.1.0"
"supertest": "1.1.0",
"typescript": "^2.0.0"
},
"files": [
"lib/",
"LICENSE",
"HISTORY.md",
"README.md",
"index.js"
"index.js",
"index.d.ts"
],
"main": "index.js",
"types": "index.d.ts",
"engines": {
"node": ">= 0.8"
},
"scripts": {
"lint": "eslint --plugin markdown --ext js,md .",
"test": "mocha --reporter spec --bail --check-leaks test/",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/",
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/"
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/",
"test-types": "tsc --project tsconfig.json --noEmit"
}
}
61 changes: 61 additions & 0 deletions test/type-check.ts
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) => {
//
})
})



22 changes: 22 additions & 0 deletions tsconfig.json
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"
]
}

0 comments on commit 9a04177

Please sign in to comment.