
Product
Socket Now Supports pylock.toml Files
Socket now supports pylock.toml, enabling secure, reproducible Python builds with advanced scanning and full alignment with PEP 751's new standard.
rpc-request
Advanced tools
rpc-request
is a simple wrapper of the Fetch API in a class.
npm install rpc-request
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");
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");
FAQs
A simple wrapper of the Fetch API in a class
The npm package rpc-request receives a total of 7,930 weekly downloads. As such, rpc-request popularity was classified as popular.
We found that rpc-request demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Product
Socket now supports pylock.toml, enabling secure, reproducible Python builds with advanced scanning and full alignment with PEP 751's new standard.
Security News
Research
Socket uncovered two npm packages that register hidden HTTP endpoints to delete all files on command.
Research
Security News
Malicious Ruby gems typosquat Fastlane plugins to steal Telegram bot tokens, messages, and files, exploiting demand after Vietnam’s Telegram ban.