react-api
Install
Install this package with npm
npm i @salsita/react-api
or yarn
yarn add @salsita/react-api
API
Modules
Classes
- ApiError
-
The base class for API errors
- BusinessValidationError
-
The class for business validation error
Constants
- HTTP_CONFLICT
-
The error code for HTTP conflict
Functions
-
apiReducer(state, action) ⇒
ApiState
-
The reducer of the API module
- withErrorHandling(saga, ...args)
-
Wraps saga into try/catch block catching all the ApiErrors and translating them to API_ERROR actions.
- withLoadingIndicator(saga, ...args)
-
Wraps saga into start/stop loading actions
-
wrapApiCall(call) ⇒
function
-
Wraps function with try/catch block to catch all the API errors and translate them to according exceptions.
- ApiErrorToast()
-
A simple component that shows an error toast message
- ApiLoader()
-
A simple component that shows a loading indicator
ApiActions
-
ApiActions
-
.apiError(errorType, errorReason) ⇒
Action
-
.startLoading() ⇒
Action
-
.stopLoading() ⇒
Action
-
.apiError(errorType, errorReason) ⇒
Action
ApiActions.apiError(errorType, errorReason) ⇒ Saves API error
Kind: static method of ApiActions
Param | Type | Description |
---|---|---|
errorType | string |
Type of the error |
errorReason | string |
Reason of the error |
Action
ApiActions.startLoading() ⇒ Sets the global loading indicator on
Kind: static method of ApiActions
Action
ApiActions.stopLoading() ⇒ Sets the global loading indicator off
Kind: static method of ApiActions
ApiSelectors
-
ApiSelectors
-
.getError(state) ⇒
string
|null
-
.isLoading(state) ⇒
boolean
-
.getError(state) ⇒
string
| null
ApiSelectors.getError(state) ⇒ Returns the latest API error type
Kind: static method of ApiSelectors
Returns: string
| null
- The latest API error type
Param | Type | Description |
---|---|---|
state | RootState |
The root state |
boolean
ApiSelectors.isLoading(state) ⇒ Returns true if a loading is in progress
Kind: static method of ApiSelectors
Returns: boolean
- True if a loading indicator is in progress, false otherwise
Param | Type | Description |
---|---|---|
state | RootState |
The root state |
ApiError
The base class for API errors
Kind: global class
-
ApiError
- .constructor(type, reason)
-
.getReason() ⇒
string
-
.getType() ⇒
string
apiError.constructor(type, reason)
Creates an API error
Kind: instance method of ApiError
Param | Type | Description |
---|---|---|
type | string |
Type of the error |
reason | string |
Reason of the error |
string
apiError.getReason() ⇒ Get the error reason
Kind: instance method of ApiError
Returns: string
- The error reason
string
apiError.getType() ⇒ Get the error type
Kind: instance method of ApiError
Returns: string
- The error type
BusinessValidationError
The class for business validation error
businessValidationError.constructor(reason)
Creates a business validation error
Kind: instance method of BusinessValidationError
Param | Type | Description |
---|---|---|
reason | string |
The error reason |
HTTP_CONFLICT
The error code for HTTP conflict
ApiState
apiReducer(state, action) ⇒ The reducer of the API module
Kind: global function
Param | Type | Description |
---|---|---|
state | ApiState |
The state of the API module |
action | Action |
A redux action |
withErrorHandling(saga, ...args)
Wraps saga into try/catch block catching all the ApiErrors and translating them to API_ERROR actions.
Kind: global function
Param | Type | Description |
---|---|---|
saga | Generator |
API call saga which may throw an ApiError |
...args | any |
Arguments to be passed to the saga |
withLoadingIndicator(saga, ...args)
Wraps saga into start/stop loading actions
Kind: global function
Param | Type | Description |
---|---|---|
saga | Generator |
A long running saga |
...args | any |
Arguments to be passed to the saga |
function
wrapApiCall(call) ⇒ Wraps function with try/catch block to catch all the API errors and translate them to according exceptions.
Kind: global function
Returns: function
- Wrapped API call function
Param | Type | Description |
---|---|---|
call | function |
A function calling an API client and returning promise |
ApiErrorToast()
A simple component that shows an error toast message
ApiLoader()
A simple component that shows a loading indicator
Kind: global function