# conflux-web-providers

> ConfluxWeb module to handle requests to external providers.

Latest version **1.36.7-alpha.0** (published 2020-04-01) · LGPL-3.0 license · 0 weekly downloads

## Install

```sh
npm install conflux-web-providers
pnpm add conflux-web-providers
yarn add conflux-web-providers
bun add conflux-web-providers
```

## Health

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

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

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.36.7-alpha.0 |
| Published | 2020-04-01 |
| First published | 2019-04-09 |
| Weekly downloads | 0 |
| License | LGPL-3.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 7 |
| Unpacked size | 177.9 KB |
| Known vulnerabilities | 0 (+7 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 7 |
| Maintainers | geekberry, liuis, ypliu |

## Links

- npm: https://www.npmjs.com/package/conflux-web-providers
- Repository: https://github.com/Conflux-Chain/ConfluxWeb/tree/master/packages/conflux-web-providers
- npm.io page: https://npm.io/package/conflux-web-providers

## Dependencies (7)

- [lodash](https://npm.io/package/lodash.md) ^4.17.11
- [url-parse](https://npm.io/package/url-parse.md) 1.4.4
- [websocket](https://npm.io/package/websocket.md) ^1.0.28
- [@types/node](https://npm.io/package/@types/node.md) ^10.12.18
- [xhr2-cookies](https://npm.io/package/xhr2-cookies.md) 1.1.0
- [eventemitter3](https://npm.io/package/eventemitter3.md) 3.1.0
- [@babel/runtime](https://npm.io/package/@babel/runtime.md) ^7.3.1

## Recent versions

- 1.36.7-alpha.0 (latest) — 2020-04-01
- 1.36.6-alpha.0 — 2020-04-01
- 1.36.5-alpha.0 — 2020-04-01
- 1.36.4-alpha.0 — 2020-04-01
- 1.36.3-alpha.0 — 2020-04-01
- 1.36.2-alpha.0 — 2020-04-01
- 1.36.1-alpha.0 — 2020-04-01
- 1.36.0-alpha.0 — 2020-03-31
- 1.35.1-alpha.0 — 2020-03-06
- 1.35.0 — 2020-01-07
- 0.1.35-alpha.2 — 2019-11-05
- 0.1.35-alpha.1 — 2019-10-28
- 0.1.35-alpha.0 — 2019-10-28
- 0.1.34 — 2019-10-17
- 0.1.33 — 2019-10-17
- … 24 more at https://npm.io/package/conflux-web-providers/versions

## README

# conflux-web-providers

This is a sub package of [ConfluxWeb.js][repo]

## Installation

```bash
npm install conflux-web-providers
```

## Usage Examples

#### HttpProvider
You can pass with the options object the timeout and all known HTTP headers. 

```js 
import {HttpProvider} from 'conflux-web-providers';

const options = {
    timeout: 20000,
    headers: [
        {
            name: 'Access-Control-Allow-Origin', value: '*'
        },
        ...
    ]
};

const httpProvider = new HttpProvider('http://localhost:8545', options); 
```

#### WebsocketProvider

Instead of setting a authorization header you could also define the credentials over the URL with:
```ws://username:password@localhost:8546```

```js 
import {WebsocketProvider} from 'conflux-web-providers';
const options = { 
    timeout: 30000, 
    headers: {
        authorization: 'Basic username:password'
    }
};
 
const websocketProvider = new WebsocketProvider('ws://localhost:8546', options);
```

#### IpcProvider
```js 
import {IpcProvider} from 'conflux-web-providers';
import net from 'net';

const ipcProvider = new IpcProvider('/Users/me/Library/Conflux/geth.ipc', net);
```

#### BatchRequest
The BatchRequest provides the possibility to send JSON-RPC requests as batch.

```js 
import {ProviderResolver, BatchRequest} 'conflux-web-providers';

const provider = new ProviderResolver().resolve('ws://localhost:8546');
const batchRequest = new BatchRequest(provider);

batchRequest.add(confluxWeb.cfx.getBalance.request(
    '0x0000000000000000000000000000000000000000',
    'latest_state',
    callback
));

await batchRequest.execute();
```

#### ProviderDetector
Checks if an provider is given from the environment (Mist, MetaMask) and returns the provider.

```js
import {ProviderDetector} from 'conflux-web-providers';

const givenProvider = ProviderDetector.detect();
```

#### ProviderResolver
The ProviderResolver resolves an url or an given provider object to the correct provider class. 
Because of the resolves does conflux-web has internally just one provider interface and we have no direct dependency to third party providers.

```js 
import {ProviderResolver} 'conflux-web-providers';

const socketProviderAdapter = new ProviderResolver().resolve('ws://localhost:8546');
```

## Types 

All the typescript typings are placed in the types folder. 

[repo]: https://github.com/Conflux-Chain/ConfluxWeb

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