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

@ghranek/gatsby-source-custom-api

v1.0.2

Published

Source data from any API and transform it into Gatsby-nodes. Download your image files and use them with Gatsby Image.

Downloads

6

Readme

Logo of gatsby-source-custom-api

gatsby-source-custom-api helps you sourcing data from any API and transform it into Gatsby nodes. Define keys you want to be transformed into image-nodes and use them with Gatsby Image.

Getting Started

  1. Install the package with yarn or npm

yarn add gatsby-source-custom-api

  1. Add to plugins in your gatsby-config.js
module.exports = {
    plugins: [
        {
            resolve: "gatsby-source-custom-api",
            options: {
                url: "www.my-custom-api.com"
            }
        }
    ]
};

Options

| Name | Type | Description | | :-------- | :--------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | url | object or string | Required. Url of your API as a string. If you have two different APIs for development and production, define an object with the keys production and development. | | rootKey | string | Optional. Name your API. | | imageKeys | array | Define the keys of image objects. These must have a childkey called url, which is a string that defines the path to an image file. Gatsby-Images are added as childkey local. Default: ['image']. | | schemas | object | Define default-schemas for the objects of your API. See "Provide Default Schemas" for more information. |

Provide Default Schemas

You need to provide default schemas for the arrays and objects of your API to avoid GraphQl-errors. You can provide default schemas via the prop schemas. More information: https://graphql.org/learn/schema/

// Lets assume this is the data from your API:
const exampleDataFromApi = [
    {
        url: "post-1",
        images: [
            {
                url: "image-1.jpg",
                modified: 1556752476267
            },
            {
                url: "image-2.jpg",
                modified: 1556752702168
            }
        ],
        author: {
            firstname: "John",
            lastname: "Doe"
        }
    }
];

// This is the content of your gatsby-config.js
// and what you need to provide as schema:
module.exports = {
    plugins: [
        {
            resolve: "gatsby-source-custom-api",
            options: {
                url: {
                    development: "http://my-local-api.dev", // on "gatsby develop"
                    production: "https://my-remote-api.com" // on "gatsby build"
                },
                imageKeys: ["images"],
                rootKey: "posts",
                schemas: {
                    posts: `
                        url: String
                        images: [images]
                        author: author
                    `,
                    images: `
                        url: String
                        modified: Int
                    `,
                    author: `
                        firstname: String
                        lastname: String
                    `
                }
            }
        }
    ]
};

Multiple Sources? Multiple Instances!

If you have multiple sources for your API in your project, just instantiate the plugin multiple times. Just be sure to set a different rootKey for every instance.

Connect different APIs You can connect the different APIs with @link. Find out more about this at https://www.gatsbyjs.org/docs/schema-customization/#foreign-key-fields.

module.exports = {
    plugins: [
        {
            resolve: "gatsby-source-custom-api",
            options: {
                url: "https://my-first-api.com",
                rootKey: 'authors',
                schemas:  {
                    authors: `
                        name: String
                        description: String
                    `
                }
            }
        },
        {
            resolve: "gatsby-source-custom-api",
            options: {
                url: "https://my-second-api.com",
                rootKey: 'posts',
                schemas:  {
                    posts: `
                        text: String
                        authors: authors @link(by: "name")
                    `
                }
            }
        }
    ]
};

Images

Gatsby Source Custom API automatically downloads your image-files, so you can use them with Gatsby Image.

How does it recognize images?

The default key for images is image. You can also define your own image keys with the option imageKeys. Images have to be objects containing a childkey called url, which is a string that defines the path to an image file. Gatsby-Images are added as childkey local.

What about Caching?

If your image object provides a key called modified, this key gets cached and compared every time you build or develop. If it stays the same, the already downloaded version of the image-file is used.

Transform Nodes to Pages

This is an example of how you use the required nodes to automatically generate pages: Insert the following code into the file gatsby-node.js. The sample key here is an array called posts. All array-elements can be required in GraphQl via allPosts. In this example the posts have a child-key called "url", which defines their path and serves as marker to find them in your matching React-component (pages/post.js).

const path = require("path");

exports.createPages = async ({ graphql, actions }) => {
    const { createPage } = actions;
    const result = await graphql(`
        {
            allPosts {
                nodes {
                    url
                }
            }
        }
    `);
    return Promise.all(
        result.data.allPosts.nodes.map(async node => {
            await createPage({
                path: node.url,
                component: path.resolve("./src/pages/post.js"),
                context: {
                    // Data passed to context is available
                    // in page queries as GraphQL variables.
                    url: node.url
                }
            });
        })
    );
};

In your pages/post.js you can require the data like so:

import React from "react";
import { graphql } from "gatsby";

const Post = ({ data }) => {
    return <h1>{data.posts.title}</h1>;
};

export const query = graphql`
    query($url: String) {
        posts(url: { eq: $url }) {
            url
            title
            image {
                local {
                    childImageSharp {
                        fluid(maxWidth: 2000) {
                            ...GatsbyImageSharpFluid_withWebp
                        }
                    }
                }
                alttext
            }
        }
    }
`;

export default Post;

Replace conflicting Keys

Some of the returned keys may be transformed, if they conflict with restricted keys used for GraphQL such as the following ['id', 'children', 'parent', 'fields', 'internal']. These conflicting keys will now show up as [key]_normalized. (Thanks to gatsby-source-apiserver)

Contributing

Every contribution is very much appreciated. Feel free to file bugs, feature- and pull-requests.

❤️ If this plugin is helpful for you, star it on GitHub.