项目作者: henvo

项目描述 :
JSON API data provider for react-admin.
高级语言: JavaScript
项目地址: git://github.com/henvo/ra-jsonapi-client.git
创建时间: 2018-09-10T21:51:41Z
项目社区:https://github.com/henvo/ra-jsonapi-client

开源协议:MIT License

下载


ra-jsonapi-client

CircleCI
npm version

A JSONAPI compatible data provider for
react-admin.

Features

Currently these actions are supported:

  • GET_LIST
  • GET_ONE
  • CREATE
  • UPDATE
  • DELETE
  • GET_MANY
  • GET_MANY_REFERENCE

Installation

  1. # via npm
  2. npm install ra-jsonapi-client
  3. # via yarn
  4. yarn add ra-jsonapi-client

Usage

Import this package, set the base url and pass it as the dataProvider to
react-admin.

  1. //in app.js
  2. import React from "react";
  3. import { Admin, Resource } from "react-admin";
  4. import jsonapiClient from "ra-jsonapi-client";
  5. const dataProvider = jsonapiClient('http://localhost:3000');
  6. const App = () => (
  7. <Admin dashboard={Dashboard} dataProvider={dataProvider}>
  8. ...
  9. </Admin>
  10. );
  11. export default App;

Options

This client allows you to set some optional settings as the second parameter:

  1. // Configure some settings.
  2. const settings = { ... };
  3. // Pass it as the second parameter after the base URL.
  4. const dataProvider = jsonapiClient('http://localhost:3000', settings);

Total count

Since JSONAPI does not specify
a standard for the total count key in the meta object, you can set it with:

  1. const settings = { total: 'total-count' };

Which will work for:

  1. {
  2. "data": { ... },
  3. "meta": {
  4. "total-count": 436
  5. }
  6. }

If this option is not set it will fall back to total.

In addition, if your server doesn’t provide a count field, you can set total
count
to null, and the provider will assume the total count is the same as
the length of the data array:

  1. const dataProvider = jsonapiClient('http://localhost:3000', { total: null });

Custom HTTP headers

Custom headers can be set by providing a headers object in options:

  1. const settings = {
  2. headers: {
  3. Authorization: 'Bearer ...',
  4. 'X-Requested-With': 'XMLHttpRequest'
  5. }
  6. }

The default value is:

  1. {
  2. Accept: 'application/vnd.api+json',
  3. 'Content-Type': 'application/vnd.api+json',
  4. }

Authentication

This client assumes that you are using an
authProvider for your react-admin
application. In order to use authentication with your backend your authProvider
needs to store credentials in localStorage.

Basic auth

For basic auth your authProvider needs to store username and password like this:

  1. localStorage.setItem('username', 'bob');
  2. localStorage.setItem('password', 'secret');

Bearer Token

For authentication via (access) token your authProvider needs to store the token
like this:

  1. localStorage.setItem('token', '123token');

Update method (PUT vs. PATCH)

First versions used PUT as the default update HTTP method.
In version 0.5.0 this was changed to PATCH since it complies with the
JSONAPI standard.. You can still use PUT by declaring the update method in
the settings:

  1. {
  2. // Set the update method from PATCH to PUT.
  3. updateMethod: 'PUT'
  4. }

Array format for GET_MANY filter

This package makes usage of the aweseome qs querystring parsing library.

Default: brackets
Options: indices, repeat, comma

Key for GET_MANY filter

In most cases filter[id] is enough for get many operation.
But it is not a specification of JSONAPI.
You can change this key id to any string.

  1. {
  2. // When your api requires filter[id_in] for get many operation.
  3. getManyKey: 'id_in'
  4. }

Default: id

Contributors