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

fajax

v0.5.0

Published

A tiny wrapper around XMLHttpRequest.

Downloads

3

Readme

fajax

A tiny wrapper around XMLHttpRequest.

Browser support

It is tested in the following browsers:

  • Chrome v. 23
  • Firefox v. 17
  • Opera v. 12.11
  • Safari v. 6
  • IE v. 9
  • IE v. 8

It requires an ES6 compatible Promise implementation. You can shim this with either es6-shim or es6-promise. The Promise constructor should be available in global scope.

Note for IE8 and below: You need to supply a shim for Function.prototype.bind.

It does not work in IE7 or below out of the box when receiving application/json, because JSON.parse is not supported. If this is a requirement, then use something like json2 from Douglas Crockford. Everything else seems to work.

Examples

Using this could not be more simple:

var fajax = require('fajax')
fajax.defaults({
    baseUrl: 'http://my-server.com',
    accept: 'application/json'
})
fajax.get('/some-resource').then(function(xhr) {
    // if the server returns Content-Type: application/json,
    // this is already parsed for us
    var jsonBody = xhr.body
})

// Sometime later
fajax.post('/some-action', { json: jsonBody })
    .then(function(xhr) {
        // Report success
    },
    // The promise is rejected if the status code is 400 or higher
    function(xhr) {
        // Handle validation error or the like
    })

API

Include this by calling var fajax = require('fajax'). This require that you use a build-tool that is compatible with CommonJS, such as browserify or webpack.

The returned function have two static methods:

  1. fajax.qs(queryStringCreator): Assigns a specific query-string creator function. This is used when form is specified in the options.

    For example, it is simple to use the excellent qs library:

    // fajax and qs loaded via ender:
    $.ajax.qs($.stringify)
  2. fajax.defaults(newDefaults): Updates the defaults. Notice that this adds to the defaults, so

    fajax.defaults({ method: 'post' });
    fajax.defaults({ accept: 'application/json' })

    is the same as

    fajax.defaults(
    { method: 'post'
    , accept: 'application/json'
    })

An interesting parameter in the defaults is baseUrl. This is resolved on the url passed in when initiating a request, so this is an easy way to normalize urls based on either a folder or a server.

Example:
fajax.defaults({ baseUrl: 'http://example.com' })
fajax.get('/test') // -> GET to http://example.com/test

fajax.defaults({ baseUrl: '/foo' })
fajax.get('bar') // -> GET to <currentDomain>/foo/bar
fajax.get('/absolute') // -> GET to <currentDomain>/absolute

The function itself can take 3 arguments:

  1. An options dictionary (more on this below).
  2. A string of the url for the request. This can also be given as url in the options dictionary.
  3. An optional function that will be called when the request is complete. This can also be given as onload in the options dictionary.

The options dictionary supports the following keys:

  • headers: A key-value based list of request headers to set.

  • accept: The accept header. This defaults to the browsers normal value. If accept is set here, it will override the value in the headers-list.

  • method: The http-method to use. It defaults to GET.

  • body: Sends some text to the server. It defaults the Content-Type header to text/plain, but won't override a specifically set header.

  • json: Sends the given object as JSON. This requires JSON.stringify to be available in global scope.

    It will override Content-Type with application/json.

  • form: Sends the given string or object as form-parameters. The basic implementation will only send a flat object, discarding any values that are not String, Boolean or Number.

    For a more extensive implementation, you can provide any function via the fajax.qs(func) function.

    It will override Content-Type with application/x-www-form-urlencoded.

It is also possible to initiate a request with fajax.request(). It takes the method as the first parameter, but otherwise acts exactly like fajax().

For convenience, there are shorthands for the basic HTTP methods:

  • fajax.get()
  • fajax.head()
  • fajax.post()
  • fajax.put()
  • fajax.delete() or fajax.del()
  • fajax.patch()
  • fajax.options()

They all act as the primary function (fajax()), except they also enforce the method option.