rpc-request

Simple wrapper of the node-fetch library in a class

Usage no npm install needed!

<script type="module">
  import rpcRequest from 'https://cdn.skypack.dev/rpc-request';
</script>

README

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

rpc-request is a simple wrapper of the node-fetch library in a class.

Installation

npm install rpc-request

Usage

rpc-request 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-request";
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 response = await myClass.fetch(path);
const data = await response.json();
  • fetch
import FetchClient from "rpc-request";
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-request";
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);