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

undecim

v0.3.0

Published

Elegant APIs for undici

Downloads

5

Readme

undecim

Elegant APIs for undici.

Not ready for production

npm i undici undecim

Feature

  • Method shortcuts (.post(), .get(), etc.)
  • Body mixins helpers like fetch (.json(), .text()) before response
  • Throw error if status code is not ok (>= 200 and < 300)
  • Parse outgoing body (options.data) and set content-type header.
  • Instances with custom defaults

Installation

npm i undecim

Usage

import un, { create } from "undecim";
// CommonJS: const un = require("undecim").default;

// Set Accept header and get response as JSON shortcut
const json = await un
  .post("https://example.com", { data: { foo: "bar" } })
  .json();

// Set Accept header and get response as text shortcut
const text = await un
  .post("https://example.com", { data: { foo: "bar" } })
  .text();

// Retrieve response as it is
const response = await un.post("https://example.com", { data: { foo: "bar" } });
console.log(response.body); // body is of type Readable & BodyMixin
console.log(response.headers);
console.log(response.statusCode);
// body mixins also available here
await response.text();
await response.json();

// Create a new instance of undecim
const un = create(options);

APIs

un(url[, options])

Make a HTTP request to url with an optional options.

un.(url[, options])

There are methods provided as shortcuts to set HTTP methods.

  • un.get(url[, options])
  • un.post(url[, options])
  • un.put(url[, options])
  • un.delete(url[, options])
  • un.patch(url[, options])

options

The same with options in undici.request with following additions.

options.data

Set the approriate body for the request based on data. In some cases, content-type will be set automatically if not provided.

This can be one of the following:

  • string
  • URLSearchParams. Content Type will be application/x-www-form-urlencoded.
  • Buffer
  • other object. Content Type will be application/x-www-form-urlencoded.

create(defaults)

Create a new instance with specific defaults. These default will be merged into each of its requests' options.

If default.origin is defined, a Client is created. Requests made then should match the origin.

Error handling

Only difference in undecim is that it also throws HTTPStatusError when status code is not >= 200 and < 300.

Errors thrown by undici is also augmented with additional properties:

  • response (non-enumerable) the original response object (including body mixins). Only available if there is no undici error.
  • options: the request options
  • url: the request URL
import { UndecimError } from "undecim";

try {
  const response = await un("https://example.com");
  // or
  const response = await un("https://example.com").json();
} catch (err) {
  if (err instanceof UndecimError) {
    console.log(err.options);
    console.log(err.url);
    // Not always available
    if (err.response) {
      console.log(err.response.statusCode);
      console.log(err.response.headers);
      console.log(await err.response.text());
    }
  } else {
    // some other handling
  }
}

License

MIT