@uxland/fetch-client
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

UXL Fetch Client npm version

Build Status Statements Branches Functions Lines
BuildStatus Statements Branches Functions Lines

Installation

npm i @uxland/fetch-client

Usage

Configuration

Set Base URL

All relative fetch will use provided url as its own base URL. If using absolute uri when fetching, this will not be taken into account.

import { setBaseUrl } from "@uxland/fetch-client";
setBaseUrl("http://localhost");

Register response handlers

Register a response handler to manipulate result.

import { registerResponseHandler } from "@uxland/fetch-client";
registerResponseHandler((response) => JSON.stringify(response));

Change fetch client configuration

Change configuration globally. All fetch requests will use this configuration.

import { configure } from '@uxland/fetch-client';
configure({mode: 'no-cors',{ headers: {authorization: 'Bearer <token>' }}});

Change fetch client headers

Change headers globally. All fetch requests will use specified headers.

import { setHeaders } from "@uxland/fetch-client";
setHeaders({ authorization: "Bearer <token>" });

Delete specific header

Remove a header from global headers using provided id.

import { removeHeader } from "@uxland/fetch-client";
removeHeader("authorization");

Reset fetch client headers

Reset global headers to default.

import { resetHeaders } from "@uxland/fetch-client";
resetHeaders();

Fetch data

import { doFetch } from "@uxland/fetch-client";
const response = doFetch("/dummy", { mode: "no-cors" }, { foo: "bar" })
  .then((r) => r)
  .catch((e) => e);

In addition, fetchClient also publishes (via @uxland/event-aggregator) two events INVALID_CREDENTIALS_EVENT and INVALID_REQUEST_EVENT when credentials are invalid or request has failed.

Readme

Keywords

none

Package Sidebar

Install

npm i @uxland/fetch-client

Weekly Downloads

8

Version

1.0.1

License

BSD-4-Clause

Unpacked Size

30.8 kB

Total Files

22

Last publish

Collaborators

  • danielcabiscol
  • uxland-admin