An extensible and simple library for managing REST resources in JavaScript.
var resource = require('rest-in-peace');
var ArticleModel = resource({
url: '/articles'
});
// GET '/articles'
ArticleModel.query().then(function (articles) {
// Do something with articles, for example:
articles.forEach(function (article) {
article.views += 1;
// PATCH '/articles' { name: 'Article name', views: 15 }
article.$save();
});
});
// GET '/articles/123'
ArticleModel.get(123).then(function (article) {
// Do something with an article
article.name = 'Some new name';
article.$save();
});
var article = new ArticleModel();
article.name = 'Article name';
article.views = 0;
// POST '/articles' { name: 'Article name', views: 0 }
article.$save();
ArticleModel.get(1234).then(function (article) {
article.name = 'Omg, an update!';
// PATCH '/articles/1234' { name: 'Omg, an update!', views: 536 }
article.$save();
});
ArticleModel.get(1234).then(function (article) {
// DELETE '/articles/1234'
article.$delete();
});