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

alite

v0.1.4

Published

Tiny ajax helper

Downloads

14

Readme

Alite

A simple, tiny promise-based AJAX library.

  • Zero dependencies
  • Less than 450 bytes minified and gzipped

Build Status

Usage

alite({
  // Required: the URL to send/receive from
  url: '/api/foo/bar',

  method: 'POST',

  // Optional: the object to send as JSON, or raw if the raw flag is set
  data: { foo: 'bar' },

  // Optional: A flag indicating whether or not this is a raw request
  // (by default, this is false, and data is turned into a JSON string)
  raw: false,

  // Optional: an object containing the HTTP headers to set
  headers: {
    'Authorization': 'Basic ' + btoa(username + ':' + password)
  },

  // Optional: a function that constructs and returns an XMLHttpRequest object
  xhr: function () {
    // ...
  }
}).then(function (result) {
  console.log('GOT ', result);
}).catch(function (err) {
  console.error(err);
});

Promises

Each of these methods returns a promise, and so is generally called like this:

alite({ url: 'https://api.github.com/users', method: 'GET' })
  .then(function (result) {
    // Result is the deserialized JSON object/array that came back from the server
    console.log(result);
  })
  .catch(function (err) {
    // err is the deserialzed JSON object/array that was returned from the server,
    // or a string, if the response was not JSON
    console.log(err);
  });

Example file upload

The following code is taken from a production app that uploads files to S3:

// File is a file object obtained from an input[type=file] element
// presigned is an object representing AWS S3 presigned URL info...
function upload(file, presigned) {
  var data = new FormData();
  data.append('utf8', '✓');
  data.append('Content-Type', file.type);
  data.append('Content-Disposition', 'attachment; filename = "${filename}"');
  data.append('key', presigned.fullKey);
  data.append('AWSAccessKeyId', presigned.awsAccessKeyId);
  data.append('acl', presigned.acl);
  data.append('policy', presigned.policy);
  data.append('signature', presigned.signature);
  data.append('file', file);

  return alite({
    url: presigned.url,
    method: 'post',
    data: data,
    raw: true,
    xhr: function () {
      var xhr = new XMLHttpRequest();
      xhr.upload.addEventListener('progress', function (e) {
        const progress = Math.ceil((e.loaded / e.total) * 100);

        // This is a Redux dispatch to update progress in the UI
        dispatch({
          type: 'upload_progress',
          abort: () => xhr.abort(),
          progress,
          file
        });
      }, false);
      return xhr;
    }
  });
}

Global AJAX notifications: ajaxStart/ajaxStop

The Alite object has two properties which you can set in order to receive notifications when an ajax request starts or completes:

var alite = Alite();

// xhr is the raw XMLHttpRequest object
// opts is the argument that was passed to the Alite ajax method
//   that triggered this request.
alite.ajaxStart = function (xhr, opts) {
  // Pass the CSRF token along with the request...
  xhr.setRequestHeader('X-CSRF-Token', document.querySelector('meta[name="csrf-token"]').getAttribute('content'));
};

alite.ajaxStop = function (xhr, opts) {
  // This runs whenever an AJAX request has completed (success or error)
};

Assumptions

If raw is not specified or is false, data is sent as JSON and is automatically serialized for you.

Alite requires Promise support. In older browsers, Promises can be shimmed easily enough with something like Plite.

Alite currently has a naive method of detecting if a response is JSON... If it starts with { or [, then the response is considered JSON.

Installing

Download and include alite.min.js

Or install using NPM:

npm install alite

Or install using Bower:

bower install alite

License MIT

Copyright (c) 2015 Chris Davies

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.