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

@tpp/req

v3.2.0

Published

A wrapper around XMLHttpRequest for Ajax

Downloads

4

Readme

Req

Make Ajax calls correctly with a thin wrapper around XMLHttpRequest and Node’s HTTP module.

Ideal for small requests and responses - especially JSON. Perfect for microservices.

Why?

We make ajax calls often but the mechanism (XMLHttpRequest) is not very intuitive to use. It is surprisingly hard to get it right across all browsers with proper error handling.

Even if we work out XMLHttpRequest, the event management for the backend Node’s HTTP module means we have to solve all the tricky bits there too.

Req wraps XMLHttpRequest and Node’s HTTP so that both use the same simple API in the most efficient manner possible.

Usage

Install:

$ npm install @tpp/req

Use:

const req = require("@tpp/req")

req.get(url, cb)
req.get(url, data, cb)

req.post(url, cb)
req.post(url, data, cb)

req.send({
  method: 'GET' | 'POST' | 'PUT' | ...
  url: ...,
  data: ...,
  timeout: ...,
  headers: ...,
}, cb)

Content-Type

If you don’t set a Content-Type header, Req will set:

  • application/json if the ‘data’ is a JSON object
  • application/x-www-form-urlencoded or multipart/form-data for FormData
  • text/plain for everything else (strings etc)

Callback

Req checks the response codes and error cases for you so the callback doesn’t have to. So the callback can be treated like a ‘normal’ async callback.

cb(err, resp)

The response contains the the http status code, the body of the response with data, and the HTTP response headers.

{
  status: 200,
  headers: headerMapFn(),
  body: {data: 1234}
}

For the response body itself, Req will try it's best to parse it as JSON. If it is unable to do so it will return an object with a single response field.

{
  response: <response string>
}

Note that resp can also be null if no response data was sent back.

Use the headers() method to parse and retrieve the header map:

function onResp(err, resp) {
  ...
  let headers = resp.headers()
  if(headers['content-type'] == "text/plain") {
    ...
  }
}

FAQ

Why not use the fetch api?

You can! It’s a great replacement for XMLHttpRequest on the browser. Req is if you want a tight, clean and simple callback that works well with JSON request/responses.

Plus it works both on the browser and in the backend which makes code sharing easier.

Why not use request, axis, superagent etc?

They are great libraries with support for a lot of stuff which we mostly don’t need. Req is tiny, focused and fast. For comparison:

| Library | Size | | ---------- | --------- | | request | 684.2kB | | axios | 13.4kB | | superagent | 32.8kB | | Req | 2.9kB |

What happens when the Server responds with a HTML error?

Sometimes servers are configured to send back full HTML pages as error responses (remember the “fail-whale”?). Many servers do this because they expect the requester to be a browser and so sending back HTML makes sense.

However, in a Microservice environment, we have found it is better to be able to see/log text rather than HTML. For this reason, when there are error HTML documents sent back, Req will parse them and extract out the message as a text.