rpc-request
TypeScript icon, indicating that this package has built-in type declarations

8.0.0 • Public • Published

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 (based on undici) 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 by default
const transform = "json";
// 2. Base url for the `.fetch()` method
const base_url = new URL("http://worldtimeapi.org/");
// 3. Throws an error when `response.ok !== true`
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");

Readme

Keywords

Package Sidebar

Install

npm i rpc-request

Weekly Downloads

4,716

Version

8.0.0

License

MIT

Unpacked Size

21.9 kB

Total Files

6

Last publish

Collaborators

  • vansergen