Comparing version 3.2.2 to 4.0.0
@@ -1,13 +0,19 @@ | ||
module.exports = function xhr2 (params, callback) { | ||
module.exports = minixhr | ||
function minixhr (params, done) { | ||
var url = typeof params === 'string' ? params : params.url | ||
var method = params.method || (params.data ? 'POST' : 'GET') | ||
var body = params.data | ||
var H = params.headers ? params.headers : params.body ? { | ||
var method = params.method || body ? 'POST' : 'GET' | ||
var headers = params.headers || method !== 'POST' ? {} : { | ||
'X-Requested-With' :'XMLHttpRequest', | ||
'Content-Type' :'application/x-www-form-urlencoded' | ||
} : {} | ||
'Content-Type' :'application/x-www-form-urlencoded' } | ||
var timeout = params.timeout || 0 | ||
var xhr = new XMLHttpRequest() | ||
xhr.open(method, url) | ||
for (var key in H) xhr.setRequestHeader(key, H[key]) | ||
xhr.onload = xhr.onerror = function (response) { | ||
var async = timeout !== 'sync' | ||
async && (xhr.timeout = timeout) | ||
async && (xhr.ontimeout = e => finish((e.info = e.type, e))) | ||
xhr.open(method, url, async) | ||
for (var key in headers) xhr.setRequestHeader(key, headers[key]) | ||
if (typeof done === 'function') xhr.onload = xhr.onerror = finish | ||
function finish ({ type, path, info = xhr.statusText }) { | ||
var Hjson = {}, h = xhr.getAllResponseHeaders() | ||
@@ -18,5 +24,9 @@ ;(h.match(/([^\n\r:]+):([^\n\r]+)/g)||[]).forEach(function(item){ | ||
}) | ||
if (callback) callback(this.response, response, xhr, Hjson) | ||
if (type === 'error' || type === 'timeout') { | ||
var { status, responseURL: sender } = xhr | ||
done({ type: 'error', status, info, sender, path }, null, Hjson) | ||
} else done(null, xhr.responseText, Hjson) | ||
done = xhr = xhr.response = xhr.responseText = xhr.onload = xhr.onerror = 0 | ||
} | ||
xhr.send(body||null) | ||
body = xhr.send(body) | ||
} |
{ | ||
"name": "minixhr", | ||
"version": "3.2.2", | ||
"version": "4.0.0", | ||
"description": "super simpel and small cross-browser xhr", | ||
@@ -5,0 +5,0 @@ "main": "minixhr.js", |
@@ -8,22 +8,33 @@ # minixhr | ||
// EXAMPLE 1 | ||
minixhr('https://jsonplaceholder.typicode.com/posts/1', response) | ||
function response (data, response, xhr, header) { | ||
function response (error, data, header) { | ||
if (error) return console.error(error) | ||
console.log(data) | ||
console.log(header) | ||
} | ||
// EXAMPLE 2 | ||
// make a temporary `http://requestb.in` to try the next example, e.g. | ||
var data = { foo: 123, bar: "abc" } | ||
var request = { // can be 'url string' or object: | ||
/*required*/url : 'http://requestb.in/18b4srl1', // replace with your example | ||
/*optional*/method : 'POST', // (defaults to 'GET') | ||
/*optional*/data : JSON.stringify(data), // payload data could be <formdata> or {key:val}'s or any string | ||
/*optional*/headers : {} // (defaults to '{}' OR in case of 'POST' it defaults to: | ||
// {'X-Requested-With':'XMLHttpRequest','Content-Type':'application/x-www-form-urlencoded' } ) | ||
const string = JSON.stringify({ foo: 123, bar: "abc" }) // payload | ||
const URL1 = 'https://jsonplaceholder.typicode.com/posts/1' | ||
// @NOTE check http://requestb.in/18b4srl1?inspect after a request to inspect server | ||
const URL2 = 'http://requestb.in/18b4srl1' // make a `http://requestb.in` to get your own | ||
var request1 = URL1 | ||
var request2 = { // can be 'url string' or object: | ||
/*required*/url : URL2, | ||
/*optional*/method : 'POST', // (defaults to `GET`) | ||
// can be any http method like `['GET', 'POST', 'HEAD', 'PUT', ...]` or `'JSONP'` | ||
/*optional*/data : string, // (defaults to: `undefined`) | ||
// can be any string, maybe formatted as e.g. <FORMDATA> or JSON e.g. '{"key":"val"}' | ||
// if set and no method provided, method will be set to 'POST' | ||
/*optional*/headers : {}, // (defaults to `{}`) | ||
// in case of `method === 'POST'` it defaults to: | ||
// {'X-Requested-With':'XMLHttpRequest','Content-Type':'application/x-www-form-urlencoded' } | ||
/*optional*/timeout : 1000, // (defaults to `0`, wich means NO timeout) | ||
// can be any number of miliseconds, or "sync" (to make a synchronous request) | ||
} | ||
minixhr(request) // [optional] callback - (e.g. leave out for POST Request where you don't care about a response | ||
// check http://requestb.in/18b4srl1?inspect afterwards to inspect | ||
// EXAMPLE 1 | ||
minixhr(/*required*/request1, /*optional*/response) | ||
// EXAMPLE 2 | ||
minixhr(/*required*/request2) | ||
``` | ||
@@ -30,0 +41,0 @@ |
4346
30
43
4