isomorphic-fetch-http
Advanced tools
Comparing version 1.1.1 to 2.0.0
@@ -39,11 +39,8 @@ 'use strict'; | ||
this.header = {}; | ||
this.filter = { | ||
before: function before() { | ||
return false; | ||
}, | ||
after: function after() { | ||
return false; | ||
} | ||
this.filter = function () { | ||
return false; | ||
}; | ||
this.exception = []; | ||
this.callback = function (_) { | ||
return _; | ||
}; | ||
this[config] = { | ||
@@ -65,8 +62,6 @@ headers: { | ||
value: function value(url) { | ||
var _this = this; | ||
var option = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var header = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
this.filter.before && this.filter.before(); | ||
this.filter && this.filter(); | ||
return (0, _isomorphicFetch2.default)('' + this.prefix + url, _extends({}, this[config], { headers: _extends({}, this[config].headers, this.header, header) }, option)).then(function (resp) { | ||
@@ -83,28 +78,15 @@ if (resp.status >= 400) { | ||
} | ||
}).then(function (_ref) { | ||
var status = _ref.status, | ||
code = _ref.code, | ||
data = _ref.data, | ||
message = _ref.message; | ||
_this.filter.after && _this.filter.after({ status: status, code: code, data: data, message: message }); | ||
if (status === false) { | ||
if (_this.exception.indexOf(code) > -1) { | ||
throw Error(code); | ||
} | ||
} | ||
return { status: status, data: data, message: message }; | ||
}); | ||
}).then(this.callback); | ||
} | ||
}, { | ||
key: 'setup', | ||
value: function setup(_ref2) { | ||
var _ref2$prefix = _ref2.prefix, | ||
prefix = _ref2$prefix === undefined ? "" : _ref2$prefix, | ||
_ref2$header = _ref2.header, | ||
header = _ref2$header === undefined ? {} : _ref2$header, | ||
_ref2$filter = _ref2.filter, | ||
filter = _ref2$filter === undefined ? this.filter : _ref2$filter, | ||
_ref2$exception = _ref2.exception, | ||
exception = _ref2$exception === undefined ? [] : _ref2$exception; | ||
value: function setup(_ref) { | ||
var _ref$prefix = _ref.prefix, | ||
prefix = _ref$prefix === undefined ? "" : _ref$prefix, | ||
_ref$header = _ref.header, | ||
header = _ref$header === undefined ? {} : _ref$header, | ||
_ref$filter = _ref.filter, | ||
filter = _ref$filter === undefined ? this.filter : _ref$filter, | ||
_ref$callback = _ref.callback, | ||
callback = _ref$callback === undefined ? this.callback : _ref$callback; | ||
@@ -114,3 +96,3 @@ this.prefix = prefix; | ||
this.filter = filter; | ||
this.exception = exception; | ||
this.callback = callback; | ||
} | ||
@@ -160,11 +142,2 @@ }, { | ||
}, { | ||
key: 'option', | ||
value: function option(url) { | ||
var param = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var header = arguments[2]; | ||
console.error("WARNING: 在isomorphic-fetch-http 1.0.0版本及以上版本,option方法已经由json方法代替,option方法将在1.1.0版本中去除"); | ||
return this[http](url, { method: 'POST', body: JSON.stringify(param) }, _extends({}, header, { "Content-Type": "application/json" })); | ||
} | ||
}, { | ||
key: 'json', | ||
@@ -171,0 +144,0 @@ value: function json(url) { |
{ | ||
"name": "isomorphic-fetch-http", | ||
"version": "1.1.1", | ||
"version": "2.0.0", | ||
"description": "react fetch by isomorphic-fetch", | ||
@@ -5,0 +5,0 @@ "main": "lib/isomorphic-fetch-http.js", |
@@ -18,10 +18,2 @@ ## isomorphic-fetch-http | ||
// isomorphic-fetch-http 要求数据返回格式为 | ||
{ | ||
status: true, // 请求状态 true/false | ||
code: 'INVALIDError', // 发生特定异常返回的错误码 与 exception 对应 | ||
data: {} / [] // 请求状态为 true 时返回的数据体 | ||
message // 请求状态为 false 时返回的详细错误信息 | ||
} | ||
// 全局配置 | ||
@@ -31,7 +23,4 @@ http.setup({ | ||
header: {}, // 自定义请求头 | ||
filter: { // 自定义过滤器 before 发生在请求执行之前 after 发生在请求执行之后 | ||
before: () => false, | ||
after: () => false | ||
} | ||
exception: ['INVALIDError'] // 自定义捕获类型 | ||
filter: () => false // 自定义过滤器 发生在请求执行之前 | ||
callback: data => data // 数据请求成功之后的回调函数 | ||
}); | ||
@@ -38,0 +27,0 @@ |
@@ -19,7 +19,4 @@ import fetch from 'isomorphic-fetch'; | ||
this.header = {}; | ||
this.filter = { | ||
before: () => false, | ||
after: () => false, | ||
}; | ||
this.exception = []; | ||
this.filter = () => false; | ||
this.callback = _ => _; | ||
this[config] = { | ||
@@ -39,3 +36,3 @@ headers: { | ||
[http](url, option = {}, header = {}) { | ||
this.filter.before && this.filter.before(); | ||
this.filter && this.filter(); | ||
return fetch(`${this.prefix}${url}`, { ...this[config], headers: { ...this[config].headers, ...this.header, ...header }, ...option }) | ||
@@ -55,18 +52,10 @@ .then((resp) => { | ||
}) | ||
.then(({status, code, data, message}) => { | ||
this.filter.after && this.filter.after({status, code, data, message}); | ||
if (status === false) { | ||
if (this.exception.indexOf(code) > -1) { | ||
throw Error(code); | ||
} | ||
} | ||
return {status, data, message}; | ||
}); | ||
.then(this.callback); | ||
} | ||
setup({prefix = "", header = {}, filter = this.filter, exception = []}) { | ||
setup({prefix = "", header = {}, filter = this.filter, callback = this.callback}) { | ||
this.prefix = prefix; | ||
this.header = header; | ||
this.filter = filter; | ||
this.exception = exception; | ||
this.callback = callback; | ||
} | ||
@@ -98,7 +87,2 @@ | ||
option(url, param = {}, header) { | ||
console.error("WARNING: 在isomorphic-fetch-http 1.0.0版本及以上版本,option方法已经由json方法代替,option方法将在1.1.0版本中去除"); | ||
return this[http](url, {method: 'POST', body: JSON.stringify(param)}, {...header, "Content-Type": "application/json"}); | ||
} | ||
json(url, param = {}, header) { | ||
@@ -105,0 +89,0 @@ return this[http](url, {method: 'POST', body: JSON.stringify(param)}, {...header, "Content-Type": "application/json"}); |
@@ -5,10 +5,2 @@ import $http from './isomorphic-fetch-http'; | ||
header: {}, | ||
// filter: { | ||
// before: () => { | ||
// alert('before'); | ||
// }, | ||
// after: (data) => { | ||
// alert('after'); | ||
// } | ||
// } | ||
}); | ||
@@ -15,0 +7,0 @@ |
21239
403
83