rpc-bluebird
v5.0.1
Published
Simple wrapper of the node-fetch module in a class
Downloads
31
Readme
rpc-bluebird
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);