Skip to content

Commit

Permalink
adding root path config
Browse files Browse the repository at this point in the history
  • Loading branch information
dbashford committed Oct 1, 2015
1 parent 84a374b commit d15c14a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ new RouteBuilder()
* [new RouteBuilder()](#constructor)
* [RouteBuilder.addDefault()](#routebuilderadddefault)
* [RouteBuilder.clearDefaults()](#routebuildercleardefaults)
* [RouteBuilder.setRootPath()](#routebuildersetdefaultpath)
* [app()](#app)
* [build()](#build)
* [cache()](#cache)
Expand Down Expand Up @@ -257,6 +258,19 @@ This function is handy for clearing defaults after creating a group of routes, p
RouteBuilder.clearDefaults();
```

#### `RouteBuilder.setRootPath`
This static function sets a root path for all paths created after `setRootPath` is called. So if `path('/profile')` is called on a RouteBuilder after `setRootPath('/api')` is called, then the resulting route path would be `/api/profile`.

To clear the root path, simply call `setRootPath` with no parameters.

```javascript
RouteBuilder.setRootPath("/api");

... build a route

RouteBuilder.setRootPath();
```

#### `app`
Sets the `config.app` property.

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hapi-route-builder",
"version": "0.2.0",
"version": "0.3.0",
"homepage": "https://github.com/dbashford/hapi-route-builder",
"author": "David Bashford",
"description": "A fluid/builder pattern approach to building Hapi routes.",
Expand All @@ -25,7 +25,7 @@
},
"devDependencies": {
"coveralls": "^2.11.2",
"hapi": "8.6.1",
"hapi": "10.1.0",
"joi": "6.4.3",
"lab": "5.13.0",
"code": "1.4.1"
Expand Down
2 changes: 1 addition & 1 deletion src/configs/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ RouteBuilder.prototype.vhost = function(_vhost) {
};

RouteBuilder.prototype.path = function(_path) {
this.route.path = _path;
this.route.path = (RouteBuilder._rootPath || "") + _path;
return this;
};

Expand Down
6 changes: 6 additions & 0 deletions src/route_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ function RouteBuilder() {
this._applyDefaults(false);
}

RouteBuilder._rootPath = undefined;

RouteBuilder.setRootPath = function(rootPath) {
RouteBuilder._rootPath = rootPath;
};

RouteBuilder.defaultsArray = [];

/**
Expand Down
17 changes: 17 additions & 0 deletions test/units/builder_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ describe("building config", function() {
done();
});

it("uses the rootPath", function(done) {
RouteBuilder.setRootPath("/api");
var config1 = new RouteBuilder()
.path("/foo")
.build();

RouteBuilder.setRootPath(undefined);

var config2 = new RouteBuilder()
.path("/foo")
.build();

expect(config1.path).to.equal("/api/foo");
expect(config2.path).to.equal("/foo");
done();
});

it("applies defaults", function(done) {
RouteBuilder.addDefault(new RBDefault(function(rb) {
// will time out if not called
Expand Down

0 comments on commit d15c14a

Please sign in to comment.