1.4.6 • Published 7 months ago

easy-redux-http-call v1.4.6

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

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

1.4.6

7 months ago

1.4.4

11 months ago

1.4.3

12 months ago

1.4.2

12 months ago

1.4.1

12 months ago

1.4.0

12 months ago

1.3.9

12 months ago

1.3.7

1 year ago

1.3.8

1 year ago

1.2.18

1 year ago

1.2.19

1 year ago

1.3.6

1 year ago

1.3.5

1 year ago

1.3.4

1 year ago

1.3.3

1 year ago

1.3.2

1 year ago

1.3.1

1 year ago

1.3.0

1 year ago

1.2.20

1 year ago

1.2.23

1 year ago

1.2.24

1 year ago

1.2.21

1 year ago

1.2.22

1 year ago

1.2.27

1 year ago

1.2.28

1 year ago

1.2.25

1 year ago

1.2.26

1 year ago

1.2.12

1 year ago

1.2.10

1 year ago

1.2.11

1 year ago

1.2.16

1 year ago

1.2.17

1 year ago

1.2.14

1 year ago

1.2.15

1 year ago

1.2.8

1 year ago

1.2.7

1 year ago

1.2.6

1 year ago

1.2.5

1 year ago

1.2.4

1 year ago

1.2.9

1 year ago

1.2.3

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.0

2 years ago