Gathering detailed insights and metrics for spring-data-rest.js
Gathering detailed insights and metrics for spring-data-rest.js
Gathering detailed insights and metrics for spring-data-rest.js
Gathering detailed insights and metrics for spring-data-rest.js
npm install spring-data-rest.js
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
Spring Data Rest is makes it easy to build hypermedia-driven REST web services. This lib provider useful util to play with the service in js.
1$ npm install springRest-data-rest.js --save
then use it in commonjs env
1let springRest = require('spring-data-rest.js');
add query param in url
1let request = springRest.request.get(springRest.request.config.restBasePath).query({page: 0, size: 2}); 2assert.equal(request.options.url, springRest.request.config.restBasePath + '?page=0&size=2');
send json as request body
1let param = {name: '吴浩麟', age: 23}; 2let request = springRest.request.post('/').body(param); 3assert.deepEqual(request.options.body, JSON.stringify(param)); 4assert.equal(request.options.headers['Content-Type'], 'application/json');
1springRest.request.config = { 2 /** 3 * options used to every fetch request 4 */ 5 globalOptions: {}, 6 /** 7 * API base url 8 */ 9 basePath: 'http://api.hostname', 10 /** 11 * springRestRest data rest base url 12 * @type {string} 13 */ 14 restBasePath: 'http://api.hostname/rest' 15};
fetch API request options see detail
1springRest.request.config.globalOptions = { 2 method: 'POST', 3 headers: { 4 'Accept': 'application/json', 5 'Content-Type': 'application/json' 6 }, 7 body: JSON.stringify({ 8 name: 'Hubot', 9 login: 'hubot', 10 }), 11 credentials: 'same-origin' 12}
request return response in Promise
,if request success Promise
will resolve json data,if will reject a Request
object will Request.error
store error reason
get response data
1let classroom = new Classroom({name: 'D1143'}); 2 classroom.save().then(function () { 3 return springRest.request.get(`${Classroom.entityBaseURL}/${classroom.id}`).data(); 4}).then(function (data) { 5 assert.equal(data.name, 'D1143'); 6 done(); 7}).catch(function (err) { 8 done(err); 9});
follow links
1let student = new Student({name: '吴浩麟', age: 23}); 2 let academy = new Academy({name: '计算机学院'}); 3 student.set('academy', academy); 4 student.save().then(()=> { 5 return springRest.request.get(`${Student.entityBaseURL}/${student.id}`).follow(['self', 'academy', 'self', 'self']); 6}).then((data)=> { 7 assert.equal(data.name, '计算机学院'); 8 done(); 9}).catch(err=> { 10 done(err); 11});
extend get a class by entity path name
1let Student = springRest.extend('students'); 2let Academy = springRest.extend('academies'); 3let Classroom = springRest.extend('classrooms');
create entity class ref to spring data entity,use entity class to make a new entity instance and then create it on service.
1let student = new Student(); 2 student.set('name', 'Tom'); 3 student.save().then(()=> { 4 assert(student.id != null); 5 return springRest.request.get(`${Student.entityBaseURL}/${student.id}`).data(); 6}).then((data)=> { 7 assert.equal(data.name, 'Tom'); 8 done(); 9}).catch(err=> { 10 done(err); 11})
id the entity instance's id. for a existed entity set instance's id then you can use instance
fetch
method to fetch entity's datasave
method to update entity's updated propertiesdelete
method to delete this entity1let student = new Student(); 2student.id = 26;
update entity
if a entity instance has id attr,and use entity's set(key,value)
method update attr,then can call entity's save()
method to patch update change to service.
1let academy = new Academy({name: 'Math'}); 2 academy.save().then(()=> { 3 academy.set('name', 'Physics'); 4 return academy.save(); 5}).then(()=> { 6 assert.deepEqual(academy.get('name'), 'Physics'); 7 done(); 8}).catch(err=> { 9 done(err); 10})
save or update create or update entity if id properties is set,then will send HTTP PATCH request to update an entity(will watch change in data properties to track change fields) if id is null,then will send HTTP POST request to create an entity
delete entity
use entity's delete()
method to remove this entity in service.
1let student = new Student(); 2 student.save().then(()=> { 3 return student.delete(); 4}).then(()=> { 5 return Student.findOne(student.id); 6}).catch(err=> { 7 assert.equal(err.response.status, 404); 8 done(); 9});
Entity Class also has a static method to delete an entity by id
1Student.delete(42).then(()=>{},err=>{})
fetch data entity's data properties store in data
1let name = 'Ace'; 2 let age = 20; 3 let ace = new Student({name: name, age: age}); 4 let student = new Student(); 5 ace.save().then(()=> { 6 student.id = ace.id; 7 return student.fetch(); 8}).then(data=> { 9 assert.equal(data.name, name); 10 assert.equal(data.age, age); 11 assert.equal(student.get('name'), name); 12 assert.equal(student.get('age'), age); 13 done(); 14}).catch(err=> { 15 done(err); 16});
findOne get an entity instance by id
1let classRoom = new Classroom({name: '东16412'}); 2classRoom.save().then(()=> { 3 return Classroom.findOne(classRoom.id); 4}).then(entity=> { 5 assert.equal(entity.get('name'), '东16412'); 6 done(); 7}).catch(err=> { 8 done(err); 9});
get an not exist instance will reject 404 err
1Student.findOne('404404').then(()=> { 2 done('should be 404 error'); 3}).catch(req=> { 4 assert.equal(req.response.status, 404); 5 done(); 6})
findAll find entity list with page and sort method param opts = { page:'the page number to access (0 indexed, defaults to 0)', size:'the page size requested (defaults to 20)', sort:'a collection of sort directives in the format ($propertyName,)+[asc|desc]? etc:name,age,desc' }
1let size = 13; 2let pageIndex = 1; 3Student.findAll({page: pageIndex, size: size, sort: 'age,desc'}).then(function (arr) { 4 assert(Array.isArray(arr)); 5 assert.equal(arr.length, size); 6 assert.equal(arr.page.number, pageIndex); 7 assert.equal(arr.page.size, size); 8 assert.equal(springRest.extend.isEntity(arr[0]), true); 9 assert.equal(arr[0].constructor, Student); 10 for (let i = 1; i < size - 2; i++) { 11 assert.equal(arr[i].get('age') > arr[i + 1].get('age'), true); 12 assert.equal(arr[i - 1].get('age') > arr[i].get('age'), true); 13 } 14 done(); 15}).catch(req=> { 16 done(req); 17});
all error will be reject by return promise,and the error object is instance of Request
will Request.error
properties store error reason
1Student.findOne(42).then(()=>{}).catch(req=>{ 2 console.error(req.error); 3 console.log(req.response.status); 4 console.log(req.response.statusText); 5})
require es6 Object.assign
and Promise
,this lib build on the top of es6 fetch API,use isomorphic-fetch as polyfill.
Browser Support
Copyright (c) 2016 HalWu
No vulnerabilities found.
No security vulnerabilities found.