npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ra-directus

v1.1.1

Published

Directus Data Provider & Auth Provider for [react-admin](https://github.com/marmelab/react-admin), the frontend framework for building admin applications on top of REST/GraphQL services.

Downloads

18

Readme

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.

| Method | API calls | | ------------------ | -------------------------------------------------------------------------------------------------------- | | getList | GET http://my.api.url/items/posts?page=1&limit=10&sort=title&meta=* | | getOne | GET http://my.api.url/items/posts/123 | | getMany | GET http://my.api.url/items/posts?filter={"id":{"_in":["123","456","789"]}} | | getManyReference | GET http://my.api.url/items/posts?filter={"author_id":{"_eq":119}} | | create | POST http://my.api.url/items/posts | | update | PATCH http://my.api.url/items/posts/123 | | updateMany | PATCH http://my.api.url/items/posts | | delete | DELETE 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.