šŸš€ Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more →
Socket
Sign inDemoInstall
Socket

rpc-request

Package Overview
Dependencies
Maintainers
1
Versions
41
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rpc-request

A simple wrapper of the Fetch API in a class

9.0.0
latest
Source
npm
Version published
Weekly downloads
4.7K
-41.98%
Maintainers
1
Weekly downloads
Ā 
Created
Source

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 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");

Keywords

rpc

FAQs

Package last updated on 04 May 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts