Skip to content

Commit 9f950d7

Browse files
authored
feat: added additional fetch response properties (#18)
* feat: added more properties to the useFetch composable * docs: added new properties to the fetch docs
1 parent 315ff52 commit 9f950d7

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

docs/guide/fetch.md

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ import { useFetch } from 'vue-use-web';
1212
const { isLoading, json, text, error, success } = useFetch('http://myurl.com');
1313
```
1414

15-
| State | Type | Description |
16-
| --------- | --------- | -------------------------------------------------------------- |
17-
| isLoading | `Boolean` | If the request is pending. |
18-
| error | `Boolean` | If the request resulted in a non 200 status code. |
19-
| success | `Boolean` | If the request is successful. i.e resulted in 200 status code. |
20-
| json | `any` | The response body as JSON. |
21-
| text | `string` | The raw response body as a string. |
15+
| State | Type | Description |
16+
| ---------- | ------------------------ | -------------------------------------------------------------------------------- |
17+
| error | `Boolean` | If the request resulted in a non 200 status code. |
18+
| headers | `Record<string, string>` | The response headers. |
19+
| isLoading | `Boolean` | If the request is pending. |
20+
| json | `any` | The response body as JSON. |
21+
| status | `number` | The HTTP status code. |
22+
| statusText | `number` | The HTTP status text, eg: "OK" for 200. |
23+
| success | `Boolean` | If the request is successful. i.e resulted in 200 status code. |
24+
| text | `string` | The raw response body as a string. |
25+
| type | `string` | [Response type](https://developer.mozilla.org/en-US/docs/Web/API/Response/type). |
2226

2327
## Methods
2428

@@ -50,20 +54,11 @@ const { cancel } = useFetch(elem);
5054
</template>
5155
5256
<script>
53-
import { useFetch } from "vue-use-web";
57+
import { useFetch } from 'vue-use-web';
5458
5559
export default {
5660
setup() {
57-
const {
58-
isLoading,
59-
error,
60-
success,
61-
cancel,
62-
text,
63-
blob,
64-
json,
65-
cancelled
66-
} = useFetch("/data.json");
61+
const { isLoading, error, success, cancel, text, blob, json, cancelled } = useFetch('/data.json');
6762
6863
return { isLoading, error, success, cancel, text, blob, json, cancelled };
6964
}

src/Fetch.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ export function useFetch(url: RequestInfo, options: RequestInit) {
55
blob: Blob | null;
66
json: any;
77
text: string;
8+
statusText: string;
9+
status: number | undefined;
810
isLoading: boolean;
11+
headers: Record<string, string>;
912
success: boolean;
13+
type: ResponseType | 'unknown';
1014
error: boolean;
1115
cancelled: boolean;
1216
} = {
@@ -16,7 +20,11 @@ export function useFetch(url: RequestInfo, options: RequestInit) {
1620
cancelled: false,
1721
json: null,
1822
blob: null,
19-
text: ''
23+
text: '',
24+
type: 'unknown',
25+
status: undefined,
26+
statusText: '',
27+
headers: {}
2028
};
2129

2230
const state = reactive(stateDefs);
@@ -37,6 +45,15 @@ export function useFetch(url: RequestInfo, options: RequestInit) {
3745
state.success = res.ok;
3846
state.error = !res.ok;
3947
state.isLoading = false;
48+
state.statusText = res.statusText;
49+
state.status = res.status;
50+
state.type = res.type;
51+
const headers: Record<string, string> = {};
52+
res.headers.forEach((value, key) => {
53+
headers[key] = value;
54+
});
55+
56+
state.headers = headers;
4057

4158
return Promise.all([res.clone().text(), res.clone().blob(), res.json()]);
4259
})

0 commit comments

Comments
 (0)