mobx-model-json-api

0.0.15 • Public • Published

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

Package Sidebar

Install

npm i mobx-model-json-api

Weekly Downloads

15

Version

0.0.15

License

MIT

Unpacked Size

133 kB

Total Files

13

Last publish

Collaborators

  • staltec