isomorphic-fetch-http
Advanced tools
Comparing version 2.0.1 to 3.0.0
@@ -19,7 +19,14 @@ 'use strict'; | ||
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var config = Symbol('config'); | ||
var http = Symbol('http'); | ||
var defaultHeaders = { | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
"Access-Control-Allow-Origin": "*", | ||
"Access-Control-Max-Age": "86400", | ||
"Access-Control-Allow-Methods": "POST, GET, OPTIONS, PUT, DELETE", | ||
"Access-Control-Allow-Headers": "token, host, x-real-ip, x-forwarded-ip, accept, content-type" | ||
}; | ||
/* | ||
@@ -29,3 +36,3 @@ * Requests a URL, returning a promise. | ||
* @param {object} [options] The options we want to pass to "fetch" | ||
* @param {object} header The request header | ||
* @param {object} headers The request headers | ||
* @return {object} An object containing either "data" or "err" | ||
@@ -40,5 +47,7 @@ */ | ||
this.prefix = ""; | ||
this.header = {}; | ||
this.filter = function () { | ||
return false; | ||
this.headers = {}; | ||
this.filter = function (_) { | ||
return new Promise(function (resolve) { | ||
return resolve(_); | ||
}); | ||
}; | ||
@@ -48,13 +57,4 @@ this.callback = function (_) { | ||
}; | ||
this[config] = { | ||
headers: { | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
"Access-Control-Allow-Origin": "*", | ||
"Access-Control-Max-Age": "86400", | ||
"Access-Control-Allow-Methods": "POST, GET, OPTIONS, PUT, DELETE", | ||
"Access-Control-Allow-Headers": "token, host, x-real-ip, x-forwarded-ip, accept, content-type" | ||
}, | ||
credentials: "include", | ||
client_max_body_size: "2048m" | ||
}; | ||
this.cookies = true; | ||
this.option = {}; | ||
} | ||
@@ -66,6 +66,19 @@ | ||
var option = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var header = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var headers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
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) { | ||
// url | ||
var _url = '' + this.prefix + url; | ||
// request headers | ||
var _headers = _extends({}, defaultHeaders, this.headers, headers); | ||
// fetch option | ||
var _option = _extends({}, this.option, { | ||
credentials: this.cookies ? "include" : undefined | ||
}, option); | ||
return this.filter({ url: _url, headers: _headers, option: _option }).then(function (_ref) { | ||
var u = _ref.url, | ||
h = _ref.headers, | ||
o = _ref.option; | ||
return (0, _isomorphicFetch2.default)(u, _extends({}, o, { headers: h })); | ||
}).then(function (resp) { | ||
if (resp.status >= 400) { | ||
@@ -81,41 +94,48 @@ throw new Error('400+Error'); | ||
} | ||
}).then(this.callback); | ||
}).then(this.callback).catch(function (msg) { | ||
throw new Error(msg); | ||
}); | ||
} | ||
}, { | ||
key: 'setup', | ||
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; | ||
value: function setup(_ref2) { | ||
var _ref2$prefix = _ref2.prefix, | ||
prefix = _ref2$prefix === undefined ? "" : _ref2$prefix, | ||
_ref2$headers = _ref2.headers, | ||
headers = _ref2$headers === undefined ? this.headers : _ref2$headers, | ||
_ref2$filter = _ref2.filter, | ||
filter = _ref2$filter === undefined ? this.filter : _ref2$filter, | ||
_ref2$callback = _ref2.callback, | ||
callback = _ref2$callback === undefined ? this.callback : _ref2$callback, | ||
_ref2$cookies = _ref2.cookies, | ||
cookies = _ref2$cookies === undefined ? this.cookies : _ref2$cookies, | ||
option = _objectWithoutProperties(_ref2, ['prefix', 'headers', 'filter', 'callback', 'cookies']); | ||
this.prefix = prefix; | ||
this.header = header; | ||
this.headers = headers; | ||
this.filter = filter; | ||
this.callback = callback; | ||
this.cookies = cookies; | ||
this.option = option; | ||
} | ||
}, { | ||
key: 'setHeader', | ||
value: function setHeader() { | ||
var header = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
key: 'setHeaders', | ||
value: function setHeaders() { | ||
var headers = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
this.header = _extends({}, this.header, header); | ||
this.headers = _extends({}, this.headers, headers); | ||
} | ||
}, { | ||
key: 'get', | ||
value: function get(url, param, cors) { | ||
var header = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | ||
value: function get(url, param) { | ||
var headers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return this[http](url + '?' + (0, _qs.stringify)(param), cors ? { mode: "no-cors" } : {}, header); | ||
return this[http](url + '?' + (0, _qs.stringify)(param), {}, headers); | ||
} | ||
}, { | ||
key: 'post', | ||
value: function post(url, param, cors) { | ||
var header = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | ||
value: function post(url, param) { | ||
var headers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return this[http](url, _extends({ method: 'POST', body: (0, _qs.stringify)(param) }, cors ? { mode: "no-cors" } : {}), header); | ||
return this[http](url, { method: 'POST', body: (0, _qs.stringify)(param) }, headers); | ||
} | ||
@@ -125,5 +145,5 @@ }, { | ||
value: function put(url, param) { | ||
var header = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var headers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return this[http](url, { method: 'PUT', body: (0, _qs.stringify)(param) }, header); | ||
return this[http](url, { method: 'PUT', body: (0, _qs.stringify)(param) }, headers); | ||
} | ||
@@ -133,5 +153,5 @@ }, { | ||
value: function _delete(url, param) { | ||
var header = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var headers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return this[http](url + '?' + (0, _qs.stringify)(param), { method: 'DELETE' }, header); | ||
return this[http](url + '?' + (0, _qs.stringify)(param), { method: 'DELETE' }, headers); | ||
} | ||
@@ -141,5 +161,5 @@ }, { | ||
value: function options(url, param) { | ||
var header = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var headers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return this[http](url, { method: 'OPTIONS' }, header); | ||
return this[http](url, { method: 'OPTIONS' }, headers); | ||
} | ||
@@ -150,5 +170,5 @@ }, { | ||
var param = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var header = arguments[2]; | ||
var headers = arguments[2]; | ||
return this[http](url, { method: 'POST', body: JSON.stringify(param) }, _extends({}, header, { "Content-Type": "application/json" })); | ||
return this[http](url, { method: 'POST', body: JSON.stringify(param) }, _extends({}, headers, { "Content-Type": "application/json" })); | ||
} | ||
@@ -155,0 +175,0 @@ }]); |
{ | ||
"name": "isomorphic-fetch-http", | ||
"version": "2.0.1", | ||
"version": "3.0.0", | ||
"description": "react fetch by isomorphic-fetch", | ||
@@ -5,0 +5,0 @@ "main": "lib/isomorphic-fetch-http.js", |
130
README.md
@@ -12,65 +12,99 @@ ## isomorphic-fetch-http | ||
## Usage | ||
### Use prebuilt bundle | ||
```javascript | ||
import { http } from 'isomorphic-fetch-http' | ||
// 全局配置 | ||
http.setup({ | ||
prefix: '/api', // url 前缀 | ||
header: {}, // 自定义请求头 | ||
filter: () => false // 自定义过滤器 发生在请求执行之前 | ||
callback: data => data // 数据请求成功之后的回调函数 | ||
cookies: true, | ||
}); | ||
http.setHeader({ // 覆盖/新增 请求头 | ||
token: '' | ||
}) | ||
// get方法 一般用于查询 | ||
// @param {Object} param | ||
// @param {Object} header | ||
// @return {Object} {status, data, message} | ||
return http.get('url', param, header); | ||
``` | ||
// post方法 一般用于数据提交 | ||
// @param {Object} param | ||
// @param {Object} header | ||
// @return {Object} {status, data, message} | ||
return http.post('url', param, header); | ||
## Options | ||
// put方法 一般用于数据修改 | ||
// @param {Object} param | ||
// @param {Object} header | ||
// @return {Object} {status, data, message} | ||
return http.put('url', param, header); | ||
### setup | ||
// delete方法 一般用于数据删除 | ||
// @param {Object} param | ||
// @param {Object} header | ||
// @return {Object} {status, data, message} | ||
return http.delete('url', param, header); | ||
全局配置 | ||
// json方法 本质是请求头为 "Content-Type": "application/json" 的post方法 | ||
// @param {Object} param | ||
// @param {Object} header | ||
// @return {Object} {status, data, message} | ||
return http.json('url', param, header); | ||
> #### prefix | ||
请求 url 前缀 | ||
> #### headers | ||
// option方法 本质是请求头为 "Content-Type": "application/json" 的post方法 | ||
// WARNING: 在isomorphic-fetch-http 1.0.0版本及以上版本,option方法已经由json方法代替,option方法将在1.1.0版本中去除 | ||
// @param {Object} param | ||
// @param {Object} header | ||
// @return {Object} {status, data, message} | ||
return http.option('url', param, header); | ||
配置请求头 | ||
> #### cookies | ||
// options方法 一般用于请求时的预检 | ||
// @param {Object} param | ||
// @param {Object} header | ||
// @return {Object} {status, data, message} | ||
return http.options('url', param, header); | ||
``` | ||
是否携带cookies | ||
> #### filter | ||
过滤函数,发生在请求发出之前,可以对url, headers,fetch option进行更改,类型为 Promise function。 | ||
```javascript | ||
http.setup({ | ||
filter: data => new Promise((resolve, reject) => { | ||
// 获取 | ||
const { url, headers, option } = data; | ||
// 修改 | ||
const newHeaders = { | ||
...headers, | ||
token: 'zhnagsan' | ||
}; | ||
// 返回 | ||
resolve({ url, headers: newHeaders, option }); | ||
}) | ||
}); | ||
``` | ||
> #### callback | ||
回调数据函数,对请求成功获取到的数据进行预处理 | ||
```javascript | ||
http.setup({ | ||
callback: data => { | ||
const { status, data, messgae } = data; | ||
if (status) { | ||
return data; | ||
} else { | ||
throw new Erroe(message); | ||
} | ||
} | ||
``` | ||
### get | ||
get 方法,一般用于数据查询 | ||
```javascript | ||
return http.get('/get', {currentPage: 1}, {token: 'zhangsan'}); // 此处的headers优先级最高 | ||
``` | ||
### post | ||
post 方法,一般用于数据提交 | ||
```javascript | ||
return http.post('/add', {username: 'zhangsan'}); | ||
``` | ||
### put | ||
put 方法,一般用于数据修改 | ||
```javascript | ||
return http.put('/update/1', {username: 'lisi'}); | ||
``` | ||
### delete | ||
delete 方法,一般用于数据删除 | ||
```javascript | ||
return http.put('/delete/1'); | ||
``` | ||
### json | ||
请求头 "Content-Type" 为 "application/json" 的 post 方法 | ||
```javascript | ||
return http.post('/add', {username: 'zhangsan'}); | ||
``` | ||
### options | ||
option 方法,一般用于请求时的预检 | ||
## Links | ||
@@ -77,0 +111,0 @@ |
import fetch from 'isomorphic-fetch'; | ||
import { stringify } from 'qs'; | ||
const config = Symbol('config'); | ||
const http = Symbol('http'); | ||
const defaultHeaders = { | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
"Access-Control-Allow-Origin": "*", | ||
"Access-Control-Max-Age": "86400", | ||
"Access-Control-Allow-Methods": "POST, GET, OPTIONS, PUT, DELETE", | ||
"Access-Control-Allow-Headers": "token, host, x-real-ip, x-forwarded-ip, accept, content-type", | ||
}; | ||
/* | ||
@@ -11,3 +17,3 @@ * Requests a URL, returning a promise. | ||
* @param {object} [options] The options we want to pass to "fetch" | ||
* @param {object} header The request header | ||
* @param {object} headers The request headers | ||
* @return {object} An object containing either "data" or "err" | ||
@@ -19,70 +25,84 @@ */ | ||
this.prefix = ""; | ||
this.header = {}; | ||
this.filter = () => false; | ||
this.headers = {}; | ||
this.filter = _ => new Promise(resolve => resolve(_)); | ||
this.callback = _ => _; | ||
this[config] = { | ||
headers: { | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
"Access-Control-Allow-Origin": "*", | ||
"Access-Control-Max-Age": "86400", | ||
"Access-Control-Allow-Methods": "POST, GET, OPTIONS, PUT, DELETE", | ||
"Access-Control-Allow-Headers": "token, host, x-real-ip, x-forwarded-ip, accept, content-type", | ||
}, | ||
credentials: "include", | ||
client_max_body_size: "2048m" | ||
}; | ||
this.cookies = true; | ||
this.option = {}; | ||
} | ||
[http](url, option = {}, header = {}) { | ||
this.filter && this.filter(); | ||
return fetch(`${this.prefix}${url}`, { ...this[config], headers: { ...this[config].headers, ...this.header, ...header }, ...option }) | ||
.then((resp) => { | ||
if (resp.status >= 400) { | ||
throw new Error('400+Error'); | ||
} | ||
return resp; | ||
}) | ||
.then((resp) => { | ||
try { | ||
return resp.json(); | ||
} catch (e) { | ||
throw new Error('JSONError'); | ||
} | ||
}) | ||
.then(this.callback); | ||
[http](url, option = {}, headers = {}) { | ||
// url | ||
const _url = `${this.prefix}${url}`; | ||
// request headers | ||
const _headers = { ...defaultHeaders, ...this.headers, ...headers }; | ||
// fetch option | ||
const _option = { | ||
...this.option, | ||
credentials: this.cookies ? "include" : undefined, | ||
...option, | ||
}; | ||
return this.filter({url: _url, headers: _headers, option: _option}) | ||
.then(({url: u, headers: h, option: o}) => fetch(u, {...o, headers: h})) | ||
.then((resp) => { | ||
if (resp.status >= 400) { | ||
throw new Error('400+Error'); | ||
} | ||
return resp; | ||
}) | ||
.then((resp) => { | ||
try { | ||
return resp.json(); | ||
} catch (e) { | ||
throw new Error('JSONError'); | ||
} | ||
}) | ||
.then(this.callback) | ||
.catch(msg => { | ||
throw new Error(msg); | ||
}); | ||
} | ||
setup({prefix = "", header = {}, filter = this.filter, callback = this.callback}) { | ||
setup({ | ||
prefix = "", | ||
headers = this.headers, | ||
filter = this.filter, | ||
callback = this.callback, | ||
cookies = this.cookies, | ||
...option | ||
}) { | ||
this.prefix = prefix; | ||
this.header = header; | ||
this.headers = headers; | ||
this.filter = filter; | ||
this.callback = callback; | ||
this.cookies = cookies; | ||
this.option = option; | ||
} | ||
setHeader(header = {}) { | ||
this.header = {...this.header, ...header}; | ||
setHeaders(headers = {}) { | ||
this.headers = {...this.headers, ...headers}; | ||
} | ||
get(url, param, cors, header = {}) { | ||
return this[http](`${url}?${stringify(param)}`, cors ? { mode: "no-cors" } : {}, header); | ||
get(url, param, headers = {}) { | ||
return this[http](`${url}?${stringify(param)}`, {}, headers); | ||
} | ||
post(url, param, cors, header = {}) { | ||
return this[http](url, {...{method: 'POST', body: stringify(param)}, ...cors ? { mode: "no-cors" } : {}}, header); | ||
post(url, param, headers = {}) { | ||
return this[http](url, {method: 'POST', body: stringify(param)}, headers); | ||
} | ||
put(url, param, header = {}) { | ||
return this[http](url, {method: 'PUT', body: stringify(param)}, header); | ||
put(url, param, headers = {}) { | ||
return this[http](url, {method: 'PUT', body: stringify(param)}, headers); | ||
} | ||
delete(url, param, header = {}) { | ||
return this[http](`${url}?${stringify(param)}`, {method: 'DELETE'}, header); | ||
delete(url, param, headers = {}) { | ||
return this[http](`${url}?${stringify(param)}`, {method: 'DELETE'}, headers); | ||
} | ||
options(url, param, header = {}) { | ||
return this[http](url, {method: 'OPTIONS'}, header); | ||
options(url, param, headers = {}) { | ||
return this[http](url, {method: 'OPTIONS'}, headers); | ||
} | ||
json(url, param = {}, header) { | ||
return this[http](url, {method: 'POST', body: JSON.stringify(param)}, {...header, "Content-Type": "application/json"}); | ||
json(url, param = {}, headers) { | ||
return this[http](url, {method: 'POST', body: JSON.stringify(param)}, {...headers, "Content-Type": "application/json"}); | ||
} | ||
@@ -89,0 +109,0 @@ } |
@@ -1,11 +0,12 @@ | ||
import $http from './isomorphic-fetch-http'; | ||
$http.setup({ | ||
prefix: '19634', | ||
header: {}, | ||
import http from './isomorphic-fetch-http'; | ||
http.setup({ | ||
prefix: '/19634', | ||
headers: { | ||
"clent_max_body_size": "1024m" | ||
}, | ||
// filter: ({url, headers, option}) => new Promise((resolve) => { | ||
// resolve({url, headers: {...headers, token: 'asdasd'}, option}); | ||
// }) | ||
}); | ||
$http.setHeader({ | ||
token: 'asdasd' | ||
}); | ||
$http.get("/add?currentPage=1"); | ||
http.get("/add", {currentPage: 1}); |
@@ -19,15 +19,21 @@ 'use strict'; | ||
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var config = Symbol('config'); | ||
var http = Symbol('http'); | ||
/* | ||
* Requests a URL, returning a promise. | ||
* @param {string} url The URL we want to request | ||
* @param {object} [options] The options we want to pass to "fetch" | ||
* @param {object} header The request header | ||
* @return {object} An object containing either "data" or "err" | ||
*/ | ||
var defaultHeaders = { | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
"Access-Control-Allow-Origin": "*", | ||
"Access-Control-Max-Age": "86400", | ||
"Access-Control-Allow-Methods": "POST, GET, OPTIONS, PUT, DELETE", | ||
"Access-Control-Allow-Headers": "token, host, x-real-ip, x-forwarded-ip, accept, content-type" | ||
/* | ||
* Requests a URL, returning a promise. | ||
* @param {string} url The URL we want to request | ||
* @param {object} [options] The options we want to pass to "fetch" | ||
* @param {object} headers The request headers | ||
* @return {object} An object containing either "data" or "err" | ||
*/ | ||
}; | ||
var _http = function () { | ||
@@ -39,23 +45,13 @@ function _http() { | ||
this.prefix = ""; | ||
this.header = {}; | ||
this.filter = { | ||
before: function before() { | ||
return false; | ||
}, | ||
after: function after() { | ||
return false; | ||
} | ||
this.headers = {}; | ||
this.filter = function (_) { | ||
return new Promise(function (resolve) { | ||
return resolve(_); | ||
}); | ||
}; | ||
this.exception = []; | ||
this[config] = { | ||
headers: { | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
"Access-Control-Allow-Origin": "*", | ||
"Access-Control-Max-Age": "86400", | ||
"Access-Control-Allow-Methods": "POST, GET, OPTIONS, PUT, DELETE", | ||
"Access-Control-Allow-Headers": "token, host, x-real-ip, x-forwarded-ip, accept, content-type" | ||
}, | ||
credentials: "include", | ||
client_max_body_size: "2048m" | ||
this.callback = function (_) { | ||
return _; | ||
}; | ||
this.cookies = true; | ||
this.option = {}; | ||
} | ||
@@ -66,9 +62,20 @@ | ||
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] : {}; | ||
var headers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
this.filter.before && this.filter.before(); | ||
return (0, _isomorphicFetch2.default)('' + this.prefix + url, _extends({}, this[config], { headers: _extends({}, this[config].headers, this.header, header) }, option)).then(function (resp) { | ||
// url | ||
var _url = '' + this.prefix + url; | ||
// request headers | ||
var _headers = _extends({}, defaultHeaders, this.headers, headers); | ||
// fetch option | ||
var _option = _extends({}, this.option, { | ||
credentials: this.cookies ? "include" : undefined | ||
}, option); | ||
return this.filter({ url: _url, headers: _headers, option: _option }).then(function (_ref) { | ||
var u = _ref.url, | ||
h = _ref.headers, | ||
o = _ref.option; | ||
return (0, _isomorphicFetch2.default)(u, _extends({}, o, { headers: h })); | ||
}).then(function (resp) { | ||
if (resp.status >= 400) { | ||
@@ -84,15 +91,4 @@ throw new Error('400+Error'); | ||
} | ||
}).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).catch(function (msg) { | ||
throw new Error(msg); | ||
}); | ||
@@ -105,20 +101,25 @@ } | ||
prefix = _ref2$prefix === undefined ? "" : _ref2$prefix, | ||
_ref2$header = _ref2.header, | ||
header = _ref2$header === undefined ? {} : _ref2$header, | ||
_ref2$headers = _ref2.headers, | ||
headers = _ref2$headers === undefined ? this.headers : _ref2$headers, | ||
_ref2$filter = _ref2.filter, | ||
filter = _ref2$filter === undefined ? this.filter : _ref2$filter, | ||
_ref2$exception = _ref2.exception, | ||
exception = _ref2$exception === undefined ? [] : _ref2$exception; | ||
_ref2$callback = _ref2.callback, | ||
callback = _ref2$callback === undefined ? this.callback : _ref2$callback, | ||
_ref2$cookies = _ref2.cookies, | ||
cookies = _ref2$cookies === undefined ? this.cookies : _ref2$cookies, | ||
option = _objectWithoutProperties(_ref2, ['prefix', 'headers', 'filter', 'callback', 'cookies']); | ||
this.prefix = prefix; | ||
this.header = header; | ||
this.headers = headers; | ||
this.filter = filter; | ||
this.exception = exception; | ||
this.callback = callback; | ||
this.cookies = cookies; | ||
this.option = option; | ||
} | ||
}, { | ||
key: 'setHeader', | ||
value: function setHeader() { | ||
var header = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
key: 'setHeaders', | ||
value: function setHeaders() { | ||
var headers = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
this.header = _extends({}, this.header, header); | ||
this.headers = _extends({}, this.headers, headers); | ||
} | ||
@@ -128,5 +129,5 @@ }, { | ||
value: function get(url, param) { | ||
var header = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var headers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return this[http](url + '?' + (0, _qs.stringify)(param), {}, header); | ||
return this[http](url + '?' + (0, _qs.stringify)(param), {}, headers); | ||
} | ||
@@ -136,5 +137,5 @@ }, { | ||
value: function post(url, param) { | ||
var header = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var headers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return this[http](url, { method: 'POST', body: (0, _qs.stringify)(param) }, header); | ||
return this[http](url, { method: 'POST', body: (0, _qs.stringify)(param) }, headers); | ||
} | ||
@@ -144,5 +145,5 @@ }, { | ||
value: function put(url, param) { | ||
var header = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var headers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return this[http](url, { method: 'PUT', body: (0, _qs.stringify)(param) }, header); | ||
return this[http](url, { method: 'PUT', body: (0, _qs.stringify)(param) }, headers); | ||
} | ||
@@ -152,5 +153,5 @@ }, { | ||
value: function _delete(url, param) { | ||
var header = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var headers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return this[http](url + '?' + (0, _qs.stringify)(param), { method: 'DELETE' }, header); | ||
return this[http](url + '?' + (0, _qs.stringify)(param), { method: 'DELETE' }, headers); | ||
} | ||
@@ -160,22 +161,13 @@ }, { | ||
value: function options(url, param) { | ||
var header = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var headers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
return this[http](url, { method: 'OPTIONS' }, header); | ||
return this[http](url, { method: 'OPTIONS' }, headers); | ||
} | ||
}, { | ||
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', | ||
value: function json(url) { | ||
var param = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var header = arguments[2]; | ||
var headers = arguments[2]; | ||
return this[http](url, { method: 'POST', body: JSON.stringify(param) }, _extends({}, header, { "Content-Type": "application/json" })); | ||
return this[http](url, { method: 'POST', body: JSON.stringify(param) }, _extends({}, headers, { "Content-Type": "application/json" })); | ||
} | ||
@@ -182,0 +174,0 @@ }]); |
22622
438
117