Skip to content

Commit f8b29bb

Browse files
committed
123123123
1 parent 997ea18 commit f8b29bb

File tree

2 files changed

+101
-90
lines changed

2 files changed

+101
-90
lines changed

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"scripts": {
77
"start": "better-npm-run start",
88
"compile": "better-npm-run compile",
9-
"eslint:check": "eslint src",
10-
"flow:check": "flow",
9+
"lint": "eslint src",
10+
"flow": "flow",
1111
"lint:fix": "npm run lint -- --fix",
1212
"start:dist": "better-npm-run start:dist"
1313
},
@@ -40,6 +40,9 @@
4040
}
4141
}
4242
},
43+
"pre-commit": [
44+
"lint"
45+
],
4346
"author": "likun",
4447
"license": "MIT",
4548
"devDependencies": {
@@ -94,8 +97,5 @@
9497
"react-router": "^4.1.1",
9598
"redux": "^3.6.0",
9699
"whatwg-fetch": "^2.0.3"
97-
},
98-
"pre-commit": [
99-
"eslint:check"
100-
]
101-
}
100+
}
101+
}

src/utils/request.js

Lines changed: 94 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,44 @@ import 'whatwg-fetch';
22
import { stringify } from 'qs';
33
// 本工具类只能用于接收和处理json数据,其他数据请使用fetch原生;
44

5-
const noop = () => { };
5+
const noop = () => {};
66

