reststate / reststate-client

Zero-configuration JSON:API web service client
https://client.reststate.codingitwrong.com
Other
9 stars 11 forks source link

@reststate/client

This package is no longer maintained. The original maintainer has forked it to @codingitwrong/jsonapi-client and continues development there. It is forked to avoid breaking compatibility with packages that depend on @reststate/client, including other @reststate packages and forks of them.


A lightweight client for making requests to a JSON:API service.

@reststate/client provides a simple Promise-based API suitable for just about any JavaScript application. It doesn't handle persistence, though; for that, wrappers are available for a variety of popular state stores:

Synopsis

const resource = new ResourceClient({
  name: 'widgets',
  httpClient: axios.create(...),
});

resource.all()
  .then(widgets => widgets);

resource.create({
  attributes: {
    title: 'My Widget',
  },
});

Installation

$ npm install --save @reststate/client

@reststate/client needs to be configured with an httpClient object that handles the requests and responses. The easiest way to do this is to provide an axios instance configured with your server's base URL, the standard JSON:API content type, and optionally any authentication info your server requires.

import axios from 'axios';
import { ResourceClient } from '@reststate/client';

const token = "FILL_ME";

const httpClient = axios.create({
  baseURL: 'https://jsonapi-sandbox.herokuapp.com',
  headers: {
    'Content-Type': 'application/vnd.api+json',
    'Authentication': `Bearer ${token}`,
  },
});
const client = new ResourceClient({ name: 'widgets', httpClient });

client.all().then(results => console.log(results));

Usage

For more information on usage, see the @reststate/client docs.

License

Apache-2.0