Giter VIP home page Giter VIP logo

like-fetch's Introduction

like-fetch

Fetch with added features: timeout, retry, status validation, simplified response, etc.

npm i like-fetch

Compatible with Node.js, browser and React Native.
All options are optional, so by default it's the same as a normal fetch(...).

Usage

const fetch = require('like-fetch')

// As usual
const response = await fetch('https://example.com')
const body = await response.text()

// Using some features
const ip = await fetch('https://example.com', {
  timeout: 5000, // Uses AbortController, signal, etc
  retry: { max: 3 }, // This retry object is passed to like-retry
  validateStatus: 200, // Throw if status is not correct
  responseType: 'text' // Will automatically do response.text()
})

Timeout

const response = await fetch('https://example.com', { timeout: 5000 })
const body = await response.json()
// Throws an AbortError in case of timeout (for both request and body consuming)

Retry

const retry = { max: 5, delay: 3000, strategy: 'linear' }
const response = await fetch('https://example.com', { retry })
// If the first attempt fails then: waits 3s and retries, 6s, 9s, 12s, 15s and finally throws

Check like-retry for documentation about retry option.

Status validation

// response.status must match 200, otherwise throws
const response = await fetch('https://example.com', { validateStatus: 200 })

// response.ok must be true
const response = await fetch('https://example.com', { validateStatus: 'ok' })

// Custom function, must return true
const validateStatus = status => status >= 200 && status < 300
const response = await fetch('https://example.com', { validateStatus })

At this moment, when validateStatus fails it throws the response object.

Request types

// Sets the 'Content-Type' header to 'application/json'
// And if body is not undefined then does a JSON.stringify(body)
const response = await fetch('https://example.com', { requestType: 'json', body: { username: 'test' } })

// Sets the 'Content-Type' header to 'application/x-www-form-urlencoded'
// And if body is not undefined then it stringifes URLSearchParams using the body
const response = await fetch('https://example.com', { requestType: 'url', body: { username: 'test' } })

Response types

// Sets the 'Accept' header to 'application/json' and does response.json()
const body = await fetch('https://example.com', { responseType: 'json' })
console.log(body) // => Object {...}

// Automatic response.text()
const body = await fetch('https://example.com', { responseType: 'text' })
console.log(body) // => String '...'

Signal controller

Just using useEffect as an example of manual cancelling the request.

useEffect(() => {
  if (!account) return

  // Start request
  const promise = fetch('https://example.com/api/balance/' + account, { responseType: 'json' })

  // Propagate values
  promise.then(body => setBalance(body.balance))

  // Handle exceptions
  promise.catch(error => setBalance('~'))

  // Clean up
  return () => promise.controller.abort()
}, [account])

Caution with controller

Don't do this with the controller variable and retry option:

useEffect(() => {
  const promise = fetch(..., { retry: { max: 3 } })
  const controller = promise.controller // DON'T
  // ...
  return () => controller.abort()
}, [])

This also applies outside of React:
The promise.controller property will change on every retry.
That's because signals can't be reused.
If you don't use the retry option then that case is okay.
Still a bad practice as it's easy to fall in that bug.
So avoid isolating the controller, check the first useEffect example again.

License

MIT

like-fetch's People

Contributors

lukks 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.