npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@kalkuz/fetch-http

v0.1.8

Published

A library that provides shortcuts for http requests

Downloads

16

Readme

npm version

Fetch HTTP

A simple HTTP client for Browsers.

Warning If you want to use this package in a Node.js, you should use @kalkuz/node-fetch-http instead.

Installation

You can install this package using npm:

npm install @kalkuz/fetch-http

Using yarn:

yarn add @kalkuz/fetch-http

Usage

Importing The Package

For configuring the package, you can use the default export:

import FetchHttp from '@kalkuz/fetch-http';

FetchHttp.Configurate(...);

Or you can use the named export:

import { Configurate } from '@kalkuz/fetch-http';

Configurate(...);

For supported types of HTTP requests, you can use the named export:

import { Get, Post, Put, Patch, Delete } from '@kalkuz/fetch-http';

Configuring The Package

Configuration is optional. If you don't configure the package, it will use the default configuration.

The default configuration is:

{
  baseUrl: '',
  getAuthorization: () => '',
}

Configuring The Base URL

The baseUrl parameter will be used as the base URL for all requests. Example:

// Configured URL: https://example.com/api/v1

Get('/users');

// Final URL: https://example.com/api/v1/users

If you don't want to use a base URL, you can pass an empty string as the baseUrl parameter or skip the configuration. When you skip configuration of baseUrl, you can pass the full URL as the first parameter of the HTTP request function.

Get('https://example.com/api/v1/users');

// Final URL: https://example.com/api/v1/users

If you configured the baseUrl parameter and you want to use another full URL endpoint, you can pass the full URL as the optionalApiAddress parameter of the HTTP request function. Example:

// Configured URL: https://example.com/api/v1

Configurate({ baseUrl: 'https://example.com/api/v1' });

// Get(endpoint, headers, optionalApiAddress)
Get('/users', null, 'https://example.com/api/v2');

// Post(endpoint, body, headers, optionalApiAddress)
Post('/users', { name: 'John Doe' }, null, 'https://example.com/api/v2')

// Final URL: https://example.com/api/v2/users

Configuring The Authorization Header

The getAuthorization function will be used to get the authorization header for all requests.

The getAuthorization function will be called before every request and it should return the authorization header value. Example:

Configurate({
  getAuthorization: () => {
    const token = localStorage.getItem('token');

    // as bearer token
    return `Bearer ${token}`;
    // or as basic auth
    return `${token}`;
  },
});

// If you send a request:
Get('/users');

// Headers will be something like:
{
  ...
  'Content-Type': 'application/json',
  authorization: 'Bearer %YOUR_TOKEN%', 
  // or
  authorization: '%YOUR_TOKEN%',
}

Promise Based

All HTTP request functions return a promise. Example:

Get('/users')
  .then((response) => {
    // response is a json
    // response.json() is applied before
  })
  .catch((error) => {
    // handle the error
  });

License

MIT License