1+ 'use strict' ;
2+
3+ Object . defineProperty ( exports , "__esModule" , {
4+ value : true
5+ } ) ;
6+ exports . Mock = undefined ;
7+
8+ var _typeof = typeof Symbol === "function" && typeof Symbol . iterator === "symbol" ? function ( obj ) { return typeof obj ; } : function ( obj ) { return obj && typeof Symbol === "function" && obj . constructor === Symbol && obj !== Symbol . prototype ? "symbol" : typeof obj ; } ;
9+
10+ var _createClass = function ( ) { function defineProperties ( target , props ) { for ( var i = 0 ; i < props . length ; i ++ ) { var descriptor = props [ i ] ; descriptor . enumerable = descriptor . enumerable || false ; descriptor . configurable = true ; if ( "value" in descriptor ) descriptor . writable = true ; Object . defineProperty ( target , descriptor . key , descriptor ) ; } } return function ( Constructor , protoProps , staticProps ) { if ( protoProps ) defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) defineProperties ( Constructor , staticProps ) ; return Constructor ; } ; } ( ) ;
11+
12+ var _mockjs = require ( 'mockjs' ) ;
13+
14+ Object . defineProperty ( exports , 'Mock' , {
15+ enumerable : true ,
16+ get : function get ( ) {
17+ return _interopRequireDefault ( _mockjs ) . default ;
18+ }
19+ } ) ;
20+
21+ var _pathToRegexp = require ( 'path-to-regexp' ) ;
22+
23+ var _pathToRegexp2 = _interopRequireDefault ( _pathToRegexp ) ;
24+
25+ var _util = require ( './util' ) ;
26+
27+ var _response = require ( './response' ) ;
28+
29+ var _response2 = _interopRequireDefault ( _response ) ;
30+
31+ function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
32+
33+ function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( "Cannot call a class as a function" ) ; } }
34+
35+ var FetchMock = function ( ) {
36+ function FetchMock ( required ) {
37+ var options = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : {
38+ fetch : function fetch ( ) { } ,
39+ exclude : [ ] ,
40+ proxy : [ ]
41+ } ;
42+
43+ _classCallCheck ( this , FetchMock ) ;
44+
45+ if ( 'object' !== ( typeof required === 'undefined' ? 'undefined' : _typeof ( required ) ) ) {
46+ throw new Error ( 'There is no required defined.' ) ;
47+ }
48+
49+ this . urls = [ ] ;
50+ this . raw = options . fetch ;
51+ this . exclude = options . exclude ;
52+ this . proxy = options . proxy ;
53+
54+ this . loadMocks = this . loadMocks . bind ( this ) ;
55+ this . loadMock = this . loadMock . bind ( this ) ;
56+ this . matchReqUrl = this . matchReqUrl . bind ( this ) ;
57+ this . isExclude = this . isExclude . bind ( this ) ;
58+ this . isProxied = this . isProxied . bind ( this ) ;
59+ this . fetch = this . fetch . bind ( this ) ;
60+
61+ this . loadMocks ( required ) ;
62+ }
63+
64+ _createClass ( FetchMock , [ {
65+ key : 'loadMocks' ,
66+ value : function loadMocks ( required ) {
67+ var _this = this ;
68+
69+ var __mocks__ = required . default || required ; // es6 import or amd
70+ var mocks = Object . keys ( __mocks__ ) ;
71+ mocks . forEach ( function ( key ) {
72+ _this . loadMock ( key , __mocks__ [ key ] ) ;
73+ } ) ;
74+ }
75+ } , {
76+ key : 'loadMock' ,
77+ value : function loadMock ( key , mock ) {
78+ var _this2 = this ;
79+
80+ if ( 'object' !== ( typeof mock === 'undefined' ? 'undefined' : _typeof ( mock ) ) ) {
81+ if ( 'function' === typeof mock ) {
82+ var items = key . split ( ' ' ) ;
83+ var method = items . length === 2 ? items [ 0 ] : 'GET' ;
84+ var url = items . length === 2 ? items [ 1 ] : key ;
85+ this . urls . push ( {
86+ method : method ,
87+ url : url ,
88+ func : mock
89+ } ) ;
90+ }
91+ return ;
92+ }
93+ var keys = Object . keys ( mock ) ;
94+ keys . map ( function ( key ) {
95+ _this2 . loadMock ( key , mock [ key ] ) ;
96+ } ) ;
97+ }
98+ } , {
99+ key : 'matchReqUrl' ,
100+ value : function matchReqUrl ( request ) {
101+ var insideParams = void 0 ;
102+ var filters = this . urls . filter ( function ( uri ) {
103+ var obj = ( 0 , _util . matchUrl ) ( uri . url , request . url ) ;
104+ if ( obj . result && uri . method . toUpperCase ( ) === request . method . toUpperCase ( ) ) {
105+ insideParams = obj . params ;
106+ return true ;
107+ }
108+ return false ;
109+ } ) ;
110+ if ( ! filters || filters . length == 0 ) throw new Error ( 'No url ' + request . url + ' is defined.' ) ;
111+ request . urlparams = insideParams ;
112+ return {
113+ request : request ,
114+ mock : filters [ 0 ]
115+ } ;
116+ }
117+ } , {
118+ key : 'isExclude' ,
119+ value : function isExclude ( url ) {
120+ for ( var i = 0 ; i < this . exclude . length ; i ++ ) {
121+ var excludeUrl = this . exclude [ i ] ;
122+ if ( excludeUrl === url || ( 0 , _pathToRegexp2 . default ) ( '' + excludeUrl ) . exec ( url ) !== null ) {
123+ return true ;
124+ }
125+ }
126+ return false ;
127+ }
128+ } , {
129+ key : 'isProxied' ,
130+ value : function isProxied ( url ) {
131+ if ( this . proxy . length === 0 ) return false ;
132+ var proxied = this . proxy . filter ( function ( item ) {
133+ return ( 0 , _pathToRegexp2 . default ) ( '' + item . path ) . exec ( url ) !== null ;
134+ } ) ;
135+ if ( proxied . length > 1 ) throw new Error ( url + ' proxied has two proxies, you should specific only one' ) ;
136+
137+ return proxied [ 0 ] ;
138+ }
139+ } , {
140+ key : 'proxied' ,
141+ value : function proxied ( url ) {
142+ // get proxied info
143+ var matches = void 0 ,
144+ proxied = void 0 ;
145+ this . proxy . forEach ( function ( item ) {
146+ var tmp = ( 0 , _pathToRegexp2 . default ) ( item . path ) . exec ( url ) ;
147+ if ( tmp . length > 1 ) {
148+ matches = tmp ;
149+ proxied = item ;
150+ return false ;
151+ }
152+ } ) ;
153+
154+ return proxied . process ? proxied . process ( proxied , matches ) : proxied . target + '/' + matches [ 1 ] ;
155+ }
156+ } , {
157+ key : 'fetch' ,
158+ value : function fetch ( url , options ) {
159+ // using proxy
160+ if ( this . isProxied ( url ) ) {
161+ url = this . proxied ( url ) ;
162+ }
163+
164+ // using raw fetch while match exclude
165+ if ( this . isExclude ( url ) ) {
166+ // using raw fetch
167+ return this . raw ( url , options ) ;
168+ }
169+
170+ var _matchReqUrl = this . matchReqUrl ( ( 0 , _util . parseRequest ) ( url , options ) ) ,
171+ request = _matchReqUrl . request ,
172+ mock = _matchReqUrl . mock ;
173+
174+ if ( 'function' !== typeof mock . func ) {
175+ throw new Error ( 'There is no url defined in __mocks__' ) ;
176+ }
177+ var obj = mock . func ( request ) ;
178+
179+ if ( ( 0 , _util . isNull ) ( obj ) ) {
180+ throw 'response data should not be undefined or null, it will be an object or an array at least' ;
181+ }
182+
183+ // resolve prue data object
184+ if ( ( 0 , _util . isNull ) ( obj . status ) ) {
185+ obj = {
186+ status : 200 ,
187+ data : obj
188+ } ;
189+ }
190+
191+ var response = new _response2 . default ( obj ) ;
192+ return Promise . resolve ( response ) ;
193+ }
194+ } ] ) ;
195+
196+ return FetchMock ;
197+ } ( ) ;
198+
199+ exports . default = FetchMock ;
0 commit comments