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-request-biolerplate-actions

v1.0.9

Published

dispatches boilerplate redux actions such as START_LOADING, STOP_LOADING, SET_SUCCESS, and SET_ERROR whenever you make an api request using axios, and handles START_LOADING, STOP_LOADING, and SET_ERROR for you

Downloads

5

Readme

api-request-boilerplate-actions

  • If you prefer videos, watch this video [Arabic speaking].

  • For the most of api requests you make in your React/React-Native app, you need to dispatch boilerplate redux actions such as:

    • START: before sending the request to show any loading feedback for the user.
    • STOP: after the request has been succeeded or failed to hide loading feedback.
    • SUCCESS: if the request has beensucceeded to handle the response and update app state.
    • ERROR: if the request has been failed to handle the error and may be show an error message in the relevent screen.
  • These actions are necessary to keep the user aware of what is going on. However, this makes api requests have boilerplate code that you should duplicate each time. This package uses axios interceptors to dispatch such boilerplate actions for you and also handle them in their relevant reducer letting you care about success case only. It also gives you the ability to decide what actions to dispatch for which requests. So if you have a request that you do not care if it fails, you have a way to force the package to not dispatch SET_ERROR for such request. This applies to all actions not just SET_ERROR.

Installation

npm i api-request-biolerplate-actions

Usage

  • For each api request, you define only one ActionType:
export const GET_DATA = "GET_DATA";
  • To define an api request action creator:
export const getData = value => (dispatch, getState) => {
  axios.get("some-end-point?param=" + value);
};

This is how a basic api request action creator should look like.

  • To get these boilerplate actions handled for you in a given reducer, wrap your reducer with highOrderReducer:
import {highOrderReducer} from 'api-request-boilerplate-actions';
// import GET_DATA

// define myReducer to handle getData success response which is an action with this type 'SUCCESS_' + GET_DATA
const myReducer = (state, {type, payload}) => {
  switch(type) {
    case 'SUCCESS_' + GET_DATA:
      return {...state, data: payload.data.data};
    default:
      return state;
  }
};

const outOfTheBoxReducer = highOrderReducer(
  {data: []}
  myReducer,
  [{
    requestEndPoint: 'some-end-point',
    baseActionType: GET_DATA
  }]
);

// use outOfTheBoxReducer instead of myReducer

now you get these attributes handled for you in outOfTheBoxReducer whenever you make getData request:

  • GET_DATA + 'Loading'
  • GET_DATA + 'Error'

Configuration once

  • The package should be configured through config method.
import { config } from "api-request-boilerplate-actions";
// import store

config(store.dispatch, "https://example.come/api/", errorMessage =>
  alert("error: " + errorMessage)
);

API

  • config a function that should be called once during app start up. It accepts:

    • dispatch a reference to dispatch function of redux store.
    • BASE_URL your backend base url.
    • errorHandler an optional default method to handle your errors which will be called for api requests that have errorMessage attribute set in their config object.
  • highOrderReducer a function that should wrap each reducer handling api requests. This function returns another reducer that handles boilerplate actions for you. It accepts:

    • initialState which is the initial state of the reducer.
    • reducer where you handle only the SET_SUCCESS action.
    • ApiRequestsConfigObjects an array of configuration object for each api request. The configuration object should be of this shape:
  {
    requestEndPoint: string | RegExp,
    requestMethod: AxiosRequestMethod,
    baseActionType: string,
    errorMessage?: string,
    noStart?: boolean,
    noStop?: boolean,
    noSuccess?: boolean,
    noError?: boolean
  }

Note that only 2 attributes are mandatory: requestEndPoint which is the endpoint this action targets and baseActionType which is the action type this endpoint should be mapped to. The rest of attributes are optional. The attributes noStart, noStop, and noError can be set to true whenever you do not want the package to dispatch an action neither handle that action in the reducer for a given request. noSuccess can be set to true whenever you do not want the package to dispatch success action for a given request.

Example

you find an example app under example dir in the repository.

Test

The package is tested using Jest test runner.

License

MIT