Git Product home page Git Product logo

strong-fetch's Introduction

strong-fetch is a wrapper around the fetch API. It provides axios' basic features and has built-in (and optional) automatic AbortController handling. You only need to provide an unique signalKey for each request. Say no more to creating new AbortController instances everywhere, strong-fetch will do everything for you.

strong-fetch features:

  • Automatic (and optional) AbortController handling.
  • Automatic JSON response data handling.
  • Automatically throws on non 2xx responses.
  • A FetchError object that extends the Error object and includes the status of the error response for better exceptions handling.
  • Responses have the shape of StrongFetchResponse.
  • Utilities for manually aborting requests if needed.

strong-fetch is TypeScript friendly.

Response schema:

The response for a request contains the following information:

data: {
} // Object containing the response provided by the server.

status: number // HTTP status from the server response.

statusText: string // HTTP status message from the server response.

headers: {
} // HTTP headers the server responded with.

Usage examples:

import fetch, { type StrongFetchResponse } from 'strong-fetch'
import type { ProductResponse } from './types'

const searchProducts = (
  query: string
): Promise<StrongFetchResponse<ProductResponse>> => {
  return fetch(`https://dummyjson.com/products/search?q=${query}`, {
    signalKey: 'searchProducts'
  })
}

const results = await searchProducts('something')
/* 
  {
    data: ProductResponse
    status: number
    statusText: string
    headers: Record<string, string>
  }
*/
import fetch from 'strong-fetch'
import type { ProductResponse } from './types'

const searchProducts = (query: string) => {
  return fetch<ProductResponse>(
    `https://dummyjson.com/products/search?q=${query}`,
    {
      signalKey: 'searchProducts'
    }
  )
}

const results = await searchProducts('something')
/* 
    {
      data: ProductResponse
      status: number
      statusText: string
      headers: Record<string, string>
    }
*/

Run locally:

// Clone the repository
git clone https://github.com/ivanatias/strong-fetch.git

// Install the dependencies
npm install

// Start a development server
npm run dev

strong-fetch's People

Contributors

ivanatias avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.