Skip to content
This repository has been archived by the owner on Mar 30, 2022. It is now read-only.

Latest commit

 

History

History
101 lines (74 loc) · 1.9 KB

README.md

File metadata and controls

101 lines (74 loc) · 1.9 KB

fetch-browser

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

Install

npm i @asd14/fetch-browser

Initialize

import { setup } from "@asd14/fetch-browser"

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

Default headers

import { setup } from "@asd14/fetch-browser"

setup({
  // 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 { setup } from "@asd14/fetch-browser"
import { stringify } from "qs"

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

GET

import { GET } from "@asd14/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.