axios-mock-adapter
Version:
Axios adapter that allows to easily mock requests
2 lines • 8.27 kB
JavaScript
/*! For license information please see axios-mock-adapter.min.js.LICENSE.txt */
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("axios")):"function"==typeof define&&define.amd?define(["axios"],e):"object"==typeof exports?exports.AxiosMockAdapter=e(require("axios")):t.AxiosMockAdapter=e(t.axios)}(self,(t=>{return e={63:t=>{"use strict";t.exports=function t(e,r){if(e===r)return!0;if(e&&r&&"object"==typeof e&&"object"==typeof r){if(e.constructor!==r.constructor)return!1;var n,o,i;if(Array.isArray(e)){if((n=e.length)!=r.length)return!1;for(o=n;0!=o--;)if(!t(e[o],r[o]))return!1;return!0}if(e.constructor===RegExp)return e.source===r.source&&e.flags===r.flags;if(e.valueOf!==Object.prototype.valueOf)return e.valueOf()===r.valueOf();if(e.toString!==Object.prototype.toString)return e.toString()===r.toString();if((n=(i=Object.keys(e)).length)!==Object.keys(r).length)return!1;for(o=n;0!=o--;)if(!Object.prototype.hasOwnProperty.call(r,i[o]))return!1;for(o=n;0!=o--;){var s=i[o];if(!t(e[s],r[s]))return!1}return!0}return e!=e&&r!=r}},738:t=>{t.exports=function(t){return null!=t&&null!=t.constructor&&"function"==typeof t.constructor.isBuffer&&t.constructor.isBuffer(t)}},141:(t,e,r)=>{"use strict";var n=r(555);function o(t,e){return{status:t[0],data:(r=t[1],n.isArrayBuffer(r)||n.isBuffer(r)||n.isStream(r)||n.isBlob(r)?r:n.isObjectOrArray(r)?JSON.parse(JSON.stringify(r)):r),headers:t[2],config:e,request:{responseURL:e.url}};var r}function i(t,e,r,n){var o=n.baseURL;if(n.baseURL&&!/^https?:/.test(n.baseURL)&&(o=void 0),"function"==typeof t.originalAdapter)return t.originalAdapter(n).then(e,r);t.axiosInstanceWithoutInterceptors(Object.assign({},n,{baseURL:o,adapter:t.originalAdapter,transformRequest:[],transformResponse:[]})).then(e,r)}function s(t,e){var r;return 8===e.length&&(r=e[7]),r||t.delayResponse}t.exports=function(t,e,r,a){var u=a.url||"";a.baseURL&&u.substr(0,a.baseURL.length)===a.baseURL&&(u=u.slice(a.baseURL.length)),delete a.adapter,t.history[a.method].push(a);var c=n.findHandler(t.handlers,a.method,u,a.data,a.params,a.headers&&"AxiosHeaders"===a.headers.constructor.name?Object.assign({},a.headers):a.headers,a.baseURL);if(c)if(7===c.length&&n.purgeIfReplyOnce(t,c),2===c.length)i(t,e,r,a);else if("function"!=typeof c[3])n.settle(e,r,o(c.slice(3),a),s(t,c));else{var f=c[3](a);"function"!=typeof f.then?n.settle(e,r,o(f,a),s(t,c)):f.then((function(i){i.config&&i.status?n.settle(e,r,o([i.status,i.data,i.headers],i.config),0):n.settle(e,r,o(i,a),s(t,c))}),(function(e){t.delayResponse>0?setTimeout((function(){r(e)}),s(t,c)):r(e)}))}else switch(t.onNoMatch){case"passthrough":i(t,e,r,a);break;case"throwException":throw n.createCouldNotFindMockError(a);default:n.settle(e,r,{status:404,config:a},t.delayResponse)}}},138:(t,e,r)=>{"use strict";var n=r(141),o=r(555),i=["get","post","head","delete","patch","put","options","list","link","unlink"];function s(){return i.reduce((function(t,e){return t[e]=[],t}),{})}function a(){u.call(this),c.call(this)}function u(){this.handlers=s()}function c(){this.history=s()}function f(t,e){if(a.call(this),!t)throw new Error("Please provide an instance of axios to mock");this.axiosInstance=t,this.axiosInstanceWithoutInterceptors=t.create?t.create():void 0,this.originalAdapter=t.defaults.adapter,this.delayResponse=e&&e.delayResponse>0?e.delayResponse:null,this.onNoMatch=e&&e.onNoMatch||null,t.defaults.adapter=this.adapter.call(this)}function l(t,e,r){if("any"===t)i.forEach((function(t){e[t].push(r)}));else{var n=function(t,e,r){for(var n=-1,i=0;i<e[t].length;i+=1){var s=e[t][i],a=7===s.length;(s[0]instanceof RegExp&&r[0]instanceof RegExp?String(s[0])===String(r[0]):s[0]===r[0])&&o.isEqual(s[1],r[1])&&o.isEqual(s[2],r[2])&&!a&&(n=i)}return n}(t,e,r);n>-1&&r.length<7?e[t].splice(n,1,r):e[t].push(r)}}f.prototype.adapter=function(){return function(t){var e=this;return new Promise((function(r,o){n(e,r,o,t)}))}.bind(this)},f.prototype.restore=function(){this.axiosInstance&&(this.axiosInstance.defaults.adapter=this.originalAdapter,this.axiosInstance=void 0)},f.prototype.reset=a,f.prototype.resetHandlers=u,f.prototype.resetHistory=c,i.concat("any").forEach((function(t){var e="on"+t.charAt(0).toUpperCase()+t.slice(1);f.prototype[e]=function(e,r,n){var i=this;function s(o,s,a){var u=[e,r,n,o,s,a];return l(t,i.handlers,u),i}function a(o,s,a){var u=[e,r,n,o,s,a,!0];return l(t,i.handlers,u),i}return e=void 0===e?/.*/:e,{reply:s,replyOnce:a,withDelayInMs:function(o){return function(s,a,u){!function(o,s,a,u){var c=[e,r,n,s,a,u,!1,o];l(t,i.handlers,c)}(o,s,a,u)}},passThrough:function(){var n=[e,r];return l(t,i.handlers,n),i},abortRequest:function(){return s((function(t){var e=o.createAxiosError("Request aborted",t,void 0,"ECONNABORTED");return Promise.reject(e)}))},abortRequestOnce:function(){return a((function(t){var e=o.createAxiosError("Request aborted",t,void 0,"ECONNABORTED");return Promise.reject(e)}))},networkError:function(){return s((function(t){var e=o.createAxiosError("Network Error",t);return Promise.reject(e)}))},networkErrorOnce:function(){return a((function(t){var e=o.createAxiosError("Network Error",t);return Promise.reject(e)}))},timeout:function(){return s((function(t){var e=o.createAxiosError(t.timeoutErrorMessage||"timeout of "+t.timeout+"ms exceeded",t,void 0,t.transitional&&t.transitional.clarifyTimeoutError?"ETIMEDOUT":"ECONNABORTED");return Promise.reject(e)}))},timeoutOnce:function(){return a((function(t){var e=o.createAxiosError(t.timeoutErrorMessage||"timeout of "+t.timeout+"ms exceeded",t,void 0,t.transitional&&t.transitional.clarifyTimeoutError?"ETIMEDOUT":"ECONNABORTED");return Promise.reject(e)}))}}}})),t.exports=f,t.exports.default=f},376:t=>{t.exports=function(t){return"undefined"!=typeof Blob&&(t instanceof Blob||"[object Blob]"===Object.prototype.toString.call(t))}},555:(t,e,r)=>{"use strict";var n=r(300),o=r(63),i=r(738),s=r(376),a=Object.prototype.toString;function u(t,e){for(var r=t.length,n=0;n<r;n++){var o=t[n];if(e(o))return o}}function c(t){return"[object Function]"===a.call(t)}function f(t){return null!==t&&"object"==typeof t}function l(t,e){return t?t.replace(/\/+$/,"")+"/"+e.replace(/^\/+/,""):e}function p(t,e){return("/"===t[0]?t.substr(1):t)===("/"===e[0]?e.substr(1):e)}function d(t,e,r,n){if(["delete","get","head","options"].indexOf(t.toLowerCase())>=0){var o=n?n.data:void 0;return h(r,n?n.params:void 0)&&y(e,o)}return y(e,n)}function h(t,e){return void 0===e||("function"==typeof e.asymmetricMatch?e.asymmetricMatch(t):o(t,e))}function y(t,e){if(void 0===e)return!0;var r;try{r=JSON.parse(t)}catch(t){}return h(r||t,e)}function v(t,e,r,o){if("function"==typeof n.AxiosError)return n.AxiosError.from(new Error(t),o,e,null,r);var i=new Error(t);return i.isAxiosError=!0,i.config=e,void 0!==r&&(i.response=r),void 0!==o&&(i.code=o),i.toJSON=function(){return{message:this.message,name:this.name,description:this.description,number:this.number,fileName:this.fileName,lineNumber:this.lineNumber,columnNumber:this.columnNumber,stack:this.stack,config:this.config,code:this.code}},i}t.exports={find:u,findHandler:function(t,e,r,n,o,i,s){return u(t[e.toLowerCase()],(function(t){return"string"==typeof t[0]?(p(r,t[0])||p(l(s,r),t[0]))&&d(e,n,o,t[1])&&h(i,t[2]):t[0]instanceof RegExp?(t[0].test(r)||t[0].test(l(s,r)))&&d(e,n,o,t[1])&&h(i,t[2]):void 0}))},purgeIfReplyOnce:function(t,e){Object.keys(t.handlers).forEach((function(r){var n=t.handlers[r].indexOf(e);n>-1&&t.handlers[r].splice(n,1)}))},settle:function t(e,r,n,o){o>0?setTimeout(t,o,e,r,n):!n.config.validateStatus||n.config.validateStatus(n.status)?e(n):r(v("Request failed with status code "+n.status,n.config,n))},isStream:function(t){return f(t)&&c(t.pipe)},isArrayBuffer:function(t){return"[object ArrayBuffer]"===a.call(t)},isFunction:c,isObjectOrArray:f,isBuffer:i,isBlob:s,isBodyOrParametersMatching:d,isEqual:o,createAxiosError:v,createCouldNotFindMockError:function(t){var e="Could not find mock for: \n"+JSON.stringify(t,["method","url"],2),r=new Error(e);return r.isCouldNotFindMockError=!0,r.url=t.url,r.method=t.method,r}}},300:e=>{"use strict";e.exports=t}},r={},function t(n){var o=r[n];if(void 0!==o)return o.exports;var i=r[n]={exports:{}};return e[n](i,i.exports,t),i.exports}(138);var e,r}));