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

@clickup/rest-client

v2.11.0

Published

A syntax sugar tool around Node fetch() API, tailored to work with TypeScript and response validators

Downloads

30,819

Readme

rest-client: a syntax sugar tool around Node fetch() API, tailored to work with typescript-is or superstruct validators

See also Full API documentation.

CI run

Examples

In the example below we use superstruct library to build a strongly typed response validator. Validator is just a function passed to json() method: it must throw if the argument passed doesn't match what it expects the value to be.

import { array, number, object, string } from "superstruct";
import RestClient from "rest-client";

// Initialized once. See RestOptions for lots of other options.
const client = new RestClient()
  .withOptions({
    timeoutMs: 1000,
    logger: (_event) => {
      /* myLogger.log(event); */
    },
  })
  .withBase("https://reqres.in");

async function main() {
  // Send individual request using superstruct as a validation backend.
  const res = await client
    .get("/api/users", { page: 2 })
    .setDebug()
    .json(
      object({
        total: number(),
        data: array(
          object({
            id: number(),
            email: string(),
          })
        ),
      })
    );
  console.log("Masked and validated response: ", res);
  // Notice that `res` is strongly typed! You won't make a typo.
  console.log("Here is a TS strongly-typed field:", res.data[0].email);
}

main().catch((e) => console.error(e));

The above script prints the following:

+++ GET https://reqres.in/api/users?page=2
+++ Accept: application/json
=== HTTP 200 (took 105 ms)
=== { data:
===    [ { id: 7,
===        email: '[email protected]',
===        first_name: 'Michael',
===        last_name: 'Lawson',
===        avatar: 'https://reqres.in/img/faces/7-image.jpg' },
===      { id: 8,
===        email: '[email protected]',
===        first_name: 'Lindsay',
===        last_name: 'Ferguson',
===        avatar: 'https://reqres.in/img/faces/8-image.jpg' },
===      { id: 9,
===        email: '[email protected]',
===        first_name: 'Tobias',
===        last_name: 'Funke',
===        avatar: 'https://reqres.in/img/faces/9-image.jpg' },
===      { id: 10,
===        email: '[email protected]',
===        first_name: 'Byron',
===        last_name: 'Fields',
===        avatar: 'https://reqres.in/img/faces/10-image.jpg' },
===      { id: 11,
===        email: '[email protected]',
===        first_name: 'George',
===        last_name: 'Edwards',
===        avatar: 'https://reqres.in/img/faces/11-image.jpg' },
===      { id: 12,
===        email: '[email protected]',
===        first_name: 'Rachel',
===        last_name: 'Howell',
===        avatar: 'https://reqres.in/img/faces/12-image.jpg' } ],
===   page: 2,
===   per_page: 6,
===   support:
===    { url: 'https://reqres.in/#support-heading',
===      text: 'To keep ReqRes free, contributions!' },
===   total: 12,
===   total_pages: 2 }

Masked and validated response:  {
  total: 12,
  data: [
    { id: 7, email: '[email protected]' },
    { id: 8, email: '[email protected]' },
    { id: 9, email: '[email protected]' },
    { id: 10, email: '[email protected]' },
    { id: 11, email: '[email protected]' },
    { id: 12, email: '[email protected]' }
  ]
}
Here is a TS strongly-typed field: [email protected]

If the API response doesn't match the expected shape (e.g. we expect "other_field" to be presented too), superstruct will throw a descriptive message:

StructError: At path: data.0.other_field -- Expected a number, but received: undefined

Validation Backends

You can use one of the following libraries to type-enforce the response: