Skip to content

Commit

Permalink
fix: fixed the params type of the group method
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmet Atay committed Oct 12, 2024
1 parent 52d754f commit d5248dc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"scripts": {
"start": "tsdx watch",
"build": "tsdx build",
"build": "tsdx build && cp src/types.d.ts dist/types.d.ts",
"test": "tsdx test --coverage",
"lint": "tsdx lint",
"prepare": "tsdx build",
Expand Down Expand Up @@ -78,4 +78,4 @@
"lodash.camelcase": "^4.3.0",
"pluralize": "^8.0.0"
}
}
}
18 changes: 6 additions & 12 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import {
NextFunction,
IRouter as IExpressRouter,
Request,
RequestHandler,
Response,
Express
} from 'express';

import RouteGroup from './index';
Expand All @@ -14,12 +11,9 @@ export type EndpointNames = keyof typeof RESOURCES;
export type RequestMethods = 'get' | 'post' | 'put' | 'patch' | 'delete';
export type RegisterCb = (g: IRouter) => void;

export type GroupArgs =
| [path: string, RegisterCb]
| [path: string, ...NextFunction[], RegisterCb]
| [...NextFunction[], RegisterCb]
export type GroupArgs = [...unknown[], RegisterCb];

export interface IResource {
export interface IResource {
index?: RequestHandler;
show?: RequestHandler;
store?: RequestHandler;
Expand All @@ -38,16 +32,16 @@ export type ResourceType = {
update?: NextFunction[];
patch?: NextFunction[];
destroy?: NextFunction[];
},
};
parameters?: {
[prop: string]: string;
}
};
};

export type IRouter = IExpressRouter & {
get path(): string;
getPath(): string;
getRouter: () => IExpressRouter;
listRoutes: () => ({ method: string; path: string; })[];
listRoutes: () => { method: string; path: string }[];
group: (...args: GroupArgs) => IRouter;
resource: (ins: IResource | ResourceType) => IRouter;
};
20 changes: 16 additions & 4 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ describe('express-route-grouping', () => {
// Assert
sandbox.assert.calledOnceWithExactly(
registerSpy,
sinon.match.instanceOf(RouteGroup).and(sinon.match.has('path', 'b'))
sinon.match.instanceOf(RouteGroup)
);

expect(result).toBeInstanceOf(RouteGroup);

const { firstArg: ins } = registerSpy.getCall(0);
expect(ins.getPath()).toEqual('b');
});

it('should be register nested route groups', () => {
Expand All @@ -46,10 +49,13 @@ describe('express-route-grouping', () => {
// Assert
sandbox.assert.calledOnceWithMatch(
registerSpy,
sinon.match.instanceOf(RouteGroup).and(sinon.match.has('path', 'a/b/c'))
sinon.match.instanceOf(RouteGroup)
);

expect(result).toBeInstanceOf(RouteGroup);

const { firstArg: ins } = registerSpy.getCall(0);
expect(ins.getPath()).toEqual('a/b/c');
});

it('should be register the group without additional path', () => {
Expand All @@ -63,10 +69,13 @@ describe('express-route-grouping', () => {
// Assert
sandbox.assert.calledOnceWithExactly(
registerSpy,
sinon.match.instanceOf(RouteGroup).and(sinon.match.has('path', ''))
sinon.match.instanceOf(RouteGroup)
);

expect(result).toBeInstanceOf(RouteGroup);

const { firstArg: ins } = registerSpy.getCall(0);
expect(ins.getPath()).toEqual('');
});

it('should be register the nested groups without additional path', () => {
Expand Down Expand Up @@ -97,10 +106,13 @@ describe('express-route-grouping', () => {
// Assert
sandbox.assert.calledOnceWithExactly(
registerSpy,
sinon.match.instanceOf(RouteGroup).and(sinon.match.has('path', 'c/d'))
sinon.match.instanceOf(RouteGroup)
);

expect(result).toBeInstanceOf(RouteGroup);

const { firstArg: ins } = registerSpy.getCall(0);
expect(ins.getPath()).toEqual('c/d');
});
});

Expand Down

0 comments on commit d5248dc

Please sign in to comment.