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

@humble-barnacle001/fetch-api-data-action

v2.2.0

Published

GitHub action for handling authenticated API requests, allowing you to save the data from the request into your workspace as an environment variable and a .json file.

Downloads

4

Readme

This GitHub Action will handle authenticated API requests for you, allowing you to save the data from the request into your workspace as an environment variable and a file. Using this action will allow you to save data from these queries on a schedule so they can be used in a static page without exposing your API credentials.

Getting Started ✈️

You can include the action in your workflow to trigger on any event that GitHub actions supports. You'll need to provide the action with the endpoint you'd like to request along with any configuration options as stringified JSON.

name: Refresh Feed
on: [push]
jobs:
  refresh-feed:
    runs-on: ubuntu-latest
    steps:
      - name: Fetch API Data 📦
        uses: JamesIves/[email protected]
        with:
          endpoint: https://example.com
          configuration: '{ "method": "GET", "headers": {"Authorization": "Bearer ${{ secrets.API_TOKEN }}"} }'

Once the action has run the requested data will be exported into the FETCH_API_DATA environment variable and will also be available as a .json file in your workspace located by default in the fetch-api-data-action/data.json directory. If you need something other than .json file please refer to the format parameter.

You can combine the use of this with the GitHub Pages Deploy Action to trigger scheduled updates to a feed on your website.

In one workflow you can fetch data from an API on a schedule and push it to your main branch.

name: Refresh Feed
on:
  schedule:
    - cron: 10 15 * * 0-6
jobs:
  refresh-feed:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout 🛎️
        uses: actions/checkout@v2
        with:
          persist-credentials: false

      - name: Fetch API Data 📦
        uses: JamesIves/[email protected]
        with:
          endpoint: https://example.com
          configuration: '{ "method": "GET", "headers": {"Authorization": "Bearer ${{ secrets.API_TOKEN }}"} }'

      - name: Build and Deploy 🚀
        uses: JamesIves/github-pages-deploy-action@v4
        with:
          branch: main # Pushes the updates to the main branch.
          folder: fetch-api-data-action # The location of the data.json file saved by the Fetch API Data action.
          target-folder: data # Saves the data into the 'data' directory on the main branch.

In another workflow you can then build and deploy your page.

name: Build and Deploy
on:
  schedule:
    - cron: 10 16 * * 0-6
jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout 🛎️
        uses: actions/checkout@v2
        with:
          persist-credentials: false

      - name: Install 🔧
        run: |
          npm install
          npm run-script build

      - name: Build and Deploy 🚀
        uses: JamesIves/github-pages-deploy-action@v4
        with:
          branch: gh-pages
          folder: build

In your project you can import the JSON file and make it part of your build script. This way your site will re-build and deploy whenever refreshed data has been fetched from the server.

Install as a Node Module 📦

If you'd like to use the functionality provided by this action in your own action you can either create a composite action, or you can install it using yarn or npm by running the following commands. It's available on both the npm and GitHub registry.

yarn add @jamesives/fetch-api-data-action

It can then be imported into your project like so.

import run, {
  retrieveData,
  generateExport,
  ActionInterface
} from 'fetch-api-data-action'

Calling the functions directly will require you to pass in an object containing the variables found in the configuration section.

import run from 'fetch-api-data-action'

run({
  endpoint: 'https://example.com',
  configuration: JSON.stringify({
    method: 'GET',
    headers: {Authorization: `Bearer ${process.env['TOKEN']}`}
  })
})

Configuration 📁

The with portion of the workflow must be configured before the action will work. You can add these in the with section found in the examples above. Any secrets must be referenced using the bracket syntax and stored in the GitHub repositories Settings/Secrets menu. You can learn more about setting environment variables with GitHub actions here.

Minimal Setup

The following configuration options should be set.

| Key | Value Information | Type | Required | | --------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------- | -------- | | endpoint | The URL of the endpoint you'd like to retrieve data from. For example: https://example.com/data. If no configuration is provided then the default request method will be GET. | with | Yes | | configuration | Any applicable configuration settings that should be set such as authentication tokens. You can reference secrets using the ${{ secrets.secret_name }} syntax, or you can reference data returned from the token-endpoint request using the triple bracket syntax: {{{ data.access_token }}}. For more information refer to the Token Request part of the readme and the Fetch API documentation. | secrets / with | No |

Optional Choices

| Key | Value Information | Type | Required | | --------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------- | -------- | | token-endpoint | If the endpoint API requires you to make a request to get an access token prior to fetching data you can perform this task by specifying a token endpoint. Any data returned from the token end can be referenced in the configuration variable using the triple bracket syntax: {{{ access_token }}}. For more information refer to the Token Request part of the readme; | with | No | | token-configuration | Any applicable configuration settings that should be set such as authentication tokens. You can reference secrets using the ${{ secrets.secret_name }} syntax. For more information refer to the Fetch API documentation. | secrets / with | No | | retry | If you're working with an intermittent API you can toggle this option to true. Doing so will make the action try the request 3 times at random invervals before failing. | with | No | | save-location | By default the save location of the file is fetch-api-data-action/data.json, if you'd like to override the directory you can do so by specifying a new one with this variable. | with | No | | save-name | You can override the name of the exported .json file by specifying a new one here. You should not include the file extension in your name. | with | No | | format | Allows you to modify the extension of the file saved from the API response, for example you can set this field to json or txt. This field defaults to json. | with | No | | encoding | Allows you to modify the encoding of the file saved from the API response, for example you can set this field to utf8 or hex. This field defaults to hex. Choose from ascii or utf8 or utf-8 or utf16le or ucs2 or ucs-2 or base64 or latin1 or binary or hex | with | No | | debug | If set to true the action will log the API responses it receives in the terminal. | with | No |


Token Request 🎟️

If you need to make a request to another endpoint in order to request an access token or something similar you can do so using the token-endpoint and token-configuration parameters. You can then use the returned token in your configuration variable using the triple syntax like so {{{ tokens.access_token }}}. You can find an example of this below.

name: Refresh Feed
on: [push]
jobs:
  refresh-feed:
    runs-on: ubuntu-latest
    steps:
      - name: Fetch API Data 📦
        uses: JamesIves/[email protected]
        with:
          # The token endpoint is requested first. This retrieves the access token for the other endpoint.
          token-endpoint: https://example.com/auth/token
          # The configuration contains secrets held in the Settings/Secrets menu of the repository.
          token-configuration: '{ "method": "POST", "body": {"client_id": "${{ secrets.client_id }}", "client_secret": "${{ secrets.client_secret }}"} }'
          # Once the token endpoint has fetched then this endpoint is requested.
          endpoint: https://example.com/data
          # The bearer token here is returned from the TOKEN_ENDPOINT call. The returned data looks like so: {data: {access_token: '123'}}, meaning it can be accessed using the triple bracket syntax.
          configuration: '{ "method": "GET", "headers": {"Authorization": "Bearer {{{ data.access_token }}}"} }'

Support 💖

This project would not be possible without all of our fantastic contributors and sponsors. If you'd like to support the maintenance and upkeep of this project you can donate via GitHub Sponsors.