Skip to content

Commit f91a74c

Browse files
committed
lint and publish dist directory instead
1 parent 2d08073 commit f91a74c

File tree

9 files changed

+415
-20
lines changed

9 files changed

+415
-20
lines changed

.eslintrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"parser": "babel-eslint",
33
"env": {
4-
"browser": true
4+
"browser": true,
5+
"es6": true
56
},
67
"extends": "eslint:recommended",
78
"rules": {

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,5 @@ jspm_packages
3636
# Optional REPL history
3737
.node_repl_history
3838

39-
# outpu
40-
dist/
41-
4239
# MACOS
4340
.DS_Store

dist/index.js

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
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;

dist/response.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
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; }; }();
8+
9+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
10+
11+
var _status = Symbol('status');
12+
var _data = Symbol('data');
13+
var _statusText = Symbol('statusText');
14+
15+
var Response = function () {
16+
function Response(_ref) {
17+
var status = _ref.status,
18+
_ref$data = _ref.data,
19+
data = _ref$data === undefined ? {} : _ref$data,
20+
_ref$statusText = _ref.statusText,
21+
statusText = _ref$statusText === undefined ? '' : _ref$statusText;
22+
23+
_classCallCheck(this, Response);
24+
25+
this[_status] = status;
26+
this[_data] = data;
27+
this[_statusText] = statusText;
28+
}
29+
30+
_createClass(Response, [{
31+
key: 'text',
32+
value: function text() {
33+
try {
34+
return Promise.resolve(JSON.stringify(this[_data]));
35+
} catch (err) {
36+
return Promise.reject(new Error('failed text invoke.'));
37+
}
38+
}
39+
}, {
40+
key: 'json',
41+
value: function json() {
42+
return this[_data];
43+
}
44+
}, {
45+
key: 'status',
46+
get: function get() {
47+
return this[_status];
48+
}
49+
}, {
50+
key: 'statusText',
51+
get: function get() {
52+
return this[_statusText];
53+
}
54+
}]);
55+
56+
return Response;
57+
}();
58+
59+
exports.default = Response;

0 commit comments

Comments
 (0)