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

xr

v0.3.2

Published

Ultra-simple wrapper around XMLHttpRequest.

Downloads

1,764

Readme

xr

Really simple wrapper around XHR that provides a few bits of nice functionality, exposes the XHR object wherever relevant, and returns an ES6 Promise (or whatever Promise is set to globally, if you want to use something else).

The idea was to make a pragmatic library that's pre-configured for the 90% use case, but override-able for anyone that wants to do anything a bit off the beaten track.

For instance, the library is by default set up to send/receive JSON (with the associated headers and parser/dumper already set up), but if you wanted to use something like XML, it's easy enough to override that with a few lines.

It's lightweight, has no dependencies (other than having either Promise in the global namespace or provided via xr.config), and adds pretty much no overhead over the standard XHR API.

Install

npm install xr --save

Quickstart

import xr from 'xr';

const res = await xr.get('/api/items', { take: 5 });
logger.log(res.data);

const res = await xr.post('/api/item', { name: 'hello' });
logger.log('new item', res.data);

Extended syntax:

import xr from 'xr';

xr({
  method: xr.Methods.GET,
  url: '/api/items',
  params: {take: 5},
  events: {
    [xr.Events.PROGRESS]: (xhr, xhrProgressEvent) => {
      logger.log("xhr", xhr);
      logger.log("progress", xhrProgressEvent);
    },
  },
});

Custom promise:

xr.get('/url', {}, {
  promise: fn => new myPromiseClass(fn),
});

Raw mode (data is not dumped/loaded):

xr.put('/url', 'some data', {
  raw: true,
});

Custom dump/load:

xr.post('/url', { 'some': 'data' }, {
  dump: data => msgpack.encode(data),
  load: data => msgpack.decode(data),
});

Global configuration

One thing that I've always found irritating with libraries it that if you want to override the defaults, you have to do it per-request, or wrap the libraries.

With XR, this is simple, as you can globally configure the module for your project.

xr.configure({
  promise: fn => new myPromise(fn),
});

API is simple, for now consult source.

Features

  • Returns ES6 promises.
  • Has query parameter generation.
  • Supports events.

Alias Methods

You can do some quick aliases to requests, for instance:

xr.get('/my-url');

Requirements

There must be a polyfill or browser that supports at least the standard ES6 promise API (xr will use whatever's there).

License

See LICENSE