@capsulajs/capsulajs-transport-providers
v0.1.2
Published
Impements Dispatcher-like transport providers for HTTP and WebSocket protocols.
Downloads
9
Readme
CapsulaJS Transport Providers
Impements Dispatcher-like transport providers for HTTP and WebSocket protocols.
Base class:
export abstract class Dispatcher {
// Base URL
protected baseUrl: string;
// Base Constructor
constructor(baseUrl: string) {
this.baseUrl = baseUrl;
};
// Interface for the Dispatcher
abstract dispatch<T, R>(api: string, request: T): Promise<R>;
// Optional Destructor-like method
finalize?(): Promise<void>;
};
baseUrl - base URL of the service, being set by Constructor
constructor(baseUrl: string) - base constructor, sets the baseUrl, call it in the Constructor of any subclasses
dispatch<T, R> (request: T, api: string): Promise<R> - dispatching method, simply dispatches a Request, returns a Promise with the Response (or a rejected one with an Error)
finalize?(): Promise<null> - optional method, implement it if a subclass needs to free resources
AxiosDispatcher and WebSocketDispatcher
Inherits from base Dispatcher class. Pass a base URL to its Constructor, then call the dispatch method, passing an end-point and a body of a request.
import { AxiosDispatcher } from '@capsulajs/capsulajs-transport-providers';
const dispatcher = new AxiosDispatcher('http://my-server.com');
dispatcher.dispatch('/end-point', request)
.then(res => {
// Process the response
})
.catch(err => {
// Process the error
});
WebSocketDispatcher's usage is the same