# @capsulajs/capsulajs-transport-providers

> Impements Dispatcher-like transport providers for HTTP and WebSocket protocols.

Latest version **0.1.2** (published 2022-05-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install @capsulajs/capsulajs-transport-providers
pnpm add @capsulajs/capsulajs-transport-providers
yarn add @capsulajs/capsulajs-transport-providers
bun add @capsulajs/capsulajs-transport-providers
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.2 |
| Published | 2022-05-16 |
| First published | 2019-01-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 22.3 KB |
| Known vulnerabilities | 0 (+24 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | CapsulaJS |
| Maintainers | idanilt, capsulajsci, mikesquall, 95kach, capsulajsbot |
| Keywords | microservice, micro-frontend, capsula |

## Links

- npm: https://www.npmjs.com/package/@capsulajs/capsulajs-transport-providers
- Repository: https://github.com/capsulajs/capsulajs-transport-providers
- Homepage: https://github.com/capsulajs/capsulajs-transport-providers#readme
- Issues: https://github.com/capsulajs/capsulajs-transport-providers/issues
- npm.io page: https://npm.io/package/@capsulajs/capsulajs-transport-providers

## Dependencies (2)

- [axios](https://npm.io/package/axios.md) ^0.27.2
- [typescript](https://npm.io/package/typescript.md) ^4.6.4

## Recent versions

- 0.1.2 (latest) — 2022-05-16
- 0.1.1 — 2019-01-29
- 0.1.0 — 2019-01-28

## README

# CapsulaJS Transport Providers

Impements Dispatcher-like transport providers for HTTP and WebSocket protocols.

## Base class:
```javascript
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&lt;T, R&gt; (request: T, api: string): Promise&lt;R&gt; - dispatching method,
simply dispatches a Request, returns a Promise with the Response (or a rejected one with an Error)

finalize?(): Promise&lt;null&gt; - 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.

```javascript
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

---
_Source: https://npm.io/package/@capsulajs/capsulajs-transport-providers · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
