0.2.0 • Published 3 years ago

very-simple-fetch v0.2.0

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

Simple Fetch 🌐

Utility for easy use of the Fetch API

  • Returns final (parsed to JSON, text or raw) result, including custom errors
  • Uses local cache to store and retrieve results of GET-requests
  • Accepts options.params object which converted to encoded search parameters
  • Can be cancelled (for example, if the request takes too long)
  • Contains baseUrl and authToken setters
  • Tested in Chrome, Firefox & Safari
  • Tested with React & Vue

Installation

yarn add very-simple-fetch
# or
npm i very-simple-fetch

Usage

import simpleFetch from 'very-simple-fetch'

const getTodos = async () => {
  const { data: todos, info } = await simpleFetch.get(
    'https://jsonplaceholder.typicode.com/todos',
    {
      params: {
        _limit: 10
      }
    }
  )
  console.table(todos)
  console.log(JSON.stringify(info, null, 2))

  const { error } = await simpleFetch('https://wrong.com')
  console.error(error)
}
getTodos()

Full example on CodeSanbox

Signature of the main function

simpleFetch(options: string | object)
// alias for simpleFetch.get()

Signatures of helper functions

// GET
simpleFetch.get(url: string, options: object)
// or if you set base URL
simpleFetch.get(options: object)

// POST
simpleFetch.post(url: string, body: any, options: object)
// with baseUrl
simpleFetch.post(body: any, options: object)

// PUT
simpleFetch.update(url: string, body: any, options: object)
// with baseUrl
simpleFetch.update(body: any, options: object)

// DELETE
simpleFetch.remove(url: string, options: object)
// with baseUrl
simpleFetch.remove(options: object)

Setting base URL

simpleFetch.baseUrl = 'https://example.com'

Setting auth token

simpleFetch.authToken = token
/*
simpleFetch.headers = {
  // ...
  Authorization: 'Bearer [token]'
}
*/

Cancellation of the request

// current (last sended) request will be cancelled
simpleFetch.cancel()

// send request
simpleFetch(url)
// retrieve id of this request
const { currentRequestId } = simpleFetch
// cancel this request
simpleFetch.cancel(currentRequestId)

Options

  • common
  • custom:
    • customCache: boolean - if true, result of the GET-request will be stored in the local cache - Map. Result of the same request will be retrieved from this cache until customCache is set to false. Default value is true
    • log: boolean - if true, options, cache and result will be output to DevTools concole. Default value is false
    • params?: object - this object is converted to encoded search parameters that are appended to the URL:
      • key: string
      • value: string
    • handlers?: object:
      • onSuccess: function
      • onError: function
      • onAbort: function

Default options

{
  method: 'GET',
  headers: {
    'Content-Type': 'application/json'
  },
  referrerPolicy: 'no-referrer',
  customCache: true,
  log: false,
  signal: new window.AbortController().signal
}

Response

  • data: any | null - result of the request or null if there war an error
  • error: null | any - null or custom error
  • info: object:
    • headers: object
    • status: number,
    • statusText: string
    • url: string

Utility trying to send request with any arguments and return something even if an exception was thrown.

0.2.0

3 years ago

0.1.9

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.2

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.5

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago