text: var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});Object.defineProperty(exports,"Mock",{enumerable:true,get:function get(){return _mockjs.default;}});exports.default=void 0;var _classCallCheck2=_interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));var _createClass2=_interopRequireDefault(require("@babel/runtime/helpers/createClass"));var _pathToRegexp=require("path-to-regexp");var _util=require("./util");var _response=_interopRequireDefault(require("./response"));var _mockjs=_interopRequireDefault(require("mockjs"));var FetchMock=function(){function FetchMock(required){var options=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{fetch:function fetch(){},exclude:[],fallbackToNetwork:false,proxy:[],delay:2000};(0,_classCallCheck2.default)(this,FetchMock);if('object'!==typeof required){throw new Error('There is no required defined.');}this.urls=[];this.raw=options.fetch;this.exclude=options.exclude||[];this.fallbackToNetwork=options.fallbackToNetwork||false;this.proxy=options.proxy||[];this.delayTime=options.delay;this.loadMocks=this.loadMocks.bind(this);this.loadMock=this.loadMock.bind(this);this.matchReqUrl=this.matchReqUrl.bind(this);this.isExclude=this.isExclude.bind(this);this.isProxied=this.isProxied.bind(this);this.fetch=this.fetch.bind(this);this.loadMocks(required);}(0,_createClass2.default)(FetchMock,[{key:"loadMocks",value:function loadMocks(required){var _this=this;var __mocks__=required.default||required;var mocks=Object.keys(__mocks__);mocks.forEach(function(key){_this.loadMock(key,__mocks__[key]);});}},{key:"loadMock",value:function loadMock(key,mock){var _this2=this;if('object'!==typeof mock){if('function'===typeof mock){var items=key.split(' ');var method=items.length===2?items[0]:'GET';var url=items.length===2?items[1]:key;this.urls.push({method:method,url:url,func:mock});}return;}var keys=Object.keys(mock);keys.map(function(key){_this2.loadMock(key,mock[key]);});}},{key:"matchReqUrl",value:function matchReqUrl(request){var insideParams;var filters=this.urls.filter(function(uri){var obj=(0,_util.matchUrl)(uri.url,request.url);if(obj.result&&uri.method.toUpperCase()===request.method.toUpperCase()){insideParams=obj.params;return true;}return false;});if(!filters||filters.length==0){return{matched:false};}request.urlparams=insideParams;return{matched:true,request:request,mock:filters[0]};}},{key:"isExclude",value:function isExclude(url){for(var i=0;i<this.exclude.length;i++){var excludeUrl=this.exclude[i];if(excludeUrl===url||(0,_pathToRegexp.pathToRegexp)(""+excludeUrl).exec(url)!==null){return true;}}return false;}},{key:"isProxied",value:function isProxied(url){if(this.proxy.length===0)return false;var proxied=this.proxy.filter(function(item){return(0,_pathToRegexp.pathToRegexp)(""+item.path).exec(url)!==null;});if(proxied.length>1)throw new Error(url+" proxied has two proxies, you should specific only one");return proxied[0];}},{key:"proxied",value:function proxied(url){var matches,proxied;this.proxy.forEach(function(item){var tmp=(0,_pathToRegexp.pathToRegexp)(item.path).exec(url);if(tmp.length>1){matches=tmp;proxied=item;return false;}});return proxied.process?proxied.process(proxied,matches):proxied.target+"/"+matches[1];}},{key:"fetch",value:function fetch(url){var options=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};if(this.isProxied(url)){url=this.proxied(url);}if(this.isExclude(url)||this.fallbackToNetwork==='always'){return this.raw(url,options);}var _this$matchReqUrl=this.matchReqUrl((0,_util.parseRequest)(url,options)),matched=_this$matchReqUrl.matched,request=_this$matchReqUrl.request,mock=_this$matchReqUrl.mock;if(!matched){if(this.fallbackToNetwork){return this.raw(url,options);}else{throw new Error("No url "+url+" is defined.");}}if('function'!==typeof mock.func){throw new Error("The url "+url+" defined in __mocks__ is not a function");}var obj=mock.func(request);if((0,_util.isNull)(obj)){throw'response data should not be undefined or null, it will be an object or an array at least';}if((0,_util.isNull)(obj.status)){obj={status:200,data:obj};}var response=new _response.default(obj);var delayTime=options.delay||this.delayTime||0;return(0,_util.delay)(delayTime).then(function(){return Promise.resolve(response);});}}]);return FetchMock;}();var _default=FetchMock;exports.default=_default;