77
let globalCallbacks = {
88
onStart: noop,
99
onComplete: noop,
1010
onSuccess: noop,
1111
onError: noop,
12-
onSuccessFilter: result => result
12+
onSuccessFilter: result => result,
1313
};
1414
let globalConifg = {};
15-
export const config = ({
16-
method = 'GET',
17-
headers,
18-
credentials = 'omit',
19-
body,
20-
onStart = noop,
21-
onComplete = noop,
22-
onError = noop,
23-
onSuccessFilter = result => result,
24-
onSuccess = noop
25-
} = {}) => {
15+
export const config = (
16+
{
17+
method = 'GET',
18+
headers,
19+
credentials = 'omit',
20+
body,
21+
onStart = noop,
22+
onComplete = noop,
23+
onError = noop,
24+
onSuccessFilter = result => result,
25+
onSuccess = noop,
26+
} = {}
27+
) => {
2628
globalCallbacks = {
2729
onStart,
2830
onComplete,
2931
onSuccess,
3032
onError,
31-
onSuccessFilter
33+
onSuccessFilter,
3234
};
3335
globalConifg = {
3436
method,
3537
headers,
3638
body,
37-
credentials
39+
credentials,
3840
};
3941
};
40-
const catchCallbackFn = fn => (params) => {
42+
const catchCallbackFn = fn => params => {
4143
try {
4244
fn(params);
4345
} catch (error) {
@@ -46,57 +48,61 @@ const catchCallbackFn = fn => (params) => {
4648
throw error;
4749
}
4850
};
49-
const request = (
50-
{
51-
headers = {},
52-
credentials = 'omit'
53-
} = {}) => (urlWithMethod, params = {}) => {
54-
let [method, url] = urlWithMethod.split(' '); // eslint-disable-line
55-
const { onStart, onComplete, onSuccess, onError, onSuccessFilter } = globalCallbacks;
56-
const assignHeaders = globalConifg.headers ? {
51+
const request = ({ headers = {}, credentials = 'omit' } = {}) => (urlWithMethod, params = {}) => {
52+
let [method, url] = urlWithMethod.split(' '); // eslint-disable-line
53+
const { onStart, onComplete, onSuccess, onError, onSuccessFilter } = globalCallbacks;
54+
const assignHeaders = globalConifg.headers
55+
? {
5756
...globalConifg.headers,
58-
...headers
59-
} : headers;
60-
const assignBody = globalConifg.body ? {
57+
...headers,
58+
}
59+
: headers;
60+
let assignBody = globalConifg.body
61+
? {
6162
...globalConifg.body,
62-
...params
63-
} : params;
64-
const options = {
65-
...globalConifg,
66-
headers: assignHeaders,
67-
method,
68-
credentials
69-
};
70-
assignBody = JSON.stringify(JSON.parse(JSON.stringify(assignBody)));
71-
if (Object.keys(assignBody).length) {
72-
if (method.toUpperCase() !== 'GET') {
73-
options.body = assignBody;
74-
} else {
75-
url += `?${stringify(assignBody)}`;
76-
}
63+
...params,
64+
}
65+
: params;
66+
const options = {
67+
...globalConifg,
68+
headers: assignHeaders,
69+
method,
70+
credentials,
71+
};
72+
assignBody = JSON.stringify(JSON.parse(JSON.stringify(assignBody)));
73+
if (Object.keys(assignBody).length) {
74+
if (method.toUpperCase() !== 'GET') {
75+
options.body = assignBody;
76+
} else {
77+
url += `?${stringify(assignBody)}`;
7778
}
78-
const promise = new Promise((resolve, reject) => {
79-
onStart();
80-
fetch(url, options).then((response) => {
79+
}
80+
const promise = new Promise((resolve, reject) => {
81+
onStart();
82+
fetch(url, options).then(
83+
response => {
8184
if (response.ok) {
82-
response.json().then((result) => {
83-
const filterResult = onSuccessFilter(result);
84-
if (Object.prototype.toString.call(filterResult) === '[object Error]') {
85-
onComplete(filterResult);
86-
onError(filterResult);
87-
reject(filterResult);
88-
} else {
89-
onSuccess(filterResult);
90-
onComplete(null, filterResult);
91-
resolve(filterResult);
85+
response.json().then(
86+
result => {
87+
const filterResult = onSuccessFilter(result);
88+
if (Object.prototype.toString.call(filterResult) === '[object Error]') {
89+
onComplete(filterResult);
90+
onError(filterResult);
91+
reject(filterResult);
92+
} else {
93+
onSuccess(filterResult);
94+
onComplete(null, filterResult);
95+
resolve(filterResult);
96+
}
97+
},
98+
error => {
99+
const e = new Error('服务器返回数据错误');
100+
onComplete(e);
101+
onError(e);
102+
reject(e);
103+
throw new Error(error);
92104
}
93-
}, (error) => {
94-
const e = new Error('服务器返回数据错误');
95-
onComplete(e);
96-
onError(e);
97-
reject(e);
98-
throw new Error(error);
99-
});
105+
);
100106
} else {
101107
console.log(response);
102108
const error = new Error(response.statusText);
@@ -105,33 +111,38 @@ const request = (
105111
onComplete(error);
106112
reject(error);
107113
}
108-
}, (error) => {
114+
},
115+
error => {
109116
onError(error);
110117
onComplete(error);
111118
reject(error);
112-
});
119+
}
120+
);
121+
});
122+
promise.success = fn => {
123+
promise.then(result => {
124+
if (typeof fn === 'function') {catchCallbackFn(fn)(result);}
125+
});
126+
return promise;
127+
};
128+
promise.error = fn => {
129+
promise.then(null, error => {
130+
if (typeof fn === 'function') {catchCallbackFn(fn)(error);}
113131
});
114-
promise.success = (fn) => {
115-
promise.then((result) => {
116-
if (typeof fn === 'function') catchCallbackFn(fn)(result);
117-
});
118-
return promise;
119-
};
120-
promise.error = (fn) => {
121-
promise.then(null, (error) => {
122-
if (typeof fn === 'function') catchCallbackFn(fn)(error);
123-
});
124-
return promise;
125-
};
126-
promise.complete = (fn) => {
127-
fn = (typeof fn === 'function') ? catchCallbackFn(fn) : noop;
128-
promise.then((result) => {
132+
return promise;
133+
};
134+
promise.complete = fn => {
135+
fn = typeof fn === 'function' ? catchCallbackFn(fn) : noop;
136+
promise.then(
137+
result => {
129138
fn(null, result);
130-
}, (error) => {
139+
},
140+
error => {
131141
fn(error);
132-
});
133-
return promise;
134-
};
142+
}
143+
);
135144
return promise;
136145
};
146+
return promise;
147+
};
137148
export default request;

0 commit comments

Comments
 (0)