Intercepting APIs with redux-api-middleware made easy
Building single page applications and utilising some kind of API ? Probably you'd want to
include some kind of JWT
or you'd want to preppend base URL to all of your http requests ?
Well redux-api-middleware-interceptor
does that kind of stuff for you automatically.
1.1 Installation
npm: npm install redux-api-middleware-interceptor --save
OR
yarn: yarn add redux-api-middleware-interceptor
Note: You must have redux-api-middleware ^2.0.0 installed as peer dependency.
1.2 Usage
;;;; const apiMiddlewareInterceptor = ; const store = ;
1.3 API
redux-api-middleware-interceptor
exports a function that when called with config object returns you a middleware that acts as an interceptor:
<-- returns a redux middleware
1.3.1 configObj.headers (object|function)
Usefull when you want to add additional headers to all of your routes.
Note: If you pass headers that were already provided, then its gonna override original headers.
You can either pass an Object:
;;; const store =
or if you want more customised solution like adding JWT then you can pass a function
instead of object
. The first parameter of it will be the already present headers and second will be redux state
like so:
Note: It should return an object otherwise it'll default to original headers
;;; const store =
There are some Useful functions which you can use when you want to do something like when request fails (e.g showing a toastr
automatically or logging out User automatically if statusCode
is 401 :D), when request is success or when request is initiated (e.g showing a Youtube like loader and hiding when request resolves)
1.3.2 configObj.getURL(passedUrl: string, state: object): string
Usefull when you don't want to include base URL to all of your http requests. e.g on production
API url can be different than development
environment.
Note:
- If getURL is not defined, then interceptor is not gonna do anything.
- Its gonna throw an error if
getURL
is not a function.- It'll only call this function, when
passedUrl
is not complete (e.g/foo/1
)- It should return a string
;;; const store = ;
1.3.3 onRequestInit(state: object)
Usefull when you don't want to show a Youtube like loader when a request is initiated.
Note:
- If onRequestInit provided is not a function, it'll throw an Error.
;;; const store = ;
1.3.4 onRequestSuccess(state: object, response: object)
Usefull when you want to do something when request has a success response. E.g hiding the Youtube like loader.
Note:
- If onRequestSuccess provided is not a function, it'll throw an Error.
;;; const store = ;
1.3.5 onRequestError(state: object, response: object)
Usefull when you want to do something when request has an error response. E.g Logging out of the app if endpoint returns a 401 code.
Note:
- If onRequestSuccess provided is not a function, it'll throw an Error.
- If the response is not JSON, the library will return the ressult as
raw_res
withis_json=false
.
;;; const store = ;
I Like it
Please share and don't forget to ⭐️ this repo :)
License
MIT © Hamza Baig