Skip to content

Commit 6170ac9

Browse files
committed
update superagent
1 parent 4738056 commit 6170ac9

File tree

2 files changed

+61
-52
lines changed

2 files changed

+61
-52
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-http-request",
3-
"version": "1.0.4",
3+
"version": "2.0.0-alpha.1",
44
"description": "React component exposes network request functionality",
55
"main": "lib/index.js",
66
"scripts": {
@@ -30,7 +30,7 @@
3030
},
3131
"dependencies": {
3232
"prop-types": "^15.6.0",
33-
"superagent": "1.7.2"
33+
"superagent": "^4.1.0"
3434
},
3535
"devDependencies": {
3636
"babel-cli": "6.5.1",

src/index.js

+59-50
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,62 @@ import React from 'react';
22
import request from 'superagent';
33
import PropTypes from 'prop-types';
44

5-
class Request extends React.Component {
5+
export default class Request extends React.Component {
66

77
static defaultProps = {
88
method: 'get',
9+
};
10+
11+
static propTypes = {
12+
children: PropTypes.func,
13+
method: PropTypes.string.isRequired,
14+
type: PropTypes.string,
15+
accept: PropTypes.string,
16+
url: PropTypes.string.isRequired,
17+
timeout: PropTypes.number,
18+
verbose: PropTypes.bool,
19+
query: PropTypes.oneOfType([
20+
PropTypes.string,
21+
PropTypes.object,
22+
]),
23+
send: PropTypes.oneOfType([
24+
PropTypes.string,
25+
PropTypes.object,
26+
]),
27+
headers: PropTypes.object,
28+
auth: PropTypes.object,
29+
withCredentials: PropTypes.bool,
30+
buffer: PropTypes.bool,
31+
attach: PropTypes.array,
32+
fields: PropTypes.array,
33+
onRequest: PropTypes.func,
34+
};
35+
36+
request = null;
37+
38+
state = {
39+
error: null,
40+
result: null,
41+
loading: true,
42+
};
43+
44+
componentDidMount() {
45+
this.performRequest(this.props);
946
}
1047

11-
constructor(props) {
12-
super(props);
13-
this.request = null;
14-
this.state = {
15-
error: null,
16-
result: null,
17-
loading: true,
18-
};
48+
componentWillReceiveProps(...params) {
49+
this.willReceiveProps(...params);
1950
}
2051

21-
componentWillMount() {
22-
this.performRequest(this.props);
52+
UNSAFE_componentWillReceiveProps(...params) {
53+
this.willReceiveProps(...params);
2354
}
24-
componentWillReceiveProps(nextProps) {
55+
56+
componentWillUnmount() {
57+
this.request.abort();
58+
}
59+
60+
willReceiveProps = nextProps => {
2561
if (JSON.stringify(this.props) === JSON.stringify(nextProps)) {
2662
return;
2763
}
@@ -36,10 +72,6 @@ class Request extends React.Component {
3672
this.performRequest(nextProps);
3773
}
3874

39-
componentWillUnmount() {
40-
this.request.abort();
41-
}
42-
4375
performRequest(props) {
4476
let { method } = props;
4577
if (method === 'delete') {
@@ -102,17 +134,21 @@ class Request extends React.Component {
102134
}
103135

104136
this.request
105-
.end((error, result) => {
106-
if (error || !result.ok) {
107-
this.printLog(props, error);
108-
} else {
109-
this.printLog(props, result);
110-
}
137+
.then(result => {
138+
this.printLog(props, result);
111139
this.setState({
112-
error,
140+
error: null,
113141
result,
114142
loading: false,
115143
});
144+
})
145+
.catch(error => {
146+
this.printLog(props, error);
147+
this.setState({
148+
error,
149+
result: null,
150+
loading: false,
151+
});
116152
});
117153
}
118154

@@ -126,30 +162,3 @@ class Request extends React.Component {
126162
return this.props.children(this.state);
127163
}
128164
}
129-
130-
Request.propTypes = {
131-
children: PropTypes.func,
132-
method: PropTypes.string.isRequired,
133-
type: PropTypes.string,
134-
accept: PropTypes.string,
135-
url: PropTypes.string.isRequired,
136-
timeout: PropTypes.number,
137-
verbose: PropTypes.bool,
138-
query: PropTypes.oneOfType([
139-
PropTypes.string,
140-
PropTypes.object,
141-
]),
142-
send: PropTypes.oneOfType([
143-
PropTypes.string,
144-
PropTypes.object,
145-
]),
146-
headers: PropTypes.object,
147-
auth: PropTypes.object,
148-
withCredentials: PropTypes.bool,
149-
buffer: PropTypes.bool,
150-
attach: PropTypes.array,
151-
fields: PropTypes.array,
152-
onRequest: PropTypes.func,
153-
};
154-
155-
export default Request;

0 commit comments

Comments
 (0)