common-api-client
ApiClient holds the logic of interaction with other external services that are used in your application. It allows you to segregate API calls from main application logic.
ApiClient consists of ApiModules which collects all API calls and other logic of interaction with particular backend service.
Each ApiModule consist of four basic components:
store
- an object to store module state or data such as tokens, account data, etc.channels
- represents communication channels using protocols: http, ws, rpc, graphql, etc.strategies
- defines how to apply specific logic to channels (setup auth headers, add request params etc.). Uses hooks to bind to adapters in order to extract necessary data.adapters
- represents API of particular external service. Uses channels to communicate with them.
Also you can pass options
and dependencies
into ApiModules constructor settings
.
Install
Install on Node.JS with npm
$ npm install --save common-api-client
Usage
To create a new api-client:
const client = const api = clientapi
To use 'auth' module:
// uses JWTAuthModule by defaultawait apiauth
To extend then functionality of ApiClinet you should define new ApiModules, Adapters, Strategies and so on:
const JWTAuthModule = modules const store = {} const http = axiosconst rpc = 'ws://localhost:8080' const auth = channels: http adapters: auth: channels: http strategies: auth: store const account = channels: http rpc const messages = channels: rpc const client = modules: auth account messages const api = clientapi await apiauth // auth headers already setup to http requestawait apiaccount // account_id already binded to rpc callawait apimessages
License
MIT © Taras Panasyuk