Skip to content

Commit c0981d6

Browse files
committed
Refactor response and closer to raw fetch
1 parent 73ef247 commit c0981d6

File tree

4 files changed

+75
-21
lines changed

4 files changed

+75
-21
lines changed

example/Basic/src/app.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,33 @@ class App extends Component {
1919
}
2020

2121
async getData() {
22-
const { data } = await fetch('/api/users');
22+
const { data, err } = await fetch('/api/users')
23+
.then(res => {
24+
if (res.status !== 200) {
25+
throw new Error('fetch failed');
26+
}
27+
return res.json();
28+
})
29+
.then(data => ({ data }))
30+
.catch(err => ({ err }));
31+
32+
if (err) {
33+
return false;
34+
}
35+
2336
this.setState({
2437
data,
2538
});
2639
}
2740

2841
render() {
2942
return (
30-
<View style={ {marginTop: 100} }>
31-
{
32-
this.state.data.map((item, index) => {
33-
return <Text key={ index }>{ item.name }</Text>
34-
})
35-
}
43+
<View style={{ marginTop: 100 }}>
44+
{
45+
this.state.data.map((item, index) => {
46+
return <Text key={index}>{item.name}</Text>
47+
})
48+
}
3649
</View>
3750
);
3851
}

example/WithMockJs/src/app.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,20 @@ class App extends Component {
1919
}
2020

2121
async getData() {
22-
const { data } = await fetch('/api/users/mockjs');
22+
const { data, err } = await fetch('/api/users/mockjs')
23+
.then(res => {
24+
if (res.status !== 200) {
25+
throw new Error('fetch failed');
26+
}
27+
return res.json();
28+
})
29+
.then(data => ({ data }))
30+
.catch(err => ({ err }));
31+
32+
if (err) {
33+
return false;
34+
}
35+
2336
this.setState({
2437
data,
2538
});

src/response.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,37 @@
11

2+
const _status = Symbol('status');
3+
const _data = Symbol('data');
4+
const _statusText = Symbol('statusText');
5+
26
class Response {
37
constructor({
48
status,
59
data = {},
610
statusText = '',
711
}) {
8-
this.status = status;
9-
this.data = data;
10-
this.statusText = statusText;
12+
this[_status] = status;
13+
this[_data] = data;
14+
this[_statusText] = statusText;
15+
}
16+
17+
get status() {
18+
return this[_status];
19+
}
20+
21+
get statusText() {
22+
return this[_statusText];
1123
}
1224

1325
text() {
1426
try {
15-
return Promise.resolve(JSON.stringify(data));
27+
return Promise.resolve(JSON.stringify(this[_data]));
1628
} catch (err) {
1729
return Promise.reject(new Errror('failed text invoke.'));
1830
}
1931
}
2032

2133
json() {
22-
return this.data;
34+
return this[_data];
2335
}
2436

2537
}

test/index.test.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,40 @@ import FetchMock, { Mock } from '../';
55
const fetch = new FetchMock(require('../__mocks__')).fetch;
66
describe('test fetch mock', () => {
77
it('fetch /api/users data', async () => {
8-
const { status, data } = await fetch('/api/users');
8+
const response = await fetch('/api/users');
9+
const { status } = response;
910
expect(status).to.be.eql(200);
11+
const data = await response.json();
1012
expect(data).not.to.be(undefined);
1113
expect(data).not.to.be.empty();
1214
expect(data).to.be.an('array');
1315
expect(data).to.have.length(2);
1416
});
1517

1618
it('fetch /api/users?a=b', async () => {
17-
const { status, data } = await fetch('/api/users');
19+
const response = await fetch('/api/users');
20+
const { status } = response;
1821
expect(status).to.be.eql(200);
22+
const data = await response.json();
1923
expect(data).not.to.be(undefined);
2024
expect(data).not.to.be.empty();
2125
expect(data).to.be.an('array');
2226
expect(data).to.have.length(2);
2327
});
2428

2529
it('fetch /api/users with url parameters', async () => {
26-
const { status, data } = await fetch('/api/users?name=John');
30+
const response = await fetch('/api/users?name=John');
31+
const { status } = response;
2732
expect(status).to.be.eql(200);
33+
const data = await response.json();
2834
expect(data).not.to.be(undefined);
2935
expect(data).not.to.be.empty();
3036
expect(data).to.be.an('array');
3137
expect(data).to.have.length(1);
3238
});
3339

3440
it('fetch /api/users with post parameters', async () => {
35-
const { status, data } = await fetch('/api/users', {
41+
const response = await fetch('/api/users', {
3642
method: 'GET',
3743
headers: {
3844
'Content-Type': 'application/json',
@@ -41,33 +47,41 @@ describe('test fetch mock', () => {
4147
name: 'John',
4248
}),
4349
});
50+
const { status } = response;
4451
expect(status).to.be.eql(200);
52+
const data = await response.json();
4553
expect(data).not.to.be(undefined);
4654
expect(data).not.to.be.empty();
4755
expect(data).to.be.an('array');
4856
expect(data).to.have.length(1);
4957
});
5058

5159
it('fetch /api/users/{userId}', async () => {
52-
const { status, data } = await fetch('/api/users/123');
60+
const response = await fetch('/api/users/123');
61+
const { status } = response;
5362
expect(status).to.be.eql(200);
63+
const data = await response.json();
5464
expect(data).not.to.be(undefined);
5565
expect(data).not.to.be.empty();
5666
expect(data).to.be.property('userId', '123');
5767
});
5868

5969
it('fetch /api/users/mockjs with mockjs', async () => {
60-
const { status, data } = await fetch('/api/users/mockjs');
70+
const response = await fetch('/api/users/mockjs');
71+
const { status } = response;
6172
expect(status).to.be.eql(200);
73+
const data = await response.json();
6274
expect(data).not.to.be(undefined);
6375
expect(data).not.to.be.empty();
6476
expect(data).to.be.an('array');
6577
expect(data).to.have.length(2);
6678
});
6779

6880
it('fetch /api/users/mockjs with mockjs', async () => {
69-
const { status, data } = await fetch('/api/users/mockjs');
81+
const response = await fetch('/api/users/mockjs');
82+
const { status } = response;
7083
expect(status).to.be.eql(200);
84+
const data = await response.json();
7185
expect(data).not.to.be(undefined);
7286
expect(data).not.to.be.empty();
7387
expect(data).to.be.an('array');
@@ -88,7 +102,7 @@ describe('test fetch mock', () => {
88102
});
89103

90104
it('put /api/users/123', async () => {
91-
const { status, data } = await fetch('/api/users/123', {
105+
const response = await fetch('/api/users/123', {
92106
method: 'PUT',
93107
headers: {
94108
'Content-Type': 'application/json',
@@ -97,7 +111,9 @@ describe('test fetch mock', () => {
97111
name: 'John2',
98112
}),
99113
});
114+
const { status } = response;
100115
expect(status).to.be.eql(204);
116+
const data = await response.json();
101117
expect(data).not.to.be(undefined);
102118
expect(data).not.to.be.empty();
103119
expect(data).to.be.an('object');

0 commit comments

Comments
 (0)