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

gatsby-source-directus-wahyu

v0.0.45

Published

Source plugin for Gatsby to fetch data from Directus Headless CMS

Downloads

1

Readme

gatsby-source-directus

Build Status

Source plugin for pulling data into Gatsby from Directus CMS.

Features

This plugin pulls all your custom tables and creates Gatsby nodes for it.

For example, if you have a Posts table, you'll get access to AllDirectusPost and directusPost queries which return values for the predefined fields for that column.

It works well with Gatsby's createPages function if you want to dynamically create Blog posts from Directus, for instance.

Installation Guide

  • Install Gatsby
  • Install plugin by running npm npm i gatsby-source-directus -S
  • Configure the plugin in gatsby-config.js file:
module.exports = {
  siteMetadata: {
    title: `A sample site using Directus API`,
    subtitle: `My sample site using Directus`,
  },
  plugins: [
    {
      resolve: `gatsby-source-directus`,
      options: {
        /*
        * The base URL of Directus without the trailing slash. 
        * Example: 'example.com' or 'www.example.com'
        */
        url: `directus.example.com`,
        /*
         * Directus protocol. Can be either http or https
         */
        protocol: 'http',
        /*
         * Directus API key. You can find it on the bottom of your account settings page.
         */
        apiKey: '123456789',   
        /*
         * This plugin will automatically transform plural table names into their singular counterparts.
         * However, if the name generated isn't correct, you can overwrite it by setting the `nameExceptions` object.
         * So, on the example below, a table "Posts", will be transformed to "Article" node type.
         * This config is optional
         */
        nameExceptions: {
          posts: "Article",
        },
        /*
         * This plugin will call the Directus API with the default request parameters.
         * In case you want to override these, you can pass in a `requestParams` object.
         */
        requestParams: {
          depth: 2,
        },
      },
    },
  ],
}

Usage

For every table in Directus, the plugin will create a separate node with Directus prefix.

So, for your posts and categories tables, the queries would be directusPost, allDirectusPost and directusCategory, allDirectusCategory.

This plugin is using Pluralize module to transform plural table names into singular node types to conform to the Gatsby naming convention. If for some reason, the generated name doesn't seem right, you can overwrite the node name using the nameExceptions object in the plugin config. (see example above)

Example plugin with Gatsby's createPages

This example assumes that you have created a posts table in Directus with title, author and content fields. It will use src/templates/post.jsx file as your template file.

const path = require('path')

exports.createPages = ({ boundActionCreators, graphql }, { urlPrefix }) => {
  const { createPage } = boundActionCreators

  return new Promise((resolve, reject) => {
    resolve(
      graphql(
        `
          {
            allDirectusPost {
              edges {
                node {
                  id
                  title
                  author
                  content
                }
              }
            }
          }
        `
      ).then(result => {
        if (result.errors) {
          console.error('GraphQL query returned errors')
          reject(result.errors)
        }

        result.data.allDirectusPost.edges.forEach(edge => {
          try {
            let node = edge.node
            let path = `posts/${node.id}`
            createPage({
              path,
              layout: 'index',
              component: path.resolve('src/templates/post.jsx'),
              context: {
                post: node,
              },
            })
            console.log(`Generated page '${path}'`)
          }
          catch (error) {
            console.error(`Failed to generate page posts/'${path}': ${error}`)
          }
        })
      })
    )
  })
}

To do

For now, the plugin cares only about your tables and table items.

I'm planning to extend the plugin to include:

  • Settings
  • Activity
  • Files

Contributions

Feel free to contribute if you feel something's missing.