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

easy-redux-http-call

v1.4.6

Published

Created to make http call easy

Downloads

67

Readme

easy-redux-http-call

Created to make http call easy

NPM JavaScript Style Guide

Install

npm install --save easy-redux-http-call

or

yarn add easy-redux-http-call

Usage

Create Base

The CreateBase helps you to make HTTP calls easier. It's like having a special helper who knows how to talk to websites and get information for us. We can use it to ask http server for data without having to do all the setup work ourselves.

To use CreateBase, we need to give it some information. Here's an example:

const main = new CreateBase({
  store: store, // global store
  baseApiUrl: "https://dummy.restapiexample.com/api/v1", // API Endpoint
  httpFn: (data) =>  window.fetch[data.method](data.endpoint, data.params).then(
      (res) => res.data
    ), // HTTP Reqeust function
});
  • store: your existing store where you keep important information. don't worry, it will not wipe your data :)
  • baseApiUrl: your API endpoint (without / at the end)
  • httpFn (Optional): Gets the data we want and delivers it like a treasure, httpFn enabling you to write your own logic to make HTTP requests using Fetch, Axios or any other tool.
  • defaultState (Optional): The initial state for every reducer (Note: It can be override later on).
  • reducer (Optional): default reducer to store your data. (Note: It can be override later on).
  • middleware (Optional): middleware allow you to perform different action on HTTP Request life cycle. Possility are limit less, you can do paggination, show notification on certain event, filter out data and much more (Note: It can be override later on).

Create base will return your 2 method. this method uses default configuration from the createBase class. you can override this configuration at any time you want.

Now, we have main with below methods

  • createInstance: let you create basic instance of api (Can be used multiple time base don number of api's you have).
  • createListInstance: it's same as createInstance but with additional functionality. allow you to perform delete and update operation without writing additional code. we will talk difference later on. stay tunned : )

Create API Instance

As metion before it let's you create instance to make your API call. surely you have multiple api's in your project. so for that you can use many time as you want. let's create first api instance

export const AI_updateTourConfig = main.createInstance({
  endpoint: "/employees",  // api endpoint
  method: "get",  // http metion
  reducer: "user",  // reducer name
  reduxKey: "userDetail",  // key inside you reducer
  action: "GET_USER_DETAIL",
  onSuccess: (data) => data,
});
  • endpoint: your API Endpoint (must start with /)
  • method: HTTP Method (GET, POST, PUT, DELETE)
  • reducer: Your Reducer name in which you want to store result
  • reduxKey: Redux which in which you want to store result
  • action: static action name used to easily identification to your actions
  • onSucess: callback executed when HTTP call succeed

createInstance will generate new instance and store it into AI_updateTourConfig. This instance has method "call" which makes the actual HTTP request.

Create Listing Instance

createListInstance use to create listing instance to make HTTP call with 2 methods(updateAction, deleteAction). This 2 method will help you to perform update and delete of records from the redux. this functions can be directly call from Socket.

export const AI_BranchUserList = main.createListInstance({
  endpoint: GET_BRANCH_USER_LIST_ENDPOINT,
  method: "get",
  id: "product_id",
  reducer: "product",
  reduxKey: "productList",
  action: "GET_PRODUCT_LIST",
  onSuccess: (data) => data,
  middleware: myMiddleWare,
});

Auto generated method will automatically update or remove records from given information at configuration time.

Middleware

Middleware in create instance is used to perform option on different state of the execution. All the callback of middleware are as bellow

  • beforeStart
  • afterSuccess
  • afterUpdate
  • afterDelete

Bind Reducer

Used to bind all the dynamic action to specified reducer. This will allow easy-reduc-http-call to handle state update for different situation.

const newUserReducer = bindReducer(configReducer, { key: "userReducer" });

Above code will be result as new reducer from the give reducer called newUserReducer. which can be used in combineRedux or directly used to create store.

useInstance

use useInstance hook which help you get easily fetch state of current HTTP request. you don't have to make any configuation.

const reduxState = useInstance(httpInstance);

reduxState contain value of given(httpInstance) instance state from the redux.

License

MIT © prafuliihglobal