Skip to content

wearevolt/mobx-model-json-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mobx-model JSON API npm version

This library is wrapper on mobx-model which implements JSON API specification.

Install

npm install mobx-model-json-api

API.request

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 instead endpoint, id and params options (string).
  • onSuccess(body) - callback for success server response. Argument body contain response body object (function).
  • onError(err) - callback for error server response. Argument err contain response body object with error data (function).

Method API.request return Bluebird promise with server response.

Use option params in API.request

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
        }
      }
    })

Example action to fetch JSON API data to model

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);
        }
      });
    });

Common RESTful methods

Module already have common RESTful actions:

  • BaseModel.fetch({ id, params })
  • BaseModel.create(attributes)
  • model.update(attributes)
  • model.destroy()

Issues

Have a bug? Please create an issue here on GitHub!

https://github.com/wearevolt/mobx-model-json-api/issues

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •