# rpc-bluebird

> Simple wrapper of the node-fetch module in a class

Latest version **5.0.1** (published 2022-06-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install rpc-bluebird
pnpm add rpc-bluebird
yarn add rpc-bluebird
bun add rpc-bluebird
```

## 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 | 5.0.1 |
| Published | 2022-06-24 |
| First published | 2019-11-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=16.15.0 |
| Dependencies | 2 |
| Unpacked size | 17.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Sergey Bakulin |
| Maintainers | vansergen |
| Keywords | rpc, fetch, bluebird |

## Links

- npm: https://www.npmjs.com/package/rpc-bluebird
- Repository: https://github.com/vansergen/rpc-bluebird
- Homepage: https://github.com/vansergen/rpc-bluebird#readme
- Issues: https://github.com/vansergen/rpc-bluebird/issues
- Funding: https://commerce.coinbase.com/checkout/0ddad386-6902-48d8-8026-47723a0203e6
- npm.io page: https://npm.io/package/rpc-bluebird

## Dependencies (2)

- [bluebird](https://npm.io/package/bluebird.md) ^3.7.2
- [node-fetch](https://npm.io/package/node-fetch.md) ^3.2.6

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 5.0.1 (latest) — 2022-06-24
- 5.0.0 — 2022-05-15
- 4.0.0 — 2022-05-15
- 3.0.3 — 2021-03-07
- 3.0.2 — 2021-02-14
- 3.0.1 — 2020-12-20
- 3.0.0 — 2020-12-12
- 2.0.3 — 2020-08-10
- 2.0.2 — 2020-03-21
- 2.0.1 — 2020-03-20
- 2.0.0 — 2020-01-22
- 1.0.5 — 2019-12-18
- 1.0.4 — 2019-12-01
- 1.0.3 — 2019-11-29
- 1.0.2 — 2019-11-19
- … 2 more at https://npm.io/package/rpc-bluebird/versions

## README

# rpc-bluebird ![CI Status](https://github.com/vansergen/rpc-bluebird/workflows/CI/badge.svg) ![npm](https://img.shields.io/npm/v/rpc-bluebird) [![Coverage Status](https://coveralls.io/repos/github/vansergen/rpc-bluebird/badge.svg?branch=main)](https://coveralls.io/github/vansergen/rpc-bluebird?branch=main) [![Known Vulnerabilities](https://snyk.io/test/github/vansergen/rpc-bluebird/badge.svg)](https://snyk.io/test/github/vansergen/rpc-bluebird) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier) [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release) [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org) ![NPM license](https://img.shields.io/npm/l/rpc-bluebird) ![node version](https://img.shields.io/node/v/rpc-bluebird) ![npm downloads](https://img.shields.io/npm/dt/rpc-bluebird) ![GitHub top language](https://img.shields.io/github/languages/top/vansergen/rpc-bluebird)

`rpc-bluebird` is a simple wrapper of the [`node-fetch`](https://github.com/node-fetch/node-fetch) library in a class. Note, that the [`Blubird`](https://github.com/petkaantonov/bluebird/) promise library is used instead of native promises.

## Installation

```bash
npm install rpc-bluebird
```

## Usage

`rpc-bluebird` accepts the following parameters

```typescript
const fetchClientOptions = {
  baseUrl: "http://worldtimeapi.org/", // used to resolve the url - `new URL(path, baseUrl)`
  rejectNotOk: false, // default - `true`, reject a promise on non 2xx responses
  transform: "text", // default - `'raw'`
};
```

as `fetchClientOptions`. Please refer to the [`node-fetch` documentation](https://github.com/node-fetch/node-fetch#options) for the full list of all supported options you can pass as `fetchOptions`.

```typescript
import { FetchClient } from "rpc-bluebird";
const fetchOptions = { headers: { "User-Agent": "MyClass-User-Agent" } };
const fetchClientOptions = { baseUrl: "http://worldtimeapi.org/" };
const path = "/api/ip";
class MyClass extends FetchClient {
  constructor() {
    super(fetchOptions, fetchClientOptions);
  }
}
const myClass = new MyClass();
const bluebirdPromise = myClass.fetch(path);
bluebirdPromise
  .then((response) => {
    console.info(bluebirdPromise.isResolved());
    response.json().then(console.log).catch(console.error);
  })
  .catch(console.error);
console.info(bluebirdPromise.isPending());
```

- `fetch`

```typescript
import FetchClient from "rpc-bluebird";
const client = new FetchClient<unknown>({}, { transform: "json" });
client
  .fetch("http://worldtimeapi.org/api/ip")
  .then(console.log)
  .catch(console.error);
```

### HTTP methods.

- `get`

```typescript
const client = new FetchClient<Buffer>({}, { transform: "buffer" });
client
  .get("http://worldtimeapi.org/api/ip")
  .then((data) => {
    console.log(data instanceof Buffer);
  })
  .catch(console.error);
```

- `post`

```typescript
const client = new FetchClient<Buffer>(
  { body: JSON.stringify({ data: "Hello World!" }) },
  { transform: "buffer" }
);
client
  .post("https://httpbin.org/anything")
  .then((data) => {
    console.log(data instanceof Buffer);
  })
  .catch(console.error);
```

- `put`

```typescript
const client = new FetchClient<ArrayBuffer>(
  { body: JSON.stringify({ data: "Hello World!" }) },
  { transform: "arrayBuffer" }
);
client
  .put("https://httpbin.org/anything")
  .then((data) => {
    console.log(data instanceof ArrayBuffer);
  })
  .catch(console.error);
```

- `patch`

```typescript
import Blob from "fetch-blob";
const client = new FetchClient<Blob>(
  { body: JSON.stringify({ data: "Hello World!" }) },
  { transform: "blob", rejectNotOk: true }
);
client
  .patch("https://httpbin.org/anything")
  .then((data) => {
    console.log(data instanceof Blob);
  })
  .catch(console.error);
```

- `delete`

```typescript
const baseUrl = "https://httpbin.org/";
const client = new FetchClient<string>({ transform: "text", baseUrl });
client
  .delete("/anything")
  .then((data) => {
    console.log(typeof data === "string");
  })
  .catch(console.error);
```

- `head`

```typescript
import { UnsuccessfulFetch, FetchClient } from "rpc-bluebird";
const baseUrl = "http://worldtimeapi.org/";
const client = new FetchClient<unknown>({}, { transform: "json", baseUrl });
client
  .head("/badurl")
  .then(console.log)
  .catch((error) => {
    if (error instanceof UnsuccessfulFetch) {
      console.log(error.response.status); // 404
    } else {
      console.error(error);
    }
  });
```

- `options`

```typescript
const baseUrl = "https://httpbin.org/";
const client = new FetchClient<unknown>({}, { transform: "json", baseUrl });
client.options("/anything").then(console.log).catch(console.error);
```

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