#rest4js
Stability: Under Development
Fetch data from your RESTfull web service in the easy way.
var client = new rest.Client({
port: 8080
});
var userRepository = new rest.Repository({
client: client,
path: '/api/users'
});
var cancellationTokenSource = new rest.CancellationTokenSource();
userRepository
.get(cancellationTokenSource.token)
.then(users => {
console.log(users);
})
.catch(reason => {
console.log(reason.message || reason);
});
Below is a sample conversation between an HTTP client and an HTTP server running on localhost, port 8080.
Client request
GET /api/users HTTP/1.1
Host: localhost:8080
Accept: application/json
Cache-Control:no-cache
X-Requested-With: XMLHttpRequest
Server response
HTTP/1.1 200 OK
Content-Type: application/json
Date: Sat, 07 May 2016 06:54:38 GMT
Content-Length: 114
[
{
"id": 1,
"username": "jon"
},
{
"id": 2,
"username": "tom"
}
]
CRUD
repository.get
You can easily fetch data by given parameters.
repository.get(parameters[, cancellationToken])
Parameters
parameters - parameters
cancellationToken - cancellation token
Returns
Promise
The following example shows a regular fetch by id:
usersRepository.get(id, cancellationTokenSource.token)
.then(users => {
console.log(users);
}
.catch(reason => {
console.log(reason.message || reason);
});
Sample output from the example:
[
{
"id": 2,
"username": "tom"
}
]
repository.save(value[, cancellationToken])
repository.update(id, value[, cancellationToken])
repository.patch(id, value[, cancellationToken])
repository.del(id[, cancellationToken])
Queries
There is an option to build queries.
var userRepository = new rest.Repository({
client: client,
path: '/api/users/:id'
});
var cancellationTokenSource = new rest.CancellationTokenSource();
userRepository
.query()
.get()
.setParameter('id', 2)
.skip(0)
.take(10)
.execute(cancellationTokenSource.token)
.then(() => {
})
.catch(reason => {
});
If you are not confortable with current query API or something is missing in it, you can easily extend it. You need to create your own query class and inherit it from *QueryBase or Query. Look bellow to the example how to do it.
class MyQuery inhertis Query {
include(fields) {
return this.setParameter('include', fields);
}
}
Also query factory class has to be replaced.
class MyQueryFactory {
create(options) {
return new MyQuery(options);
}
}
After creating your own query factory, you have to register it, so that new custom queries could be used.
client.services.queryFactory = new MyQueryFactory();
Now you are ready to go.
Cancellation
var cancellationTokenSource = new rest.CancellationTokenSource();
repository
.get(id, cancellationTokenSource.token)
.then(() => {})
.catch(reason => {
});
cancellationTokenSource.cancelAfer(200);
Batching
var batch = new rest.Batch();
.add(() => return repository.get(id))
.add(() => return repository.get(id))
.execute();
Retry
Interceptions
Authentication
Caching
Customization
If you are not confortable with repository pattern or it restricts you to much, you can always use lower level API, which allows to create request manually. See an example bellow.
var client = new rest.RestClient({
port: 8080
});
var cancellationTokenSource = new rest.CancellationTokenSource();
var requestMessage = new rest.RestRequestMessage({
method: 'GET',
path: '/api/users',
headers: {
'Accept': 'application/json'
}
});
client
.send(requestMessage, cancellationTokenSource.token)
.then(responseMessage => {
console.log("Users has been fetched successfuly");
})
.catch(ex => {
if (cancellationTokenSource.canceled) {
console.log(ex.message || ex);
} else {
console.error(ex.message || ex);
}
});
#Sandbox