axiosbridge v1.1.0
axiosbridge
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."),
}
))();
API
Table of Contents
- 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
Bridge
A wrapper for an axios instance that converts the returned Promises in to Results when a request is made.
Parameters
config
CreateAxiosDefaults? The axios create defaults.axios
AxiosStatic? The axios default exported object.
axios_instance
Main axios instance to be used for all requests
Type: AxiosInstance
connection_timeout
Time before giving up on server discovery.
get
Request fetch
Parameters
route
stringconfig
AxiosRequestConfig?
Returns Promise<Result<Option\, E>>
post
Request create
Parameters
route
stringdata
Tconfig
AxiosRequestConfig?
Returns Promise<Result<Option\, E>>
patch
Request patch
Parameters
route
stringdata
T?config
AxiosRequestConfig?
Returns Promise<Result<Option\, E>>
put
Request update
Parameters
route
stringdata
T?config
AxiosRequestConfig?
Returns Promise<Result<Option\, E>>
delete
Request delete
Parameters
route
stringconfig
AxiosRequestConfig?
Returns Promise<Result<Option\, E>>
setErrorConstructor
Sets the error constructor.
Parameters
errorConstructor
function (err: any): E New error constructor
convertPromiseToResult
Runs the provided function and if it throws an error it catches and logs then it converts Promise to a Result before returning it.
Parameters
_function
function (...args: any): Promise\ The function to be runonReject
function (err: any): E? Runs if the function throws an erroronFulfill
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
Returns Promise<Result\<T, E>>
execSafeAsync
Runs an async function that will always resolve to a Result and runs the proper handler with the resulting value.
Parameters
_function
function (): Promise<Result<Option\, E>> The function to be runhandlers
SafeAsyncExecHandlers\<T, E, F> The handlers for the execution result
Returns Promise<(F | void)>
cleanableExec
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.
Parameters
_function
function (): void The function to be rundelay
The delay after which the function is to be run (optional, default0
)onClean
CleanableExecOnCleanConfig? Handler configuration for cleanup
cleanableSafeAsyncExec
Wraps cleanable [execSafeAsync] in a {cleanableExec}
Parameters
_function
function (): Promise<Result<Option\, E>> The function to be runhandlers
CleanableSafeAsyncExecHandlers\<T, E, F> The handlers for the execution resultdelay
number? The delay after which the function is to be run
processAbortController
It aborts if an external controller is provided and produces a new controller if not.
Parameters
controller
AbortController?
PromiseToResultConverterConfig
Type: {logErrors: boolean}
Properties
logErrors
boolean
logErrors
Determines whether the converter logs errors when the function being run throws.
Type: boolean
SafeAsyncExecHandlers
Type: {onFulfilled: function (value: T): void, onError: function (error: E): void?, onNone: function (): void?, _finally: function (): (F | void)?}
Properties
onFulfilled
function (value: T): voidonError
function (error: E): void?onNone
function (): void?_finally
function (): (F | void)?
onFulfilled
Runs if the execution resolved without errors, and it yields a non-empty value.
Type: function (value: T): void
onError
Runs if the execution fails.
Type: function (error: E): void
onNone
Runs if the execution is resolved without errors, but it yields an empty value.
Type: function (): void
_finally
Runs after all other handlers without condition if provided.
Type: function (): (F | void)
CleanableExecOnCleanConfig
Type: {handler: function (): void, cleanAfter: boolean?}
Properties
handler
function (): voidcleanAfter
boolean?
handler
The function to be appended.
Type: function (): void
cleanAfter
Determines whether to run the function after
the timeout clean up. If true
it runs the
function after.
Type: boolean
CleanableSafeAsyncExecHandlers
Type: {onFulfilled: function (value: T): void, onError: function (error: E): void?, onNone: function (): void?, _finally: function (): (F | void)?, onClean: CleanableExecOnCleanConfig?}
Properties
onFulfilled
function (value: T): voidonError
function (error: E): void?onNone
function (): void?_finally
function (): (F | void)?onClean
CleanableExecOnCleanConfig?
onFulfilled
Runs if the execution resolved without errors, and it yields a non-empty value.
Type: function (value: T): void
onError
Runs if the execution fails.
Type: function (error: E): void
onNone
Runs if the execution is resolved without errors, but it yields an empty value.
Type: function (): void
_finally
Runs after all other handlers without condition if provided.
Type: function (): (F | void)
onClean
Appended to the cleaner function that is returned from a cleanable execution.
Type: CleanableExecOnCleanConfig
DEFAULT_CONNECTION_TIME_OUT
Time before giving up on server discovery.
Type: number
DEFAULT_REQUEST_TIME_OUT
Time before giving up on response from a server after a handshake.
Type: number
Option
Result
AbortController
AxiosInstance
CreateAxiosDefaults
AxiosRequestConfig
AxiosResponse
AxiosStatic
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago