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

micro-xhr

v1.0.5

Published

A tiny library for interfacing XMLHttpRequest through a promise

Downloads

9

Readme

NPM version Size Build Status Coverage Status Dependency Status Known Vulnerabilities

micro-xhr

A tiny barebones request library for browser environments. Performs only the most essential grunt work of calling XMLHttpRequest and returns a promise holding the future result. Further allows you to access the underlying XMLHttpRequest instance to perform lower level actions such as aborting the request.

Stringifies and parses JSON request payloads and responses automatically when required.

Installation

npm install micro-xhr

Usage

const xhr = require('micro-xhr');

const myRequest = xhr({
    url: 'https://domain.com',
    method: 'post',
    headers: {
        'X-My-Custom-Header': 'My custom value'
    },
    data: {
        firstName: 'John',
        lastName: 'Doe'
    }
})

myRequest.then(response => {
    /* Do something with response.data, response.headers or response.status */
}).catch(response => {
    /* Do something with response.data, response.headers or response.status */
});

myRequest.xhr.abort(); // abort the request

API: micro-xhr

function({url, method, headers, data}) -> result

Perform an HTTP(S) request to the given url, with the given method, headers and data.

arguments

  • requestObject (Object)
    • url (string): URL to perform the request to
    • method (string): HTTP method to use (defaults to GET)
    • headers (Object): Headers to use, keys are case insensitive and content-type defaults to application/json
    • data (any): Request payload to send. Automatically stringifies to JSON if the content-type header contains application/json.

returns

  • (Promise): Resolves when the request finalizes, except in case of a non-2XX status or on parse errors
    • - resolves to (Object) -
      • status (int): The received status code
      • headers (Object): Received headers, all keys are lowercase
      • data (string | any): Received response body, auto-parses JSON, else returns a string
    • - or rejects to (Object) -
      • status (int): The received status code, 0 if request failed
      • headers (Object): Received headers, all keys are lowercase
      • data (string | any)?: Received response body, auto-parses JSON, else returns a string
      • error (Error)?: Returned instead of data an exception occurs during parsing
        • rawBody (string): Unparsed response body
    • xhr (XmlHttpRequest): The underlying XMLHttpRequest

API: micro-xhr/xhr

function({url, method, headers, data}) -> result

The same as micro-xhr, except that the promise still resolves on a non-2XX status. Called internally by micro-xhr