0.0.8 • Published 2 years ago
@brainspore/transport v0.0.8
Network-transporter
A lightweight network communication utility library
- From Web services communication to real time communication to remote procedure calls, we got your back.
This utility library brings you a plug and play implementation to all known communication services inluding;
RESTful Clients RESTful APIs
WebSockets WebSockets API
WebRTC Read about WebRTC
SOAP SOAP API Developer Guide
Message Ques Message Ques
SignalIR SignalIR
graphQL GraphQl
gRPC gRPC
RPC RPC
Installation
To install this utility run
npm i @brainspore/transportHow to use this utility
RESTful API
- Axios example
// auth.js
export class AuthService {
#token;
#user;
/*
*isAuthenticated: boolean
*/
//If you don't call this method isAuthenticated, you must give it this alias how you
//prefer
isAuthenticated() {
return true;
}
//If you don't call this method getToken, you must give it this alias how you
//prefer
getToken() {
return this.#token;
}
//If you don't call this method logout, you must give it this alias how you
//prefer
async logout() {
session.clear();
}
}
// Util Index file for API service calls
// index.js
import { Request } from "@bs/transport";
import {apiBaseUrl} from "@environment" //update this path to point to your environment file
import {AuthService} from "auth.js";
export _request = new Request(new AuthService(), apiBaseUrl);
//OR
export _request = new Request(new AuthService(), apiBaseUrl, { timeout: 5000 });
// You can now import _request to any of your file.
// Using _request in your file
// customer.js
const postCustomer = (payload) => {
// start loader for UX
// This method is async. Supports both async and async and Promise
_request.axiosRequest({
url: 'customer',
method: 'POST',
data: payload,
})
.then((response) =>{
console.log(response);
})
.catch((err) =>{});
}
// Example of How environment file can look like:
// environment/index.js
export {
apiBaseUrl
} = process.env