Skip to content

Commit

Permalink
Add support for resource and action metadata in rendered HAL output
Browse files Browse the repository at this point in the history
  • Loading branch information
arobson committed Dec 16, 2014
1 parent cf1cf9c commit 53deef2
Show file tree
Hide file tree
Showing 14 changed files with 207 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules/
*npm-debug.log
data
tmp
coverage/
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
demo/
tmp/
node_modules/
node_modules/
coverage/
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 0.1.0

### prerelease 5
Add `_resource` and `_action` fields to resources so that consumers can determine "friendly" names by which each resource was produced.

### prerelease 4
Bug fix - acions with conditions should always render during options.

Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ __HyperModel Structure__
// top level properties starting with a '_' are metadata
// all other properties are attached based on the resource definition
_origin: { href: "", method: "" },
_resource: "thing",
_action: "self",
_links: {
self: { href: "/thing/100", method: "get" },
child: { href: "/thing/100/child/:childId", method: "get", templated: true }
Expand Down Expand Up @@ -464,6 +466,26 @@ Provides a data structure identical to the link that would be called to produce

We added this to make it easier for clients to know which representation they have in memory for the sake of both local caching and requesting updated versions. We recommend including etag or last-modified data along with resources to further improve efficiency.

```javascript
{
"id": 1,
"_origin": { "href": "/item/1", "method": "GET" }
...
}
```

### Resource and Action
We also expose the resource and action name as part of each resource (including embedded resources).

```javascript
{
"id": 1,
"_origin": { "href": "/item/1", "method": "GET" }
"_resource": "item",
"_action": "self"
}
```

### Links

#### Methods
Expand Down
63 changes: 53 additions & 10 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,64 @@
var gulp = require( 'gulp' ),
mocha = require( 'gulp-mocha' );
var istanbul = require( 'gulp-istanbul' );
var gulp = require( 'gulp' );
var mocha = require( 'gulp-mocha' );
var open = require( 'open' ); //jshint ignore : line

function cover( done ) {
gulp.src( [ './src/**/*.js' ] )
.pipe( istanbul() )
.pipe( istanbul.hookRequire() )
.on( 'finish', function() {
done( runSpecs() );
} );
}

function runSpecs() { // jshint ignore : line
return gulp.src( [ './spec/*.spec.js' ], { read: false } )
.pipe( mocha( { reporter: 'spec' } ) );
}

function writeReport( cb, openBrowser, tests ) {
tests
.on( 'error', function() {
cb();
} )
.pipe( istanbul.writeReports() )
.on( 'end', function() {
if( openBrowser ) {
open( './coverage/lcov-report/index.html' );
}
cb();
} );
}

gulp.task( 'continuous-coverage', function( cb ) {
cover( writeReport.bind( undefined, cb, false ) );
} );

gulp.task( 'continuous-test', function() {
return runSpecs();
} );

gulp.task( 'coverage', function( cb ) {
cover( writeReport.bind( undefined, cb, true ) );
} );

gulp.task( 'coverage-watch', function() {
gulp.watch( [ './src/**/*', './spec/*.spec.js' ], [ 'continuous-coverage' ] );
} );

gulp.task( 'test', function() {
return gulp.src( [ './spec/**.spec.js' ], { read: false } )
.pipe( mocha( { reporter: 'spec' } ) )
return runSpecs()
.on( 'end', process.exit.bind( process, 0 ) )
.on( 'error', process.exit.bind( process, 1 ) );
} );

gulp.task( 'continuous-test', function() {
return gulp.src( [ './spec/**.spec.js' ], { read: false } )
.pipe( mocha( { reporter: 'spec' } ) );
gulp.task( 'test-watch', function() {
gulp.watch( [ './src/**/*', './spec/*.spec.js' ], [ 'continuous-test' ] );
} );

gulp.task( 'watch', function() {
gulp.watch( [ './src/**', './spec/**' ], [ 'continuous-test' ] );
gulp.task( 'default', [ 'continuous-coverage', 'coverage-watch' ], function() {
} );

gulp.task( 'default', [ 'continuous-test', 'watch' ], function() {
gulp.task( 'specs', [ 'continuous-test', 'test-watch' ], function() {
} );
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hyped",
"version": "0.1.0-4",
"version": "0.1.0-5",
"description": "Hypermedia response generation engine",
"main": "src/index.js",
"dependencies": {
Expand All @@ -14,7 +14,9 @@
"devDependencies": {
"autohost": "~0.3.0-3",
"gulp": "^3.8.6",
"gulp-istanbul": "^0.5.0",
"gulp-mocha": "^0.5.2",
"open": "0.0.5",
"request": "^2.47.0",
"should": "^4.0.4",
"when": "^3.5.0"
Expand Down
4 changes: 3 additions & 1 deletion spec/auth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ describe( "when filtering links by permission", function() {
_origin: { href: "/board/100", method: "GET" },
_links: {
self: { href: "/board/100", method: "GET" }
}
},
_resource: "board",
_action: "self"
};

var authCheck = function( actionName ) {
Expand Down
Loading

0 comments on commit 53deef2

Please sign in to comment.