GithubHelp home page GithubHelp logo

dylanhillier / ra-jsonapi-client Goto Github PK

View Code? Open in Web Editor NEW

This project forked from henvo/ra-jsonapi-client

0.0 0.0 0.0 796 KB

JSON API data provider for react-admin.

License: MIT License

JavaScript 100.00%

ra-jsonapi-client's Introduction

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.

If your server supports the X-Total-Count header, and this header is sent on the response, then this will be used in preference to any total key/value found in the meta object.

In addition, if your server doesn't provide either the X-Total-Count header or provide a count field in the meta object, then 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

Contributors

ra-jsonapi-client's People

Contributors

770studio avatar dependabot[bot] avatar henvo avatar hootbah avatar multimeric avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.