npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rpc-request

v8.0.0

Published

A simple wrapper of the Fetch API in a class (based on undici)

Downloads

22,507

Readme

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