Comparing version 0.3.1 to 0.4.0
116
dist/Ax.js
@@ -0,38 +1,42 @@ | ||
/** | ||
* axjs - A microscopic, RESTful, AJAX browser library; just under 2kb. | ||
* Copyright (c) Matt McFarland - All Rights Reserved | ||
* @version v0.3.1 | ||
* @link https://github.com/MattMcFarland/ax.js | ||
* @license MIT | ||
*/ | ||
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Ax = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
module.exports = function (options) { | ||
var | ||
args = Array.prototype.slice.call(arguments), | ||
xhr = require ('./xhr'), | ||
getOpts = require ('./getOptions'); | ||
var | ||
ax = {}, | ||
xhr = require ('./xhr'), | ||
getOpts = require ('./getOptions'); | ||
ax.protoype = function () { | ||
return xhr(getOpts(options)); | ||
}; | ||
return ax; | ||
module.exports = function() { | ||
return xhr(getOpts(args))(); | ||
} | ||
}; | ||
},{"./getOptions":2,"./xhr":4}],2:[function(require,module,exports){ | ||
module.exports = function(_args) { | ||
var _def = { | ||
method: 'GET', type: 'application/json' | ||
}; | ||
var args = new Array.slice(_args); | ||
if (typeof args[0] === 'object') { | ||
var obj = args[0]; | ||
return { | ||
method: obj.method || _def.method, | ||
url: obj.url || null, | ||
data: obj.data || null, | ||
type: obj.type || _def.type | ||
module.exports = function () { | ||
if (arguments) { | ||
var args = Array.prototype.slice.call(arguments)[0]; | ||
if (typeof args[0] === 'object') { | ||
var obj = args[0]; | ||
return { | ||
method: obj.method || 'GET', | ||
url: obj.url || null, | ||
data: obj.data || null, | ||
type: obj.type || 'application/json', | ||
done: (typeof args[args.length-1] === 'function') ? args[args.length-1] : obj.done | ||
} | ||
} else { | ||
return { | ||
method: args[0] || null, | ||
url: args[1] || args[0], | ||
data: (typeof args[2] === 'object') ? args[2] : (typeof args[1] === 'object') ? args[1] : null, | ||
type: (typeof args[3] === 'string') ? args[3] : (typeof args[2] === 'string') ? args[2] : null, | ||
done: (typeof args[args.length-1] === 'function') ? args[args.length-1] : null | ||
} | ||
} | ||
} else { | ||
return { | ||
method: args[0] || _def.method, | ||
url: args[1] || null, | ||
data: args[2] || null, | ||
type: args[3] || _def.type | ||
} | ||
} | ||
@@ -43,8 +47,10 @@ }; | ||
var result; | ||
var err = false; | ||
try { | ||
result = JSON.parse(req.responseText); | ||
} catch (e) { | ||
result = req.responseText; | ||
result = String(req.responseText); | ||
err = e; | ||
} | ||
return [result, req]; | ||
return [result, req, err]; | ||
}; | ||
@@ -60,29 +66,23 @@ },{}],4:[function(require,module,exports){ | ||
module.exports = function (opts) { | ||
var methods = { | ||
success: function () {}, | ||
error: function () {} | ||
if (!opts.done) { console.error('done callback is undefined')} | ||
if (!opts.method) { console.error('Ax.js method is undefined')} | ||
if (typeof opts.done !== 'function') { console.error('done callback is not a function')} | ||
var res, | ||
req = new XMLHttpRequest() || new ActiveXObject("Microsoft.XMLHTTP"); | ||
req.open(opts.method, opts.url, true); | ||
req.setRequestHeader('Content-type', opts.content_type || 'application/json'); | ||
req.onreadystatechange = function () { | ||
var state = req.readyState, res = function () {}; | ||
if (state === 4) { opts.done.apply(res, parse(req)); } | ||
}; | ||
var request = new XMLHttpRequest(); | ||
request.open(opts.method || 'get', opts.url, true); | ||
request.setRequestHeader('Content-type', opts.content_type || 'application/json'); | ||
request.onreadystatechange = function () { | ||
if (request.readyState === 4) { | ||
if (request.status === 200) { | ||
methods.success.apply(methods, parse(request)); | ||
} else { | ||
methods.error.apply(methods, parse(request)); | ||
} | ||
} | ||
}; | ||
request.send(data); | ||
return { | ||
success: function (callback) { | ||
methods.success = callback; | ||
return methods; | ||
}, | ||
error: function (callback) { | ||
methods.error = callback; | ||
return methods; | ||
} | ||
}; | ||
if (opts.data) { | ||
if (opts.content_type !== 'application/json') { req.send(encodeURIComponent(JSON.stringify(opts.data))); } | ||
else { req.send(opts.data); } | ||
} | ||
else { req.send(); } | ||
return res; | ||
}; | ||
@@ -89,0 +89,0 @@ |
@@ -1,2 +0,2 @@ | ||
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var t;t="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,t.Ax=e()}}(function(){return function e(t,n,r){function o(i,f){if(!n[i]){if(!t[i]){var p="function"==typeof require&&require;if(!f&&p)return p(i,!0);if(u)return u(i,!0);var s=new Error("Cannot find module '"+i+"'");throw s.code="MODULE_NOT_FOUND",s}var a=n[i]={exports:{}};t[i][0].call(a.exports,function(e){var n=t[i][1][e];return o(n?n:e)},a,a.exports,e,t,n,r)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<r.length;i++)o(r[i]);return o}({1:[function(e,t,n){t.exports=function(t){var n={},r=e("./xhr"),o=e("./getOptions");return n.protoype=function(){return r(o(t))},n}},{"./getOptions":2,"./xhr":4}],2:[function(e,t,n){t.exports=function(e){var t={method:"GET",type:"application/json"},n=new Array.slice(e);if("object"==typeof n[0]){var r=n[0];return{method:r.method||t.method,url:r.url||null,data:r.data||null,type:r.type||t.type}}return{method:n[0]||t.method,url:n[1]||null,data:n[2]||null,type:n[3]||t.type}}},{}],3:[function(e,t,n){t.exports=function(e){var t;try{t=JSON.parse(e.responseText)}catch(n){t=e.responseText}return[t,e]}},{}],4:[function(e,t,n){var r=e("./parse");t.exports=function(e){var t={success:function(){},error:function(){}},n=new XMLHttpRequest;return n.open(e.method||"get",e.url,!0),n.setRequestHeader("Content-type",e.content_type||"application/json"),n.onreadystatechange=function(){4===n.readyState&&(200===n.status?t.success.apply(t,r(n)):t.error.apply(t,r(n)))},n.send(data),{success:function(e){return t.success=e,t},error:function(e){return t.error=e,t}}}},{"./parse":3}]},{},[1])(1)}); | ||
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var n;n="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,n.Ax=e()}}(function(){return function e(n,t,o){function r(f,u){if(!t[f]){if(!n[f]){var a="function"==typeof require&&require;if(!u&&a)return a(f,!0);if(i)return i(f,!0);var p=new Error("Cannot find module '"+f+"'");throw p.code="MODULE_NOT_FOUND",p}var c=t[f]={exports:{}};n[f][0].call(c.exports,function(e){var t=n[f][1][e];return r(t?t:e)},c,c.exports,e,n,t,o)}return t[f].exports}for(var i="function"==typeof require&&require,f=0;f<o.length;f++)r(o[f]);return r}({1:[function(e,n,t){var o=Array.prototype.slice.call(arguments),r=e("./xhr"),i=e("./getOptions");n.exports=function(){return r(i(o))()}},{"./getOptions":2,"./xhr":4}],2:[function(e,n,t){n.exports=function(){if(arguments){var e=Array.prototype.slice.call(arguments)[0];if("object"==typeof e[0]){var n=e[0];return{method:n.method||"GET",url:n.url||null,data:n.data||null,type:n.type||"application/json",done:"function"==typeof e[e.length-1]?e[e.length-1]:n.done}}return{method:e[0]||null,url:e[1]||e[0],data:"object"==typeof e[2]?e[2]:"object"==typeof e[1]?e[1]:null,type:"string"==typeof e[3]?e[3]:"string"==typeof e[2]?e[2]:null,done:"function"==typeof e[e.length-1]?e[e.length-1]:null}}}},{}],3:[function(e,n,t){n.exports=function(e){var n,t=!1;try{n=JSON.parse(e.responseText)}catch(o){n=String(e.responseText),t=o}return[n,e,t]}},{}],4:[function(e,n,t){var o=e("./parse");n.exports=function(e){e.done||console.error("done callback is undefined"),e.method||console.error("Ax.js method is undefined"),"function"!=typeof e.done&&console.error("done callback is not a function");var n,t=new XMLHttpRequest||new ActiveXObject("Microsoft.XMLHTTP");return t.open(e.method,e.url,!0),t.setRequestHeader("Content-type",e.content_type||"application/json"),t.onreadystatechange=function(){var n=t.readyState,r=function(){};4===n&&e.done.apply(r,o(t))},e.data?t.send("application/json"!==e.content_type?encodeURIComponent(JSON.stringify(e.data)):e.data):t.send(),n}},{"./parse":3}]},{},[1])(1)}); | ||
//# sourceMappingURL=Ax.min.js.map |
'use strict'; | ||
var header = require('gulp-header'); | ||
var browserify = require('browserify'); | ||
@@ -14,2 +14,13 @@ var gulp = require('gulp'); | ||
var banner = ['/**', | ||
' * <%= pkg.name %> - <%= pkg.description %>', | ||
' * Copyright (c) Matt McFarland - All Rights Reserved', | ||
' * @version v<%= pkg.version %>', | ||
' * @link <%= pkg.homepage %>', | ||
' * @license <%= pkg.license %>', | ||
' */', | ||
''].join('\n'); | ||
gulp.task('dist', function () { | ||
@@ -22,5 +33,7 @@ // set up the browserify instance on a task basis | ||
}); | ||
return b.bundle() | ||
return b | ||
.bundle() | ||
.pipe(source('Ax.js')) | ||
.pipe(buffer()) | ||
.pipe(header(banner, { pkg : pkg } )) | ||
// Add transformation tasks to the pipeline here. | ||
@@ -38,3 +51,4 @@ .on('error', gutil.log) | ||
}); | ||
return b.bundle() | ||
return b | ||
.bundle() | ||
.pipe(source('Ax.min.js')) | ||
@@ -56,3 +70,4 @@ .pipe(buffer()) | ||
}); | ||
return b.bundle() | ||
return b | ||
.bundle() | ||
.pipe(source('index.js')) | ||
@@ -62,2 +77,3 @@ .pipe(buffer()) | ||
.on('error', gutil.log) | ||
.pipe(header(banner, { pkg : pkg } )) | ||
.pipe(gulp.dest('./lib')); | ||
@@ -64,0 +80,0 @@ }); |
118
lib/index.js
@@ -0,38 +1,42 @@ | ||
/** | ||
* axjs - A microscopic, RESTful, AJAX browser library; just under 2kb. | ||
* Copyright (c) Matt McFarland - All Rights Reserved | ||
* @version v0.3.1 | ||
* @link https://github.com/MattMcFarland/ax.js | ||
* @license MIT | ||
*/ | ||
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
module.exports = function (options) { | ||
var | ||
args = Array.prototype.slice.call(arguments), | ||
xhr = require ('./xhr'), | ||
getOpts = require ('./getOptions'); | ||
var | ||
ax = {}, | ||
xhr = require ('./xhr'), | ||
getOpts = require ('./getOptions'); | ||
ax.protoype = function () { | ||
return xhr(getOpts(options)); | ||
}; | ||
return ax; | ||
module.exports = function() { | ||
return xhr(getOpts(args))(); | ||
} | ||
}; | ||
},{"./getOptions":2,"./xhr":4}],2:[function(require,module,exports){ | ||
module.exports = function(_args) { | ||
var _def = { | ||
method: 'GET', type: 'application/json' | ||
}; | ||
var args = new Array.slice(_args); | ||
if (typeof args[0] === 'object') { | ||
var obj = args[0]; | ||
return { | ||
method: obj.method || _def.method, | ||
url: obj.url || null, | ||
data: obj.data || null, | ||
type: obj.type || _def.type | ||
module.exports = function () { | ||
if (arguments) { | ||
var args = Array.prototype.slice.call(arguments)[0]; | ||
if (typeof args[0] === 'object') { | ||
var obj = args[0]; | ||
return { | ||
method: obj.method || 'GET', | ||
url: obj.url || null, | ||
data: obj.data || null, | ||
type: obj.type || 'application/json', | ||
done: (typeof args[args.length-1] === 'function') ? args[args.length-1] : obj.done | ||
} | ||
} else { | ||
return { | ||
method: args[0] || null, | ||
url: args[1] || args[0], | ||
data: (typeof args[2] === 'object') ? args[2] : (typeof args[1] === 'object') ? args[1] : null, | ||
type: (typeof args[3] === 'string') ? args[3] : (typeof args[2] === 'string') ? args[2] : null, | ||
done: (typeof args[args.length-1] === 'function') ? args[args.length-1] : null | ||
} | ||
} | ||
} else { | ||
return { | ||
method: args[0] || _def.method, | ||
url: args[1] || null, | ||
data: args[2] || null, | ||
type: args[3] || _def.type | ||
} | ||
} | ||
@@ -43,8 +47,10 @@ }; | ||
var result; | ||
var err = false; | ||
try { | ||
result = JSON.parse(req.responseText); | ||
} catch (e) { | ||
result = req.responseText; | ||
result = String(req.responseText); | ||
err = e; | ||
} | ||
return [result, req]; | ||
return [result, req, err]; | ||
}; | ||
@@ -60,29 +66,23 @@ },{}],4:[function(require,module,exports){ | ||
module.exports = function (opts) { | ||
var methods = { | ||
success: function () {}, | ||
error: function () {} | ||
if (!opts.done) { console.error('done callback is undefined')} | ||
if (!opts.method) { console.error('Ax.js method is undefined')} | ||
if (typeof opts.done !== 'function') { console.error('done callback is not a function')} | ||
var res, | ||
req = new XMLHttpRequest() || new ActiveXObject("Microsoft.XMLHTTP"); | ||
req.open(opts.method, opts.url, true); | ||
req.setRequestHeader('Content-type', opts.content_type || 'application/json'); | ||
req.onreadystatechange = function () { | ||
var state = req.readyState, res = function () {}; | ||
if (state === 4) { opts.done.apply(res, parse(req)); } | ||
}; | ||
var request = new XMLHttpRequest(); | ||
request.open(opts.method || 'get', opts.url, true); | ||
request.setRequestHeader('Content-type', opts.content_type || 'application/json'); | ||
request.onreadystatechange = function () { | ||
if (request.readyState === 4) { | ||
if (request.status === 200) { | ||
methods.success.apply(methods, parse(request)); | ||
} else { | ||
methods.error.apply(methods, parse(request)); | ||
} | ||
} | ||
}; | ||
request.send(data); | ||
return { | ||
success: function (callback) { | ||
methods.success = callback; | ||
return methods; | ||
}, | ||
error: function (callback) { | ||
methods.error = callback; | ||
return methods; | ||
} | ||
}; | ||
if (opts.data) { | ||
if (opts.content_type !== 'application/json') { req.send(encodeURIComponent(JSON.stringify(opts.data))); } | ||
else { req.send(opts.data); } | ||
} | ||
else { req.send(); } | ||
return res; | ||
}; | ||
@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2YgYXJnc1swXSA9PT0gJ29iamVjdCcpIHtcbiAgICAgICAgdmFyIG9iaiA9IGFyZ3NbMF07XG4gICAgICAgIHJldHVybiB7XG4gICAgICAgICAgICBtZXRob2Q6IG9iai5tZXRob2QgIHx8IF9kZWYubWV0aG9kLFxuICAgICAgICAgICAgdXJsOiAgICBvYmoudXJsICAgICB8fCBudWxsLFxuICAgICAgICAgICAgZGF0YTogICBvYmouZGF0YSAgICB8fCBudWxsLFxuICAgICAgICAgICAgdHlwZTogICBvYmoudHlwZSAgICB8fCBfZGVmLnR5cGVcbiAgICAgICAgfVxuICAgIH0gZWxzZSB7XG4gICAgICAgIHJldHVybiB7XG4gICAgICAgICAgICBtZXRob2Q6IGFyZ3NbMF0gICAgIHx8IF9kZWYubWV0aG9kLFxuICAgICAgICAgICAgdXJsOiAgICBhcmdzWzFdICAgICB8fCBudWxsLFxuICAgICAgICAgICAgZGF0YTogICBhcmdzWzJdICAgICB8fCBudWxsLFxuICAgICAgICAgICAgdHlwZTogICBhcmdzWzNdICAgICB8fCBfZGVmLnR5cGVcbiAgICAgICAgfVxuICAgIH1cbn07IiwibW9kdWxlLmV4cG9ydHMgPSBmdW5jdGlvbiAocmVxKSB7XG4gICAgdmFyIHJlc3VsdDtcbiAgICB0cnkge1xuICAgICAgICByZXN1bHQgPSBKU09OLnBhcnNlKHJlcS5yZXNwb25zZVRleHQpO1xuICAgIH0gY2F0Y2ggKGUpIHtcbiAgICAgICAgcmVzdWx0ID0gcmVxLnJlc3BvbnNlVGV4dDtcbiAgICB9XG4gICAgcmV0dXJuIFtyZXN1bHQsIHJlcV07XG59OyIsIi8qKlxuICogQXhcbiAqIEBhdXRob3IgTWF0dCBNY0ZhcmxhbmRcbiAqXG4gKiBBamF4IHRoYXQgaXMgc2hhcnAuXG4gKi9cbnZhciBwYXJzZSA9IHJlcXVpcmUgKCcuL3BhcnNlJyk7XG5tb2R1bGUuZXhwb3J0cyA9IGZ1bmN0aW9uIChvcHRzKSB7XG4gIC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Rpb24gKCkge1xuICAgICAgICB2YXIgc3RhdGUgPSByZXEucmVhZHlTdGF0ZSwgcmVzID0gZnVuY3Rpb24gKCkge307XG4gICAgICAgIGlmIChzdGF0ZSA9PT0gNCkgeyBvcHRzLmRvbmUuYXBwbHkocmVzLCBwYXJzZShyZXEpKTsgfVxuICAgIH07XG5cbiAgICBpZiAob3B0cy5kYXRhKSB7XG4gICAgICAgIGlmIChvcHRzLmNvbnRlbnRfdHlwZSAhPT0gJ2FwcGxpY2F0aW9uL2pzb24nKSB7IHJlcS5zZW5kKGVuY29kZVVSSUNvbXBvbmVudChKU09OLnN0cmluZ2lmeShvcHRzLmRhdGEpKSk7IH1cbiAgICAgICAgZWxzZSB7IHJlcS5zZW5kKG9wdHMuZGF0YSk7IH1cbiAgICB9XG4gICAgZWxzZSB7IHJlcS5zZW5kKCk7IH1cblxuICAgIHJldHVybiByZXM7XG59O1xuXG4iXX0= |
{ | ||
"name": "axjs", | ||
"filename": "ax.js", | ||
"version": "0.3.1", | ||
"description": "A RESTful, ajax micro browser library that is under 2kb.", | ||
"version": "0.4.0", | ||
"description": "A microscopic, RESTful, AJAX browser library; just under 2kb.", | ||
"main": "lib", | ||
@@ -23,5 +23,7 @@ "scripts": { | ||
"devDependencies": { | ||
"babelify": "^6.0.2", | ||
"browserify": "^10.1.3", | ||
"gulp": "^3.8.11", | ||
"gulp-browserify": "^0.5.1", | ||
"gulp-header": "^1.2.2", | ||
"gulp-sourcemaps": "^1.5.2", | ||
@@ -28,0 +30,0 @@ "gulp-uglify": "^1.2.0", |
116
README.md
![Ax.js](./src/ax.js.gif) | ||
A microscopic, RESTful, AJAX browser library; just under 2kb. | ||
* [Usage](#usage) | ||
* [Use with browser window](#quick-start) | ||
* [Use as generic module](#use-with-module) | ||
* [Use with nodejs](#use-with-nodejs) | ||
* [Usage](#ax) | ||
* [Examples](#examples) | ||
## Usage | ||
Installation: | ||
* [Browser](#browser) | ||
* [NodeJS](#nodejs) | ||
* [UMD / AMD Module](#commonjs) | ||
## Ax | ||
```javascript | ||
var request = new Ax( Object || Arguments); | ||
``` | ||
### Arguments Method | ||
```javascript | ||
var request = new Ax(method, url, [data], [type], done); | ||
``` | ||
#### Arguments | ||
1. method *(String)*: http request (e.g., GET, PUT, POST, PATCH, DELETE, etc...) | ||
2. url *(String)*: location/href | ||
3. [data] *(Object|JSON)*: required if uploading data to the server | ||
4. [type] *(String)*: Content-Type header (default is application/json) | ||
5. done *(Function)* Callback when request is finished, is always the *last argument* passed. | ||
### Object Method | ||
Similar approach, see [arguments](#arguments) above for explanation of values... | ||
```javascript | ||
var request = new Ajax({ | ||
method: method, | ||
url: url, | ||
data: [data], | ||
type: [type], | ||
done: done | ||
}, [done]); | ||
``` | ||
NOTE: the *done* callback can either be in the first argument that is an object, or the *last* argument; but not both. If a function is detected as the *last* argument, Ax will use that and ignore the other. | ||
### Callback | ||
The *done* callback function is passed as the *last* argument. | ||
```javascript | ||
var request = new Ax(...args, function (res, xhr, err) { | ||
// Single error detection | ||
if (err) { | ||
// Whoops lets do something | ||
} | ||
console.log('response is', res); | ||
}); | ||
``` | ||
It may also be part of the options object | ||
```javascript | ||
var request = new Ax({ | ||
...options, | ||
done: function (res, xhr, err) { | ||
if (err) console.error(err); | ||
console.log(res, xhr); | ||
} | ||
}); | ||
``` | ||
## Examples | ||
Get user data... | ||
```javscript | ||
var request = new Ax('GET', '/users/' + userid', function (res, xhr, err) { | ||
if (err) { | ||
// An error has occurred | ||
} else { | ||
var userdata = res; | ||
// Do something with user data. | ||
} | ||
}); | ||
``` | ||
ax | ||
Update a user profile... | ||
```javascript | ||
var request = new Ax({ | ||
method: 'PUT', | ||
url: userUrl, | ||
data: newData,, | ||
done: callBack() | ||
}); | ||
``` | ||
Post a form... | ||
```javscript | ||
var request = new Ax('POST', '/submit/', formData, function (res, xhr, err) { | ||
if (err) { | ||
alert ('total failure', err); | ||
} else { | ||
console.log('server response is', res); | ||
} | ||
}); | ||
``` | ||
## Use with browser window | ||
## Browser | ||
``` | ||
<script src="ax.min.0.3.1.js"></script> | ||
<script src="ax.min"></script> | ||
``` | ||
## Use with nodejs | ||
## NodeJS | ||
@@ -35,6 +127,6 @@ Ax.js has been built with browserify, and works with both browserify and webpack. | ||
``` javascript | ||
var ax = require('axjs'); | ||
var Ax = require('axjs'); | ||
``` | ||
## Use with generic module | ||
## CommonJS | ||
@@ -44,4 +136,4 @@ Ax.js can be *required* in with a UMD or AMD library like requirejs. | ||
``` javascript | ||
var ax = require('ax.0.3.1'); | ||
var Ax = require('ax'); | ||
``` | ||
@@ -1,22 +0,23 @@ | ||
module.exports = function(_args) { | ||
var _def = { | ||
method: 'GET', type: 'application/json' | ||
}; | ||
var args = new Array.slice(_args); | ||
if (typeof args[0] === 'object') { | ||
var obj = args[0]; | ||
return { | ||
method: obj.method || _def.method, | ||
url: obj.url || null, | ||
data: obj.data || null, | ||
type: obj.type || _def.type | ||
module.exports = function () { | ||
if (arguments) { | ||
var args = Array.prototype.slice.call(arguments)[0]; | ||
if (typeof args[0] === 'object') { | ||
var obj = args[0]; | ||
return { | ||
method: obj.method || 'GET', | ||
url: obj.url || null, | ||
data: obj.data || null, | ||
type: obj.type || 'application/json', | ||
done: (typeof args[args.length-1] === 'function') ? args[args.length-1] : obj.done | ||
} | ||
} else { | ||
return { | ||
method: args[0] || null, | ||
url: args[1] || args[0], | ||
data: (typeof args[2] === 'object') ? args[2] : (typeof args[1] === 'object') ? args[1] : null, | ||
type: (typeof args[3] === 'string') ? args[3] : (typeof args[2] === 'string') ? args[2] : null, | ||
done: (typeof args[args.length-1] === 'function') ? args[args.length-1] : null | ||
} | ||
} | ||
} else { | ||
return { | ||
method: args[0] || _def.method, | ||
url: args[1] || null, | ||
data: args[2] || null, | ||
type: args[3] || _def.type | ||
} | ||
} | ||
}; |
@@ -1,15 +0,6 @@ | ||
module.exports = function (options) { | ||
var | ||
args = Array.prototype.slice.call(arguments), | ||
xhr = require ('./xhr'), | ||
getOpts = require ('./getOptions'); | ||
var | ||
ax = {}, | ||
xhr = require ('./xhr'), | ||
getOpts = require ('./getOptions'); | ||
ax.protoype = function () { | ||
return xhr(getOpts(options)); | ||
}; | ||
return ax; | ||
}; | ||
module.exports = function () { return xhr(getOpts(args)) }; |
module.exports = function (req) { | ||
var result; | ||
var err = false; | ||
try { | ||
result = JSON.parse(req.responseText); | ||
} catch (e) { | ||
result = req.responseText; | ||
result = String(req.responseText); | ||
err = e; | ||
} | ||
return [result, req]; | ||
return [result, req, err]; | ||
}; |
@@ -9,30 +9,24 @@ /** | ||
module.exports = function (opts) { | ||
var methods = { | ||
success: function () {}, | ||
error: function () {} | ||
if (!opts.done) { console.error('done callback is undefined')} | ||
if (!opts.method) { console.error('Ax.js method is undefined')} | ||
if (typeof opts.done !== 'function') { console.error('done callback is not a function')} | ||
var res, | ||
req = new XMLHttpRequest() || new ActiveXObject("Microsoft.XMLHTTP"); | ||
req.open(opts.method, opts.url, true); | ||
req.setRequestHeader('Content-type', opts.content_type || 'application/json'); | ||
req.onreadystatechange = function () { | ||
var state = req.readyState, res = function () {}; | ||
if (state === 4) { opts.done.apply(res, parse(req)); } | ||
}; | ||
var request = new XMLHttpRequest(); | ||
request.open(opts.method || 'get', opts.url, true); | ||
request.setRequestHeader('Content-type', opts.content_type || 'application/json'); | ||
request.onreadystatechange = function () { | ||
if (request.readyState === 4) { | ||
if (request.status === 200) { | ||
methods.success.apply(methods, parse(request)); | ||
} else { | ||
methods.error.apply(methods, parse(request)); | ||
} | ||
} | ||
}; | ||
request.send(data); | ||
return { | ||
success: function (callback) { | ||
methods.success = callback; | ||
return methods; | ||
}, | ||
error: function (callback) { | ||
methods.error = callback; | ||
return methods; | ||
} | ||
}; | ||
if (opts.data) { | ||
if (opts.content_type !== 'application/json') { req.send(encodeURIComponent(JSON.stringify(opts.data))); } | ||
else { req.send(opts.data); } | ||
} | ||
else { req.send(); } | ||
return res; | ||
}; | ||
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
51219
138
10
322
1