A wrapper for axios that converts Promises
from incoming responses
into Monads
.
Usage:
import axios from "axios";
import axiosbridge from "axiosbridge";
const bridge = new axiosbridge.Bridge({
baseURL: "https://www.google.com",
}, axios);
(async () => await axiosbridge.execSafeAsync(
async () => bridge.get("/"),
{
onFulfilled: (result) => console.dir(result),
onError: (error) => console.error(error),
_finally: () => console.log("I get logged after the handlers are done in both cases."),
}
))();
- Bridge
- convertPromiseToResult
- execSafeAsync
- cleanableExec
- cleanableSafeAsyncExec
- processAbortController
- PromiseToResultConverterConfig
- SafeAsyncExecHandlers
- CleanableExecOnCleanConfig
- CleanableSafeAsyncExecHandlers
- DEFAULT_CONNECTION_TIME_OUT
- DEFAULT_REQUEST_TIME_OUT
- Option
- Result
- AbortController
- AxiosInstance
- CreateAxiosDefaults
- AxiosRequestConfig
- AxiosResponse
A wrapper for an axios instance that converts the returned Promises in to Results when a request is made.
-
config
CreateAxiosDefaults? The axios create defaults. -
axios
AxiosStatic? The axios default exported object.
Main axios instance to be used for all requests
Type: AxiosInstance
Time before giving up on server discovery.
Request fetch
-
route
string -
config
AxiosRequestConfig?
Returns Promise<Result<Option<T>, E>>
Request create
-
route
string -
data
T -
config
AxiosRequestConfig?
Returns Promise<Result<Option<H>, E>>
Request patch
-
route
string -
data
T? -
config
AxiosRequestConfig?
Returns Promise<Result<Option<H>, E>>
Request update
-
route
string -
data
T? -
config
AxiosRequestConfig?
Returns Promise<Result<Option<H>, E>>
Request delete
-
route
string -
config
AxiosRequestConfig?
Returns Promise<Result<Option<T>, E>>
Sets the error constructor.
-
errorConstructor
function (err: any): E New error constructor
Runs the provided function and if it throws an error it catches and logs then it converts Promise to a Result before returning it.
-
_function
function (...args: any): Promise<T> The function to be run -
onReject
function (err: any): E? Runs if the function throws an error -
onFulfill
function (result: T): T Runs if the function completes successfully (optional, default(result:T)=>result
) -
config
PromiseToResultConverterConfig Configuration for the convertor (optional, default{logErrors:false}
) -
errorConstructor
function (err: any): E? Transforms one type of error into another
Runs an async function that will always resolve to a Result and runs the proper handler with the resulting value.
-
_function
function (): Promise<Result<Option<T>, E>> The function to be run -
handlers
SafeAsyncExecHandlers<T, E, F> The handlers for the execution result
Returns Promise<(F | void)>
Delays the execution of a function
by the provided delay and returns a cleanup
function to clean the timeout later.
The delay is 0
by default.
N.B. This is to be used for
calling asynchronous tasks in useEffect
s
that execute on mount.
-
_function
function (): void The function to be run -
delay
The delay after which the function is to be run (optional, default0
) -
onClean
CleanableExecOnCleanConfig? Handler configuration for cleanup
Wraps cleanable [execSafeAsync] in a {cleanableExec}
-
_function
function (): Promise<Result<Option<T>, E>> The function to be run -
handlers
CleanableSafeAsyncExecHandlers<T, E, F> The handlers for the execution result -
delay
number? The delay after which the function is to be run
It aborts if an external controller is provided and produces a new controller if not.
-
controller
AbortController?
Type: {logErrors: boolean}
-
logErrors
boolean
Determines whether the converter logs errors when the function being run throws.
Type: boolean
Type: {onFulfilled: function (value: T): void, onError: function (error: E): void?, onNone: function (): void?, _finally: function (): (F | void)?}
-
onFulfilled
function (value: T): void -
onError
function (error: E): void? -
onNone
function (): void? -
_finally
function (): (F | void)?
Runs if the execution resolved without errors, and it yields a non-empty value.
Type: function (value: T): void
Runs if the execution fails.
Type: function (error: E): void
Runs if the execution is resolved without errors, but it yields an empty value.
Type: function (): void
Runs after all other handlers without condition if provided.
Type: function (): (F | void)
Type: {handler: function (): void, cleanAfter: boolean?}
-
handler
function (): void -
cleanAfter
boolean?
The function to be appended.
Type: function (): void
Determines whether to run the function after
the timeout clean up. If true
it runs the
function after.
Type: boolean
Type: {onFulfilled: function (value: T): void, onError: function (error: E): void?, onNone: function (): void?, _finally: function (): (F | void)?, onClean: CleanableExecOnCleanConfig?}
-
onFulfilled
function (value: T): void -
onError
function (error: E): void? -
onNone
function (): void? -
_finally
function (): (F | void)? -
onClean
CleanableExecOnCleanConfig?
Runs if the execution resolved without errors, and it yields a non-empty value.
Type: function (value: T): void
Runs if the execution fails.
Type: function (error: E): void
Runs if the execution is resolved without errors, but it yields an empty value.
Type: function (): void
Runs after all other handlers without condition if provided.
Type: function (): (F | void)
Appended to the cleaner function that is returned from a cleanable execution.
Type: CleanableExecOnCleanConfig
Time before giving up on server discovery.
Type: number
Time before giving up on response from a server after a handshake.
Type: number