This library is wrapper on mobx-model which implements JSON API specification.
npm install mobx-model-json-api
Method API.request(options)
accept following options:
endpoint
- backend endpoint URL (string),id
- model ID (number)params
- additional parameters for fetching data: pagination, filtration, includes and field names (object)link
- direct url with query params. Use insteadendpoint
,id
andparams
options (string).onSuccess(body)
- callback for success server response. Argumentbody
contain response body object (function).onError(err)
- callback for error server response. Argumenterr
contain response body object with error data (function).
Method API.request
return Bluebird promise with server response.
For fetching inclusion of related resources.
AnMobxModel.fetch({
id,
params:{
include: ['user', 'reported_by'],
}
})
For fetching inclusion of sparse fieldsets.
AnMobxModel.fetch({
id,
params:{
include: 'author',
fields: {
articles: ['title', 'body'],
people: 'name'
}
}
})
For fetching sorted data.
AnMobxModel.fetch({
id,
params:{
sort: 'age',
}
})
For fetching paginated data.
AnMobxModel.fetch({
id,
params:{
page: {
number: 1
size: 20
}
}
})
For setting data from backend using JSON API use method .setByBody()
;
BaseModel.addClassAction('fetch', function ({ link, id, params } = {}) {
return API.request({
link,
endpoint: this.urlRoot,
id,
params,
onSuccess: (body) => {
this.setByBody(body);
},
onError: function (err) {
console.log('onError', err);
}
});
});
Module already have common RESTful actions:
- BaseModel.fetch({ id, params })
- BaseModel.create(attributes)
- model.update(attributes)
- model.destroy()
Have a bug? Please create an issue here on GitHub!
https://github.com/wearevolt/mobx-model-json-api/issues
MIT