Skip to content
This repository was archived by the owner on Mar 30, 2022. It is now read-only.
/ fetch-browser Public archive

Thin wrapper over `window.fetch`

License

Notifications You must be signed in to change notification settings

asd-xiv/fetch-browser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

999c352 · May 29, 2020

History

12 Commits
May 27, 2020
May 29, 2020
May 20, 2020
May 20, 2020
May 20, 2020
May 20, 2020
May 20, 2020
May 20, 2020
May 20, 2020
May 20, 2020
May 20, 2020
May 27, 2020
May 29, 2020
May 29, 2020

Repository files navigation

fetch-browser

Thin wrapper over window.fetch. Sister library of @mutant-ws/fetch-node.

Install

npm i @mutant-ws/fetch-browser

Initialize

import { set } from "@mutant-ws/fetch-browser"

set({
  // Throws if not set and using relative paths
  baseURL: "http://localhost",
})

Default headers

import { set } from "@mutant-ws/fetch-browser"

set({
  // Persistent headers
  headers: {
    // Library defaults
    "accept": "application/json",
    "content-type": "application/json",

    // Set JWT for authorized requests
    authorization: "signed-payload-with-base64-over",
  },
})

Query string parameters

There is no built-in way to handle query params but you can set a custom transform function.

import { set } from "@mutant-ws/fetch-browser"
import { stringify } from "qs"

set({
  // Throws if query params passed and no stringify function defined
  queryStringifyFn: source =>
    stringify(source, {
      allowDots: true,
      encode: false,
      arrayFormat: "brackets",
      strictNullHandling: true,
    })
})

GET

import { GET } from "@mutant-ws/fetch-browser"

const myIP = await GET("https://api.ipify.org", {
  query: {
    format: "json"
  }
})
// => {"ip":"213.127.80.141"}

PATCH

POST

DELETE

MULTIPART

Changelog

See the releases section for details.