Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

alite

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

alite - npm Package Compare versions

Comparing version 0.1.3 to 0.1.4

71

alite.js

@@ -1,8 +0,7 @@

function Alite(XMLHttpRequest) {
XMLHttpRequest = XMLHttpRequest || this.XMLHttpRequest;
function alite(opts) {
function noop() { }
function response(req) {
var responseText = req && req.responseText;
var isJson = responseText &&
(responseText[0] == '{' || responseText[0] == '[');
var isJson = /^[\{\[]/.test(responseText);

@@ -12,52 +11,36 @@ return isJson ? JSON.parse(responseText) : responseText;

var alite = {
ajaxStart: function () { },
ajaxStop: function () { },
return new Promise(function(resolve, reject) {
var req = (opts.xhr || noop)() || new XMLHttpRequest();
var data = opts.data;
ajax: function (opts) {
return new Promise(function(resolve, reject) {
var req = new XMLHttpRequest();
var data = opts.raw ? opts.data : (opts.data ? JSON.stringify(opts.data) : undefined);
req.onreadystatechange = function () {
if (req.readyState == 4) {
if (req.status >= 200 && req.status < 300) {
resolve(response(req));
} else {
reject(response(req));
}
alite.ajaxStop(req, opts);
}
req.onreadystatechange = function () {
if (req.readyState == 4) {
if (req.status >= 200 && req.status < 300) {
resolve(response(req));
} else {
reject(response(req));
}
req.open(opts.method, opts.url);
!opts.raw && req.setRequestHeader('content-type', 'application/json');
(alite.ajaxStop || noop)(req, opts);
}
}
if (opts.headers) {
for (var name in opts.headers) {
req.setRequestHeader(name, opts.headers[name]);
}
}
req.open(opts.method, opts.url);
!opts.raw && req.setRequestHeader('Content-Type', 'application/json');
alite.ajaxStart(req, opts);
opts.ajaxStart && opts.ajaxStart(req);
req.send(data);
});
if (opts.headers) {
for (var name in opts.headers) {
req.setRequestHeader(name, opts.headers[name]);
}
}
};
['put', 'post', 'patch', 'get', 'delete'].forEach(function (httpMethod) {
alite[httpMethod] = function (opts) {
opts.method = httpMethod;
return this.ajax(opts);
}
});
(alite.ajaxStart || noop)(req, opts);
(opts.ajaxStart || noop)(req);
return alite;
req.send(opts.raw ? data : (data ? JSON.stringify(data) : undefined));
})
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = Alite;
}
module.exports = alite;
}

@@ -1,2 +0,2 @@

function Alite(t){function e(t){var e=t&&t.responseText,a=e&&("{"==e[0]||"["==e[0]);return a?JSON.parse(e):e}t=t||this.XMLHttpRequest;var a={ajaxStart:function(){},ajaxStop:function(){},ajax:function(n){return new Promise(function(r,o){var s=new t,u=n.raw?n.data:n.data?JSON.stringify(n.data):void 0;if(s.onreadystatechange=function(){4==s.readyState&&(s.status>=200&&s.status<300?r(e(s)):o(e(s)),a.ajaxStop(s,n))},s.open(n.method,n.url),!n.raw&&s.setRequestHeader("content-type","application/json"),n.headers)for(var i in n.headers)s.setRequestHeader(i,n.headers[i]);a.ajaxStart(s,n),n.ajaxStart&&n.ajaxStart(s),s.send(u)})}};return["put","post","patch","get","delete"].forEach(function(t){a[t]=function(e){return e.method=t,this.ajax(e)}}),a}"undefined"!=typeof module&&module.exports&&(module.exports=Alite);
function alite(e){function t(){}function a(e){var t=e&&e.responseText,a=/^[\{\[]/.test(t);return a?JSON.parse(t):t}return new Promise(function(n,r){var s=(e.xhr||t)()||new XMLHttpRequest,o=e.data;if(s.onreadystatechange=function(){4==s.readyState&&(s.status>=200&&s.status<300?n(a(s)):r(a(s)),(alite.ajaxStop||t)(s,e))},s.open(e.method,e.url),!e.raw&&s.setRequestHeader("Content-Type","application/json"),e.headers)for(var i in e.headers)s.setRequestHeader(i,e.headers[i]);(alite.ajaxStart||t)(s,e),(e.ajaxStart||t)(s),s.send(e.raw?o:o?JSON.stringify(o):void 0)})}"undefined"!=typeof module&&module.exports&&(module.exports=alite);
//# sourceMappingURL=alite.min.js.map
{
"name": "alite",
"main": "alite.js",
"version": "0.1.3",
"version": "0.1.4",
"homepage": "https://github.com/chrisdavies/alite",

@@ -6,0 +6,0 @@ "authors": [

{
"name": "alite",
"version": "0.1.3",
"version": "0.1.4",
"description": "Tiny ajax helper",

@@ -5,0 +5,0 @@ "main": "alite.js",

@@ -6,3 +6,3 @@ # Alite

- Zero dependencies
- Roughly 500 bytes minified and gzipped
- Less than 450 bytes minified and gzipped

@@ -13,19 +13,9 @@ [![Build Status](https://travis-ci.org/chrisdavies/alite.svg?branch=master)](https://travis-ci.org/chrisdavies/alite)

Create a new instance of Alite:
```javascript
var alite = Alite();
```
### Ajax methods
There are several methods, each with an identical signature: put, post, patch, get, delete, and ajax.
Each of these takes a single argument, which is an object of the following shape:
```js
{
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

@@ -43,36 +33,13 @@ data: { foo: 'bar' },

// Optional: a function to be called before the AJAX request is sent
ajaxStart: function (xhr) {
// Optional: a function that constructs and returns an XMLHttpRequest object
xhr: function () {
// ...
}
}
```
Some examples follow:
```js
alite.get({ url: '/api/foo' });
alite.delete({ url: '/api/foo/1' });
alite.patch({
url: '/api/foo/1',
data: { name: 'Joe', age: 32 }
}).then(function (result) {
console.log('GOT ', result);
}).catch(function (err) {
console.error(err);
});
alite.put({
url: '/api/foo/1',
data: { name: 'Joe', age: 32 }
});
alite.post({
url: '/api/foo',
data: { name: 'Joe', age: 32 }
});
alite.ajax({
url: '/api/foo',
data: { name: 'Joe', age: 32 },
method: 'POST'
});
```

@@ -85,3 +52,3 @@

```javascript
alite.get({ url: 'https://api.github.com/users' })
alite({ url: 'https://api.github.com/users', method: 'GET' })
.then(function (result) {

@@ -92,3 +59,4 @@ // Result is the deserialized JSON object/array that came back from the server

.catch(function (err) {
// err is the deserialzed JSON object/array that was returned from the server
// 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);

@@ -98,13 +66,2 @@ });

The promise object has a non-standard property attached to it: `xhr` which can be accessed
inside then/catch via `this.xhr`.
```js
alite.delete('users/24')
.then(function (result) {
console.log(result); // The JSON object received
console.log(this.xhr.getResponseHeader('Server')); // Get the XHR object from the promise
});
```
### Example file upload

@@ -118,5 +75,3 @@

function upload(file, presigned) {
var alite = Alite();
var data = new FormData();
data.append('utf8', '✓');

@@ -132,10 +87,21 @@ data.append('Content-Type', file.type);

return alite.post({
return alite({
url: presigned.url,
method: 'post',
data: data,
raw: true, // This isn't going to be a JSON request
ajaxStart: function (xhr) {
raw: true,
xhr: function () {
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener('progress', function (e) {
updateProgressBar(Math.ceil((e.loaded / e.total) * 100));
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;
}

@@ -142,0 +108,0 @@ });

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc