diff --git a/README.md b/README.md new file mode 100644 index 0000000..5ee5730 --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +# codex.ajax + +Native AJAX module + +## Usage + +```js +ajax.call({ + type: 'POST', + url: '/target/url', + data: {}, + before: function () {}, + progress: function (percentage) { + console.log(percentage + '%'); + // ... + }, + success: function (response) { + response = JSON.parse(response); + console.log(response); + // ... + }, + error: function (response) { + response = JSON.parse(response); + console.log(response); + // ... + }, + after: function () {}, +}); +``` diff --git a/lib/bundle.js b/lib/bundle.js index 7ddef6c..1c0b390 100644 --- a/lib/bundle.js +++ b/lib/bundle.js @@ -1,2 +1,2 @@ -!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.ajax=e():t.ajax=e()}(this,function(){return function(t){function e(o){if(n[o])return n[o].exports;var r=n[o]={i:o,l:!1,exports:{}};return t[o].call(r.exports,r,r.exports,e),r.l=!0,r.exports}var n={};return e.m=t,e.c=n,e.d=function(t,n,o){e.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:o})},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=0)}([function(t,e,n){"use strict";t.exports=function(){var t=function(t){return t instanceof FormData};return{call:function(e){if(e&&e.url){var n=window.XMLHttpRequest?new window.XMLHttpRequest:new window.ActiveXObject("Microsoft.XMLHTTP"),o=e.progress||null,r=e.success||function(){},a=e.error||function(){},u=e.before||null,s=e.after||null;if(e.async=!0,e.type=e.type||"GET",e.data=e.data||"",e["content-type"]=e["content-type"]||"application/json; charset=utf-8","GET"===e.type&&e.data&&(e.url=/\?/.test(e.url)?e.url+"&"+e.data:e.url+"?"+e.data),e.withCredentials&&(n.withCredentials=!0),u&&"function"==typeof u&&u(),n.open(e.type,e.url,e.async),!t(e.data)){var i=new FormData;for(var c in e.data)i.append(c,e.data[c]);e.data=i}o&&"function"==typeof o&&n.upload.addEventListener("progress",function(t){var e=parseInt(t.loaded/t.total*100);o(e)},!1),n.setRequestHeader("X-Requested-With","XMLHttpRequest"),n.onreadystatechange=function(){4===n.readyState&&(200===n.status?r(n.responseText):a(n.responseText),s&&"function"==typeof s&&s())},n.send(e.data)}}}}()}])}); +!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.ajax=e():t.ajax=e()}(this,function(){return function(t){function e(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,e),o.l=!0,o.exports}var n={};return e.m=t,e.c=n,e.d=function(t,n,r){e.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:r})},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=0)}([function(t,e,n){"use strict";t.exports=function(){var t=function(t){return t instanceof FormData};return{call:function(e){if(e&&e.url){var n=window.XMLHttpRequest?new window.XMLHttpRequest:new window.ActiveXObject("Microsoft.XMLHTTP"),r=e.progress||null,o=e.success||function(){},a=e.error||function(){},u=e.before||null,i=e.after||null;if(e.async=!0,e.type=e.type||"GET",e.data=e.data||"",e["content-type"]=e["content-type"]||"application/json; charset=utf-8","GET"===e.type&&e.data&&(e.url=/\?/.test(e.url)?e.url+"&"+e.data:e.url+"?"+e.data),e.withCredentials&&(n.withCredentials=!0),u&&"function"==typeof u&&u(),n.open(e.type,e.url,e.async),!t(e.data)){var c=new FormData;for(var s in e.data)c.append(s,e.data[s]);e.data=c}r&&"function"==typeof r&&n.upload.addEventListener("progress",function(t){var e=parseInt(t.loaded/t.total*100);r(e)},!1),n.setRequestHeader("X-Requested-With","XMLHttpRequest"),n.onreadystatechange=function(){if(4===n.readyState){var t=n.responseText;try{t=JSON.parse(t)}catch(t){}200===n.status?o(t):a(t),i&&"function"==typeof i&&i()}},n.send(e.data)}}}}()}])}); //# sourceMappingURL=bundle.js.map \ No newline at end of file diff --git a/lib/bundle.js.map b/lib/bundle.js.map index aafeb8a..78f8464 100644 --- a/lib/bundle.js.map +++ b/lib/bundle.js.map @@ -1 +1 @@ -{"version":3,"sources":["webpack:///webpack/universalModuleDefinition","webpack:///lib/bundle.js","webpack:///webpack/bootstrap f7713f06c5e70dc741af","webpack:///./src/ajax.js"],"names":["root","factory","exports","module","define","amd","this","modules","__webpack_require__","moduleId","installedModules","i","l","call","m","c","d","name","getter","o","Object","defineProperty","configurable","enumerable","get","n","__esModule","object","property","prototype","hasOwnProperty","p","s","isFormData","FormData","data","url","XMLHTTP","window","XMLHttpRequest","ActiveXObject","progressCallback","progress","successFunction","success","errorFunction","error","beforeFunction","before","afterFunction","after","async","type","test","withCredentials","open","requestData","key","append","upload","addEventListener","e","percentage","parseInt","loaded","total","setRequestHeader","onreadystatechange","readyState","status","responseText","send"],"mappings":"CAAA,SAAAA,EAAAC,GACA,gBAAAC,UAAA,gBAAAC,QACAA,OAAAD,QAAAD,IACA,kBAAAG,gBAAAC,IACAD,UAAAH,GACA,gBAAAC,SACAA,QAAA,KAAAD,IAEAD,EAAA,KAAAC,KACCK,KAAA,WACD,MCAgB,UAAUC,GCN1B,QAAAC,GAAAC,GAGA,GAAAC,EAAAD,GACA,MAAAC,GAAAD,GAAAP,OAGA,IAAAC,GAAAO,EAAAD,IACAE,EAAAF,EACAG,GAAA,EACAV,WAUA,OANAK,GAAAE,GAAAI,KAAAV,EAAAD,QAAAC,IAAAD,QAAAM,GAGAL,EAAAS,GAAA,EAGAT,EAAAD,QAvBA,GAAAQ,KA4DA,OAhCAF,GAAAM,EAAAP,EAGAC,EAAAO,EAAAL,EAGAF,EAAAQ,EAAA,SAAAd,EAAAe,EAAAC,GACAV,EAAAW,EAAAjB,EAAAe,IACAG,OAAAC,eAAAnB,EAAAe,GACAK,cAAA,EACAC,YAAA,EAC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nBqB,EAAYE,OAAOD,EAAKtB,EAAKA,KAAKsB,GAGpCtB,GAAKA,KAAOqB,EAMVf,GAAgD,kBAArBA,IAC7BJ,EAAQsB,OAAOC,iBAAiB,WAAY,SAAUC,GACpD,GAAIC,GAAaC,SAASF,EAAEG,OAASH,EAAEI,MAAQ,IAE/CxB,GAAiBqB,KAChB,GAGLzB,EAAQ6B,iBAAiB,mBAAoB,kBAC7C7B,EAAQ8B,mBAAqB,WAWA,IAAvB9B,EAAQ+B,aACa,MAAnB/B,EAAQgC,OACV1B,EAAgBN,EAAQiC,cAExBzB,EAAcR,EAAQiC,cAGpBrB,GAA0C,kBAAlBA,IAC1BA,MAKNZ,EAAQkC,KAAKpC,EAAKA","file":"./lib/bundle.js","sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"ajax\"] = factory();\n\telse\n\t\troot[\"ajax\"] = factory();\n})(this, function() {\nreturn \n\n\n// WEBPACK FOOTER //\n// webpack/universalModuleDefinition","(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"ajax\"] = factory();\n\telse\n\t\troot[\"ajax\"] = factory();\n})(this, function() {\nreturn /******/ (function(modules) { // webpackBootstrap\n/******/ \t// The module cache\n/******/ \tvar installedModules = {};\n/******/\n/******/ \t// The require function\n/******/ \tfunction __webpack_require__(moduleId) {\n/******/\n/******/ \t\t// Check if module is in cache\n/******/ \t\tif(installedModules[moduleId]) {\n/******/ \t\t\treturn installedModules[moduleId].exports;\n/******/ \t\t}\n/******/ \t\t// Create a new module (and put it into the cache)\n/******/ \t\tvar module = installedModules[moduleId] = {\n/******/ \t\t\ti: moduleId,\n/******/ \t\t\tl: false,\n/******/ \t\t\texports: {}\n/******/ \t\t};\n/******/\n/******/ \t\t// Execute the module function\n/******/ \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n/******/\n/******/ \t\t// Flag the module as loaded\n/******/ \t\tmodule.l = true;\n/******/\n/******/ \t\t// Return the exports of the module\n/******/ \t\treturn module.exports;\n/******/ \t}\n/******/\n/******/\n/******/ \t// expose the modules object (__webpack_modules__)\n/******/ \t__webpack_require__.m = modules;\n/******/\n/******/ \t// expose the module cache\n/******/ \t__webpack_require__.c = installedModules;\n/******/\n/******/ \t// define getter function for harmony exports\n/******/ \t__webpack_require__.d = function(exports, name, getter) {\n/******/ \t\tif(!__webpack_require__.o(exports, name)) {\n/******/ \t\t\tObject.defineProperty(exports, name, {\n/******/ \t\t\t\tconfigurable: false,\n/******/ \t\t\t\tenumerable: true,\n/******/ \t\t\t\tget: getter\n/******/ \t\t\t});\n/******/ \t\t}\n/******/ \t};\n/******/\n/******/ \t// getDefaultExport function for compatibility with non-harmony modules\n/******/ \t__webpack_require__.n = function(module) {\n/******/ \t\tvar getter = module && module.__esModule ?\n/******/ \t\t\tfunction getDefault() { return module['default']; } :\n/******/ \t\t\tfunction getModuleExports() { return module; };\n/******/ \t\t__webpack_require__.d(getter, 'a', getter);\n/******/ \t\treturn getter;\n/******/ \t};\n/******/\n/******/ \t// Object.prototype.hasOwnProperty.call\n/******/ \t__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };\n/******/\n/******/ \t// __webpack_public_path__\n/******/ \t__webpack_require__.p = \"\";\n/******/\n/******/ \t// Load entry module and return exports\n/******/ \treturn __webpack_require__(__webpack_require__.s = 0);\n/******/ })\n/************************************************************************/\n/******/ ([\n/* 0 */\n/***/ (function(module, exports, __webpack_require__) {\n\n\"use strict\";\n\n\n/**\n * AJAX module\n *\n * @example\najax.call({\n type: 'POST',\n url: '/target/url',\n data: {},\n before: function () {},\n progress: function (percentage) {\n console.log(percentage + '%');\n // ...\n },\n success: function (response) {\n response = JSON.parse(response);\n console.log(response);\n // ...\n },\n error: function (response) {\n response = JSON.parse(response);\n console.log(response);\n // ...\n },\n after: function () {},\n});\n */\n\nmodule.exports = function () {\n 'use strict';\n\n /**\n * Function for checking is it FormData object to send.\n *\n * @param {Object} object to check\n * @return boolean\n */\n\n var isFormData = function isFormData(object) {\n return object instanceof FormData;\n };\n\n /**\n * Call AJAX request function\n *\n * @param {Object} data\n * @param {String} data.type 'GET' or 'POST' request type\n * @param {String} data.url request url\n * @param {String} data.data data to send\n * @param {Function} data.before call this function before request\n * @param {Function} data.progress callback function for progress\n * @param {Function} data.success success function\n * @param {Function} data.error error function\n * @param {Function} data.atfer call this function after request\n */\n var call = function call(data) {\n if (!data || !data.url) return;\n\n var XMLHTTP = window.XMLHttpRequest ? new window.XMLHttpRequest() : new window.ActiveXObject('Microsoft.XMLHTTP'),\n progressCallback = data.progress || null,\n successFunction = data.success || function () {},\n errorFunction = data.error || function () {},\n beforeFunction = data.before || null,\n afterFunction = data.after || null;\n\n data.async = true;\n data.type = data.type || 'GET';\n data.data = data.data || '';\n data['content-type'] = data['content-type'] || 'application/json; charset=utf-8';\n\n if (data.type === 'GET' && data.data) {\n data.url = /\\?/.test(data.url) ? data.url + '&' + data.data : data.url + '?' + data.data;\n }\n\n if (data.withCredentials) {\n XMLHTTP.withCredentials = true;\n }\n\n if (beforeFunction && typeof beforeFunction === 'function') {\n beforeFunction();\n }\n\n XMLHTTP.open(data.type, data.url, data.async);\n\n /**\n * If data is not FormData then create FormData\n */\n if (!isFormData(data.data)) {\n var requestData = new FormData();\n\n for (var key in data.data) {\n requestData.append(key, data.data[key]);\n }\n\n data.data = requestData;\n }\n\n /**\n * Add progress listener\n */\n if (progressCallback && typeof progressCallback === 'function') {\n XMLHTTP.upload.addEventListener('progress', function (e) {\n var percentage = parseInt(e.loaded / e.total * 100);\n\n progressCallback(percentage);\n }, false);\n }\n\n XMLHTTP.setRequestHeader('X-Requested-With', 'XMLHttpRequest');\n XMLHTTP.onreadystatechange = function () {\n /**\n * XMLHTTP.readyState\n * https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState\n *\n * 0 UNSENT Client has been created. open() not called yet.\n * 1 OPENED open() has been called.\n * 2 HEADERS_RECEIVED send() has been called, and headers and status are available.\n * 3 LOADING Downloading; responseText holds partial data.\n * 4 DONE The operation is complete.\n */\n if (XMLHTTP.readyState === 4) {\n if (XMLHTTP.status === 200) {\n successFunction(XMLHTTP.responseText);\n } else {\n errorFunction(XMLHTTP.responseText);\n }\n\n if (afterFunction && typeof afterFunction === 'function') {\n afterFunction();\n }\n }\n };\n\n XMLHTTP.send(data.data);\n };\n\n return {\n call: call\n };\n}();\n\n/***/ })\n/******/ ]);\n});\n\n\n// WEBPACK FOOTER //\n// lib/bundle.js"," \t// The module cache\n \tvar installedModules = {};\n\n \t// The require function\n \tfunction __webpack_require__(moduleId) {\n\n \t\t// Check if module is in cache\n \t\tif(installedModules[moduleId]) {\n \t\t\treturn installedModules[moduleId].exports;\n \t\t}\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\ti: moduleId,\n \t\t\tl: false,\n \t\t\texports: {}\n \t\t};\n\n \t\t// Execute the module function\n \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n \t\t// Flag the module as loaded\n \t\tmodule.l = true;\n\n \t\t// Return the exports of the module\n \t\treturn module.exports;\n \t}\n\n\n \t// expose the modules object (__webpack_modules__)\n \t__webpack_require__.m = modules;\n\n \t// expose the module cache\n \t__webpack_require__.c = installedModules;\n\n \t// define getter function for harmony exports\n \t__webpack_require__.d = function(exports, name, getter) {\n \t\tif(!__webpack_require__.o(exports, name)) {\n \t\t\tObject.defineProperty(exports, name, {\n \t\t\t\tconfigurable: false,\n \t\t\t\tenumerable: true,\n \t\t\t\tget: getter\n \t\t\t});\n \t\t}\n \t};\n\n \t// getDefaultExport function for compatibility with non-harmony modules\n \t__webpack_require__.n = function(module) {\n \t\tvar getter = module && module.__esModule ?\n \t\t\tfunction getDefault() { return module['default']; } :\n \t\t\tfunction getModuleExports() { return module; };\n \t\t__webpack_require__.d(getter, 'a', getter);\n \t\treturn getter;\n \t};\n\n \t// Object.prototype.hasOwnProperty.call\n \t__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };\n\n \t// __webpack_public_path__\n \t__webpack_require__.p = \"\";\n\n \t// Load entry module and return exports\n \treturn __webpack_require__(__webpack_require__.s = 0);\n\n\n\n// WEBPACK FOOTER //\n// webpack/bootstrap f7713f06c5e70dc741af","/**\n * AJAX module\n *\n * @example\najax.call({\n type: 'POST',\n url: '/target/url',\n data: {},\n before: function () {},\n progress: function (percentage) {\n console.log(percentage + '%');\n // ...\n },\n success: function (response) {\n response = JSON.parse(response);\n console.log(response);\n // ...\n },\n error: function (response) {\n response = JSON.parse(response);\n console.log(response);\n // ...\n },\n after: function () {},\n});\n */\n\nmodule.exports = function () {\n 'use strict';\n\n /**\n * Function for checking is it FormData object to send.\n *\n * @param {Object} object to check\n * @return boolean\n */\n let isFormData = function (object) {\n return object instanceof FormData;\n };\n\n /**\n * Call AJAX request function\n *\n * @param {Object} data\n * @param {String} data.type 'GET' or 'POST' request type\n * @param {String} data.url request url\n * @param {String} data.data data to send\n * @param {Function} data.before call this function before request\n * @param {Function} data.progress callback function for progress\n * @param {Function} data.success success function\n * @param {Function} data.error error function\n * @param {Function} data.atfer call this function after request\n */\n let call = function call(data) {\n if (!data || !data.url) return;\n\n let XMLHTTP = window.XMLHttpRequest ? new window.XMLHttpRequest() : new window.ActiveXObject('Microsoft.XMLHTTP'),\n progressCallback = data.progress || null,\n successFunction = data.success || function () {},\n errorFunction = data.error || function () {},\n beforeFunction = data.before || null,\n afterFunction = data.after || null;\n\n\n data.async = true;\n data.type = data.type || 'GET';\n data.data = data.data || '';\n data['content-type'] = data['content-type'] || 'application/json; charset=utf-8';\n\n if (data.type === 'GET' && data.data) {\n data.url = /\\?/.test(data.url) ? data.url + '&' + data.data : data.url + '?' + data.data;\n }\n\n if (data.withCredentials) {\n XMLHTTP.withCredentials = true;\n }\n\n if (beforeFunction && typeof beforeFunction === 'function') {\n beforeFunction();\n }\n\n XMLHTTP.open(data.type, data.url, data.async);\n\n /**\n * If data is not FormData then create FormData\n */\n if (!isFormData(data.data)) {\n let requestData = new FormData();\n\n for (let key in data.data) {\n requestData.append(key, data.data[key]);\n }\n\n data.data = requestData;\n }\n\n /**\n * Add progress listener\n */\n if (progressCallback && typeof progressCallback === 'function') {\n XMLHTTP.upload.addEventListener('progress', function (e) {\n let percentage = parseInt(e.loaded / e.total * 100);\n\n progressCallback(percentage);\n }, false);\n }\n\n XMLHTTP.setRequestHeader('X-Requested-With', 'XMLHttpRequest');\n XMLHTTP.onreadystatechange = function () {\n /**\n * XMLHTTP.readyState\n * https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState\n *\n * 0 UNSENT Client has been created. open() not called yet.\n * 1 OPENED open() has been called.\n * 2 HEADERS_RECEIVED send() has been called, and headers and status are available.\n * 3 LOADING Downloading; responseText holds partial data.\n * 4 DONE The operation is complete.\n */\n if (XMLHTTP.readyState === 4) {\n if (XMLHTTP.status === 200) {\n successFunction(XMLHTTP.responseText);\n } else {\n errorFunction(XMLHTTP.responseText);\n }\n\n if (afterFunction && typeof afterFunction === 'function') {\n afterFunction();\n }\n }\n };\n\n XMLHTTP.send(data.data);\n };\n\n return {\n call: call\n };\n}();\n\n\n\n// WEBPACK FOOTER //\n// ./src/ajax.js"],"sourceRoot":""} \ No newline at end of file +{"version":3,"sources":["webpack:///webpack/universalModuleDefinition","webpack:///lib/bundle.js","webpack:///webpack/bootstrap 483d132053b0a04b018a","webpack:///./src/ajax.js"],"names":["root","factory","exports","module","define","amd","this","modules","__webpack_require__","moduleId","installedModules","i","l","call","m","c","d","name","getter","o","Object","defineProperty","configurable","enumerable","get","n","__esModule","object","property","prototype","hasOwnProperty","p","s","isFormData","FormData","data","url","XMLHTTP","window","XMLHttpRequest","ActiveXObject","progressCallback","progress","successFunction","success","errorFunction","error","beforeFunction","before","afterFunction","after","async","type","test","withCredentials","open","requestData","key","append","upload","addEventListener","e","percentage","parseInt","loaded","total","setRequestHeader","onreadystatechange","readyState","responseText","JSON","parse","status","send"],"mappings":"CAAA,SAAAA,EAAAC,GACA,gBAAAC,UAAA,gBAAAC,QACAA,OAAAD,QAAAD,IACA,kBAAAG,gBAAAC,IACAD,UAAAH,GACA,gBAAAC,SACAA,QAAA,KAAAD,IAEAD,EAAA,KAAAC,KACCK,KAAA,WACD,MCAgB,UAAUC,GCN1B,QAAAC,GAAAC,GAGA,GAAAC,EAAAD,GACA,MAAAC,GAAAD,GAAAP,OAGA,IAAAC,GAAAO,EAAAD,IACAE,EAAAF,EACAG,GAAA,EACAV,WAUA,OANAK,GAAAE,GAAAI,KAAAV,EAAAD,QAAAC,IAAAD,QAAAM,GAGAL,EAAAS,GAAA,EAGAT,EAAAD,QAvBA,GAAAQ,KA4DA,OAhCAF,GAAAM,EAAAP,EAGAC,EAAAO,EAAAL,EAGAF,EAAAQ,EAAA,SAAAd,EAAAe,EAAAC,GACAV,EAAAW,EAAAjB,EAAAe,IACAG,OAAAC,eAAAnB,EAAAe,GACAK,cAAA,EACAC,YAAA,EACAC,IAAAN,KAMAV,EAAAiB,EAAA,SAAAtB,GACA,GAAAe,GAAAf,KAAAuB,WACA,WAA2B,MAAAvB,GAAA,SAC3B,WAAiC,MAAAA,GAEjC,OADAK,GAAAQ,EAAAE,EAAA,IAAAA,GACAA,GAIAV,EAAAW,EAAA,SAAAQ,EAAAC,GAAsD,MAAAR,QAAAS,UAAAC,eAAAjB,KAAAc,EAAAC,IAGtDpB,EAAAuB,EAAA,GAGAvB,IAAAwB,EAAA,KDgBM,SAAU7B,EAAQD,EAASM,GAEjC,YE5EAL,GAAOD,QAAU,WASf,GAAI+B,GAAa,SAAUN,GACzB,MAAOA,aAAkBO,UA0G3B,Q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xB,EAAQmC,OACV7B,EAAgB0B,GAEhBxB,EAAcwB,GAGZpB,GAA0C,kBAAlBA,IAC1BA,MAKNZ,EAAQoC,KAAKtC,EAAKA","file":"./lib/bundle.js","sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"ajax\"] = factory();\n\telse\n\t\troot[\"ajax\"] = factory();\n})(this, function() {\nreturn \n\n\n// WEBPACK FOOTER //\n// webpack/universalModuleDefinition","(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"ajax\"] = factory();\n\telse\n\t\troot[\"ajax\"] = factory();\n})(this, function() {\nreturn /******/ (function(modules) { // webpackBootstrap\n/******/ \t// The module cache\n/******/ \tvar installedModules = {};\n/******/\n/******/ \t// The require function\n/******/ \tfunction __webpack_require__(moduleId) {\n/******/\n/******/ \t\t// Check if module is in cache\n/******/ \t\tif(installedModules[moduleId]) {\n/******/ \t\t\treturn installedModules[moduleId].exports;\n/******/ \t\t}\n/******/ \t\t// Create a new module (and put it into the cache)\n/******/ \t\tvar module = installedModules[moduleId] = {\n/******/ \t\t\ti: moduleId,\n/******/ \t\t\tl: false,\n/******/ \t\t\texports: {}\n/******/ \t\t};\n/******/\n/******/ \t\t// Execute the module function\n/******/ \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n/******/\n/******/ \t\t// Flag the module as loaded\n/******/ \t\tmodule.l = true;\n/******/\n/******/ \t\t// Return the exports of the module\n/******/ \t\treturn module.exports;\n/******/ \t}\n/******/\n/******/\n/******/ \t// expose the modules object (__webpack_modules__)\n/******/ \t__webpack_require__.m = modules;\n/******/\n/******/ \t// expose the module cache\n/******/ \t__webpack_require__.c = installedModules;\n/******/\n/******/ \t// define getter function for harmony exports\n/******/ \t__webpack_require__.d = function(exports, name, getter) {\n/******/ \t\tif(!__webpack_require__.o(exports, name)) {\n/******/ \t\t\tObject.defineProperty(exports, name, {\n/******/ \t\t\t\tconfigurable: false,\n/******/ \t\t\t\tenumerable: true,\n/******/ \t\t\t\tget: getter\n/******/ \t\t\t});\n/******/ \t\t}\n/******/ \t};\n/******/\n/******/ \t// getDefaultExport function for compatibility with non-harmony modules\n/******/ \t__webpack_require__.n = function(module) {\n/******/ \t\tvar getter = module && module.__esModule ?\n/******/ \t\t\tfunction getDefault() { return module['default']; } :\n/******/ \t\t\tfunction getModuleExports() { return module; };\n/******/ \t\t__webpack_require__.d(getter, 'a', getter);\n/******/ \t\treturn getter;\n/******/ \t};\n/******/\n/******/ \t// Object.prototype.hasOwnProperty.call\n/******/ \t__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };\n/******/\n/******/ \t// __webpack_public_path__\n/******/ \t__webpack_require__.p = \"\";\n/******/\n/******/ \t// Load entry module and return exports\n/******/ \treturn __webpack_require__(__webpack_require__.s = 0);\n/******/ })\n/************************************************************************/\n/******/ ([\n/* 0 */\n/***/ (function(module, exports, __webpack_require__) {\n\n\"use strict\";\n\n\n/**\n * AJAX module\n */\nmodule.exports = function () {\n 'use strict';\n\n /**\n * Function for checking is it FormData object to send.\n *\n * @param {Object} object to check\n * @return boolean\n */\n\n var isFormData = function isFormData(object) {\n return object instanceof FormData;\n };\n\n /**\n * Call AJAX request function\n *\n * @param {Object} data\n * @param {String} data.type 'GET' or 'POST' request type\n * @param {String} data.url request url\n * @param {String} data.data data to send\n * @param {Function} data.before call this function before request\n * @param {Function} data.progress callback function for progress\n * @param {Function} data.success success function\n * @param {Function} data.error error function\n * @param {Function} data.atfer call this function after request\n */\n var call = function call(data) {\n if (!data || !data.url) return;\n\n var XMLHTTP = window.XMLHttpRequest ? new window.XMLHttpRequest() : new window.ActiveXObject('Microsoft.XMLHTTP'),\n progressCallback = data.progress || null,\n successFunction = data.success || function () {},\n errorFunction = data.error || function () {},\n beforeFunction = data.before || null,\n afterFunction = data.after || null;\n\n data.async = true;\n data.type = data.type || 'GET';\n data.data = data.data || '';\n data['content-type'] = data['content-type'] || 'application/json; charset=utf-8';\n\n if (data.type === 'GET' && data.data) {\n data.url = /\\?/.test(data.url) ? data.url + '&' + data.data : data.url + '?' + data.data;\n }\n\n if (data.withCredentials) {\n XMLHTTP.withCredentials = true;\n }\n\n if (beforeFunction && typeof beforeFunction === 'function') {\n beforeFunction();\n }\n\n XMLHTTP.open(data.type, data.url, data.async);\n\n /**\n * If data is not FormData then create FormData\n */\n if (!isFormData(data.data)) {\n var requestData = new FormData();\n\n for (var key in data.data) {\n requestData.append(key, data.data[key]);\n }\n\n data.data = requestData;\n }\n\n /**\n * Add progress listener\n */\n if (progressCallback && typeof progressCallback === 'function') {\n XMLHTTP.upload.addEventListener('progress', function (e) {\n var percentage = parseInt(e.loaded / e.total * 100);\n\n progressCallback(percentage);\n }, false);\n }\n\n XMLHTTP.setRequestHeader('X-Requested-With', 'XMLHttpRequest');\n XMLHTTP.onreadystatechange = function () {\n /**\n * XMLHTTP.readyState\n * https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState\n *\n * 0 UNSENT Client has been created. open() not called yet.\n * 1 OPENED open() has been called.\n * 2 HEADERS_RECEIVED send() has been called, and headers and status are available.\n * 3 LOADING Downloading; responseText holds partial data.\n * 4 DONE The operation is complete.\n */\n if (XMLHTTP.readyState === 4) {\n var responseText = XMLHTTP.responseText;\n\n try {\n responseText = JSON.parse(responseText);\n } catch (e) {\n // Oh well, but whatever...\n }\n\n if (XMLHTTP.status === 200) {\n successFunction(responseText);\n } else {\n errorFunction(responseText);\n }\n\n if (afterFunction && typeof afterFunction === 'function') {\n afterFunction();\n }\n }\n };\n\n XMLHTTP.send(data.data);\n };\n\n return {\n call: call\n };\n}();\n\n/***/ })\n/******/ ]);\n});\n\n\n// WEBPACK FOOTER //\n// lib/bundle.js"," \t// The module cache\n \tvar installedModules = {};\n\n \t// The require function\n \tfunction __webpack_require__(moduleId) {\n\n \t\t// Check if module is in cache\n \t\tif(installedModules[moduleId]) {\n \t\t\treturn installedModules[moduleId].exports;\n \t\t}\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\ti: moduleId,\n \t\t\tl: false,\n \t\t\texports: {}\n \t\t};\n\n \t\t// Execute the module function\n \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n \t\t// Flag the module as loaded\n \t\tmodule.l = true;\n\n \t\t// Return the exports of the module\n \t\treturn module.exports;\n \t}\n\n\n \t// expose the modules object (__webpack_modules__)\n \t__webpack_require__.m = modules;\n\n \t// expose the module cache\n \t__webpack_require__.c = installedModules;\n\n \t// define getter function for harmony exports\n \t__webpack_require__.d = function(exports, name, getter) {\n \t\tif(!__webpack_require__.o(exports, name)) {\n \t\t\tObject.defineProperty(exports, name, {\n \t\t\t\tconfigurable: false,\n \t\t\t\tenumerable: true,\n \t\t\t\tget: getter\n \t\t\t});\n \t\t}\n \t};\n\n \t// getDefaultExport function for compatibility with non-harmony modules\n \t__webpack_require__.n = function(module) {\n \t\tvar getter = module && module.__esModule ?\n \t\t\tfunction getDefault() { return module['default']; } :\n \t\t\tfunction getModuleExports() { return module; };\n \t\t__webpack_require__.d(getter, 'a', getter);\n \t\treturn getter;\n \t};\n\n \t// Object.prototype.hasOwnProperty.call\n \t__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };\n\n \t// __webpack_public_path__\n \t__webpack_require__.p = \"\";\n\n \t// Load entry module and return exports\n \treturn __webpack_require__(__webpack_require__.s = 0);\n\n\n\n// WEBPACK FOOTER //\n// webpack/bootstrap 483d132053b0a04b018a","/**\n * AJAX module\n */\nmodule.exports = function () {\n 'use strict';\n\n /**\n * Function for checking is it FormData object to send.\n *\n * @param {Object} object to check\n * @return boolean\n */\n let isFormData = function (object) {\n return object instanceof FormData;\n };\n\n /**\n * Call AJAX request function\n *\n * @param {Object} data\n * @param {String} data.type 'GET' or 'POST' request type\n * @param {String} data.url request url\n * @param {String} data.data data to send\n * @param {Function} data.before call this function before request\n * @param {Function} data.progress callback function for progress\n * @param {Function} data.success success function\n * @param {Function} data.error error function\n * @param {Function} data.atfer call this function after request\n */\n let call = function call(data) {\n if (!data || !data.url) return;\n\n let XMLHTTP = window.XMLHttpRequest ? new window.XMLHttpRequest() : new window.ActiveXObject('Microsoft.XMLHTTP'),\n progressCallback = data.progress || null,\n successFunction = data.success || function () {},\n errorFunction = data.error || function () {},\n beforeFunction = data.before || null,\n afterFunction = data.after || null;\n\n\n data.async = true;\n data.type = data.type || 'GET';\n data.data = data.data || '';\n data['content-type'] = data['content-type'] || 'application/json; charset=utf-8';\n\n if (data.type === 'GET' && data.data) {\n data.url = /\\?/.test(data.url) ? data.url + '&' + data.data : data.url + '?' + data.data;\n }\n\n if (data.withCredentials) {\n XMLHTTP.withCredentials = true;\n }\n\n if (beforeFunction && typeof beforeFunction === 'function') {\n beforeFunction();\n }\n\n XMLHTTP.open(data.type, data.url, data.async);\n\n /**\n * If data is not FormData then create FormData\n */\n if (!isFormData(data.data)) {\n let requestData = new FormData();\n\n for (let key in data.data) {\n requestData.append(key, data.data[key]);\n }\n\n data.data = requestData;\n }\n\n /**\n * Add progress listener\n */\n if (progressCallback && typeof progressCallback === 'function') {\n XMLHTTP.upload.addEventListener('progress', function (e) {\n let percentage = parseInt(e.loaded / e.total * 100);\n\n progressCallback(percentage);\n }, false);\n }\n\n XMLHTTP.setRequestHeader('X-Requested-With', 'XMLHttpRequest');\n XMLHTTP.onreadystatechange = function () {\n /**\n * XMLHTTP.readyState\n * https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState\n *\n * 0 UNSENT Client has been created. open() not called yet.\n * 1 OPENED open() has been called.\n * 2 HEADERS_RECEIVED send() has been called, and headers and status are available.\n * 3 LOADING Downloading; responseText holds partial data.\n * 4 DONE The operation is complete.\n */\n if (XMLHTTP.readyState === 4) {\n let responseText = XMLHTTP.responseText;\n\n try {\n responseText = JSON.parse(responseText);\n } catch (e) {\n // Oh well, but whatever...\n }\n\n if (XMLHTTP.status === 200) {\n successFunction(responseText);\n } else {\n errorFunction(responseText);\n }\n\n if (afterFunction && typeof afterFunction === 'function') {\n afterFunction();\n }\n }\n };\n\n XMLHTTP.send(data.data);\n };\n\n return {\n call: call\n };\n}();\n\n\n\n// WEBPACK FOOTER //\n// ./src/ajax.js"],"sourceRoot":""} \ No newline at end of file diff --git a/package.json b/package.json index b7c23da..25080ae 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "codex.ajax", "exportModuleName": "ajax", - "version": "1.0.0", + "version": "2.0.0", "description": "Native AJAX module", "main": "./lib/bundle.js", "scripts": { @@ -25,6 +25,7 @@ "babel-loader": "^7.1.2", "babel-preset-es2015": "^6.24.1", "eslint": "^4.9.0", + "eslint-loader": "^1.9.0", "webpack": "^3.8.1" } } diff --git a/src/ajax.js b/src/ajax.js index 420e26c..3da64fc 100644 --- a/src/ajax.js +++ b/src/ajax.js @@ -1,30 +1,6 @@ /** * AJAX module - * - * @example -ajax.call({ - type: 'POST', - url: '/target/url', - data: {}, - before: function () {}, - progress: function (percentage) { - console.log(percentage + '%'); - // ... - }, - success: function (response) { - response = JSON.parse(response); - console.log(response); - // ... - }, - error: function (response) { - response = JSON.parse(response); - console.log(response); - // ... - }, - after: function () {}, -}); */ - module.exports = function () { 'use strict'; @@ -42,14 +18,14 @@ module.exports = function () { * Call AJAX request function * * @param {Object} data - * @param {String} data.type 'GET' or 'POST' request type - * @param {String} data.url request url - * @param {String} data.data data to send - * @param {Function} data.before call this function before request - * @param {Function} data.progress callback function for progress - * @param {Function} data.success success function - * @param {Function} data.error error function - * @param {Function} data.atfer call this function after request + * @param {String} data.type 'GET' or 'POST' request type + * @param {String} data.url request url + * @param {String} data.data data to send + * @param {Function} data.before call this function before request + * @param {Function} data.progress callback function for progress + * @param {Function} data.success success function + * @param {Function} data.error error function + * @param {Function} data.atfer call this function after request */ let call = function call(data) { if (!data || !data.url) return; @@ -118,10 +94,18 @@ module.exports = function () { * 4 DONE The operation is complete. */ if (XMLHTTP.readyState === 4) { + let responseText = XMLHTTP.responseText; + + try { + responseText = JSON.parse(responseText); + } catch (e) { + // Oh well, but whatever... + } + if (XMLHTTP.status === 200) { - successFunction(XMLHTTP.responseText); + successFunction(responseText); } else { - errorFunction(XMLHTTP.responseText); + errorFunction(responseText); } if (afterFunction && typeof afterFunction === 'function') {