0.2.1 • Published 6 years ago

@willsoto/json-api-client v0.2.1

Weekly downloads
-
License
Apache-2.0
Repository
-
Last release
6 years ago

JSON API Client

Installation

yarn add @willsoto/json-api-client
npm install @willsoto/json-api-client

30 second look

import { JSONApiClient, JSONApiModel } from '@willsoto/json-api-client';

const client = new JSONApiClient();

class Author extends JSONApiModel {
  static __type = 'authors';
  static __endpoint = '/authors';

  constructor(...args) {
    super(...args);
  }
}

// this is optional
client.register(Author.__type, () => {
  // callback to instantiate model, etc
});

client
  .query(Author)
  .all()
  .then((response) => {
    // response.data is now a usable document
  })
  .catch((err) => {
    // normal error object
  });

Usage

API Client

JSONApiClient is backed by axios and any options that axios expects can be passed during creation as axiosOptions:

const client = new JSONApiClient({
  axiosConfig: {
    /* See https://github.com/axios/axios#request-config */
  }
});

Once a client is created, you can begin making API calls:

// will make a request to `/authors`
const response = client
  .query({
    __endpoint: '/authors'
  })
  .all();
// will make a request to `/authors/1`
const response = client
  .query({
    __endpoint: '/authors'
  })
  .get('1');

Inclusion of Related Resources

client
  .query({
    __endpoint: '/authors'
  })
  .include('articles')
  .all();
client
  .query({
    __endpoint: '/authors'
  })
  .include(['articles.comments', 'tags'])
  .all();

TODO

0.2.1

6 years ago

0.2.0

6 years ago

0.1.2

6 years ago