0.3.0 • Published 5 years ago

nookfetch v0.3.0

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

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

NameTypeRequiredDefaultDescription
onError(e: Error) => voidtrue-callback function on error - could be API or validation error
generalOptionsParseOptionsType-See ParseOptionsType for default informationgeneral settings for fetch functions
ParseOptionsType

The ParseOptionsType is an object with the following properties:

NameTypeRequiredDefaultDescription
parseResponse(e: Response) => void-parseReturnDatafunction to parse incoming data for a specific fetch call
parseErrorResponseboolean--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

NameTypeRequiredDefaultDescription
urlstringtrue-the url to call
validate(input: any) => Ttrue-function to validate the return data from the fetch
fetchOptionsFetchOptionsType--configuration options for the fetch call
optionsOptionsType-See OptionsType for default informationconfiguration 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:

NameTypeRequiredDefaultDescription
useErrorHandlingboolean-truetoggles use of the onError function
parseResponse(e: Response) => void-parseReturnData OR the general parsing functionfunction to parse incoming data for a specific fetch call, if set
parseErrorResponseboolean-the general error parsing function, if setfunction 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

NameTypeRequiredDefaultDescription
messagestringtrue-the error message
statusnumbertrue-the api status code

Functions

getStatus

Function to get the status code of the API error.

Returns

Number that represents the API error code.

0.3.0

5 years ago

0.2.0

5 years ago

0.1.1

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago