Comparing version
@@ -18,3 +18,3 @@ /** | ||
function Client(apiRoot) { | ||
function Client(apiRoot, options) { | ||
if (typeof apiRoot === 'undefined') { | ||
@@ -25,2 +25,3 @@ throw new Error('Client must be initialized with an API Root'); | ||
this._apiRoot = apiRoot; | ||
this._options = options || {}; | ||
this._traversals = {}; | ||
@@ -84,4 +85,4 @@ this._nodes = {}; | ||
.then(function(descriptor) { | ||
return new Resource(descriptor); | ||
}); | ||
return new Resource(descriptor, this._options.requestOptions); | ||
}.bind(this)); | ||
@@ -88,0 +89,0 @@ this._descriptorCache[uri].catch(function() { |
define(['./uritemplate', './xhr'], function(UriTemplate, xhr) { | ||
function extend(obj) { | ||
var i, prop, source; | ||
for (i = 1; i < arguments.length; ++i) { | ||
source = arguments[i]; | ||
for (prop in source) { | ||
obj[prop] = source[prop]; | ||
} | ||
} | ||
return obj; | ||
} | ||
/** | ||
@@ -7,4 +18,5 @@ * Resource | ||
*/ | ||
function Resource(descriptor) { | ||
function Resource(descriptor, options) { | ||
this._description = {}; | ||
this._requestOptions = options || {}; | ||
this._parseDescriptor(descriptor); | ||
@@ -39,11 +51,11 @@ } | ||
Resource.prototype.get = function(params) { return this._issueRequest('get', params); }; | ||
Resource.prototype.delete = function(params) { return this._issueRequest('delete', params); }; | ||
Resource.prototype.head = function(params) { return this._issueRequest('head', params); }; | ||
Resource.prototype.get = function(params, options) { return this._issueRequest('get', params, null, options); }; | ||
Resource.prototype.delete = function(params, options) { return this._issueRequest('delete', params, null, options); }; | ||
Resource.prototype.head = function(params, options) { return this._issueRequest('head', params, null, options); }; | ||
Resource.prototype.post = function(data, params) { return this._issueRequest('post', params, data); }; | ||
Resource.prototype.put = function(data, params) { return this._issueRequest('put', params, data); }; | ||
Resource.prototype.patch = function(data, params) { return this._issueRequest('patch', params, data); }; | ||
Resource.prototype.post = function(data, params, options) { return this._issueRequest('post', params, data, options); }; | ||
Resource.prototype.put = function(data, params, options) { return this._issueRequest('put', params, data, options); }; | ||
Resource.prototype.patch = function(data, params, options) { return this._issueRequest('patch', params, data, options); }; | ||
Resource.prototype._issueRequest = function(method, params, data) { | ||
Resource.prototype._issueRequest = function(method, params, data, options) { | ||
params = params || {}; | ||
@@ -54,7 +66,7 @@ data = data || {}; | ||
return xhr({ | ||
return xhr(extend(this._requestOptions, options, { | ||
url: selfConn, | ||
method: method, | ||
data: data | ||
}); | ||
})); | ||
}; | ||
@@ -61,0 +73,0 @@ |
{ | ||
"name": "be-hippo", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "A PropositionH hypermedia consumer", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -60,2 +60,10 @@ define(['chai-as-promised', 'lib/client', 'lib/resource'], function(chaiAsPromised, Client, Resource) { | ||
}); | ||
it('passes request options to created resources', function() { | ||
var options = { requestOptions: { headers: { foo: 'hello' } } }; | ||
var client = new Client('/v1/foo', options); | ||
server.respondWith('OPTIONS', '/v1/foo', response); | ||
return expect(client.walk()).to.become(new Resource(links, options.requestOptions)); | ||
}); | ||
}); | ||
@@ -62,0 +70,0 @@ }); |
@@ -150,2 +150,41 @@ define(['chai-as-promised', 'lib/resource'], function(chaiAsPromised, Resource) { | ||
}); | ||
describe('when given resource level options', function() { | ||
it('passes those options to the ajax request method', function() { | ||
var resource = new Resource(descriptor, { headers: { foo: 'bar' } }); | ||
server.respondWith('GET', '/v1/foo?bar=10', function(req) { | ||
expect(req.requestHeaders.foo).to.equal('bar'); | ||
req.respond(200, { "Content-Type": "text/plain" }, ''); | ||
}); | ||
return resource.get({ bar: 10 }); | ||
}); | ||
}); | ||
describe('when given request level options', function() { | ||
it('passes those options to the ajax request method', function() { | ||
var resource = new Resource(descriptor); | ||
server.respondWith('GET', '/v1/foo?bar=10', function(req) { | ||
expect(req.requestHeaders.foo).to.equal('bar'); | ||
req.respond(200, { "Content-Type": "text/plain" }, ''); | ||
}); | ||
return resource.get({ bar: 10 }, { headers: { foo: 'bar' } }); | ||
}); | ||
}); | ||
describe('when given resource and request level options', function() { | ||
it('passes those options to the ajax request method', function() { | ||
var resource = new Resource(descriptor, { headers: { foo: 'bar' } }); | ||
server.respondWith('GET', '/v1/foo?bar=10', function(req) { | ||
expect(req.requestHeaders.foo).to.equal('baz'); | ||
req.respond(200, { "Content-Type": "text/plain" }, ''); | ||
}); | ||
return resource.get({ bar: 10 }, { headers: { foo: 'baz' } }); | ||
}); | ||
}); | ||
}); | ||
@@ -182,2 +221,41 @@ | ||
}); | ||
describe('when given resource level options', function() { | ||
it('passes those options to the ajax request method', function() { | ||
var resource = new Resource(descriptor, { headers: { foo: 'bar' } }); | ||
server.respondWith('DELETE', '/v1/foo?bar=10', function(req) { | ||
expect(req.requestHeaders.foo).to.equal('bar'); | ||
req.respond(200, { "Content-Type": "text/plain" }, ''); | ||
}); | ||
return resource.delete({ bar: 10 }); | ||
}); | ||
}); | ||
describe('when given request level options', function() { | ||
it('passes those options to the ajax request method', function() { | ||
var resource = new Resource(descriptor); | ||
server.respondWith('DELETE', '/v1/foo?bar=10', function(req) { | ||
expect(req.requestHeaders.foo).to.equal('bar'); | ||
req.respond(200, { "Content-Type": "text/plain" }, ''); | ||
}); | ||
return resource.delete({ bar: 10 }, { headers: { foo: 'bar' } }); | ||
}); | ||
}); | ||
describe('when given resource and request level options', function() { | ||
it('passes those options to the ajax request method', function() { | ||
var resource = new Resource(descriptor, { headers: { foo: 'bar' } }); | ||
server.respondWith('DELETE', '/v1/foo?bar=10', function(req) { | ||
expect(req.requestHeaders.foo).to.equal('baz'); | ||
req.respond(200, { "Content-Type": "text/plain" }, ''); | ||
}); | ||
return resource.delete({ bar: 10 }, { headers: { foo: 'baz' } }); | ||
}); | ||
}); | ||
}); | ||
@@ -214,2 +292,41 @@ | ||
}); | ||
describe('when given resource level options', function() { | ||
it('passes those options to the ajax request method', function() { | ||
var resource = new Resource(descriptor, { headers: { foo: 'bar' } }); | ||
server.respondWith('HEAD', '/v1/foo?bar=10', function(req) { | ||
expect(req.requestHeaders.foo).to.equal('bar'); | ||
req.respond(200, { "Content-Type": "text/plain" }, ''); | ||
}); | ||
return resource.head({ bar: 10 }); | ||
}); | ||
}); | ||
describe('when given request level options', function() { | ||
it('passes those options to the ajax request method', function() { | ||
var resource = new Resource(descriptor); | ||
server.respondWith('HEAD', '/v1/foo?bar=10', function(req) { | ||
expect(req.requestHeaders.foo).to.equal('bar'); | ||
req.respond(200, { "Content-Type": "text/plain" }, ''); | ||
}); | ||
return resource.head({ bar: 10 }, { headers: { foo: 'bar' } }); | ||
}); | ||
}); | ||
describe('when given resource and request level options', function() { | ||
it('passes those options to the ajax request method', function() { | ||
var resource = new Resource(descriptor, { headers: { foo: 'bar' } }); | ||
server.respondWith('HEAD', '/v1/foo?bar=10', function(req) { | ||
expect(req.requestHeaders.foo).to.equal('baz'); | ||
req.respond(200, { "Content-Type": "text/plain" }, ''); | ||
}); | ||
return resource.head({ bar: 10 }, { headers: { foo: 'baz' } }); | ||
}); | ||
}); | ||
}); | ||
@@ -272,2 +389,41 @@ | ||
}); | ||
describe('when given resource level options', function() { | ||
it('passes those options to the ajax request method', function() { | ||
var resource = new Resource(descriptor, { headers: { foo: 'bar' } }); | ||
server.respondWith('POST', '/v1/foo', function(req) { | ||
expect(req.requestHeaders.foo).to.equal('bar'); | ||
req.respond(200, { "Content-Type": "text/plain" }, ''); | ||
}); | ||
return resource.post({ bar: 10 }); | ||
}); | ||
}); | ||
describe('when given request level options', function() { | ||
it('passes those options to the ajax request method', function() { | ||
var resource = new Resource(descriptor); | ||
server.respondWith('POST', '/v1/foo', function(req) { | ||
expect(req.requestHeaders.foo).to.equal('bar'); | ||
req.respond(200, { "Content-Type": "text/plain" }, ''); | ||
}); | ||
return resource.post({ bar: 10 }, {}, { headers: { foo: 'bar' } }); | ||
}); | ||
}); | ||
describe('when given resource and request level options', function() { | ||
it('passes those options to the ajax request method', function() { | ||
var resource = new Resource(descriptor, { headers: { foo: 'bar' } }); | ||
server.respondWith('POST', '/v1/foo', function(req) { | ||
expect(req.requestHeaders.foo).to.equal('baz'); | ||
req.respond(200, { "Content-Type": "text/plain" }, ''); | ||
}); | ||
return resource.post({ bar: 10 }, {}, { headers: { foo: 'baz' } }); | ||
}); | ||
}); | ||
}); | ||
@@ -330,2 +486,41 @@ | ||
}); | ||
describe('when given resource level options', function() { | ||
it('passes those options to the ajax request method', function() { | ||
var resource = new Resource(descriptor, { headers: { foo: 'bar' } }); | ||
server.respondWith('PUT', '/v1/foo', function(req) { | ||
expect(req.requestHeaders.foo).to.equal('bar'); | ||
req.respond(200, { "Content-Type": "text/plain" }, ''); | ||
}); | ||
return resource.put({ bar: 10 }); | ||
}); | ||
}); | ||
describe('when given request level options', function() { | ||
it('passes those options to the ajax request method', function() { | ||
var resource = new Resource(descriptor); | ||
server.respondWith('PUT', '/v1/foo', function(req) { | ||
expect(req.requestHeaders.foo).to.equal('bar'); | ||
req.respond(200, { "Content-Type": "text/plain" }, ''); | ||
}); | ||
return resource.put({ bar: 10 }, {}, { headers: { foo: 'bar' } }); | ||
}); | ||
}); | ||
describe('when given resource and request level options', function() { | ||
it('passes those options to the ajax request method', function() { | ||
var resource = new Resource(descriptor, { headers: { foo: 'bar' } }); | ||
server.respondWith('PUT', '/v1/foo', function(req) { | ||
expect(req.requestHeaders.foo).to.equal('baz'); | ||
req.respond(200, { "Content-Type": "text/plain" }, ''); | ||
}); | ||
return resource.put({ bar: 10 }, {}, { headers: { foo: 'baz' } }); | ||
}); | ||
}); | ||
}); | ||
@@ -388,2 +583,41 @@ | ||
}); | ||
describe('when given resource level options', function() { | ||
it('passes those options to the ajax request method', function() { | ||
var resource = new Resource(descriptor, { headers: { foo: 'bar' } }); | ||
server.respondWith('PATCH', '/v1/foo', function(req) { | ||
expect(req.requestHeaders.foo).to.equal('bar'); | ||
req.respond(200, { "Content-Type": "text/plain" }, ''); | ||
}); | ||
return resource.patch({ bar: 10 }); | ||
}); | ||
}); | ||
describe('when given request level options', function() { | ||
it('passes those options to the ajax request method', function() { | ||
var resource = new Resource(descriptor); | ||
server.respondWith('PATCH', '/v1/foo', function(req) { | ||
expect(req.requestHeaders.foo).to.equal('bar'); | ||
req.respond(200, { "Content-Type": "text/plain" }, ''); | ||
}); | ||
return resource.patch({ bar: 10 }, {}, { headers: { foo: 'bar' } }); | ||
}); | ||
}); | ||
describe('when given resource and request level options', function() { | ||
it('passes those options to the ajax request method', function() { | ||
var resource = new Resource(descriptor, { headers: { foo: 'bar' } }); | ||
server.respondWith('PATCH', '/v1/foo', function(req) { | ||
expect(req.requestHeaders.foo).to.equal('baz'); | ||
req.respond(200, { "Content-Type": "text/plain" }, ''); | ||
}); | ||
return resource.patch({ bar: 10 }, {}, { headers: { foo: 'baz' } }); | ||
}); | ||
}); | ||
}); | ||
@@ -390,0 +624,0 @@ }); |
74970
16.35%1904
11.61%