Comparing version 1.2.0 to 2.0.0
440
djax.js
@@ -1,261 +0,275 @@ | ||
(function(undefined) { | ||
'use strict'; | ||
function ajax(opt, fn) { | ||
if (typeof opt === 'string') { | ||
opt = { url: opt }; | ||
} else if (typeof opt !== 'object' || !opt) { | ||
throw new Error('Wrong arguments'); | ||
} | ||
// Declare the ajax function: | ||
function ajax(opt, fn) { | ||
if (!ajax.xhr) | ||
throw new Error( | ||
'XMLHttpRequest not found. You can specify which XMLHttpRequest ' + | ||
'you want to use by using `ajax.xhr = myXHR`.' | ||
); | ||
// Get the xhr object: | ||
let xhr; | ||
// Callbacks: | ||
var successes = [], | ||
errors = [], | ||
beforeSend = []; | ||
if (typeof opt.xhr === 'function') { | ||
xhr = opt.xhr(); | ||
} else if (ajax.XHR) { | ||
xhr = new ajax.XHR(); | ||
} else { | ||
throw new Error( | ||
'XMLHttpRequest not found. You can specify which XMLHttpRequest ' + | ||
'you want to use by using `ajax({ xhr() { return myXhr; } })`.' | ||
); | ||
} | ||
// Check for given callbacks: | ||
if (typeof opt === 'string') { | ||
opt = { url: opt }; | ||
// Callbacks: | ||
let errors = []; | ||
let successes = []; | ||
if (arguments.length === 2) { | ||
if (typeof fn === 'function') | ||
successes.push(fn); | ||
else if (Array.isArray(fn)) | ||
successes = successes.concat(fn); | ||
} | ||
} else if (typeof opt !== 'object' || !opt) | ||
throw new Error('Wrong arguments'); | ||
// Check for given callbacks: | ||
if (opt && fn) { | ||
if (typeof fn === 'function') successes.push(fn); | ||
else if (Array.isArray(fn)) successes = successes.concat(fn); | ||
} | ||
if (typeof opt.success === 'function') | ||
successes.push(opt.success); | ||
else if (Array.isArray(opt.success)) | ||
successes = successes.concat(opt.success); | ||
if (typeof opt.success === 'function') { | ||
successes.push(opt.success); | ||
} else if (Array.isArray(opt.success)) { | ||
successes = successes.concat(opt.success); | ||
} | ||
if (typeof opt.error === 'function') | ||
errors.push(opt.error); | ||
else if (Array.isArray(opt.error)) | ||
errors = errors.concat(opt.error); | ||
if (typeof opt.error === 'function') { | ||
errors.push(opt.error); | ||
} else if (Array.isArray(opt.error)) { | ||
errors = errors.concat(opt.error); | ||
} | ||
// Other parameters: | ||
var key, | ||
data, | ||
timer, | ||
conclude, | ||
textStatus, | ||
done = false, | ||
url = opt.url, | ||
xhr = new ajax.xhr(), | ||
type = opt.method || opt.type || 'GET', | ||
dataType = opt.dataType || 'json', | ||
contentType = opt.contentType || 'application/x-www-form-urlencoded'; | ||
// Other parameters: | ||
let key; | ||
let data; | ||
let timer; | ||
let conclude; | ||
let textStatus; | ||
let done = false; | ||
let url = opt.url; | ||
const type = opt.method || opt.type || 'GET'; | ||
const dataType = opt.dataType || 'json'; | ||
const contentType = opt.contentType || ( | ||
opt.contentType === false && opt.processData === false ? | ||
false : | ||
'application/x-www-form-urlencoded' | ||
); | ||
if (!url || typeof url !== 'string') | ||
throw new Error('Wrong arguments'); | ||
if (!url || typeof url !== 'string') { | ||
throw new Error('Wrong arguments'); | ||
} | ||
if (opt.data) { | ||
if (typeof opt.data === 'string') | ||
data = opt.data; | ||
else if (/json/.test(contentType)) | ||
data = JSON.stringify(opt.data); | ||
else { | ||
data = []; | ||
for (key in opt.data) | ||
if (opt.data) { | ||
if (typeof opt.data === 'string' || !contentType) { | ||
data = opt.data; | ||
} else if (/json/.test(contentType)) { | ||
data = JSON.stringify(opt.data); | ||
} else { | ||
data = []; | ||
for (key in opt.data) { | ||
if ({}.hasOwnProperty.call(opt.data, key)) { | ||
data.push( | ||
encodeURIComponent(key) + '=' + encodeURIComponent(opt.data[key]) | ||
); | ||
data = data.join('&'); | ||
} | ||
} | ||
data = data.join('&'); | ||
} | ||
if (/GET|DELETE/i.test(type)) { | ||
url += /\?/.test(url) ? | ||
'&' + data : | ||
'?' + data; | ||
data = ''; | ||
} | ||
if (/GET|DELETE/i.test(type)) { | ||
url += /\?/.test(url) ? | ||
'&' + data : | ||
'?' + data; | ||
data = ''; | ||
} | ||
} | ||
xhr.onreadystatechange = function() { | ||
if (+xhr.readyState === 4) { | ||
done = true; | ||
xhr.onreadystatechange = () => { | ||
if (+xhr.readyState === 4) { | ||
done = true; | ||
if (timer) | ||
clearTimeout(timer); | ||
if (timer) clearTimeout(timer); | ||
if (/^2/.test(xhr.status)) { | ||
done = true; | ||
textStatus = 'success'; | ||
data = xhr.responseText; | ||
if (/^2/.test(xhr.status + '')) { | ||
done = true; | ||
textStatus = 'success'; | ||
data = xhr.responseText; | ||
if (/json/.test(dataType)) { | ||
try { | ||
data = data ? JSON.parse(data) : ''; | ||
} catch (e) { | ||
conclude = function(successes, errors) { | ||
errors.forEach(function(fn) { | ||
fn(xhr, textStatus = 'parsererror'); | ||
}); | ||
}; | ||
conclude(null, errors); | ||
return; | ||
} | ||
if (/json/.test(dataType)) { | ||
try { | ||
data = data ? JSON.parse(data) : ''; | ||
} catch (e) { | ||
data = data + ''; | ||
} | ||
} | ||
// Specific 204 HTTP status case: | ||
if (+xhr.status === 204) { | ||
textStatus = 'nocontent'; | ||
data = undefined; | ||
} | ||
// Specific 204 HTTP status case: | ||
if (+xhr.status === 204) { | ||
textStatus = 'nocontent'; | ||
data = undefined; | ||
} | ||
conclude = function(successes, errors) { | ||
successes.forEach(function(fn) { | ||
fn(data, textStatus, xhr); | ||
}); | ||
}; | ||
conclude(successes); | ||
} else { | ||
conclude = function(successes, errors) { | ||
errors.forEach(function(fn) { | ||
fn( | ||
xhr, | ||
+xhr.status ? 'error' : 'abort', | ||
xhr.responseText | ||
); | ||
}); | ||
}; | ||
conclude(null, errors); | ||
} | ||
conclude = succ => succ.forEach(fnc => fnc(data, textStatus, xhr)); | ||
conclude(successes); | ||
} else { | ||
conclude = (_, errs) => ( | ||
errs.forEach(fnc => ( | ||
fnc( | ||
xhr, | ||
+xhr.status ? 'error' : 'abort', | ||
xhr.responseText | ||
) | ||
)) | ||
); | ||
conclude(null, errors); | ||
} | ||
}; | ||
} | ||
}; | ||
// Check xhrFields | ||
if (opt.xhrFields && typeof opt.xhrFields === 'object') | ||
for (key in opt.xhrFields) | ||
// Check xhrFields | ||
if (opt.xhrFields && typeof opt.xhrFields === 'object') { | ||
for (key in opt.xhrFields) { | ||
if ({}.hasOwnProperty.call(opt.xhrFields, key)) { | ||
xhr[key] = opt.xhrFields[key]; | ||
} | ||
} | ||
} | ||
xhr.open(type, url, true); | ||
xhr.open(type, url, true); | ||
if (contentType) { | ||
xhr.setRequestHeader('Content-Type', contentType); | ||
} | ||
// Check custom headers: | ||
if (opt.headers) | ||
for (key in opt.headers) | ||
// Check custom headers: | ||
if (opt.headers) { | ||
for (key in opt.headers) { | ||
if ({}.hasOwnProperty.call(opt.headers, key)) { | ||
xhr.setRequestHeader(key, opt.headers[key]); | ||
// Check the "beforeSend" callback: | ||
if ( | ||
typeof opt.beforeSend === 'function' && | ||
opt.beforeSend(xhr, opt) === false | ||
) { | ||
done = true; | ||
conclude = function(successes, errors) { | ||
errors.forEach(function(fn) { | ||
fn( | ||
xhr, | ||
'abort', | ||
xhr.responseText | ||
); | ||
}); | ||
}; | ||
conclude(null, errors); | ||
return xhr.abort(); | ||
} | ||
} | ||
} | ||
// Check "timeout": | ||
if (opt.timeout) | ||
timer = setTimeout(function() { | ||
// Check the "beforeSend" callback: | ||
if ( | ||
typeof opt.beforeSend === 'function' && | ||
opt.beforeSend(xhr, opt) === false | ||
) { | ||
done = true; | ||
conclude = (_, errs) => ( | ||
errs.forEach(fnc => ( | ||
fnc( | ||
xhr, | ||
'abort', | ||
xhr.responseText | ||
) | ||
)) | ||
); | ||
conclude(null, errors); | ||
return xhr.abort(); | ||
} | ||
// Check "timeout": | ||
if (opt.timeout) { | ||
timer = setTimeout( | ||
() => { | ||
done = true; | ||
xhr.onreadystatechange = function() {}; | ||
xhr.onreadystatechange = () => {}; | ||
xhr.abort(); | ||
conclude = function(successes, errors) { | ||
errors.forEach(function(fn) { | ||
fn(xhr, 'timeout'); | ||
}); | ||
}; | ||
conclude = (_, errs) => ( | ||
errs.forEach(fnc => fnc(xhr, 'timeout')) | ||
); | ||
conclude(null, errors); | ||
}, opt.timeout); | ||
}, | ||
opt.timeout | ||
); | ||
} | ||
// Send the AJAX call: | ||
xhr.send(data); | ||
// Send the AJAX call: | ||
xhr.send(data); | ||
// Promise: | ||
xhr.done = function(callback) { | ||
if (typeof callback === 'function') | ||
successes.push(callback); | ||
else if (Array.isArray(callback)) | ||
successes = successes.concat(callback); | ||
else | ||
throw new Error('Wrong arguments.'); | ||
// Promise: | ||
xhr.done = function xhrDone(callback) { | ||
if (typeof callback === 'function') { | ||
successes.push(callback); | ||
} else if (Array.isArray(callback)) { | ||
successes = successes.concat(callback); | ||
} else { | ||
throw new Error('Wrong arguments.'); | ||
} | ||
// If the call has already been received: | ||
if (done) { | ||
if (typeof callback === 'function') | ||
conclude([callback]); | ||
else if (Array.isArray(callback)) | ||
conclude(callback); | ||
// If the call has already been received: | ||
if (done) { | ||
if (typeof callback === 'function') { | ||
conclude([callback]); | ||
} else if (Array.isArray(callback)) { | ||
conclude(callback); | ||
} | ||
} | ||
return this; | ||
}; | ||
xhr.fail = function(callback) { | ||
if (typeof callback === 'function') | ||
errors.push(callback); | ||
else if (Array.isArray(callback)) | ||
errors = errors.concat(callback); | ||
else | ||
throw new Error('Wrong arguments.'); | ||
return this; | ||
}; | ||
xhr.fail = function xhrFail(callback) { | ||
if (typeof callback === 'function') { | ||
errors.push(callback); | ||
} else if (Array.isArray(callback)) { | ||
errors = errors.concat(callback); | ||
} else { | ||
throw new Error('Wrong arguments.'); | ||
} | ||
// If the call has already been received: | ||
if (done) { | ||
if (typeof callback === 'function') | ||
conclude(null, [callback]); | ||
else if (Array.isArray(callback)) | ||
conclude(null, callback); | ||
// If the call has already been received: | ||
if (done) { | ||
if (typeof callback === 'function') { | ||
conclude(null, [callback]); | ||
} else if (Array.isArray(callback)) { | ||
conclude(null, callback); | ||
} | ||
} | ||
return this; | ||
}; | ||
xhr.then = function(success, error) { | ||
if (success) | ||
this.done(success); | ||
if (error) | ||
this.fail(error); | ||
return this; | ||
}; | ||
xhr.then = function xhrThen(success, error) { | ||
if (success) this.done(success); | ||
if (error) this.fail(error); | ||
// If the call has already been received: | ||
if (done) | ||
conclude( | ||
Array.isArray(success) ? | ||
success : | ||
typeof success === 'function' ? | ||
[success] : null, | ||
Array.isArray(error) ? | ||
error : | ||
typeof error === 'function' ? | ||
[error] : null | ||
); | ||
// If the call has already been received: | ||
if (done) { | ||
if (!Array.isArray(success)) { | ||
success = typeof success === 'function' ? | ||
[success] : null; | ||
} | ||
if (!Array.isArray(error)) { | ||
error = typeof error === 'function' ? | ||
[error] : null; | ||
} | ||
conclude(success, error); | ||
} | ||
return this; | ||
}; | ||
return this; | ||
}; | ||
return xhr; | ||
} | ||
return xhr; | ||
} | ||
// Djax version: | ||
ajax.version = '1.2.0'; | ||
// Djax version: | ||
ajax.version = '1.2.0'; | ||
// Check XMLHttpRequest presence: | ||
if (typeof XMLHttpRequest !== 'undefined') | ||
ajax.xhr = XMLHttpRequest; | ||
// Check XMLHttpRequest presence: | ||
if (typeof XMLHttpRequest !== 'undefined') { | ||
ajax.XHR = XMLHttpRequest; | ||
} | ||
// Export the AJAX method: | ||
if (typeof exports !== 'undefined') { | ||
if (typeof module !== 'undefined' && module.exports) | ||
exports = module.exports = ajax; | ||
exports.djax = ajax; | ||
} else if (typeof define === 'function' && define.amd) | ||
define('djax', [], function() { | ||
return ajax; | ||
}); | ||
else | ||
this.djax = ajax; | ||
}).call(this); | ||
export default ajax; | ||
export const ajaxSettings = { | ||
xhr() { | ||
if (XMLHttpRequest) { | ||
return new XMLHttpRequest(); | ||
} | ||
return false; | ||
}, | ||
}; |
{ | ||
"name": "djax", | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"description": "A lightweight jQuery.ajax subset", | ||
"main": "djax.js", | ||
"main": "djax.dist.js", | ||
"scripts": { | ||
"test": "gulp test", | ||
"build": "gulp build" | ||
"lint": "eslint djax.js test/*.js", | ||
"build": "babel djax.js -o djax.dist.js --presets es2015 --plugins add-module-exports", | ||
"test:server-start": "babel-node test/api-mockup.js --presets es2015 --plugins add-module-exports", | ||
"test:server-stop": "pkill --signal SIGTERM testServer", | ||
"test:build-djax": "browserify test/unit.djax.js -o test/build/unit.djax.js -t [ babelify --presets [ es2015 ] ]", | ||
"test:exec-djax": "npm run test:server-start & sleep 2 && mocha-phantomjs -p node_modules/phantomjs-prebuilt/bin/phantomjs http://localhost:8001/front/browser/unit.djax.html; npm run test:server-stop", | ||
"test:build-jquery": "browserify test/unit.jquery.js -o test/build/unit.jquery.js -t [ babelify --presets [ es2015 ] ]", | ||
"test:exec-jquery": "npm run test:server-start & sleep 2 && mocha-phantomjs -p node_modules/phantomjs-prebuilt/bin/phantomjs http://localhost:8001/front/browser/unit.jquery.html; npm run test:server-stop", | ||
"test": "npm run test:build-djax && npm run test:build-jquery && npm run test:exec-djax && npm run test:exec-jquery", | ||
"prepublish": "npm run build" | ||
}, | ||
@@ -25,17 +33,42 @@ "repository": { | ||
"devDependencies": { | ||
"assert": "^1.1.2", | ||
"body-parser": "^1.9.0", | ||
"express": "^4.9.7", | ||
"gulp": "^3.8.8", | ||
"gulp-browserify": "^0.5.0", | ||
"gulp-gjslint": "^0.1.4", | ||
"gulp-header": "^1.1.1", | ||
"gulp-jshint": "^1.8.5", | ||
"gulp-mocha-phantomjs": "^0.5.1", | ||
"gulp-rename": "^1.2.0", | ||
"gulp-uglify": "^1.0.1", | ||
"jquery": "^2.1.3", | ||
"jshint-stylish": "^1.0.0", | ||
"run-sequence": "^1.1.0" | ||
"babel-cli": "^6.7.5", | ||
"babel-eslint": "^6.0.2", | ||
"babel-plugin-add-module-exports": "^0.1.2", | ||
"babel-preset-es2015": "^6.6.0", | ||
"babelify": "^7.2.0", | ||
"body-parser": "^1.15.0", | ||
"browserify": "^13.0.0", | ||
"eslint": "^2.7.0", | ||
"eslint-config-airbnb": "^6.2.0", | ||
"eslint-plugin-react": "^4.3.0", | ||
"express": "^4.13.4", | ||
"jquery": "^2.2.3", | ||
"mocha-phantomjs": "^4.0.2", | ||
"multer": "^1.1.0", | ||
"phantomjs-prebuilt": "^2.1.7" | ||
}, | ||
"eslintConfig": { | ||
"parser": "babel-eslint", | ||
"extends": "airbnb", | ||
"env": { | ||
"browser": true | ||
}, | ||
"rules": { | ||
"no-param-reassign": 0, | ||
"prefer-template": 0, | ||
"template-curly-spacing": [ | ||
2, | ||
"always" | ||
] | ||
}, | ||
"globals": { | ||
"window": true, | ||
"describe": true, | ||
"assert": true, | ||
"it": true, | ||
"afterEach": true, | ||
"beforeEach": true, | ||
"testfile": true | ||
} | ||
} | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
19072
5
461
15
1