Skip to content

Commit e6b47cd

Browse files
committed
update readme and repaire bug
1 parent afa0fb1 commit e6b47cd

7 files changed

+105
-36
lines changed

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,29 @@ npm install isomorphic-fetch-http --save
1515
```javascript
1616
import { http } from 'isomorphic-fetch-http'
1717

18+
// isomorphic-fetch-http 要求数据返回格式为
19+
{
20+
status: true, // 请求状态 true/false
21+
code: 'INVALIDError', // 发生特定异常返回的错误码 与 exception 对应
22+
data: {} / [] // 请求状态为 true 时返回的数据体
23+
message // 请求状态为 false 时返回的详细错误信息
24+
}
25+
1826
// 全局配置
1927
http.setup({
2028
prefix: '/api', // url 前缀
2129
header: {}, // 自定义请求头
30+
filter: { // 自定义过滤器 before 发生在请求执行之前 after 发生在请求执行之后
31+
before: () => false,
32+
after: () => false
33+
}
2234
exception: ['INVALIDError'] // 自定义捕获类型
2335
});
2436

37+
http.setHeader({ // 覆盖/新增 请求头
38+
token: ''
39+
})
40+
2541
// get方法 一般用于查询
2642
// @param {Object} param
2743
// @param {Object} header

lib/isomorphic-fetch-http.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ var _http = function () {
6666
var option = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
6767
var header = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
6868

69-
this.filter.before();
69+
this.filter.before && this.filter.before();
7070
return (0, _isomorphicFetch2.default)('' + this.prefix + url, _extends({}, this[config], { headers: _extends({}, this[config].headers, this.header, header) }, option)).then(function (resp) {
7171
if (resp.status >= 400) {
7272
throw new Error('400+Error');
@@ -84,10 +84,10 @@ var _http = function () {
8484
data = _ref.data,
8585
message = _ref.message;
8686

87-
_this.filter.after();
87+
_this.filter.after && _this.filter.after({ status: status, code: code, data: data, message: message });
8888
if (status === false) {
8989
if (_this.exception.indexOf(code) > -1) {
90-
throw code;
90+
throw Error(code);
9191
}
9292
}
9393
return { status: status, data: data, message: message };
@@ -96,11 +96,14 @@ var _http = function () {
9696
}, {
9797
key: 'setup',
9898
value: function setup(_ref2) {
99-
var prefix = _ref2.prefix,
99+
var _ref2$prefix = _ref2.prefix,
100+
prefix = _ref2$prefix === undefined ? "" : _ref2$prefix,
100101
_ref2$header = _ref2.header,
101102
header = _ref2$header === undefined ? {} : _ref2$header,
102-
filter = _ref2.filter,
103-
exception = _ref2.exception;
103+
_ref2$filter = _ref2.filter,
104+
filter = _ref2$filter === undefined ? this.filter : _ref2$filter,
105+
_ref2$exception = _ref2.exception,
106+
exception = _ref2$exception === undefined ? [] : _ref2$exception;
104107

105108
this.prefix = prefix;
106109
this.header = header;
@@ -155,7 +158,7 @@ var _http = function () {
155158
var param = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
156159
var header = arguments[2];
157160

158-
console.log("WARNING: 在isomorphic-fetch-http 1.0.0版本及以上版本,option方法已经由json方法代替,option方法将在1.1.0版本中去除");
161+
console.error("WARNING: 在isomorphic-fetch-http 1.0.0版本及以上版本,option方法已经由json方法代替,option方法将在1.1.0版本中去除");
159162
return this[http](url, { method: 'POST', body: JSON.stringify(param) }, _extends({}, header, { "Content-Type": "application/json" }));
160163
}
161164
}, {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "isomorphic-fetch-http",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "react fetch by isomorphic-fetch",
55
"main": "lib/isomorphic-fetch-http.js",
66
"scripts": {

src/isomorphic-fetch-http.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class _http {
3535
}
3636

3737
[http](url, option = {}, header = {}) {
38-
this.filter.before();
38+
this.filter.before && this.filter.before();
3939
return fetch(`${this.prefix}${url}`, { ...this[config], headers: { ...this[config].headers, ...this.header, ...header }, ...option })
4040
.then((resp) => {
4141
if (resp.status >= 400) {
@@ -51,17 +51,17 @@ class _http {
5151
}
5252
})
5353
.then(({status, code, data, message}) => {
54-
this.filter.after();
54+
this.filter.after && this.filter.after({status, code, data, message});
5555
if (status === false) {
5656
if (this.exception.indexOf(code) > -1) {
57-
throw code;
57+
throw Error(code);
5858
}
5959
}
6060
return {status, data, message};
6161
});
6262
}
6363

64-
setup({prefix, header = {}, filter, exception}) {
64+
setup({prefix = "", header = {}, filter = this.filter, exception = []}) {
6565
this.prefix = prefix;
6666
this.header = header;
6767
this.filter = filter;
@@ -93,7 +93,7 @@ class _http {
9393
}
9494

9595
option(url, param = {}, header) {
96-
console.log("WARNING: 在isomorphic-fetch-http 1.0.0版本及以上版本,option方法已经由json方法代替,option方法将在1.1.0版本中去除");
96+
console.error("WARNING: 在isomorphic-fetch-http 1.0.0版本及以上版本,option方法已经由json方法代替,option方法将在1.1.0版本中去除");
9797
return this[http](url, {method: 'POST', body: JSON.stringify(param)}, {...header, "Content-Type": "application/json"});
9898
}
9999

test/index.js

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import $http from './isomorphic-fetch-http';
2-
32
$http.setup({
4-
prefix: '/123',
5-
header: {"123123": "asda"},
6-
fn: (data) => data
3+
prefix: '19634',
4+
header: {},
5+
// filter: {
6+
// before: () => {
7+
// alert('before');
8+
// },
9+
// after: (data) => {
10+
// alert('after');
11+
// }
12+
// }
713
});
814

9-
$http.setup({
10-
prefix: '/123',
11-
header: {"456456": "3453453"},
12-
fn: (data) => data
15+
$http.setHeader({
16+
token: 'asdasd'
1317
});
1418

15-
$http.get("/asd");
16-
17-
19+
$http.get("/add?currentPage=1");

test/isomorphic-fetch-http.js

+58-10
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,15 @@ var _http = function () {
3636
// bind this
3737
this.prefix = "";
3838
this.header = {};
39-
this.fn = function (data) {
40-
return data;
39+
this.filter = {
40+
before: function before() {
41+
return false;
42+
},
43+
after: function after() {
44+
return false;
45+
}
4146
};
47+
this.exception = [];
4248
this[config] = {
4349
headers: {
4450
"Content-Type": "application/x-www-form-urlencoded",
@@ -55,9 +61,12 @@ var _http = function () {
5561
_createClass(_http, [{
5662
key: http,
5763
value: function value(url) {
64+
var _this = this;
65+
5866
var option = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
5967
var header = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
6068

69+
this.filter.before && this.filter.before();
6170
return (0, _isomorphicFetch2.default)('' + this.prefix + url, _extends({}, this[config], { headers: _extends({}, this[config].headers, this.header, header) }, option)).then(function (resp) {
6271
if (resp.status >= 400) {
6372
throw new Error('400+Error');
@@ -69,21 +78,44 @@ var _http = function () {
6978
} catch (e) {
7079
throw new Error('JSONError');
7180
}
72-
}).then(this.fn).then(function (data) {
73-
return data;
81+
}).then(function (_ref) {
82+
var status = _ref.status,
83+
code = _ref.code,
84+
data = _ref.data,
85+
message = _ref.message;
86+
87+
_this.filter.after && _this.filter.after({ status: status, code: code, data: data, message: message });
88+
if (status === false) {
89+
if (_this.exception.indexOf(code) > -1) {
90+
throw Error(code);
91+
}
92+
}
93+
return { status: status, data: data, message: message };
7494
});
7595
}
7696
}, {
7797
key: 'setup',
78-
value: function setup(_ref) {
79-
var prefix = _ref.prefix,
80-
_ref$header = _ref.header,
81-
header = _ref$header === undefined ? {} : _ref$header,
82-
fn = _ref.fn;
98+
value: function setup(_ref2) {
99+
var _ref2$prefix = _ref2.prefix,
100+
prefix = _ref2$prefix === undefined ? "" : _ref2$prefix,
101+
_ref2$header = _ref2.header,
102+
header = _ref2$header === undefined ? {} : _ref2$header,
103+
_ref2$filter = _ref2.filter,
104+
filter = _ref2$filter === undefined ? this.filter : _ref2$filter,
105+
_ref2$exception = _ref2.exception,
106+
exception = _ref2$exception === undefined ? [] : _ref2$exception;
83107

84108
this.prefix = prefix;
85109
this.header = header;
86-
this.fn = fn;
110+
this.filter = filter;
111+
this.exception = exception;
112+
}
113+
}, {
114+
key: 'setHeader',
115+
value: function setHeader() {
116+
var header = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
117+
118+
this.header = _extends({}, this.header, header);
87119
}
88120
}, {
89121
key: 'get',
@@ -113,12 +145,28 @@ var _http = function () {
113145

114146
return this[http](url + '?' + (0, _qs.stringify)(param), { method: 'DELETE' }, header);
115147
}
148+
}, {
149+
key: 'options',
150+
value: function options(url, param) {
151+
var header = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
152+
153+
return this[http](url, { method: 'OPTIONS' }, header);
154+
}
116155
}, {
117156
key: 'option',
118157
value: function option(url) {
119158
var param = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
120159
var header = arguments[2];
121160

161+
console.error("WARNING: 在isomorphic-fetch-http 1.0.0版本及以上版本,option方法已经由json方法代替,option方法将在1.1.0版本中去除");
162+
return this[http](url, { method: 'POST', body: JSON.stringify(param) }, _extends({}, header, { "Content-Type": "application/json" }));
163+
}
164+
}, {
165+
key: 'json',
166+
value: function json(url) {
167+
var param = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
168+
var header = arguments[2];
169+
122170
return this[http](url, { method: 'POST', body: JSON.stringify(param) }, _extends({}, header, { "Content-Type": "application/json" }));
123171
}
124172
}]);

webpack.config.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ module.exports = {
1919
inline: true,
2020
stats: { colors: true },
2121
proxy: {
22-
'/api': {
23-
target: 'http://127.0.0.1:9001',
22+
'/19634': {
23+
target: 'http://rapapi.org/mockjsData/',
2424
changeOrigin: true
2525
}
2626
}

0 commit comments

Comments
 (0)