@sliit-foss/service-connector
v2.3.0
Published
A package to isolate filters and sorts from a given request's query parameters
Downloads
83
Maintainers
Readme
@sliit-foss/service-connector
A microservice connector for node.js that uses axios under the hood and makes the process of debugging and logging API requests easier.
Installation
# using npm
npm install @sliit-foss/service-connector
# using yarn
yarn add @sliit-foss/service-connector
Usage
# using require
const serviceConnector = require("@sliit-foss/service-connector").default;
# using import
import serviceConnector from "@sliit-foss/service-connector";
Example
const connector = serviceConnector({
// any configuration you want to pass to axios
baseUrl: "http://localhost:3000",
timeout: 1000,
// optional module specific configurations
service: "service-name",
headerIntercepts: () => ({
"x-api-key": "1234567890"
}),
loggable: (response) => {
// do something with the response, both success and error
}
});
// use the service connector as you would use axios
const response = await connector.get("/api/v1/users");
Resolver function
The connector has a resolver function that can be used to directly resolve the data from the API response if it comes in the following format.
{ "data": { ... }, // data coming from the API inside a nested data object "message": "Data retrieved successfully" }
The following will return the above data object directly without the need of calling response.data.data manually.
const data = await connector.get("/api/v1/users").then(connector.resolve);