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

nookfetch

v0.3.0

Published

A lightweight, flexible fetch wrapper written during the 2020 quarantine

Downloads

4

Readme

nookfetch

A lightweight, flexible fetch wrapper written during the 2020 quarantine

Build

Table of Contents

Why a fetch wrapper?

When hitting an endpoint, most developers want to focus on the response data. They don't want to have to worry about generalized error handling, Response parsing, or response-type validation. This project allows for simple set-up of these concerns, while being flexible enough to allow overrides for individual calls.

When starting this project, I could not find anything similar already available on NPM - so I wrote it myself!

Installation

npm i nookfetch --save

Documentation

createNookFetch

nookFetch factory - creates the nookFetch function and binds it to error handling.

Arguments

| Name | Type | Required | Default | Description | | -------------- | ------------------ | -------- | ---------------------------------------------- | ------------------------------------------------------------- | | onError | (e: Error) => void | true | - | callback function on error - could be API or validation error | | generalOptions | ParseOptionsType | - | See ParseOptionsType for default information | general settings for fetch functions |

ParseOptionsType

The ParseOptionsType is an object with the following properties:

| Name | Type | Required | Default | Description | | ------------------ | --------------------- | -------- | --------------- | --------------------------------------------------------- | | parseResponse | (e: Response) => void | - | parseReturnData | function to parse incoming data for a specific fetch call | | parseErrorResponse | boolean | - | - | function to parse error message for this specific call |

NOTE: The default parseResponse value will ONLY parse JSON data - it will return the Response object if the header is not application/json.

Returns

An nookFetch function that is bound to the onError function.

Usage

import createNookFetch from "nookfetch";

const onError = (e: Error) => {
  // some generic error handling here
  ...
};

const nookFetch = createNookFetch(onError);

export default nookFetch;

nookFetch Function

Fetch wrapper function

This function handles parsing, validation, and error handling for a fetch.

Arguments

| Name | Type | Required | Default | Description | | ------------ | ----------------- | -------- | ----------------------------------------- | --------------------------------------------------- | | url | string | true | - | the url to call | | validate | (input: any) => T | true | - | function to validate the return data from the fetch | | fetchOptions | FetchOptionsType | - | - | configuration options for the fetch call | | options | OptionsType | - | See OptionsType for default information | configuration options |

FetchOptionsType

The FetchOptionsType in typescript is as follows:

type FetchOptionsType = Omit<RequestInit, "body"> & {
  body?: object | FormData;
};

You can pass any options available to the fetch function.

NOTE: The body type has been changed - nookFetch will process it automatically into a type the fetch function can consume.

OptionsType

The OptionsType is an object with the following properties:

| Name | Type | Required | Default | Description | | ------------------ | --------------------- | -------- | ------------------------------------------------- | ----------------------------------------------------------------- | | useErrorHandling | boolean | - | true | toggles use of the onError function | | parseResponse | (e: Response) => void | - | parseReturnData OR the general parsing function | function to parse incoming data for a specific fetch call, if set | | parseErrorResponse | boolean | - | the general error parsing function, if set | function to parse error message for this specific call |

NOTE: The default parseResponse value will ONLY parse JSON data - it will return the Response object if the header is not application/json.

Returns

A promise that resolves to the output of the validation function.

Throws

API Error
  • Error thrown by fetch
  • Error thrown when api status is not in the 200 range
Validation Error
  • Error thrown by validate function
Parsing Error
  • Error thrown by the parsing function

Usage

import nookFetch from "file/with/createNookFetch";
import validate from "validation";

try {
  const info = nookFetch(
    "/testing/123",
    validate,
    {
      method: "POST",
      body: { foo: "bar" }
    },
    { useErrorHandling: true }
  );
} catch (e) {
  console.log("ERROR!!!!", e);
}

APIError

Class representing an API Error

Arguments

| Name | Type | Required | Default | Description | | ------- | ------ | -------- | ------- | ------------------- | | message | string | true | - | the error message | | status | number | true | - | the api status code |

Functions

getStatus

Function to get the status code of the API error.

Returns

Number that represents the API error code.