Skip to content

Commit

Permalink
Merge da7adac into 4f274c7
Browse files Browse the repository at this point in the history
  • Loading branch information
siddiqus committed Feb 7, 2020
2 parents 4f274c7 + da7adac commit 8f9611b
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ Here you'll notice that the request object is available with `this.req` and you
);
```
Similarly, the class methods `post`, `put`, `delete`, `patch`, `head`, and `options` are available for the Route class e.g. `Route.post`.

- You can redirect a route by simply passing the redirect url to the controller parameter e.g. `Route.get("/some-url", "/another-url")`
- Each object in the *subroutes* array can be constructed using the `subroute` function, like so:
```javascript
const { subroute } = require("@siddiqus/expressive");
Expand Down
29 changes: 29 additions & 0 deletions __tests__/RouteUtil.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,33 @@ describe("RouteUtil", () => {
expect(result).toBeTruthy();
});
});

describe("isUrlPath", () => {
it("Should return true for valid urls", () => {
[
"/hehe/some",
"/hehe",
"/hehe-blah/:id/ahhs/:someId/hash"
].forEach(str => {
expect(RouteUtil.isUrlPath(str)).toBeTruthy();
});
});

it("Should return false for invalid urls", () => {
[
"ajdlasjdksad",
"10980du90",
"/hh*ehe",
"hehe/",
"/heheh ooo"
].forEach(str => {
expect(RouteUtil.isUrlPath(str)).toBeFalsy();
});
});

it("Should return false if non string input is given", () => {
expect(RouteUtil.isUrlPath(() => { })).toBeFalsy();
expect(RouteUtil.isUrlPath(class SomeClass {})).toBeFalsy();
});
});
});
13 changes: 13 additions & 0 deletions __tests__/RouterFactory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,19 @@ describe("RouterFactory", () => {
});
});

describe("_executeController", () => {
it("Should redirect if route url", () => {
const factory = new RouterFactory();
const mockRes = {
redirect: jest.fn()
};

factory._executeController("/somepath", null, mockRes, null);

expect(mockRes.redirect).toHaveBeenCalledWith("/somepath");
});
});

it("Should get router from method", () => {
const factory = new RouterFactory();

Expand Down
3 changes: 2 additions & 1 deletion example/src/routers/Root/RootRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ module.exports = {
routes: [
Route.get("/", RootGetController, {
doc: RootGetDoc
})
}),
Route.get("/hey", "/hello")
],
subroutes: [
subroute("/hello", HelloRouter, {
Expand Down
36 changes: 18 additions & 18 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"express": "4.16.3",
"express-request-id": "1.4.1",
"express-validator": "6.2.0",
"helmet": "3.20.0",
"helmet": "3.21.2",
"swagger-ui-express": "4.0.1"
},
"jest-stare": {
Expand Down Expand Up @@ -75,7 +75,7 @@
},
"husky": {
"hooks": {
"pre-commit": "npm test || npm run lintFix || git add -A",
"pre-commit": "npm test && npm run lintFix || git add -A",
"pre-push": "npm test"
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/RouteUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,10 @@ module.exports = class RouteUtil {
if (stringPrefix.includes(CLASS_STRING)) return false;
return functionToCheck instanceof Function || stringPrefix === FUNCTION_STRING;
}

static isUrlPath(string) {
if (typeof string !== "string") return false;
const regex = /^(\/[a-zA-Z0-9\-:]+)+\/?$/g;
return regex.test(string);
}
};
4 changes: 4 additions & 0 deletions src/RouterFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ module.exports = class RouterFactory {
}

async _executeController(controller, req, res, next) {
if (this.routeUtil.isUrlPath(controller)) {
return res.redirect(controller);
}

if (this.routeUtil.isFunction(controller)) {
return controller(req, res, next);
}
Expand Down
2 changes: 1 addition & 1 deletion src/redoc/registerRedoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ module.exports = function registerRedoc(app, swaggerJson, url) {
specUrl
}));
});
}
};

0 comments on commit 8f9611b

Please sign in to comment.