npm.io
10.0.0 • Published 3 months ago

rpc-request

Licence
MIT
Version
10.0.0
Deps
0
Size
22 kB
Vulns
0
Weekly
0
Stars
1

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

rpc-request is a simple wrapper of the Fetch API in a class.

Installation

npm install rpc-request

Usage

The Fetch class accepts all parameters from RequestInit plus the following

import { Fetch } from "rpc-request";
// 1. Transform the response body. When omitted (the default) the raw
//    `Response` is returned and `reject` is ignored — the caller is then
//    responsible for inspecting `response.ok`.
const transform = "json";
// 2. Base url for the `.fetch()` method
const base_url = new URL("http://worldtimeapi.org/");
// 3. Throws an `UnsuccessfulFetch` when `response.ok !== true`. Only takes
//    effect when `transform` is set — without a transform the raw `Response`
//    is returned regardless of status.
const reject = true;
// Plus anything from `RequestInit`
const headers = { "X-TOKEN": "123" };
const client = new Fetch({ transform, base_url, reject, headers });
const response = await client.get("/api/ip");

One can easily extend it

import { Fetch } from "rpc-request";
interface IResponse1 {
  bar: "bar";
}
interface IResponse2 {
  foo: "foo";
}
class CustomFetch extends Fetch {
  public constructor() {
    super({
      transform: "json",
      base_url: new URL("http://www.example.com/api/v1/"),
    });
  }
  public post<T = unknown>(
    path: string,
    body: Record<string, unknown> = {},
  ): Promise<T> {
    return super.post<T>(path, {
      body: JSON.stringify(body),
      headers: { "Content-Type": "application/json" },
    });
  }
  public getFoo(): Promise<IResponse1> {
    return this.get<IResponse1>("/get");
  }
  public getBar(id: string): Promise<IResponse2> {
    return this.post<IResponse2>("/post", { id });
  }
}
  • fetch

The basic method

import { Fetch } from "rpc-request";
interface Ip {
  client_ip: string;
  timezone: string;
}
const base_url = new URL("http://worldtimeapi.org/api/");
const client = new Fetch({ base_url, transform: "json" });
const { client_ip, timezone } = await client.fetch<Ip>("ip");
HTTP methods.
  • get
interface Info {
  data: string;
  headers: Record<string, string | undefined>;
}
const base_url = "https://httpbin.org/";
const client = new Fetch({ transform: "json", base_url });
const { data, headers } = await client.get<Info>("anything");
  • post
const base_url = "https://httpbin.org/";
const client = new Fetch({
  base_url,
  body: JSON.stringify({ data: "Hello World!" }),
  transform: "text",
});
const string = await client.post<string>("anything");
console.log(typeof string === "string");
  • put
const base_url = "https://httpbin.org/";
const client = new Fetch({
  base_url,
  body: JSON.stringify({ data: "Hello World!" }),
  transform: "buffer",
  reject: true,
});
const buffer = await client.put<ArrayBuffer>("anything");
console.log(buffer instanceof ArrayBuffer);
  • patch
import { Blob } from "node:buffer";
const base_url = "https://httpbin.org/";
const client = new Fetch({
  base_url,
  body: JSON.stringify({ data: "Hello World!" }),
  transform: "blob",
});
const blob = await client.patch("anything");
console.log(blob instanceof Blob);
  • delete
const base_url = "https://httpbin.org/";
const client = new Fetch({
  base_url,
  body: JSON.stringify({ data: "Hello World!" }),
  transform: "buffer",
  reject: true,
});
const buffer = await client.delete<Buffer>("anything");
console.log(buffer instanceof Buffer);
  • head
const base_url = "https://httpbin.org/";
const client = new Fetch({ transform: "json", base_url });
const response = await client.head("/anything");
  • options
const client = new Fetch({ base_url: "https://httpbin.org/" });
const response = await client.options("/anything");

Keywords