Skip to content

Commit

Permalink
fix: better types for Router.
Browse files Browse the repository at this point in the history
  • Loading branch information
asos-craigmorten committed May 28, 2020
1 parent 7f058ec commit 5bab51c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# ChangeLog

## [0.5.3] - 28-05-2020

- fix: better types for `Router`.

## [0.5.2] - 28-05-2020

- fix: only set `x-powered-by` header if it is enabled as a setting.
Expand Down
9 changes: 5 additions & 4 deletions src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
PathParams,
IRoute,
Opine,
Router as IRouter,
} from "../src/types.ts";

const create = Object.create;
Expand Down Expand Up @@ -96,10 +97,10 @@ app.defaultConfiguration = function defaultConfiguration(): void {
*/
app.lazyrouter = function lazyrouter(): void {
if (!this._router) {
this._router = new Router({
this._router = new (Router as any)({
caseSensitive: this.enabled("case sensitive routing"),
strict: this.enabled("strict routing"),
});
}) as IRouter;
// TODO: query parser
this._router.use(query());
this._router.use(init(this as Opine));
Expand Down Expand Up @@ -154,7 +155,7 @@ app.use = function use(...args: any[]): Application {
this.lazyrouter();
const router = this._router;

fns.forEach(function (this: Application, fn: any): void {
fns.forEach(function (this: Application, fn: any) {
// non-opine app
if (!fn || !fn.handle || !fn.set) {
return router.use(path, fn);
Expand Down Expand Up @@ -347,7 +348,7 @@ app.all = function all(path: PathParams): Application {
const args = slice.call(arguments, 1);

for (let i = 0; i < methods.length; i++) {
route[methods[i]].apply(route, args);
(route as any)[methods[i]].apply(route, args);
}

return this;
Expand Down
7 changes: 4 additions & 3 deletions src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Response,
Request,
NextFunction,
Router as IRouter,
} from "../types.ts";

const objectRegExp = /^\[object (\S+)\]$/;
Expand All @@ -20,7 +21,7 @@ const setPrototypeOf = Object.setPrototypeOf;
* @return {Router} which is an callable function
* @public
*/
export const Router: any = function (options: any = {}): any {
export const Router: IRouter = function (options: any = {}): any {
function router(
req: Request,
res: Response,
Expand All @@ -39,7 +40,7 @@ export const Router: any = function (options: any = {}): any {
router.stack = [] as any[];

return router;
};
} as any;

// TODO: Router.param

Expand Down Expand Up @@ -440,7 +441,7 @@ Router.route = function route(path: string) {

// create Router#VERB functions
methods.concat("all").forEach(function (method) {
Router[method] = function (path: string) {
(Router as any)[method] = function (path: string) {
let route = this.route(path);
route[method].apply(route, Array.prototype.slice.call(arguments, 1));
return this;
Expand Down
25 changes: 24 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,29 @@ export interface IRouter extends RequestHandler {

route(prefix: PathParams): IRoute;

/**
* @private
*/
handle: RequestHandler;

/**
* @private
*/
process_params(
layer: any,
called: any,
req: Request,
res: Response,
done: NextFunction,
): void;

params: any;
_params: any[];

caseSensitive: boolean;
mergeParams: boolean;
strict: boolean;

/**
* Stack of configured routes
*/
Expand Down Expand Up @@ -652,7 +675,7 @@ export interface Application extends IRouter, Opine.Application {
/**
* Used to get all registered routes in Opine Application
*/
_router: any;
_router: Router;

use: ApplicationRequestHandler<this>;

Expand Down
2 changes: 1 addition & 1 deletion version.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Version of Opine.
*/
export const VERSION: string = "0.5.2";
export const VERSION: string = "0.5.3";

/**
* Supported version of Deno.
Expand Down

0 comments on commit 5bab51c

Please sign in to comment.