metal-ajax
Advanced tools
Comparing version 1.0.0-rc.2 to 1.0.0-rc.3
167
lib/Ajax.js
@@ -7,4 +7,2 @@ 'use strict'; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _metal = require('metal'); | ||
@@ -27,102 +25,97 @@ | ||
_createClass(Ajax, null, [{ | ||
key: 'parseResponseHeaders', | ||
/** | ||
* XmlHttpRequest's getAllResponseHeaders() method returns a string of | ||
* response headers according to the format described on the spec: | ||
* {@link http://www.w3.org/TR/XMLHttpRequest/#the-getallresponseheaders-method}. | ||
* This method parses that string into a user-friendly name/value pair | ||
* object. | ||
* @param {string} allHeaders All headers as string. | ||
* @return {!Array.<Object<string, string>>} | ||
*/ | ||
/** | ||
* XmlHttpRequest's getAllResponseHeaders() method returns a string of | ||
* response headers according to the format described on the spec: | ||
* {@link http://www.w3.org/TR/XMLHttpRequest/#the-getallresponseheaders-method}. | ||
* This method parses that string into a user-friendly name/value pair | ||
* object. | ||
* @param {string} allHeaders All headers as string. | ||
* @return {!Array.<Object<string, string>>} | ||
*/ | ||
value: function parseResponseHeaders(allHeaders) { | ||
var headers = []; | ||
if (!allHeaders) { | ||
return headers; | ||
} | ||
var pairs = allHeaders.split('\r\n'); | ||
for (var i = 0; i < pairs.length; i++) { | ||
var index = pairs[i].indexOf(': '); | ||
if (index > 0) { | ||
var name = pairs[i].substring(0, index); | ||
var value = pairs[i].substring(index + 2); | ||
headers.push({ | ||
name: name, | ||
value: value | ||
}); | ||
} | ||
} | ||
Ajax.parseResponseHeaders = function parseResponseHeaders(allHeaders) { | ||
var headers = []; | ||
if (!allHeaders) { | ||
return headers; | ||
} | ||
var pairs = allHeaders.split('\r\n'); | ||
for (var i = 0; i < pairs.length; i++) { | ||
var index = pairs[i].indexOf(': '); | ||
if (index > 0) { | ||
var name = pairs[i].substring(0, index); | ||
var value = pairs[i].substring(index + 2); | ||
headers.push({ | ||
name: name, | ||
value: value | ||
}); | ||
} | ||
} | ||
return headers; | ||
}; | ||
/** | ||
* Requests the url using XMLHttpRequest. | ||
* @param {!string} url | ||
* @param {!string} method | ||
* @param {?string} body | ||
* @param {MultiMap=} opt_headers | ||
* @param {MultiMap=} opt_params | ||
* @param {number=} opt_timeout | ||
* @param {boolean=} opt_sync | ||
* @param {boolean=} opt_withCredentials | ||
* @return {Promise} Deferred ajax request. | ||
* @protected | ||
*/ | ||
/** | ||
* Requests the url using XMLHttpRequest. | ||
* @param {!string} url | ||
* @param {!string} method | ||
* @param {?string} body | ||
* @param {MultiMap=} opt_headers | ||
* @param {MultiMap=} opt_params | ||
* @param {number=} opt_timeout | ||
* @param {boolean=} opt_sync | ||
* @param {boolean=} opt_withCredentials | ||
* @return {Promise} Deferred ajax request. | ||
* @protected | ||
*/ | ||
}, { | ||
key: 'request', | ||
value: function request(url, method, body, opt_headers, opt_params, opt_timeout, opt_sync, opt_withCredentials) { | ||
var request = new XMLHttpRequest(); | ||
var promise = new _metalPromise.CancellablePromise(function (resolve, reject) { | ||
request.onload = function () { | ||
if (request.aborted) { | ||
request.onerror(); | ||
return; | ||
} | ||
resolve(request); | ||
}; | ||
request.onerror = function () { | ||
var error = new Error('Request error'); | ||
error.request = request; | ||
reject(error); | ||
}; | ||
}).thenCatch(function (reason) { | ||
request.abort(); | ||
throw reason; | ||
}).thenAlways(function () { | ||
clearTimeout(timeout); | ||
}); | ||
Ajax.request = function request(url, method, body, opt_headers, opt_params, opt_timeout, opt_sync, opt_withCredentials) { | ||
var request = new XMLHttpRequest(); | ||
if (opt_params) { | ||
url = new _metalUri2.default(url).addParametersFromMultiMap(opt_params).toString(); | ||
} | ||
var promise = new _metalPromise.CancellablePromise(function (resolve, reject) { | ||
request.onload = function () { | ||
if (request.aborted) { | ||
request.onerror(); | ||
return; | ||
} | ||
resolve(request); | ||
}; | ||
request.onerror = function () { | ||
var error = new Error('Request error'); | ||
error.request = request; | ||
reject(error); | ||
}; | ||
}).thenCatch(function (reason) { | ||
request.abort(); | ||
throw reason; | ||
}).thenAlways(function () { | ||
clearTimeout(timeout); | ||
}); | ||
request.open(method, url, !opt_sync); | ||
if (opt_params) { | ||
url = new _metalUri2.default(url).addParametersFromMultiMap(opt_params).toString(); | ||
} | ||
if (opt_withCredentials) { | ||
request.withCredentials = true; | ||
} | ||
request.open(method, url, !opt_sync); | ||
if (opt_headers) { | ||
opt_headers.names().forEach(function (name) { | ||
request.setRequestHeader(name, opt_headers.getAll(name).join(', ')); | ||
}); | ||
} | ||
if (opt_withCredentials) { | ||
request.withCredentials = true; | ||
} | ||
request.send(_metal.core.isDef(body) ? body : null); | ||
if (opt_headers) { | ||
opt_headers.names().forEach(function (name) { | ||
request.setRequestHeader(name, opt_headers.getAll(name).join(', ')); | ||
}); | ||
} | ||
if (_metal.core.isDefAndNotNull(opt_timeout)) { | ||
var timeout = setTimeout(function () { | ||
promise.cancel('Request timeout'); | ||
}, opt_timeout); | ||
} | ||
request.send(_metal.core.isDef(body) ? body : null); | ||
return promise; | ||
if (_metal.core.isDefAndNotNull(opt_timeout)) { | ||
var timeout = setTimeout(function () { | ||
promise.cancel('Request timeout'); | ||
}, opt_timeout); | ||
} | ||
}]); | ||
return promise; | ||
}; | ||
return Ajax; | ||
@@ -129,0 +122,0 @@ }(); |
{ | ||
"name": "metal-ajax", | ||
"version": "1.0.0-rc.2", | ||
"version": "1.0.0-rc.3", | ||
"description": "Metal.js utility to perform Ajax requests", | ||
@@ -19,3 +19,3 @@ "license": "BSD", | ||
"scripts": { | ||
"compile": "babel --presets es2015 -d lib/ src/", | ||
"compile": "babel --presets metal -d lib/ src/", | ||
"prepublish": "npm run compile", | ||
@@ -35,3 +35,3 @@ "test": "gulp test" | ||
"babel-cli": "^6.4.5", | ||
"babel-preset-es2015": "^6.3.13", | ||
"babel-preset-metal": "^4.0.0", | ||
"gulp": "^3.8.11", | ||
@@ -38,0 +38,0 @@ "gulp-metal": "^1.0.0" |
13568
351