Skip to content

Commit

Permalink
Adding syntactic sugar to facilitate resource serialization using the
Browse files Browse the repository at this point in the history
facade.

It is now possible to serialize a User resource by doing:
`cnp('user'[, serialization-name], entity)`
  • Loading branch information
mikaa123 committed Sep 27, 2014
1 parent 0d91675 commit ca404e7
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 14 deletions.
21 changes: 17 additions & 4 deletions lib/facade.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,27 @@
* Usage:
* cnp.resource('foo', path) - Create a resource called foo.
* cnp('name') - Retrieves the resource called foo
* cnp('name', entity) - Serializes `entity` using the default serialization
* cnp('name', 'partial', entity) - Serializes `entity` using a named serialization
*/

var _resource = require('./resource'),
api = require('./group')();
group = require('./group')();

module.exports = function (server) {
var cnp = function (resource) {
return api(resource);
var cnp = function (resource, serialization, entity) {
var res = group(resource);

if (!serialization) {
return res;
}

if (!entity) {
entity = serialization;
serialization = 'default';
}

return res(serialization, entity);
};

cnp.resource = function resource(name, path) {
Expand All @@ -21,7 +34,7 @@ module.exports = function (server) {
}

var res = _resource(path, server);
api(name, res);
group(name, res);
return res;
};

Expand Down
22 changes: 15 additions & 7 deletions lib/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,21 @@ module.exports = function resource(_path, _router) {
throw new Error('A Resource requires a path.');
}

var newResource = function (representationName, entity) {
if (typeof entity === 'undefined') {
entity = representationName;
representationName = undefined;
}
var resource = _representations[representationName || 'default'];
return resource && resource(entity) || entity;
var newResource = function serializeResource(representationName, entity) {
var resource;

if (typeof entity === 'undefined') {
entity = representationName;
representationName = 'default';
}

resource = _representations[representationName];

if (!resource) {
throw new Error('Representation ' + representationName + ' not found for Resource ' + _path );
}

return resource(entity) || entity;
};

['get', 'post', 'put', 'patch', 'delete'].forEach(function (action) {
Expand Down
35 changes: 32 additions & 3 deletions test/facade_test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require('should');
var facade = require('../lib/facade');

var assert = require('assert'),
facade = require('../lib/facade');

describe('Catnap Facade', function () {
var cnp,
Expand All @@ -9,13 +11,40 @@ describe('Catnap Facade', function () {
cnp = facade(router);
});

it('Should allow creating resources', function () {
it('should allow creating resources', function () {
cnp.resource('foo', '/foo').representation.should.be.ok;
});

it('Should retrieve defined resources', function () {
it('should retrieve defined resources', function () {
cnp.resource('foo', '/foo');
cnp('foo').representation.should.be.ok;
});

it('should throw when the resource isnt registered', function () {
assert.throws(function () {
cnp('bar');
});
});

it('should facilitate resource serialization', function () {
var myEntity = {
foo: 'bar'
};

cnp.resource('foo', '/foo')
.representation(function (e) { return e; })
.representation('simple', function (e) { return 'simple'; });

cnp('foo', myEntity).should.equal(myEntity);
cnp('foo', 'simple', myEntity).should.equal('simple');
});

it('should throw if the serialization doesnt exist', function () {
cnp.resource('foo', '/foo');

assert.throws(function () {
cnp('foo', 'partial', {});
});
});

});
6 changes: 6 additions & 0 deletions test/resource_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ describe('Resource', function () {
testResource.representation('err', {});
});
});

it('should throw if the representation doesnt exist', function () {
assert.throws(function () {
testResource('unknown-representation', {});
});
});
});

describe('Attaching to routers', function () {
Expand Down

0 comments on commit ca404e7

Please sign in to comment.