Skip to content
This repository has been archived by the owner on Nov 9, 2020. It is now read-only.

Commit

Permalink
fixed #1
Browse files Browse the repository at this point in the history
  • Loading branch information
mildronize committed Nov 4, 2020
1 parent fe35d41 commit df86ed8
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"test:coverage": "jest --config jestconfig.json ---forceExit --coverage",
"test:watch": "jest --config jestconfig.json --watch --forceExit",
"prepare": "run-s build",
"prepublishOnlyx": "run-s format lint test build",
"prepublishOnly": "run-s format lint test build",
"publish:patch": "npm version patch --force && npm publish",
"example": "chokidar \"**/*.ts\" -c \" ts-node example/tester/index.ts\""
},
Expand Down
18 changes: 17 additions & 1 deletion src/express-extension/use-express-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ function addRouterToExpress(app: express.Application, combinedRoutes: CombineRou
combinedRoutes.forEach((route: any) => {
if (!route.isClass) {
const requestMethod: RequestMethod = route.requestMethod;
const routePath = prefix + route.path;
const routePath = combineRouterPath(prefix, route.path);

if (route.middlewares.length > 0) {
// Combine multiple middlewares
const middleware = combineMiddlewares(...route.middlewares);
app[requestMethod](routePath, middleware, callInstance(controllerInstance, route));
} else {
Expand All @@ -53,6 +54,21 @@ function addRouterToExpress(app: express.Application, combinedRoutes: CombineRou
});
}

export const combineRouterPath = (prefix: string, path: string) => {
let result = '';
if (prefix !== '') {
if (prefix.charAt(0) === '/') prefix = prefix.substring(1);
result += prefix;
}
result += '/';
if (path !== '') {
if (path.charAt(0) === '/') path = path.substring(1);
result += path;
}
if (result.charAt(0) !== '/') return '/' + result;
return result;
};

const callInstance = (instance: any, route: RouteMetadataArgs) =>
asyncHelper(async (req: Request, res: Response, next: NextFunction) => {
await instance[route.methodName](req, res, next);
Expand Down
54 changes: 54 additions & 0 deletions test/express-extension/utils/combine-router-path.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { combineRouterPath } from '../../../src/express-extension/use-express-server';

describe('combineRouterPath', () => {

it(`path: '' & '' path should be '/'`, () => {
const result = combineRouterPath('','');
const expected = '/';
expect(expected).toBe(result);
});

it(`path: '/' & '' should be '/'`, () => {
const result = combineRouterPath('/','');
const expected = '/';
expect(expected).toBe(result);
});

it(`path: '' & '/' should be '/'`, () => {
const result = combineRouterPath('','/');
const expected = '/';
expect(expected).toBe(result);
});

it(`path: '/' & '/' should be '/'`, () => {
const result = combineRouterPath('/','/');
const expected = '/';
expect(expected).toBe(result);
});

it(`path: 'a' & 'b' should be '/a/b'`, () => {
const result = combineRouterPath('a','b');
const expected = '/a/b';
expect(expected).toBe(result);
});

it(`path: '/a' & 'b' should be '/a/b'`, () => {
const result = combineRouterPath('/a','b');
const expected = '/a/b';
expect(expected).toBe(result);
});

it(`path: 'a' & '/b' should be '/a/b'`, () => {
const result = combineRouterPath('a','/b');
const expected = '/a/b';
expect(expected).toBe(result);
});

it(`path: '/a' & '/b' should be '/a/b'`, () => {
const result = combineRouterPath('/a','/b');
const expected = '/a/b';
expect(expected).toBe(result);
});


});

0 comments on commit df86ed8

Please sign in to comment.