Comparing version 0.0.4 to 0.1.0
ChangeLog | ||
========= | ||
0.1.0 (2017-02-13) | ||
------------------ | ||
* #7: Strip `_embedded` and `_links` from `Representation.body`. They are | ||
already available through `Representation.embedded` and | ||
`Representation.links`. | ||
* Added a cache for resources in the Client object, which ensures that if you | ||
request the same resource twice, you'll end up with the same object. | ||
* #6: Automatically parse `_embedded` and treat items in this object as real | ||
resources. | ||
* Fixed a bug in `post()`. | ||
* Allowing custom headers to be set, and allowing default headers to be | ||
overridden. | ||
* Automatically resolve all urls from `links()`. | ||
0.0.4 (2017-02-06) | ||
@@ -5,0 +21,0 @@ ----------------- |
@@ -15,7 +15,11 @@ var url = require('url'); | ||
// Default headers. | ||
requestOptions.headers = { | ||
'User-Agent' : 'Restl/' + package.version, | ||
'Accept' : 'application/hal+json, application/json' | ||
}; | ||
if (!requestOptions.headers) { | ||
requestOptions.headers = {}; | ||
} | ||
if (!requestOptions.headers['User-Agent']) { | ||
requestOptions.headers['User-Agent'] = 'Restl/' + package.version; | ||
} | ||
if (!requestOptions.headers.Accept) { | ||
requestOptions.headers.Accept = 'application/hal+json, application/json'; | ||
} | ||
@@ -31,2 +35,14 @@ // Parsing json by default. | ||
/** | ||
* Here we store all the resources that were ever requested. This will | ||
* ensure that if the same resource is requested twice, the same object is | ||
* returned. | ||
*/ | ||
resourceCache : {}, | ||
/** | ||
* Returns a resource by its uri. | ||
* | ||
* This function doesn't do any HTTP requests. | ||
*/ | ||
getResource: function(uri) { | ||
@@ -38,4 +54,9 @@ | ||
uri = url.resolve(this.bookMark, uri); | ||
return new Resource(this, uri); | ||
if (!this.resourceCache[uri]) { | ||
this.resourceCache[uri] = new Resource(this, uri); | ||
} | ||
return this.resourceCache[uri]; | ||
} | ||
@@ -42,0 +63,0 @@ |
var Link = require('./link'); | ||
var url = require('url'); | ||
@@ -9,7 +10,9 @@ /** | ||
*/ | ||
var Representation = function(contentType, body) { | ||
var Representation = function(uri, contentType, body) { | ||
this.uri = uri; | ||
this.contentType = contentType; | ||
this.body = body; | ||
this.links = []; | ||
this.embedded = {}; | ||
@@ -22,2 +25,4 @@ if (typeof this.body._links !== 'undefined') { | ||
} | ||
delete this.body._links; | ||
delete this.body._embedded; | ||
@@ -40,3 +45,3 @@ }; | ||
relType, | ||
links[ii].href, | ||
url.resolve(representation.uri, links[ii].href), | ||
links[ii].type | ||
@@ -62,8 +67,17 @@ ) | ||
for(var ii in embedded) { | ||
var uri = url.resolve( | ||
representation.uri, | ||
embedded[ii]._links.self.href | ||
); | ||
representation.links.push( | ||
new Link( | ||
relType, | ||
embedded[ii]._links.self.href | ||
uri | ||
) | ||
); | ||
representation.embedded[uri] = embedded[ii]; | ||
} | ||
@@ -70,0 +84,0 @@ } |
@@ -81,8 +81,10 @@ 'use strict'; | ||
if (response.headers.location) { | ||
return new Resource( | ||
this.client, | ||
response.headers.uri | ||
return this.client.getResource( | ||
url.resolve( | ||
this.uri, | ||
response.headers.location | ||
) | ||
); | ||
} | ||
}); | ||
}.bind(this)); | ||
}, | ||
@@ -101,5 +103,16 @@ | ||
this.repr = new Representation( | ||
this.uri, | ||
response.headers['content-type'], | ||
response.body | ||
); | ||
// Parsing and storing embedded uris | ||
for (var uri in this.repr.embedded) { | ||
var subResource = this.client.getResource(uri); | ||
subResource.repr = new Representation( | ||
uri, | ||
response.headers['content-type'], | ||
this.repr.embedded[uri] | ||
); | ||
} | ||
// Removing embedded because it just takes up extra memory. | ||
}.bind(this)); | ||
@@ -128,3 +141,3 @@ | ||
* | ||
* Returns a new Resource object | ||
* Returns a Resource object | ||
*/ | ||
@@ -141,6 +154,7 @@ follow: function(rel) { | ||
} | ||
res(new Resource( | ||
this.client, | ||
url.resolve(this.uri, links[0].href) | ||
)); | ||
var resource = this.client.getResource( | ||
links[0].href | ||
); | ||
res(resource); | ||
}.bind(this)) | ||
@@ -166,8 +180,7 @@ .catch(function(reason) { | ||
return links.map(function(link) { | ||
return new Resource( | ||
this.client, | ||
url.resolve(this.uri, link.href) | ||
return this.client.getResource( | ||
link.href | ||
); | ||
}); | ||
}); | ||
}.bind(this)); | ||
}.bind(this)); | ||
}, | ||
@@ -198,2 +211,14 @@ | ||
var uri = this.uri; | ||
if (options.uri) { | ||
// uri was provided in options. We shall use that uri to generate a new | ||
// one. | ||
uri = url.resolve(uri, options.uri); | ||
} else if (options.url) { | ||
// Same thing, but 'url' was used instead of uri | ||
uri = url.resolve(uri, options.url); | ||
// Cleanup | ||
delete options.url; | ||
} | ||
options.uri = uri; | ||
return this.client.request(options); | ||
@@ -200,0 +225,0 @@ |
{ | ||
"name": "restl", | ||
"version": "0.0.4", | ||
"version": "0.1.0", | ||
"description": "Opiniated HAL client.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -12,3 +12,3 @@ Restl - A hypermedia client for nodejs | ||
This NPM package is an attempt at creating a 'generic' hypermedia client, it | ||
supports an opiniated set of modern features REST services might have. | ||
supports an opinionated set of modern features REST services might have. | ||
@@ -31,6 +31,3 @@ This means that there's a strong focus on links and link-relationships. | ||
* Global resource cache. | ||
* Expand CURIES automatically. | ||
* Resolve every URI to an absolute URI. | ||
* Figuring caching resources from `_embedded`. | ||
* Support HTTP `Link` header. | ||
@@ -60,4 +57,6 @@ * Support non-JSON resources, including things like images. | ||
* `POST` request | ||
* Global resource cache. | ||
* Resolve every URI to an absolute URI. | ||
* Figuring caching resources from `_embedded`. | ||
Usage | ||
@@ -193,3 +192,3 @@ ----- | ||
Updates the resource with a new respresentation | ||
Updates the resource with a new representation | ||
@@ -196,0 +195,0 @@ ```js |
71
test.js
@@ -1,58 +0,31 @@ | ||
var restl = require('.'); | ||
var Client = require('./lib/client'); | ||
var options = { | ||
auth: { | ||
user: 'developers@getturnstyle.com', | ||
pass: 'password' | ||
} | ||
} | ||
auth: { | ||
user: 'developers@getturnstyle.com', | ||
pass: 'password' | ||
} | ||
}; | ||
var client = restl('http://192.168.0.26:3009/', options); | ||
var client = new Client('http://localhost:3009/', options); | ||
var home = client.getResource(); | ||
describe('creating a new location', () => async { | ||
var bla = home | ||
.follow('currentUser') | ||
.follow('accounts') | ||
.then(function(foo) { | ||
console.log(foo); | ||
}) | ||
const accounts = await home | ||
.follow('currentUser') | ||
.follow('accounts') | ||
.followAll('item'); | ||
const account = accounts[0]; | ||
const locations = await account.follow('locationCollection'); | ||
const newLocation = await locations.post({ | ||
title: 'Mac Donalds' | ||
}); | ||
it('should return 201 Created'); | ||
it('should have created the new location', () => async { | ||
const hal = await newLocation.get(); | ||
expect(hal).to.equal(...); | ||
}); | ||
}); | ||
home.follow('currentUser') | ||
.then(function(currentUser) { | ||
return currentUser.follow('accounts') | ||
}) | ||
/* | ||
.followAll('item') | ||
.then(function(accounts) { | ||
return accounts.get(); | ||
return accounts[0]; | ||
}) | ||
.then(function(result) { | ||
console.log(result); | ||
}) | ||
.catch(function(err) { | ||
console.log(err); | ||
}); | ||
.follow('locations') | ||
.get() | ||
.then(function(locationHal) { | ||
console.log(locationHal); | ||
});*/ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33353
15
377
338
1