npm install isomorphic-fetch-http --save
import { http } from 'isomorphic-fetch-http'
http.setup({
cookies: true,
});
return http.get('url', param, header);
全局配置
请求 url 前缀
配置请求头
是否携带cookies
过滤函数,发生在请求发出之前,可以对url, headers,fetch option进行更改,类型为 Promise function。
http.setup({
filter: data => new Promise((resolve, reject) => {
// 获取
const { url, headers, option } = data;
// 修改
const newHeaders = {
...headers,
token: 'zhnagsan'
};
// 返回
resolve({ url, headers: newHeaders, option });
})
});
回调数据函数,对请求成功获取到的数据进行预处理
http.setup({
callback: data => {
const { status, data, messgae } = data;
if (status) {
return data;
} else {
throw new Erroe(message);
}
}
get 方法,一般用于数据查询
return http.get('/get', {currentPage: 1}, {token: 'zhangsan'}); // 此处的headers优先级最高
post 方法,一般用于数据提交
return http.post('/add', {username: 'zhangsan'});
put 方法,一般用于数据修改
return http.put('/update/1', {username: 'lisi'});
delete 方法,一般用于数据删除
return http.put('/delete/1');
请求头 "Content-Type" 为 "application/json" 的 post 方法
return http.post('/add', {username: 'zhangsan'});
option 方法,一般用于请求时的预检
"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",
You can submit any ideas as pull requests or as a Github issue.