0.0.3 • Published 3 years ago

redux-axios-jwt v0.0.3

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

redux-axios-jwt

Kind of like a boilerplate. Plug and play, use with DJango, DRF Rest APIs.

Setting up

Installing package using NPM.

npm install redux-axios-jwt

Once installed, you know need to link package with your application.

Note: Before setting up make sure you have redux setup already.

Defining URL & AuthKey (Example)

Default URL value is https://localhost:3000

Open main parent index.js file and add following lines of code.

...
import setURL form 'redux-axios-jwt'

let URL = 'http://localhost:8000/api' // server url for the apis
let AuthKey = "Basic" // Key value for Authorization in Headers.
setURL(URL, AuthKey)
...
API setURL()
NameTypeDefaultDescription
URLstring"http://localhost:3000"Server URL, call to be made to.
AuthKeystring"Basic"Key value required for JWT Authorization in headers'

API Actions / Reducers

To use apiAction, you first need to link its reducers with the store. Open reducers index.js file and add in following lines.

...
import apiReducers from  "redux-axios-jwt/reducers/apiReducers";

export default combineReducers({
    ...
    api: apiReducers,
    ...
});
...

Actions List

NameParamsTypeParams DefaultsDescriptionState
isLoadingstatusBooleanfalseThis action runs by default (if stateLoading = true), enabling to be run at parent component level. This is linked with each action that makes an Axios call, hence follows following rules. Before axios call => trueOnce returns a callback => falselet loading = useState((state) => state.api.isLoading)api.isLoading
returnMsgmessagestring""This action runs by default, enabling to be run at parent component level.This is linked with each action that makes an Axios call, hence follows following rules.Once returns a callback => msg from .then response.status_code.catch error.response.statuslet msg = useState((state) => state.api.message)api.message
isUniversaldataobject{}Get data inform of an objectCan be accessed from anywhere.let universal = useState((state) => state.api.universal)api.universal
changeValuenamevaluestringobjectStores any value against a key. Can be used in forms as handleChange. Can be accessed from anywhere.let value = useState((state) => state.api.value)api.value
resetValueReset value of redux to default. Can be used once you are done using value param, for e.g. after completing form and submitting it to the post request. Its not called automatically by any other action.
postDatadatalinktokenstateLoadingobjectstringstringbooleantrueSends post request and returns with an error or request. et value = useState((state) => state.api[link])apilink
getDataparamslinktokenstateLoadingstringstringstringbooleantrueSends get request and returns with an error or request.api"GET-" + link
getListDatalinktokenstateLoadingstringstringbooleantrueSends get request and returns with an error or request.apilink
putDataParamsdatalinkparamstokenstateLoadingobjectstringstringstringbooleantrueSends put request with data and returns with an error or request.
putDataSimpledatalinktokenstateLoadingobjectstringstringbooleantrueSends put request with data and returns with an error or request.apilink
searchDataparamslinktokenstateLoadingstringstringstringbooleantrueSends get request with search params and returns with an error or request.apilink
deleteDataparamslinktokenstateLoadingstringstringstringbooleanstringstringstringbooleantrueSends delete request with params and returns with an error or request.apilink

API Actions / Reducers

To use userActions, you first need to link its reducers with the store. Open reducers index.js file and add in following lines.

...
import userReducers from  "redux-axios-jwt/reducers/userReducers";

export default combineReducers({
    ...
    user: userReducers,
    ...
});
...

Actions List

NameParamsTypeParams DefaultsDescriptionState
isLoadingstatusBooleanfalseThis action runs by default (if stateLoading = true), enabling to be run at parent component level. This is linked with each action that makes an Axios call, hence follows following rules. Before axios call => trueOnce returns a callback => falselet loading = useState((state) => state.user.isLoading)user.isLoading
returnMsgmessagestring""This action runs by default, enabling to be run at parent component level.This is linked with each action that makes an Axios call, hence follows following rules.Once returns a callback => msg from .then response.status_code.catch error.response.statuslet msg = useState((state) => state.user.message)user.message
changeValuenamevaluestringobjectStores any value against a key. Can be used in forms as handleChange. Can be accessed from anywhere.let value = useState((state) => state.user.value)user.value
firstTimeLoadInsert at the time when app renders for the first time. User auth credentials gets loaded on first time load, hence automatically logins.
loginUseruserNamepassworduserDetailsLinkstringstringstring"accounts/getuser/"Login user using username and password.On success gets token and refresh token which than gets stored in localstorage. Send an api call to get userdetails too. Gets stored in user_data.user.id_tokenuser.user_data

Contributar

Hamza Fayyaz

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago