Skip to content
This repository was archived by the owner on Feb 12, 2026. It is now read-only.

Commit bebaea8

Browse files
committed
Merge branch 'feature/newResponse'
2 parents 23a7b90 + 328c2af commit bebaea8

File tree

11 files changed

+128
-69
lines changed

11 files changed

+128
-69
lines changed

README.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,44 +52,53 @@ const fitch = require('fitch')
5252
### Make your first request:
5353
```js
5454
fitch.get(apiUrl)
55-
.then(data => console.log(data))
55+
.then(response => console.log(response))
56+
57+
/* Response:
58+
{
59+
data: { foo: 'bar' },
60+
status: 200,
61+
statusText: 'Ok',
62+
headers: { Content-Type: application/json },
63+
}
64+
*/
5665
```
5766

5867
## Methods available:
5968
### get
6069
```js
6170
fitch.get(apiUrl)
62-
.then(data => console.log(data))
71+
.then(response => console.log(response))
6372
```
6473

6574
### post
6675
```js
6776
const req = {body: {name: 'Happy cat'}}
6877

6978
fitch.post(apiUrl, req)
70-
.then(data => console.log(data))
79+
.then(response => console.log(response))
7180
```
7281

7382
### put
7483
```js
7584
const req = {body: {name: 'Happy cat'}}
7685

7786
fitch.put(apiUrl, req)
78-
.then(data => console.log(data))
87+
.then(response => console.log(response))
7988
```
8089

8190
### patch
8291
```js
8392
const req = {body: {name: 'Happy cat'}}
8493

8594
fitch.patch(apiUrl, req)
86-
.then(data => console.log(data))
95+
.then(response => console.log(response))
8796
```
8897

8998
### del
9099
```js
91100
fitch.del(apiUrl)
92-
.then(data => console.log(data))
101+
.then(response => console.log(response))
93102
```
94103

95104
## Use with custom configuration
@@ -107,7 +116,7 @@ const config = {
107116
}
108117

109118
fitch.get(apiUrl, config)
110-
.then(data => console.log(data))
119+
.then(response => console.log(response))
111120
```
112121
See more about fetch configuration at: [Fetch API](https://developer.mozilla.org/pt-BR/docs/Web/API/Fetch_API).
113122

docs/Config.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Receives a json object, that is transformed to string by Fitch.js, before send t
1414
const req = { body: { name: 'Happy cat' } }
1515

1616
fitch.post(apiUrl, req)
17-
.then(data => console.log(data))
17+
.then(response => console.log(response))
1818
```
1919

2020
### **dataType** *(string)*
@@ -32,14 +32,14 @@ See more at [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Body).
3232
const config = { dataType: 'blob' }
3333

3434
fitch.get('image.jpg', config)
35-
.then((imageBlob) => {
36-
const objectURL = URL.createObjectURL(imageBlob)
35+
.then((response) => {
36+
const objectURL = URL.createObjectURL(response.data)
3737
myImage.src = objectURL
3838
})
3939

4040
// Default: json.
4141
fitch.get('file.json')
42-
.then(data => console.log(data))
42+
.then(response => console.log(response))
4343
```
4444

4545
### **params** *(object)*
@@ -55,7 +55,7 @@ const config = {
5555
}
5656

5757
fitch.get(apiUrl, config)
58-
.then(data => console.log(data))
58+
.then(response => console.log(response))
5959
```
6060

6161
### **raw** *(boolean)*

docs/Intro.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,16 @@ import fitch from 'fitch'
2323
const apiUrl = 'https://localhost:8080/'
2424

2525
fitch.get(apiUrl)
26-
.then(data => console.log(data))
26+
.then(response => console.log(response))
27+
28+
/* Response:
29+
{
30+
data: { foo: 'bar' },
31+
status: 200,
32+
statusText: 'Ok',
33+
headers: { Content-Type: application/json },
34+
}
35+
*/
2736
```
2837

2938
Also, you can use cdn:

docs/Methods.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
```js
66
fitch.get(apiUrl, config)
7-
.then(data => console.log(data))
7+
.then(response => console.log(response))
88
```
99

1010
**Note:** GET method doesn't allow `body` inside config object.
@@ -15,7 +15,7 @@ fitch.get(apiUrl, config)
1515
const req = { body: { name: 'Happy cat' } }
1616

1717
fitch.post(apiUrl, req)
18-
.then(data => console.log(data))
18+
.then(response => console.log(response))
1919
```
2020

2121
**put(url, config)**
@@ -24,7 +24,7 @@ fitch.post(apiUrl, req)
2424
const req = { body: { name: 'Happy cat' } }
2525

2626
fitch.put(apiUrl, req)
27-
.then(data => console.log(data))
27+
.then(response => console.log(response))
2828
```
2929

3030
**patch(url, config)**
@@ -33,14 +33,14 @@ fitch.put(apiUrl, req)
3333
const req = { body: { name: 'Happy cat' } }
3434

3535
fitch.patch(apiUrl, req)
36-
.then(data => console.log(data))
36+
.then(response => console.log(response))
3737
```
3838

3939
**del(url, config)**
4040

4141
```js
4242
fitch.del(apiUrl)
43-
.then(data => console.log(data))
43+
.then(response => console.log(response))
4444
```
4545

4646
## Concurrency
@@ -67,7 +67,7 @@ If Fetch throws an error, we can handle this using `catch()`:
6767

6868
```js
6969
fitch.get(apiUrl)
70-
.then(data => console.log(data))
70+
.then(response => console.log(response))
7171
.catch(err => console.error(err))
7272
```
7373

docs/pt-br/Config.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Recebe um objeto json, que é transformado para string antes fazer a chamada.
1414
const req = { body: { name: 'Happy cat' } }
1515

1616
fitch.post(apiUrl, req)
17-
.then(data => console.log(data))
17+
.then(response => console.log(response))
1818
```
1919

2020
### **dataType** *(string)*
@@ -32,14 +32,14 @@ Veja mais em [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Body).
3232
const config = { dataType: 'blob' }
3333

3434
fitch.get('image.jpg', config)
35-
.then((imageBlob) => {
36-
const objectURL = URL.createObjectURL(imageBlob)
35+
.then((response) => {
36+
const objectURL = URL.createObjectURL(response.data)
3737
myImage.src = objectURL
3838
})
3939

4040
// Exemplo padrão (json).
4141
fitch.get('file.json')
42-
.then(data => console.log(data))
42+
.then(response => console.log(response))
4343
```
4444

4545
### **params** *(objeto)*
@@ -55,7 +55,7 @@ const config = {
5555
}
5656

5757
fitch.get(apiUrl, config)
58-
.then(data => console.log(data))
58+
.then(response => console.log(response))
5959
```
6060

6161
### **raw** *(boolean)*

docs/pt-br/Intro.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,16 @@ import fitch from 'fitch'
2121
const apiUrl = 'https://localhost:8080/'
2222

2323
fitch.get(apiUrl)
24-
.then(data => console.log(data))
24+
.then(response => console.log(response))
25+
26+
/* Resposta:
27+
{
28+
data: { foo: 'bar' },
29+
status: 200,
30+
statusText: 'Ok',
31+
headers: { Content-Type: application/json },
32+
}
33+
*/
2534
```
2635

2736
Também é possível usar o CDN:

docs/pt-br/Methods.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
```js
66
fitch.get(apiUrl, config)
7-
.then(data => console.log(data))
7+
.then(response => console.log(response))
88
```
99

1010
**Note:** O método GET não permite `body` dentro do objeto de configuração.
@@ -15,7 +15,7 @@ fitch.get(apiUrl, config)
1515
const req = { body: { name: 'Happy cat' } }
1616

1717
fitch.post(apiUrl, req)
18-
.then(data => console.log(data))
18+
.then(response => console.log(response))
1919
```
2020

2121
**put(url, config)**
@@ -24,7 +24,7 @@ fitch.post(apiUrl, req)
2424
const req = { body: { name: 'Happy cat' } }
2525

2626
fitch.put(apiUrl, req)
27-
.then(data => console.log(data))
27+
.then(response => console.log(response))
2828
```
2929

3030
**patch(url, config)**
@@ -33,14 +33,14 @@ fitch.put(apiUrl, req)
3333
const req = { body: { name: 'Happy cat' } }
3434

3535
fitch.patch(apiUrl, req)
36-
.then(data => console.log(data))
36+
.then(response => console.log(response))
3737
```
3838

3939
**del(url, config)**
4040

4141
```js
4242
fitch.del(apiUrl)
43-
.then(data => console.log(data))
43+
.then(response => console.log(response))
4444
```
4545

4646
## Concurrency
@@ -68,7 +68,7 @@ Se houver um erro na chamada do Fetch, você pode pegá-lo usando `catch()`:
6868

6969
```js
7070
fitch.get(apiUrl)
71-
.then(data => console.log(data))
71+
.then(response => console.log(response))
7272
.catch(err => console.error(err))
7373
```
7474

examples/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ fitch.all([fitch.get(baseUrl), fitch.get(baseUrl, { raw: true })])
3030

3131
// GET
3232
fitch.get(baseUrl).then(resp =>
33-
console.log('GET\t>>>', resp)
33+
console.log('GET\t>>>', resp.data)
3434
).catch(logError);
3535

3636
// GET with params
3737
fitch.get(baseUrl, reqGet).then(resp =>
38-
console.log('GET\t>>>', resp)
38+
console.log('GET\t>>>', resp.data)
3939
).catch(logError);
4040

4141
// GET raw output
@@ -45,20 +45,20 @@ fitch.get(baseUrl, { raw: true }).then(resp =>
4545

4646
// POST
4747
fitch.post(`${baseUrl}/new/`, reqPost).then(resp =>
48-
console.log('POST\t>>>', resp)
48+
console.log('POST\t>>>', resp.data)
4949
).catch(logError);
5050

5151
// PUT
5252
fitch.put(`${baseUrl}/1`, reqUpdate).then(resp =>
53-
console.log('PUT\t>>>', resp)
53+
console.log('PUT\t>>>', resp.data)
5454
).catch(logError);
5555

5656
// PATCH
5757
fitch.patch(`${baseUrl}/2`, reqUpdate).then(resp =>
58-
console.log('PATCH\t>>>', resp)
58+
console.log('PATCH\t>>>', resp.data)
5959
).catch(logError);
6060

6161
// DELETE
6262
fitch.del(`${baseUrl}/2`).then(resp =>
63-
console.log('DELETE\t>>>', resp)
63+
console.log('DELETE\t>>>', resp.data)
6464
).catch(logError);

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,21 @@
4747
"build:umd.min": "webpack --output-filename index.umd.min.js -p --colors",
4848
"check": "npm run lint && npm run test",
4949
"clean": "rimraf dist coverage",
50-
"coverage": "nyc ava ./tests/*",
50+
"coverage": "nyc report npm test",
5151
"examples": "node ./examples/index.js",
5252
"prebuild": "npm run check && npm run clean",
5353
"lint": "eslint src",
5454
"start": "node ./examples/api/index.js",
55-
"test": "ava ./tests/*",
55+
"test": "ava",
5656
"watch:test": "npm test -- -w"
5757
},
5858
"ava": {
59+
"files": [
60+
"tests/*.js"
61+
],
62+
"source": [
63+
"src/**/*.js"
64+
],
5965
"require": [
6066
"babel-register"
6167
],

src/utils/check.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
function check(resp, dataType = 'json') {
22
const typeList = ['arrayBuffer', 'blob', 'formData', 'json', 'text'];
3-
const included = typeList.indexOf(dataType);
3+
const included = typeList.indexOf(dataType) !== -1;
44

5-
if (resp.ok && included !== -1) {
6-
return resp[dataType]();
5+
if (resp.ok && included) {
6+
return resp[dataType]()
7+
.then(data => ({
8+
data,
9+
status: resp.status,
10+
statusText: resp.statusText,
11+
headers: resp.headers,
12+
}));
713
}
814

9-
throw new Error(`${resp.status} - ${resp.statusText}.`);
15+
const errorMerssage = !included ? 'Invalid data type' : `${resp.status} - ${resp.statusText}.`;
16+
17+
throw new Error(errorMerssage);
1018
}
1119

1220
export default check;

0 commit comments

Comments
 (0)