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

feed-fetcher

v2.7.3

Published

fetch API based abstraction to fetch json/text/blob feeds using methods similar to setInterval and setTimeout

Downloads

2

Readme

FeedFetcher

Documentation for v2.7

What is this for

This package is meant for the browser context.

In order to make scheduled fetch requests (usually for data feeds) easier and less annoying when it comes to exceptions and errors, feed fetcher handles most of it internally while providing an interface that is very similar to what you are used in the browser: setTimeout/setInterval, it also allows you to feed things once, or use the former methods and clearInterval/clearTimeout as you would normally.

For the error types when fetching look at the error types section.

Importing [Webpack]

Import into a webpack project by using

import feedFetcher from 'feed-fetcher'

If you need to support older browsers that don't have Promise, fetch, or requestAnimationFrame make sure you include polyfills before importing anything from FeedFetcher

import './your-polyfills' // Import your own polyfills if needed. ex: promise-polyfill, whatwg-fetch
import feedFetcher from 'feed-fetcher'

Importing [Script Tag]

NOTE: Reminder to include fetch and Promise polyfills for browsers like internet explorer, BEFORE including the FeedFetcher script

<script src="https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/fetch.min.js"> </script>
<script src="FeedFetcher.min.js"> </script>
// Access it through window:
window.feedFetcher

Using setInterval and clearInterval

// Minimal example, automatically sets mode to json because of filename.
// data on the method onData can be expected to be a json object
// intervalID return value can be used to clearFeedInterval when needed.
// In this case: As soon as we get working data once, we kill the feed fetcher.
let intervalID = feedFetcher.setInterval({
    url: 'https://somewebsite.com/file.json',
    onData: (data, prevData) => {
        console.log('New Data:', data)
        feedFetcher.clearInterval(intervalID)
    }
}, 10000)

setInterval parameters

let options = {
    // Url that will be fetched.
    url: '/file1.json', 

    // [optional]
    // Defines how to parse and deal with data internally. If omitted it will be set automatically to 'json' for json files, 'text' for txt files, and by default to 'text' for all other urls
    // Current possible values: 'json', 'text', 'blob'
    mode: 'json', 


    // How often we will attempt to fetch data in milliseconds
    // if omitted the default will be used: 10 seconds.
    // This refresh rate can be specified as a second parameter also like usual browser setInterval, setTimeout
    refreshRate: 5000,


    // How long we will wait for request to be completed before failing.
    // Default is 15 seconds.
    timeout: 15000,

    // Runs when we receive data.
    // data parameter will contain a string in 'text' mode, and a javascript object if mode is set to 'json'
    // prevData parameter will work similarly as data, but with the last cached data, on the first execution it will be null (no previous data)
    // Note: if compareWithCache option is set to true, this method will not execute on identical new data. 
    onData: (data, prevData) => {/* do something... */},


    // [optional]
    // if not omitted, it will run when data received is equal to the last data received that was cached.
    // Works identically to onData otherwise.
    onSameData: (data) => {/* do something... */},


    // [optional]
    // Although errors are already logged, if anything in particular needs to be done depending on the error type, this callback can be specified:
    // error parameter is always an object which contains:
    // error = {
    //     errorType: 'FETCH_FAIL' || 'JSON_PARSE_FAIL' || 'JSON_AND_TEXT_PARSE_FAIL', //... more error types, look below.
    //     errorText: 'Error text caught in the catch() of the fetch',
    //     feedObject: {'internal feed object being exposed for analysis. Do not modify it'},

    //     // if errorType == 'FETCH FAIL'
    //     status: 404,
    //     statusText: 'Not Found'

    // }
    onError: (error) => {

    }
    
}

Using setTimeout, clearTimeout and fetch

setTimeout and clearTimeout work identically to setInterval and clearInterval where the first parameter is the options, and the second parameter is milliseconds.

fetch

Fetch is a shorthand for fetching a feed just once and cancelling the subsequent fetching internally, no matter what the result ends up being.

// Simple one time fetch that times out after 5 seconds
feedFetcher.fetch({
    url: '/',
    timeout: 5000,
    mode: 'text',
    onData: (data) => {
        console.log('Data received: ', data)
    },
    onError: (error) => {
        console.warn('Error occurred with fetch: ', error)
    }
})

Alternative parameters

You can also do the following shorthand for function parameters: url[, mode], onData [, onError, onSameData], milliseconds

The mandatory parameters are url, onData, and milliseconds. you can specify mode in between url and onData, and specify other method handlers before milliseconds.

// Simple one time fetch.
feedFetcher.fetch('https://somewebsite.com/file.json', (data) => {
    console.log(data)
})

feedFetcher.setInterval('/', 'text',
    (data) => {
        console.log(data)
    },
    (error) => {
        console.error(error)
    }
, 7500)

Error Types

By logging the error object with the onError callback, and reading the errorType property, we can know what went wrong.

error.errorType == 'FETCH_TIMEOUT'
// The fetch timed out, either because you specified a timeout number in specific: ex: 10000 (10 seconds)
// Or the default timeout parameter: 15 seconds.


error.errorType == 'JSON_PARSE_FAIL'
// When fetching in JSON mode, response.json() failed internally, so whatever you fetched could not parse properly.
// error.textContents will contain the response as a string so you can see what might be the problem.


error.errorType == 'JSON_AND_TEXT_PARSE_FAIL'
// When trying to read the text contents of a JSON_PARSE_FAIL error, we also cannot get the text contents. This is not likely.


error.errorType == 'BLOB_PARSE_FAIL'
// Tried to fetch a file/url as a blob but the internal response.blob() failed. Can get text contents of request via error.textContents


error.errorType == 'BLOB_AND_TEXT_PARSE_FAIL'
// Similar as JSON_AND_TEXT_PARSE_FAIL but for blob.


error.errorType == 'FETCH_FAIL'
// General error for failed fetching, use error.errorText, or analyize error.feedObject / error.statusText / error.status to see what went wrong


error.errorType == 'USER_METHOD_ERROR'
// An error got caught within the callback methods supplied: onData, onSameData (Warning: onError will not be caught here.)
// Ex: Trying to JSON parse something that's not a proper JSON string, whether related to the fetched data or not in this method, will be caught internally.
// errorText will display the error.

requestAnimationFrame Mode

If you are confident you will have requestAnimationFrame support on your target browser and you do not want the fetches to run when the page is tabbed out; You can set the internal mode of feed fetcher to work via requestAnimationFrame. Note that you can only change this when there are no feeds set up to run at an interval or timeout. So ideally, at the beginning of the app.

feedFetcher.setMode('raf') // Default is 'interval'