api-mock-js
Version:
Promise based AJAX library
2 lines (1 loc) • 3.59 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.Api=t()}(this,function(){"use strict";var e=Object.assign||function(e){for(var t,n=1,o=arguments.length;n<o;n++){t=arguments[n];for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])}return e},t=function(){return"undefined"!=typeof console};var n=function(e,t){return Object.keys(e).forEach(function(n){return t[n]=e[n]})},o=function(e){return/\?/.test(e)?"&":"?"},r=document.getElementsByTagName("head")[0];function i(e){window[e]=void 0}function u(e){r.removeChild(document.getElementById(e))}function a(e){var n=e.href,a=e.timeout,c=e.callbackName,s=e.callbackId,f=void 0===s?"jsonp_"+Date.now()+"_"+Math.ceil(1e5*Math.random()):s,d=""+n+o(n)+c+"="+f;return new Promise(function(e,n){var o,c,s,l=setTimeout(function(){!function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];t()&&console.error.apply(console,e)}("JSONP request to "+d+" timed out"),n(d),i(f),u(f)},a);window[f]=function(t){e(t),clearTimeout(l),i(f),u(f)},o=f,c=d,(s=document.createElement("script")).id=o,s.setAttribute("src",c),r.appendChild(s)})}function c(e){var t=e.url,n=e.search,o=e.input,r=e.href,i=e.dataType,u=e.methods,a=e.async,c=e.withCredentials,s=e.header,f=e.timeout,d=e.xhrEvent,l=new XMLHttpRequest;return new Promise(function(e,p){var h,m,y,v,g="json"===i?n.slice(1):(h=o,m=new FormData,Object.keys(h).forEach(function(e){return m.append(e,h[e])}),m);l.withCredentials=c,l.timeout=f,l.onreadystatechange=function(){4===l.readyState&&(200===l.status?e(function(e){if("string"==typeof e&&e.length)try{return JSON.parse(e)}catch(t){return e}return null}(l.response)):p(l))},"GET"!==u&&"DELETE"!==u||(t=r),l.open(u,t,a),function(e,t){for(var n in t)e.setRequestHeader(n,t[n])}(l,s),l.send(g),y=l,v=d,Object.keys(v).forEach(function(e){y.addEventListener(e,v[e])}),"function"==typeof v.uploadProgress&&y.upload&&y.upload.addEventListener("progress",v.uploadProgress)})}var s=/^([a-z][a-z\d\+\-\.]*:)?\/\//i,f={domain:window.location.href,methods:"GET",dataType:"json",timeout:1e4,useMock:!1,input:{},mock:{},callbackName:"callback",callbackId:void 0,withCredentials:!1,urlModel:0,debug:!1,async:!0,filter:function(e){return e},header:{},xhrEvent:{}},d=function(){function e(e){this.mixins(e)}return e.prototype.mixins=function(e){n(e,this)},Object.defineProperty(e.prototype,"href",{get:function(){return this.url+this.search},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"search",{get:function(){var e,t=(e=this.filter(this.input),Object.keys(e).map(function(t){return t+"="+e[t]}).join("&"));return t?o(this.url)+t:""},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"url",{get:function(){var e=this.urlModel,t=this.domain,n=this.namespace;return 1===e?t:s.test(n)?n:t+n},enumerable:!0,configurable:!0}),e.prototype.send=function(){return this.debug&&function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];t()&&console.log.apply(console,e)}(""+this.namespace,this.input),this.useMock?Promise.resolve(this.mock):"jsonp"===this.dataType?a(this):c(this)},e}(),l={},p={};return{define:function(e,n){void 0===n&&(n={}),l[e]&&function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];t()&&console.warn.apply(console,e)}("redefine "+e),l[e]=new d(n)},config:function(e){void 0===e&&(e={}),n(e,p)},require:function(t,n,o){void 0===n&&(n={}),void 0===o&&(o={}),l[t]||this.define(t);var r=l[t];return r.mixins(e({},f,p,r,o,{namespace:t,input:n})),r.send()},get:function(e){return l[e]}}});