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

@teh23/gatsby-source-api

v1.1.2

Published

Gatsby source plugin for create node with images from external API.

Downloads

7

Readme

GATSBY-SOURCE-API

Table of Contents

  1. Getting Started
  2. Parameters
  3. How to use
    1. Basic usage

    2. Download images

    3. Add or edit

Getting Started

  1. install package with yarn or npm:
  • npm:
npm install @teh23/gatsby-source-api
  • yarn:
yarn add @teh23/gatsby-source-api

Parameters

| Name | Type | Description | | :-------- | :--------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | url | string | Required. Url of your API. | | schema | string | Required. Define default-schemas for the objects of your API. | | baseType | string | Required. Root name of node | | auth | object | Define the auth for your API in the following format: { username: "username", password: "password" }. Auth also authorizes image downloads | | images | array | Define the keys of images. Value of images must be a url. Gatsby-Images are added as name of images keys with suffix Local. Important keys must be unique | | transform | array | [{name: "fieldName", func: (node) => ...} ] func for argument take node

How to use

1. Basic usage

url, baseType, schema are required values. Schema is shape of the data and to avoid unwanted errors is required, baseType is name of your root node name for example below is albums we use it in baseType and in schema for more details check it out: https://graphql.org/learn/schema/ https://www.gatsbyjs.com/docs/reference/graphql-data-layer/schema-customization/

plugins: [
   //... others plugins
    {
      resolve: `@teh23/gatsby-source-api`,
      options: {
        //Url of your API. REQUIRED
        url: "https://jsonplaceholder.typicode.com/albums/1/photos",
        //Root name of node. REQUIRED
        baseType: "albums",
        //Schema is shape of the data. REQUIRED
        schema: `
          type albums implements Node {
            albumId: Int
            id: Int
            title: String
            url: String
            thumbnailUrl: String
          }
        `
      },
  ],

2. Download images

We are putting keys of urls which we want download into arrays. Also, we have to link download images. ${imageName}Local: File @link(by: "id", from: "${imageName}Local___NODE") relax if u dont understand what the heck it is doing there, look below

plugins: [
   //... others plugins
    {
      resolve: `@teh23/gatsby-source-api`,
      options: {
        url: "https://jsonplaceholder.typicode.com/albums/1/photos",
        baseType: "albums",
        //array for images is require even for one value
        images: ['url', 'thumbnailUrl']
        schema: `
          type albums implements Node {
            albumId: Int
            id: Int
            title: String
            url: String
            thumbnailUrl: String
            
            urlLocal: File @link(by: "id", from: "urlLocal___NODE")
            thumbnailUrlLocal: File @link(by: "id", from: "thumbnailUrlLocal___NODE")
          }
        `
      },
  ],

3. Add or edit

name is field name if given field doesn't exist it will be added, plugin use lodash set https://lodash.com/docs/4.17.15#set You can add or edit easily deep object for example:

const object = {
  id: 1,
  address: {
    info: {
      sth: 'hi'
    }
  }
}
//... some code
transform: [
  { 
    name: 'address.info.sth',
    func: ({address: {info: { sth }}}) => `${sth} world`
  },
  //this will add new data to object
  {
    name: 'address.info.sth.newbranch[0].exist',
    func: ({address: {info: { sth }}}) => `world ${sth}`
  }
]

func is callback function for args it take row of your data

plugins: [
  //... other plugins
  {
    resolve: `@teh23/gatsby-source-api`,
    options: {
      url: 'https://jsonplaceholder.typicode.com/albums/1/photos',
      baseType: 'albums',
      //array for images is require even for one value
      images: ['url', 'thumbnailUrl', 'test'],
      transform: [
        {
          name: 'test',
          func: ({ id }) => `https://via.placeholder.com/${id * 10}`

        },
        {
          name: 'title',
          func: (row) => {
            const { title, albumId } = row
            return `${albumId}-${title}`
          }
        }

      ],
      schema: `
          type albums implements Node {
            albumId: Int
            id: Int
            title: String
            url: String
            thumbnailUrl: String
            test: String
            
            testLocal: File @link(by: "id", from: "testLocal___NODE")
            urlLocal: File @link(by: "id", from: "urlLocal___NODE")
            thumbnailUrlLocal: File @link(by: "id", from: "thumbnailUrlLocal___NODE")
          }
        `
    },
  }
]