axiosbridge
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

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

A wrapper for an axios instance that converts the returned Promises in to Results when a request is made.

Parameters

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

Returns Promise<Result<Option<T>, E>>

post

Request create

Parameters

Returns Promise<Result<Option<H>, E>>

patch

Request patch

Parameters

Returns Promise<Result<Option<H>, E>>

put

Request update

Parameters

Returns Promise<Result<Option<H>, E>>

delete

Request delete

Parameters

Returns Promise<Result<Option<T>, 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<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

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

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 useEffects that execute on mount.

Parameters

  • _function function (): void The function to be run
  • delay The delay after which the function is to be run (optional, default 0)
  • onClean CleanableExecOnCleanConfig? Handler configuration for cleanup

cleanableSafeAsyncExec

Wraps cleanable [execSafeAsync] in a {cleanableExec}

Parameters

processAbortController

It aborts if an external controller is provided and produces a new controller if not.

Parameters

PromiseToResultConverterConfig

Type: {logErrors: boolean}

Properties

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): void
  • onError 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 (): void
  • cleanAfter 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): void
  • onError 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

Dependents (0)

Package Sidebar

Install

npm i axiosbridge

Weekly Downloads

0

Version

1.1.0

License

MIT

Unpacked Size

34 kB

Total Files

11

Last publish

Collaborators

  • the_c1oud