@lxlib/mock
Version:
This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 9.1.1.
16 lines (14 loc) • 6.53 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@angular/core"),require("@angular/common/http"),require("rxjs"),require("rxjs/operators")):"function"==typeof define&&define.amd?define("@lxlib/mock",["exports","@angular/core","@angular/common/http","rxjs","rxjs/operators"],e):e(((t=t||self).lxlib=t.lxlib||{},t.lxlib.mock={}),t.ng.core,t.ng.common.http,t.rxjs,t.rxjs.operators)}(this,(function(t,e,r,o,n){"use strict";
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */var c=function(){return(c=Object.assign||function(t){for(var e,r=1,o=arguments.length;r<o;r++)for(var n in e=arguments[r])Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n]);return t}).apply(this,arguments)};var i=function(t,e){this.status=t,this.error=e};var a=function(){this.delay=300,this.force=!1,this.log=!0,this.executeOtherInterceptors=!0};var s=function(){function t(t){this.config=t,this.cached=[],this.applyMock(),delete this.config.data}return t.prototype.applyMock=function(){this.cached=[];try{this.realApplyMock()}catch(t){this.outputError(t)}},t.prototype.realApplyMock=function(){var t=this,e=this.config.data;e&&(Object.keys(e).forEach((function(r){var o=e[r];o&&Object.keys(o).forEach((function(e){var n=o[e];if("function"!=typeof n&&"object"!=typeof n&&"string"!=typeof n)throw Error("mock value of ["+r+"-"+e+"] should be function or object or string, but got "+typeof n);var c=t.genRule(e,n);if(-1===["GET","POST","PUT","HEAD","DELETE","PATCH","OPTIONS"].indexOf(c.method))throw Error("method of "+r+"-"+e+" is not valid");var i=t.cached.find((function(t){return t.url===c.url&&t.method===c.method}));i?i.callback=c.callback:t.cached.push(c)}))})),this.cached.sort((function(t,e){return(e.martcher||"").toString().length-(t.martcher||"").toString().length})))},t.prototype.genRule=function(t,e){var r="GET",o=t;if(t.indexOf(" ")>-1){var n=t.split(" ");r=n[0].toLowerCase(),o=n[1]}var c=null,i=[];if(~o.indexOf(":")){i=o.split("/").filter((function(t){return t.startsWith(":")})).map((function(t){return t.substring(1)}));var a=o.split("/").map((function(t){return t.startsWith(":")?"([^/]+)":t})).join("/");c=new RegExp("^"+a,"i")}else/(\([^)]+\))/i.test(o)&&(c=new RegExp(o,"i"));return{url:o,martcher:c,segments:i,callback:e,method:r.toUpperCase()}},t.prototype.outputError=function(t){var e=t.message.split(": ")[0],r=t.stack.split("\n").filter((function(t){return 0!==t.trim().indexOf("at ")})).map((function(t){return t.replace(e+": ","")}));throw r.splice(1,0,""),console.group(),console.warn("==========Failed to parse mock config.=========="),console.log(r.join("\n")),console.groupEnd(),t},t.prototype.getRule=function(t,e){t=(t||"GET").toUpperCase();var r={},o=this.cached.filter((function(r){return r.method===t&&(r.martcher?r.martcher.test(e):r.url===e)}));if(0===o.length)return null;var n=o.find((function(t){return t.url===e}))||o[0];n.martcher&&n.martcher.exec(e).slice(1).map((function(t,e){r[n.segments[e]]=t}));return{url:e,method:n.method,params:r,callback:n.callback}},t.prototype.clearCache=function(){this.cached=[]},Object.defineProperty(t.prototype,"rules",{get:function(){return this.cached},enumerable:!0,configurable:!0}),t.prototype.ngOnDestroy=function(){this.clearCache()},t.decorators=[{type:e.Injectable}],t.ctorParameters=function(){return[{type:a}]},t}();var u=function(){function t(t,e){this.next=t,this.interceptor=e}return t.prototype.handle=function(t){return this.interceptor.intercept(t,this.next)},t}();var l=function(){function t(t){this.injector=t}return t.prototype.intercept=function(t,e){var l,p=this.injector.get(s),f=c({delay:300,force:!1,log:!0,executeOtherInterceptors:!0},this.injector.get(a)),h=p.getRule(t.method,t.url.split("?")[0]);if(!h&&!f.force)return e.handle(t);switch(typeof h.callback){case"function":var d={original:t,body:t.body,queryString:{},headers:{},params:h.params},g=t.url.split("?");g.length>1&&g[1].split("&").forEach((function(t){var e=t.split("="),r=e[0],o=e[1];Object.keys(d.queryString).includes(r)?(Array.isArray(d.queryString[r])||(d.queryString[r]=[d.queryString[r]]),d.queryString[r].push(o)):d.queryString[r]=o})),t.params.keys().forEach((function(e){return d.queryString[e]=t.params.get(e)})),t.headers.keys().forEach((function(e){return d.headers[e]=t.headers.get(e)}));try{l=h.callback.call(this,d)}catch(e){l=new r.HttpErrorResponse({url:t.url,headers:t.headers,status:400,statusText:e.statusText||"Unknown Error",error:e.error}),e instanceof i&&(l.status=e.status)}break;default:l=h.callback}l instanceof r.HttpResponseBase||(l=new r.HttpResponse({status:200,url:t.url,body:l})),f.log&&(console.log("%c👽"+t.method+"->"+t.url+"->request","background:#000;color:#bada55",t),console.log("%c👽"+t.method+"->"+t.url+"->response","background:#000;color:#bada55",l));var y=l instanceof r.HttpErrorResponse?o.throwError(l):o.of(l);if(f.executeOtherInterceptors){var m=this.injector.get(r.HTTP_INTERCEPTORS,[]),b=m.slice(m.indexOf(this)+1);if(b.length>0)return b.reduceRight((function(t,e){return new u(t,e)}),{handle:function(){return y}}).handle(t).pipe(n.delay(f.delay))}return y.pipe(n.delay(f.delay))},t.decorators=[{type:e.Injectable}],t.ctorParameters=function(){return[{type:e.Injector}]},t}();var p=function(){function t(){}return t.forRoot=function(e){return{ngModule:t,providers:[s,{provide:a,useValue:e},{provide:r.HTTP_INTERCEPTORS,useClass:l,multi:!0}]}},t.forChild=function(){return{ngModule:t,providers:[{provide:r.HTTP_INTERCEPTORS,useClass:l,multi:!0}]}},t.decorators=[{type:e.NgModule,args:[{}]}],t}();t.LxlibMockConfig=a,t.LxlibMockModule=p,t.MockInterceptor=l,t.MockService=s,t.MockStatusError=i,Object.defineProperty(t,"__esModule",{value:!0})}));
//# sourceMappingURL=mock.umd.min.js.map