axios-mock-adapter
Version:
Axios adapter that allows to easily mock requests
2 lines • 8.37 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={17: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 o,n,s;if(Array.isArray(e)){if((o=e.length)!=r.length)return!1;for(n=o;0!=n--;)if(!t(e[n],r[n]))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((o=(s=Object.keys(e)).length)!==Object.keys(r).length)return!1;for(n=o;0!=n--;)if(!Object.prototype.hasOwnProperty.call(r,s[n]))return!1;for(n=o;0!=n--;){var i=s[n];if(!t(e[i],r[i]))return!1}return!0}return e!=e&&r!=r}},206:t=>{t.exports=function(t){return null!=t&&null!=t.constructor&&"function"==typeof t.constructor.isBuffer&&t.constructor.isBuffer(t)}},938:(t,e,r)=>{"use strict";const o=r(413);function n(t,e){let r=e.baseURL;return r&&!/^https?:/.test(r)&&(r=void 0),"function"==typeof t.originalAdapter?t.originalAdapter(e):t.axiosInstanceWithoutInterceptors(Object.assign({},e,{baseURL:r,adapter:t.originalAdapter,transformRequest:[],transformResponse:[]}))}t.exports=async function(t,e){let r=e.url||"";e.baseURL&&r.substr(0,e.baseURL.length)===e.baseURL&&(r=r.slice(e.baseURL.length)),delete e.adapter,t.history.push(e);const s=o.findHandler(t.handlers,e.method,r,e.data,e.params,e.headers&&"AxiosHeaders"===e.headers.constructor.name?Object.assign({},e.headers.toJSON()):e.headers,e.baseURL);if(s)return s.replyOnce&&o.purgeIfReplyOnce(t,s),s.passThrough?n(t,e):o.settle(e,s.response,function(t,e){return"number"==typeof e.delay?e.delay:t.delayResponse}(t,s));switch(t.onNoMatch){case"passthrough":return n(t,e);case"throwException":throw o.createCouldNotFindMockError(e);default:return o.settle(e,{status:404},t.delayResponse)}}},44:(t,e,r)=>{"use strict";const o=r(938),n=r(413),s=["get","post","head","delete","patch","put","options","list","link","unlink"];function i(){const t=[];return s.forEach((function(e){Object.defineProperty(t,e,{get:()=>t.filter((function(t){return!t.method||t.method===e}))})})),t}class a{constructor(t,e={}){if(this.reset(),!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.delayResponse>0?e.delayResponse:null,this.onNoMatch=e.onNoMatch||null,t.defaults.adapter=this.adapter()}adapter(){return t=>o(this,t)}restore(){this.axiosInstance&&(this.axiosInstance.defaults.adapter=this.originalAdapter,this.axiosInstance=void 0)}reset(){this.resetHandlers(),this.resetHistory()}resetHandlers(){this.handlers?this.handlers.length=0:this.handlers=i()}resetHistory(){this.history?this.history.length=0:this.history=i()}}const c=["any","get","delete","head","options"],u=["headers","params","data"];function f(t,e){for(const r in e)if(!u.includes(r))throw new Error(`Invalid config property ${JSON.stringify(r)} provided to ${l(t)}. Config: ${JSON.stringify(e)}`);return e}function l(t){return`on${t.charAt(0).toUpperCase()}${t.slice(1)}`}function d(t,e,r){if("any"===t)e.push(r);else{const t=function(t,e){let r=-1;for(let o=0;o<t.length;o+=1){const s=t[o],i=s.url instanceof RegExp&&e.url instanceof RegExp?String(s.url)===String(e.url):s.url===e.url;(!s.method||s.method===e.method)&&i&&n.isEqual(s.params,e.params)&&n.isEqual(s.data,e.data)&&n.isEqual(s.headers,e.headers)&&!s.replyOnce&&(r=o)}return r}(e,r);t>-1&&!r.replyOnce?e.splice(t,1,r):e.push(r)}}s.concat("any").forEach((function(t){a.prototype[l(t)]=function(e,r,o){const s=this;let i;e=void 0===e?/.*/:e;const a=function(t,e,r){return c.includes(t)?f(t,e||{}):f(t,Object.assign({},r,{data:e}))}(t,r,o);function u(r,o,n){const c={url:e,method:"any"===t?void 0:t,params:a.params,data:a.data,headers:a.headers,replyOnce:!1,delay:i,response:"function"==typeof r?r:[r,o,n]};return d(t,s.handlers,c),s}function l(r,o,n){const c={url:e,method:"any"===t?void 0:t,params:a.params,data:a.data,headers:a.headers,replyOnce:!0,delay:i,response:"function"==typeof r?r:[r,o,n]};return d(t,s.handlers,c),s}const p={reply:u,replyOnce:l,withDelayInMs:function(t){i=t;const e=p.reply.bind(p);return Object.assign(e,p),e},passThrough(){const r={passThrough:!0,method:"any"===t?void 0:t,url:e,params:a.params,data:a.data,headers:a.headers};return d(t,s.handlers,r),s},abortRequest:()=>u((async function(t){throw n.createAxiosError("Request aborted",t,void 0,"ECONNABORTED")})),abortRequestOnce:()=>l((async function(t){throw n.createAxiosError("Request aborted",t,void 0,"ECONNABORTED")})),networkError:()=>u((async function(t){throw n.createAxiosError("Network Error",t)})),networkErrorOnce:()=>l((async function(t){throw n.createAxiosError("Network Error",t)})),timeout:()=>u((async function(t){throw n.createAxiosError(t.timeoutErrorMessage||`timeout of ${t.timeout}ms exceeded`,t,void 0,t.transitional&&t.transitional.clarifyTimeoutError?"ETIMEDOUT":"ECONNABORTED")})),timeoutOnce:()=>l((async function(t){throw n.createAxiosError(t.timeoutErrorMessage||`timeout of ${t.timeout}ms exceeded`,t,void 0,t.transitional&&t.transitional.clarifyTimeoutError?"ETIMEDOUT":"ECONNABORTED")}))};return p}})),t.exports=a,t.exports.default=a},58:t=>{t.exports=function(t){return"undefined"!=typeof Blob&&(t instanceof Blob||"[object Blob]"===Object.prototype.toString.call(t))}},413:(t,e,r)=>{"use strict";const o=r(742),n=r(17),s=r(206),i=r(58),a=Object.prototype.toString;function c(t,e){const r=t.length;for(let o=0;o<r;o++){const r=t[o];if(e(r))return r}}function u(t){return null!==t&&"object"==typeof t}function f(t,e){return t?`${t.replace(/\/+$/,"")}/${e.replace(/^\/+/,"")}`:e}function l(t,e){return("/"===t[0]?t.substr(1):t)===("/"===e[0]?e.substr(1):e)}function d(t,e,r){return p(e,r.params)&&function(t,e){if(void 0===e)return!0;let r;try{r=JSON.parse(t)}catch(t){}return p(r||t,e)}(t,r.data)}function p(t,e){return void 0===e||("function"==typeof e.asymmetricMatch?e.asymmetricMatch(t):n(t,e))}function h(t){return e=t,"[object ArrayBuffer]"===a.call(e)||s(t)||function(t){return u(t)&&function(t){return"[object Function]"===a.call(t)}(t.pipe)}(t)||i(t)?t:u(t)?JSON.parse(JSON.stringify(t)):t;var e}function y(t,e,r,n){if("function"==typeof o.AxiosError)return o.AxiosError.from(new Error(t),n,e,null,r);const s=new Error(t);return s.isAxiosError=!0,s.config=e,void 0!==r&&(s.response=r),void 0!==n&&(s.code=n),s.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}},s}t.exports={find:c,findHandler:function(t,e,r,o,n,s,i){return c(t[e.toLowerCase()],(function(t){let e=!1;return"string"==typeof t.url?e=l(r,t.url)||l(f(i,r),t.url):t.url instanceof RegExp&&(e=t.url.test(r)||t.url.test(f(i,r))),e&&d(o,n,t)&&p(s,t.headers)}))},purgeIfReplyOnce:function(t,e){const r=t.handlers.indexOf(e);r>-1&&t.handlers.splice(r,1)},settle:async function(t,e,r){r>0&&await new Promise((t=>setTimeout(t,r)));const o=await async function(t,e){"function"==typeof t&&(t=await t(e));const r=t.status||t[0],o=h(t.data||t[1]),n=t.headers||t[2];return t.config&&(e=t.config),{status:r,data:o,headers:n,config:e,request:{responseURL:e.url}}}(e,t);if(!o.config.validateStatus||o.config.validateStatus(o.status))return o;throw y(`Request failed with status code ${o.status}`,o.config,o)},isObjectOrArray:u,isBuffer:s,isBlob:i,isBodyOrParametersMatching:d,isEqual:n,createAxiosError:y,createCouldNotFindMockError:function(t){const e=`Could not find mock for: \n${JSON.stringify({method:t.method,url:t.url,params:t.params,headers:t.headers},null,2)}`,r=new Error(e);return r.isCouldNotFindMockError=!0,r.url=t.url,r.method=t.method,r}}},742:e=>{"use strict";e.exports=t}},r={},function t(o){var n=r[o];if(void 0!==n)return n.exports;var s=r[o]={exports:{}};return e[o](s,s.exports,t),s.exports}(44);var e,r}));