# rpc-request

> A simple wrapper of the Fetch API in a class

Latest version **10.0.0** (published 2026-05-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install rpc-request
pnpm add rpc-request
yarn add rpc-request
bun add rpc-request
```

## Health

**Score 60/100 (C)** — status: active.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 10.0.0 |
| Published | 2026-05-25 |
| First published | 2019-07-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=24.16.0 |
| Dependencies | 0 |
| Unpacked size | 22.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Sergey Bakulin |
| Maintainers | vansergen |
| Keywords | rpc, fetch |

## Links

- npm: https://www.npmjs.com/package/rpc-request
- Repository: https://github.com/vansergen/rpc-request
- Homepage: https://github.com/vansergen/rpc-request#readme
- Issues: https://github.com/vansergen/rpc-request/issues
- npm.io page: https://npm.io/package/rpc-request

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 10.0.0 (latest) — 2026-05-25
- 9.0.0 — 2025-05-04
- 8.0.1 — 2025-05-04
- 8.0.0 — 2024-12-08
- 7.1.12 — 2024-12-08
- 7.1.11 — 2024-06-17
- 7.1.10 — 2024-03-15
- 7.1.9 — 2023-12-29
- 7.1.8 — 2023-11-13
- 7.1.7 — 2023-09-25
- 7.1.6 — 2023-09-21
- 7.1.5 — 2023-09-13
- 7.1.4 — 2023-08-12
- 7.1.3 — 2023-05-23
- 7.1.2 — 2023-02-14
- … 27 more at https://npm.io/package/rpc-request/versions

## README

# rpc-request ![CI Status](https://github.com/vansergen/rpc-request/workflows/CI/badge.svg) ![npm](https://img.shields.io/npm/v/rpc-request) [![Coverage Status](https://coveralls.io/repos/github/vansergen/rpc-request/badge.svg?branch=main)](https://coveralls.io/github/vansergen/rpc-request?branch=main) [![Known Vulnerabilities](https://snyk.io/test/github/vansergen/rpc-request/badge.svg)](https://snyk.io/test/github/vansergen/rpc-request) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier) [![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg)](CODE_OF_CONDUCT.md) [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release) [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org) [![NPM license](https://img.shields.io/npm/l/rpc-request)](LICENSE) ![node version](https://img.shields.io/node/v/rpc-request) ![npm downloads](https://img.shields.io/npm/dt/rpc-request) ![GitHub top language](https://img.shields.io/github/languages/top/vansergen/rpc-request)

`rpc-request` is a simple wrapper of the [Fetch API](https://nodejs.org/api/globals.html#fetch) in a class.

## Installation

```bash
npm install rpc-request
```

## Usage

The `Fetch` class accepts all parameters from `RequestInit` plus the following

```typescript
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

```typescript
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

```typescript
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`

```typescript
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`

```typescript
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`

```typescript
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`

```typescript
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`

```typescript
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`

```typescript
const base_url = "https://httpbin.org/";
const client = new Fetch({ transform: "json", base_url });
const response = await client.head("/anything");
```

- `options`

```typescript
const client = new Fetch({ base_url: "https://httpbin.org/" });
const response = await client.options("/anything");
```

---
_Source: https://npm.io/package/rpc-request · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
