• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

henvo/ra-jsonapi-client: JSON API data provider for react-admin.

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

henvo/ra-jsonapi-client

开源软件地址:

https://github.com/henvo/ra-jsonapi-client

开源编程语言:

JavaScript 100.0%

开源软件介绍:

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

# via npm
npm install ra-jsonapi-client

# via yarn
yarn add ra-jsonapi-client

Usage

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

//in app.js
import React from "react";
import { Admin, Resource } from "react-admin";
import jsonapiClient from "ra-jsonapi-client";

const dataProvider = jsonapiClient('http://localhost:3000');

const App = () => (
  <Admin dashboard={Dashboard} dataProvider={dataProvider}>
    ...
  </Admin>
);

export default App;

Options

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

// Configure some settings.
const settings = { ... };

// Pass it as the second parameter after the base URL.
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:

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

Which will work for:

{
  "data": { ... },
  "meta": {
    "total-count": 436
  }
}

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:

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

Custom HTTP headers

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

const settings = {
  headers: {
    Authorization: 'Bearer ...',
    'X-Requested-With': 'XMLHttpRequest'
  }
}

The default value is:

{
  Accept: 'application/vnd.api+json; charset=utf-8',
  'Content-Type': 'application/vnd.api+json; charset=utf-8',
}

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:

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

Bearer Token

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

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:

{
  // Set the update method from PATCH to PUT.
  updateMethod: 'PUT'
}

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.

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

Default: id

Contributors




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
learn-co-students/using-to-json-lab-v-000发布时间:2022-07-09
下一篇:
liruqi/jsonviewer: online json viewer发布时间:2022-07-09
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap