Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

backend-gateway

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

backend-gateway

Front-end helpers to communicate with APIs, based on Axios

  • 0.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

Backend Gateway

Front-end helper to communicate with APIs, based on Axios

Installation

Npm

npm install backend-gateway

Yarn

yarn install backend-gateway

Initialize the gateway

To start a communication with the API we need to define the base url.

import { Backend } from "backend-gateway";

// somewhere in the code
Backend.init({
    config: {
      baseURL: `https://api.mylovelyapp.dev`,
    },
});

init function properties:

PropertyTypeDescription
configAxiosRequestConfig - exported by axiosglobal configuration of axios instance
errorMessageExtractor(error?: T) => string | Array | undefinedThe logic to extract the error message from the error response. By default we will look at the message property in the error response body

Access Token

In almost all APIs, some functionality is protected and requires an access token to consume it. Anywhere in the code you can use the following method to globally modify the HTTP request headers.

import { Backend } from "backend-gateway";

// update the authorization header and all future requests will be automatically signed
Backend.updateHeaders({
    Authorization: `Bearer ${access_token}`,
});

// clear authorization header
Backend.updateHeaders({
    Authorization: undefined,
});

You can call updateHeaders all around the app.

Make a request

The Backend class implements the singleton pattern. To get an instance and make a request use its static method getInstance.

Here is an example or request:

import { Backend } from "backend-gateway";

Backend.getInstance().call({
    config: {
        method: "get",
        url: "/user", // the Base url has already been configured
    },
    beforeStart: () => {
        // before start logic
        // example: start loading
    },
    successCallback: (response) => {
        // your success login
        // const user = response.data;
    },
    errorCallback: (error, errorMessage) => {
        // errorMessage: the value returned by errorMessageExtractor if set or the default `message` property of the response
        // your error logic
        // const errorData = error.response?.data;
        // const cancelled = Axios.isCancel(error);
    },
    finishCallback: () => {
		// on finish logic
        // example: stop loading
    },
});

Notes:

  • call is and asynchronous function.
  • call is a generic function, you can provide the type of the success response data and the type of the type of the error response data
    • Backend.getInstance().call<User[]>({})
    • Backend.getInstance().call<ProjectDetails, Error>({})

License

MIT © lambou

Keywords

FAQs

Package last updated on 18 Jan 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc