5.0.1 • Published 2 years ago

rpc-bluebird v5.0.1

Weekly downloads
81
License
MIT
Repository
github
Last release
2 years ago

rpc-bluebird CI Status npm Coverage Status Known Vulnerabilities code style: prettier semantic-release Conventional Commits NPM license node version npm downloads GitHub top language

rpc-bluebird is a simple wrapper of the node-fetch library in a class. Note, that the Blubird promise library is used instead of native promises.

Installation

npm install rpc-bluebird

Usage

rpc-bluebird accepts the following parameters

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 for the full list of all supported options you can pass as fetchOptions.

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
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
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
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
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
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
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
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
const baseUrl = "https://httpbin.org/";
const client = new FetchClient<unknown>({}, { transform: "json", baseUrl });
client.options("/anything").then(console.log).catch(console.error);
5.0.1

2 years ago

5.0.0

2 years ago

4.0.0

2 years ago

3.0.3

3 years ago

3.0.2

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

5 years ago

1.0.0

5 years ago