1.1.1 • Published 1 month ago

ra-directus v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

Directus Data Provider & Auth Provider For React-Admin

Directus Data Provider & Auth Provider for react-admin, the frontend framework for building admin applications on top of REST/GraphQL services.

This package provides:

  • A directusDataProvider
  • A directusAuthProvider

Installation

yarn add ra-directus
# or
npm install --save ra-directus

Usage

To get a dataProvider and an authProvider configured with automatic auth token refresh, call the getDirectusProviders function.

import { Admin, Resource } from 'react-admin';
import { getDirectusProviders } from 'ra-directus';
import products from './products';

const { authProvider, dataProvider } = getDirectusProviders(
    import.meta.env.VITE_DIRECTUS_URL, {
        storage: window.sessionStorage, // Optional, defaults to localStorage
        getIdentityFullName: user => user.email // Optional, defaults to `${user.last_name} ${user.first_name}`
    }
);

const App = () => (
    <Admin dataProvider={dataProvider} authProvider={authProvider}>
        <Resource name="products" {...products} />
    </Admin>
);

Data Provider

REST Dialect

This Data Provider fits REST APIs powered by Directus.

MethodAPI calls
getListGET http://my.api.url/items/posts?page=1&limit=10&sort=title&meta=*
getOneGET http://my.api.url/items/posts/123
getManyGET http://my.api.url/items/posts?filter={"id":{"_in":["123","456","789"]}}
getManyReferenceGET http://my.api.url/items/posts?filter={"author_id":{"_eq":119}}
createPOST http://my.api.url/items/posts
updatePATCH http://my.api.url/items/posts/123
updateManyPATCH http://my.api.url/items/posts
deleteDELETE http://my.api.url/items/posts/123

Usage

// in src/App.js
import * as React from "react";
import { Admin, Resource } from 'react-admin';
import { directusDataProvider } from 'ra-directus';

import { PostList } from './posts';

const App = () => (
    <Admin dataProvider={directusDataProvider('http://my-app.directus.app')}>
        <Resource name="posts" list={PostList} />
    </Admin>
);

export default App;

Adding Custom Headers

The provider function accepts an HTTP client function as second argument. By default, they use react-admin's fetchUtils.fetchJson() as HTTP client. It's similar to HTML5 fetch(), except it handles JSON decoding and HTTP error codes automatically.

That means that if you need to add custom headers to your requests, you just need to wrap the fetchJson() call inside your own function:

import { fetchUtils, Admin, Resource } from 'react-admin';
import { directusDataProvider } from 'ra-directus';

const httpClient = (url, options = {}) => {
    if (!options.headers) {
        options.headers = new Headers({ Accept: 'application/json' });
    }
    // add your own headers here
    options.headers.set('X-Custom-Header', 'foobar');
    return fetchUtils.fetchJson(url, options);
};
const dataProvider = directusDataProvider('http://my-app.directus.app', httpClient);

export const MyAdmin = () => (
    <Admin dataProvider={dataProvider} title="Example Admin">
       ...
    </Admin>
);

Now all the requests to the REST API will contain the X-Custom-Header: foobar header.

Tip: The most common usage of custom headers is for authentication. fetchJson has built-on support for the Authorization token header:

const httpClient = (url, options = {}) => {
    options.user = {
        authenticated: true,
        token: 'SRTRDFVESGNJYTUKTYTHRG'
    };
    return fetchUtils.fetchJson(url, options);
};

Now all the requests to the REST API will contain the Authorization: SRTRDFVESGNJYTUKTYTHRG header.

Filter

To search on all string and text fields: use q parameter.

const PostFilter = [
    <TextInput source="q" label="Search" alwaysOn />,
];

const PostList = () => (
    <List filters={<PostFilter />}>
        ...
    </List>
);

You can use Directus filter operators by adding one operator in the source props separated by a / :

const PostFilter = [
    <DateInput source="publish_date/_gte" label="Published after" />,
];

const PostList = () => (
    <List filters={<PostFilter />}>
        ...
    </List>
);

Directus system collections

Directus has some system collections, for example directus_users. You can use them as resource, please note that every resource starting with directus_ will be considered as Directus system collections.

const App = () => (
  <Admin dataProvider={dataProvider}>
    <Resource name="directus_users" list={UsersList} edit={UsersEdit} />
  </Admin>
);

Auth Provider

Supported Authentication Methods

We currently only support authentication using Directus local provider (email/password).

Usage

import { Admin, Resource } from 'react-admin';
import {
    directusDataProvider,
    directusAuthProvider,
    directusHttpClient,
} from 'ra-directus';
import { PostList } from './posts';

const dataProvider = directusDataProvider(
    'http://my-app.directus.app',
    directusHttpClient()
);
const authProvider = directusAuthProvider('http://my-app.directus.app');

const App = () => (
    <Admin dataProvider={dataProvider} authProvider={authProvider}>
        <Resource name="posts" list={PostList} />
    </Admin>
);

export default App;

getIdentity Support

The default getIdentity method return the user full name as {LAST_NAME} {FIRST_NAME}.

You can customize it using the getIdentityFullName option:

const authProvider = directusAuthProvider('http://my-app.directus.app', {
    getIdentityFullName: user => `${user.first_name} ${user.last_name}`
});

Authentication Tokens Storage

By default, authentication storage tokens are stored in the localStorage.

If you want them to be stored in sessionStorage instead, use the storage option:

const authProvider = directusAuthProvider('http://my-app.directus.app', {
    storage: window.sessionStorage
});

License

This data provider is licensed under the MIT License, and sponsored by marmelab.

1.1.1

1 month ago

1.1.0

11 months ago

1.0.1

12 months ago

1.0.0

1 year ago