Skip to content

Commit

Permalink
Bug fix - resources with custom urlPrefixes did not get hypermedia mi…
Browse files Browse the repository at this point in the history
…ddleware
  • Loading branch information
arobson committed Aug 17, 2015
1 parent 886774c commit bce0b77
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 0.2.x

### 0.2.4
Bug fix - resources with custom urlPrefixes did not get hypermedia middleware

### 0.2.3

> Note: API change is non-breaking as original call format still works - older version is no longer documented in README.
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,8 @@ var host = hyped.createHost( autohost, {
},
function() {
// callback gets invoked once all resources are loaded
// this is largely to support test setups
// this is where you should call start on the host variable
host.start();
} );
```

Expand Down
5 changes: 3 additions & 2 deletions demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ if ( cluster.isMaster ) {
} else {
var hyped = require( "../src/index.js" )();
var autohost = require( "autohost" );
var host = hyped.createHost( autohost, {} ); // just roll with the defaults...
host.start();
var host = hyped.createHost( autohost, {}, function() {
host.start();
} ); // just roll with the defaults...
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hyped",
"version": "0.2.3",
"version": "0.2.4",
"description": "Hypermedia response generation engine",
"main": "src/index.js",
"dependencies": {
Expand Down
13 changes: 13 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,23 @@ function addMiddleware( state, host, apiPrefix ) { // jshint ignore:line
urlPrefix,
apiPrefix
].join( "" ).replace( "//", "/" );

host.http.middleware( prefixUrl, state.optionsMiddleware, "options" );
host.http.middleware( prefixUrl, state.hyperMiddleware, "hyped" );
}
}

function addResourceMiddleware( state, host ) {
var resourcePrefixes = _.filter( _.unique( _.pluck( _.values( state.resources ), "urlPrefix" ) ) );
_.each( resourcePrefixes, function( resourcePrefix ) {
var resourcePrefixUrl = [
resourcePrefix,
state.prefix.apiPrefix
].join( "" ).replace( "//", "/" );
host.http.middleware( resourcePrefixUrl, state.hyperMiddleware, "hyped" );
} );
}

function addResource( state, resource, resourceName ) { // jshint ignore:line
state.resources[ resourceName ] = resource;
}
Expand All @@ -62,6 +74,7 @@ function createHost( state, autohost, config, done ) {
var subscription;
subscription = host.onResources( function( resources ) {
state.addResources( resources );
addResourceMiddleware( state, host );
subscription.unsubscribe();
if ( done ) {
done();
Expand Down

0 comments on commit bce0b77

Please sign in to comment.