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

fredux-api-utils

v2.2.0

Published

fabulous redux api utils

Downloads

10

Readme

Fredux API utils

Description

This library provides a set of functions which build http requests. This library relies on the standard fetch api, and should be polyfilled in older browsers.

Installing fredux-api-utils

npm install --save fredux-api-utils

Usage

To send some URL params:

import { get } from "fredux-api-utils";

get("/users", { params: { key1: "value1", key2: ["value2", "value3"] } })
  .then(response => console.log(response))
  .catch(error => console.log(error));

To send an object as the JSON body:

import { post } from "fredux-api-utils";

post("/users", { body: {name: "Peter"} })
  .then(response => console.log(response))
  .catch(error => console.log(error));

To send form data as the body:

import { post } from "fredux-api-utils";

post("/users", { formData: { key1: "value1", key2: ["value2", "value3"] } })
  .then(response => console.log(response))
  .catch(error => console.log(error));

To send form data as FormData object:

import { post } from "fredux-api-utils";

const formData = new FormData();
formData.append("name", "My Name");
formData.append("file", new Blob(["Some notes..."], { type: "text/plain" }));

// If there's not a Content-Type header, will use multipart/form-data
post("/users", { formData })
  .then(response => console.log(response))
  .catch(error => console.log(error));

To add some headers:

import { post } from "fredux-api-utils";

post("/users", { headers: { "my-custom-header": "custom" } })
  .then(response => console.log(response))
  .catch(error => console.log(error));

NOTE: when passing a body option, Content-Type: application/json will always be set unless you explicitely pass a Content-Type. The same goes with formData and Content-Type: x-www-form-urlencoded unless you pass a FormData object. In this case, Content-Type: multipart/form-data will be set automatically as default (you can override it anyway).

To set several other options just pass them:

import { get } from "fredux-api-utils";

get("/users", { timeout: 2000, mode: "no-cors" })
  .then(response => console.log(response))
  .catch(error => console.log(error));

Available API methods

  • get(endpoint, options)
  • post(endpoint, options)
  • put(endpoint, options)
  • patch(endpoint, options)
  • del(endpoint, options)

Where endpoint is a string with the resource you wish to fetch and options is an object containing custom settings you want to apply to the request. The possible options are:

  • body: any JSON body.
  • formData: any data to be passed as a x-www-form-urlencoded object or a FormData instance.
  • params: an object containing query params.
  • headers: any headers you want to add to your request.
  • timeout: any timeout in milliseconds. The default is 0.
  • credentials: sets the policy for sending cookies. One of omit, same-origin, or include. Using same-origin as default.
  • mode: the mode you want to use for the request, e.g., cors, no-cors, same-origin, or navigate. Defaults to cors.
  • redirect: the redirect mode to use, e.g. follow, error, or manual. Defaults to follow.
  • referrer: the referrer of the request, Defaults to client.
  • cache: cache mode for the request, e.g. default, no-store, reload, no-cache, force-cache,only-if-cached. Defaults to default.

This options, for the most part adhere to the fetch Request API.

CHANGELOG

v2.2.0

  • Add patch support

v2.1.0

  • Add FormData object support

v2.0.1

  • Fix headers check when forcing Content-Type to be case insensitive

v2.0.0

  • Removed the magic response parsing, thus removing the raw methods. Every method is raw now. Now all methods return a standard response

v1.3.0

  • Added support for cache, redirect and referrer options in request
  • Add new option formData, to send params x-www-form-urlencoded in the body (in order to handle classic form posts)
  • Add support for multivalued params

v1.2.0

  • Added support for credentials option in requests

v1.1.0

  • Added support for mode option in requests

v1.0.0

  • Initial release