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

xaja

v2.0.1

Published

Simple ajax lib

Downloads

6

Readme

Xaja

A simple ajax helper library.

Basic Usage

Interface is similar to jQuery's $.ajax, you can make a request:

const result = xaja.request('/some/url');
result.then(text => console.log(text));

// can also take a configuration object:
const result = xaja.request({
  url: '/some/other/url',
  method: 'GET',
  responseType: 'json',
}).then(json => {
  // do stuff with the json data
});

// alternatively using async/await
const json = await xaja.request('/some/json/data');
// do something with the result

Xaja abstracts over fetch, and will load an appropriate polyfill if there's no native implementation. NOTE: xaja does not load a Promise polyfill, if you're using browsers old enough to lack Promise support you'll need to load a polyfill before xaja. In the case of native fetch, you can get the raw response object:

const stream = xaja.request({
  url: '/some/stream/of/data',
  responseType: 'stream',
}).then(response => {
  if (response instanceof Response) {
    console.log('I am a raw Response object!');
  }
});

HTTP Error Codes

Unlike raw fetch, xaja will reject the returned Promise if the HTTP status code indicates an error. Any code not in the 200-399 range will cause a rejection.

Shorthand

In addition to the request function, xaja also offers get, post, and getJSON shorthand functions that invoke request with the appropriate configuration. They have the following signature:

(url:String, data:FormData|String|Object<optional>, options:Object<optional>) => Promise(Any)

Options Object

The options object supports the following parameters. Only the url parameter is required:

url:String

The url to be requested.

data:FormData|String|Object

Data to be passed along with the request. For POST requests it's passed as the body to the fetch call, for GET requests it is converted to a query string and appended to the url. Usernames and passwords are automatically stripped out and converted to an Authorization header.

method:String

HTTP method. Defaults to GET.

responseType:String

Expected response, when set to text or html automatically pulls the response text. For json it pulls the responsejson, etc. If set to stream or some unknown value you will get back the raw Response object. Defaults to text.

contentType:String

Mime type of the supplied data, e.g. application/x-www-form-urlencoded. Browsers are now pretty good at guessing this without any programmer input.

timeout:Number

Amount of time in milliseconds before the returned Promise rejects. Defaults to none.

cache:Boolean

If true the response's text/json/etc. will be cached in localStorage by the url, on subsequent requests if there is a timeout or error the Promise will resolve to the cached value rather than rejecting. Defaults to false.

headers:Headers|Object

The headers for the request.

credentials:String

The security setting for requests: e.g. same-origin.

Accessing the Request object

The promises returned by request et. al., if being used directly (rather than async/await) have an additional request property that contains the underlying Request object:

const promise = xaja.get('/some/url');
promise.request instanceof Request; // true