Spring Data Rest JavaScript Library
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.
Installation
$ npm install springRest-data-rest.js --save
Usage
Send Request
Build Request
add query param in url
let request = springRest;assert;
send json as request body
let param = name: '吴浩麟' age: 23;let request = springRestbodyparam;assert;assert;
Get Response
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
follow links
springRest
get response data
springRestdata
Entity Operation
Instance methods
extend build a class by entity path name
let CloudFile = springRest;let User = springRest;
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 entity
let cloudFile = ;cloudFileid = 26;
data entity's data properties store in data
let cloudFile = ;cloudFileid = 26;cloudFile
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
//save-createlet cloudFile = ;cloudFiledata = key: '232$#ds';cloudFile;
//save-updatelet cloudFile = ;cloudFileid = 26;cloudFiledata = key: '232$#ds';cloudFile;
delete
let cloudFile = ;cloudFileid = 52;cloudFile
Class static methods
findOne
CloudFile
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' }
CloudFile
delete
CloudFile
Error Handle
all error will be reject by return promise,and the error object is instance of Request
will Request.error
properties store error reason
CloudFile
Config
config = /** * options used to every fetch request */ globalOptions: {} /** * API base url */ basePath: 'http://api.hostname' /** * springRestRest data rest base url * @type */ restBasePath: 'http://api.hostname/rest';
Global Option
fetch API request options see detail
globalOptions = method: 'POST' headers: 'Accept': 'application/json' 'Content-Type': 'application/json' body: JSON credentials: 'same-origin'
Browser Support
this lib build on the top of es6 fetch API,use isomorphic-fetch as polyfill. Browser Support