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

api-req-redux

v2.0.10

Published

api request lib

Downloads

168

Readme

ApiRequestRedux

ApiRequestRedux works only with redux.

Installation

$ npm i api-request-redux

Usage

import { apiRequestRedux } from 'api-req-redux'; import { abortRequest } from 'api-req-redux';

apiRequestRedux - it is a function that takes global configuration object, and store it for apiRequest function, which you will use to perform actual request

   const apiRequest = apiRequestRedux({
      store: () => store,
      refreshFnc: state => ...your code, // function
      headers: state => [["Content-Type", "application/json"], ["token", state.userToken]]
      onErrorFnc: (err, dispatch) => // your code
    });

Than use apiRequest to create request:

    export const login = data => async dispatch => {
      abortRequest('test');
      await apiRequest({
        url: '/api/login',
        body: data,
        method: 'POST',
        abortName: 'test'
        onStart: (dispatch) => dispatch(({ type: 'login/start' })),
        onSuccess: (data, dispatch) => dispatch(({ type: 'login/success', payload: data })),
        onError: (err, dispatch) => dispatch(({ type: 'login/error', payload: err }))
      });
    };

Options

  • apiRequestRedux

    • store - function that returns redux store
    • refreshConfig - takes the same parameters as apiRequest
    • headers - function that take current state and return array entries like this:
      const getHeaders = state => [
        ['Content-Type', 'application/json'],
        ['x-language', state.Cached.language],
        ['Authorization', selectAuthToken(state) || selectCachedToken(state)]
      ];
    • errorCodes - array of codes on wich you whant to handle by default (onErrorFnc)
    • defaultCredentials - default credentials for all requests in application, by default:
    defaultCredentials = 'same-origin',
    • onErrorFnc - will be called when errorCodes.includes(fetch.status)
    • reset - will be called if refresh token failed
  • apiRequest

    • url - request endpoint
    • method - request method (GET, POST, PUT, etc..)
    • body - object with data you want to send with request (also you can selector)
    • additionalHeaders - additional headers for this request. Function similar to headers
    • onStart - functions that called before fetch, you can use it to change redux store before make request
    • onError - functions that called when fetch fail, you can use it to change redux store, and perform error handling. Takes error as parameter
    • onSuccess - functions that called when fetch successed, you can use it to change redux store, and perform success handleing. Takes data as parameter
    • onFinally - functions that called when fetch is finished
    • selector - function, use it to select data from your store and pass as request body, takes state as parameter.
    • credentials - credentials for this request, by default defaultCredentials
    • useDefaultErrorHandler - boolean, if you dont want to use defaultError handling on this request set it to false, by default - true
    • removeHeaders - array of header keys, example:
      ['content-type']
    • bodyParser - using this function you can replace default JSON.stringify(body) and modify body as you want
    • isRefresh - boolean, in case you don't want to
    • withoutBaseUrl - boolean call refresh token on this request (default: true)
    • abortName - string
  • abortRequest (use this function to abort the one or more requests)

    • key - string, should be equal to the abortName parameter that you pass into apiRequest function call
    • index - for cases when you have multiple requests with the same key, but you want to abort specific one