async-fetch 0.3.7 → 0.3.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +84 -46
- package/dist/cjs/index.js +145 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/interfaces.js +3 -0
- package/dist/cjs/interfaces.js.map +1 -0
- package/dist/cjs/normalizeError.js +36 -0
- package/dist/cjs/normalizeError.js.map +1 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/types/index.d.ts +35 -0
- package/dist/cjs/types/index.d.ts.map +1 -0
- package/dist/cjs/types/interfaces.d.ts +31 -0
- package/dist/cjs/types/interfaces.d.ts.map +1 -0
- package/dist/cjs/types/normalizeError.d.ts +14 -0
- package/dist/cjs/types/normalizeError.d.ts.map +1 -0
- package/dist/cjs/types/useInterval.d.ts +12 -0
- package/dist/cjs/types/useInterval.d.ts.map +1 -0
- package/dist/cjs/useInterval.js +26 -0
- package/dist/cjs/useInterval.js.map +1 -0
- package/dist/esm/index.js +140 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/interfaces.js +2 -0
- package/dist/esm/interfaces.js.map +1 -0
- package/dist/esm/normalizeError.js +34 -0
- package/dist/esm/normalizeError.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/types/index.d.ts +35 -0
- package/dist/esm/types/index.d.ts.map +1 -0
- package/dist/esm/types/interfaces.d.ts +31 -0
- package/dist/esm/types/interfaces.d.ts.map +1 -0
- package/dist/esm/types/normalizeError.d.ts +14 -0
- package/dist/esm/types/normalizeError.d.ts.map +1 -0
- package/dist/esm/types/useInterval.d.ts +12 -0
- package/dist/esm/types/useInterval.d.ts.map +1 -0
- package/dist/esm/useInterval.js +24 -0
- package/dist/esm/useInterval.js.map +1 -0
- package/package.json +59 -24
- package/dist/index.d.ts +0 -30
- package/dist/index.js +0 -117
- package/dist/useInterval.d.ts +0 -2
- package/dist/useInterval.js +0 -17
- package/tsconfig.json +0 -112
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nameer
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,19 +1,18 @@
|
|
|
1
|
-
#
|
|
1
|
+
# async-fetch
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A React hook for async fetch requests with built-in state management, cancellation, and polling.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
|
-
```
|
|
8
|
-
npm
|
|
7
|
+
```bash
|
|
8
|
+
npm install async-fetch
|
|
9
|
+
# or
|
|
10
|
+
yarn add async-fetch
|
|
9
11
|
```
|
|
10
12
|
|
|
11
13
|
## Usage
|
|
12
14
|
|
|
13
|
-
Provide a url and handle the request.
|
|
14
|
-
|
|
15
15
|
```javascript
|
|
16
|
-
import React from "react";
|
|
17
16
|
import useAsyncFetch from "async-fetch";
|
|
18
17
|
|
|
19
18
|
function App() {
|
|
@@ -23,14 +22,10 @@ function App() {
|
|
|
23
22
|
|
|
24
23
|
return (
|
|
25
24
|
<React.Fragment>
|
|
26
|
-
<button onClick={sendRequest}>Send request
|
|
27
|
-
<br />
|
|
28
|
-
<br />
|
|
25
|
+
<button onClick={sendRequest}>Send request</button>
|
|
29
26
|
<button onClick={cancelRequest} disabled={!pending}>
|
|
30
|
-
Cancel request
|
|
27
|
+
Cancel request
|
|
31
28
|
</button>
|
|
32
|
-
<br />
|
|
33
|
-
<br />
|
|
34
29
|
{pending
|
|
35
30
|
? "Loading..."
|
|
36
31
|
: data
|
|
@@ -41,39 +36,82 @@ function App() {
|
|
|
41
36
|
</React.Fragment>
|
|
42
37
|
);
|
|
43
38
|
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Auto-fetch on mount
|
|
42
|
+
|
|
43
|
+
By default the hook fires on mount. Set `auto: false` to disable:
|
|
44
|
+
|
|
45
|
+
```javascript
|
|
46
|
+
const { pending, data, sendRequest } = useAsyncFetch(url, { auto: false });
|
|
47
|
+
```
|
|
44
48
|
|
|
45
|
-
|
|
49
|
+
### POST with JSON body
|
|
50
|
+
|
|
51
|
+
```javascript
|
|
52
|
+
const { pending, data, error } = useAsyncFetch("/api/submit", {
|
|
53
|
+
method: "POST",
|
|
54
|
+
data: { name: "foo" },
|
|
55
|
+
});
|
|
46
56
|
```
|
|
47
57
|
|
|
48
|
-
###
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
|
77
|
-
|
|
|
78
|
-
|
|
|
79
|
-
|
|
|
58
|
+
### Polling
|
|
59
|
+
|
|
60
|
+
```javascript
|
|
61
|
+
const { data } = useAsyncFetch("/api/status", { poll: 5000 });
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Callbacks
|
|
65
|
+
|
|
66
|
+
```javascript
|
|
67
|
+
useAsyncFetch("/api/user/1", {
|
|
68
|
+
onStart: () => console.log("started"),
|
|
69
|
+
onSuccess: (data) => console.log(data),
|
|
70
|
+
onFail: (error) => console.error(error),
|
|
71
|
+
onFinish: () => console.log("finished"),
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Cancel on demand
|
|
76
|
+
|
|
77
|
+
```javascript
|
|
78
|
+
const { cancelRequest } = useAsyncFetch("/api/user/1");
|
|
79
|
+
<button onClick={cancelRequest}>Cancel</button>;
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Request options
|
|
83
|
+
|
|
84
|
+
The minimum requirement is a URL string as the first argument. The second argument accepts the following options — anything else is passed directly to the underlying `fetch` call.
|
|
85
|
+
|
|
86
|
+
| Option | Type | Default | Description |
|
|
87
|
+
| ---------------- | ----------------------------------------------------------- | -------- | ---------------------------------------------------- |
|
|
88
|
+
| `initialPending` | `boolean` | `false` | Initial state for `pending` |
|
|
89
|
+
| `initialError` | `E` | | Initial state for `error` |
|
|
90
|
+
| `initialData` | `T` | | Initial state for `data` |
|
|
91
|
+
| `auto` | `boolean` | `true` | Whether to auto-send the request on mount |
|
|
92
|
+
| `poll` | `number` | | Milliseconds between polling requests |
|
|
93
|
+
| `timeout` | `number` | `30000` | Milliseconds before the request is cancelled |
|
|
94
|
+
| `ignoreRequest` | `boolean` | | Skips sending the request when `true` |
|
|
95
|
+
| `ignoreCleanup` | `boolean` | | Skips cancelling the request on unmount when `true` |
|
|
96
|
+
| `query` | `object` | | Key-value pairs appended to the URL as search params |
|
|
97
|
+
| `params` | `object` | | Key-value pairs appended to the URL as search params |
|
|
98
|
+
| `data` | `unknown` | | Value to send as the request body |
|
|
99
|
+
| `parser` | `"json" \| "text" \| "blob" \| "formData" \| "arrayBuffer"` | `"json"` | Method used to parse the response |
|
|
100
|
+
| `onStart` | `() => void` | | Called when the request starts |
|
|
101
|
+
| `onSuccess` | `(data: T) => void` | | Called with the response data on success |
|
|
102
|
+
| `onFail` | `(error: E) => void` | | Called with the error on failure |
|
|
103
|
+
| `onFinish` | `() => void` | | Called when the request finishes |
|
|
104
|
+
|
|
105
|
+
## Response
|
|
106
|
+
|
|
107
|
+
| Property | Type | Description |
|
|
108
|
+
| --------------- | --------------------- | ----------------------------- |
|
|
109
|
+
| `pending` | `boolean` | Whether the request is active |
|
|
110
|
+
| `error` | `E` | The response error |
|
|
111
|
+
| `data` | `T` | The response data |
|
|
112
|
+
| `sendRequest` | `() => Promise<void>` | Sends the request manually |
|
|
113
|
+
| `cancelRequest` | `() => void` | Cancels the active request |
|
|
114
|
+
|
|
115
|
+
## License
|
|
116
|
+
|
|
117
|
+
MIT
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const react_1 = require("react");
|
|
7
|
+
const useInterval_js_1 = __importDefault(require("./useInterval.js"));
|
|
8
|
+
const normalizeError_js_1 = __importDefault(require("./normalizeError.js"));
|
|
9
|
+
/**
|
|
10
|
+
* A React hook for managing async fetch requests with built-in state, cancellation, and polling.
|
|
11
|
+
* @example
|
|
12
|
+
* // Auto-fetch on mount:
|
|
13
|
+
* const { pending, error, data } = useAsyncFetch<User>("/api/user/1");
|
|
14
|
+
*
|
|
15
|
+
* // Manual trigger:
|
|
16
|
+
* const { pending, error, data, sendRequest } = useAsyncFetch<User>("/api/user/1", { auto: false });
|
|
17
|
+
* <button onClick={sendRequest}>Fetch</button>
|
|
18
|
+
*
|
|
19
|
+
* // With polling:
|
|
20
|
+
* const { data } = useAsyncFetch<Status>("/api/status", { poll: 5000 });
|
|
21
|
+
*
|
|
22
|
+
* // POST with JSON body:
|
|
23
|
+
* const { pending, error, data } = useAsyncFetch<Response>("/api/submit", {
|
|
24
|
+
* method: "POST",
|
|
25
|
+
* data: { name: "foo" },
|
|
26
|
+
* });
|
|
27
|
+
*
|
|
28
|
+
* // With callbacks:
|
|
29
|
+
* useAsyncFetch<User>("/api/user/1", {
|
|
30
|
+
* onStart: () => console.log("started"),
|
|
31
|
+
* onSuccess: (data) => console.log(data),
|
|
32
|
+
* onFail: (error) => console.error(error),
|
|
33
|
+
* onFinish: () => console.log("finished"),
|
|
34
|
+
* });
|
|
35
|
+
*
|
|
36
|
+
* // Cancel on demand:
|
|
37
|
+
* const { cancelRequest } = useAsyncFetch<User>("/api/user/1");
|
|
38
|
+
* <button onClick={cancelRequest}>Cancel</button>
|
|
39
|
+
*/
|
|
40
|
+
function useAsyncFetch(urlString, props = {}) {
|
|
41
|
+
const { initialPending = false, initialError, initialData, auto = true, poll, timeout = 30000, ignoreRequest, ignoreCleanup, query, params, data: body, parser = "json", onStart, onSuccess, onFail, onFinish, ...fetchOptions } = props;
|
|
42
|
+
const [pending, setPending] = (0, react_1.useState)(initialPending);
|
|
43
|
+
const [error, setError] = (0, react_1.useState)(initialError);
|
|
44
|
+
const [data, setData] = (0, react_1.useState)(initialData);
|
|
45
|
+
const fetchOptionsRef = (0, react_1.useRef)(fetchOptions);
|
|
46
|
+
const controllerRef = (0, react_1.useRef)(null);
|
|
47
|
+
const requestIdRef = (0, react_1.useRef)(0);
|
|
48
|
+
fetchOptionsRef.current = fetchOptions;
|
|
49
|
+
const cancelRequest = (0, react_1.useCallback)(() => {
|
|
50
|
+
controllerRef.current?.abort();
|
|
51
|
+
controllerRef.current = null;
|
|
52
|
+
}, []);
|
|
53
|
+
const sendRequest = (0, react_1.useCallback)(async () => {
|
|
54
|
+
if (ignoreRequest === true)
|
|
55
|
+
return;
|
|
56
|
+
cancelRequest();
|
|
57
|
+
setPending(true);
|
|
58
|
+
setError(undefined);
|
|
59
|
+
if (onStart)
|
|
60
|
+
onStart();
|
|
61
|
+
const controller = new AbortController();
|
|
62
|
+
controllerRef.current = controller;
|
|
63
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
64
|
+
const requestId = ++requestIdRef.current;
|
|
65
|
+
try {
|
|
66
|
+
const url = new URL(urlString, window.location.origin);
|
|
67
|
+
if (query ?? params) {
|
|
68
|
+
url.search = new URLSearchParams({ ...query, ...params }).toString();
|
|
69
|
+
}
|
|
70
|
+
const headers = new Headers(fetchOptionsRef.current.headers);
|
|
71
|
+
let resolvedBody;
|
|
72
|
+
if (headers.get("content-type") === "application/x-www-form-urlencoded") {
|
|
73
|
+
resolvedBody = new URLSearchParams(body ?? {});
|
|
74
|
+
}
|
|
75
|
+
else if (body instanceof FormData || body instanceof Blob) {
|
|
76
|
+
resolvedBody = body;
|
|
77
|
+
}
|
|
78
|
+
else if (body !== undefined) {
|
|
79
|
+
resolvedBody = JSON.stringify(body);
|
|
80
|
+
}
|
|
81
|
+
const response = await fetch(url, {
|
|
82
|
+
...fetchOptionsRef.current,
|
|
83
|
+
headers,
|
|
84
|
+
body: resolvedBody ?? fetchOptionsRef.current.body,
|
|
85
|
+
signal: controller.signal,
|
|
86
|
+
});
|
|
87
|
+
if (!response.ok) {
|
|
88
|
+
throw Object.assign(new Error(response.statusText), {
|
|
89
|
+
status: response.status,
|
|
90
|
+
statusText: response.statusText,
|
|
91
|
+
response: await response.text(),
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
const parsedResponse = (await response[parser]());
|
|
95
|
+
setData(parsedResponse);
|
|
96
|
+
if (onSuccess)
|
|
97
|
+
onSuccess(parsedResponse);
|
|
98
|
+
}
|
|
99
|
+
catch (err) {
|
|
100
|
+
if (err?.name !== "AbortError") {
|
|
101
|
+
const normalizedError = (0, normalizeError_js_1.default)(err);
|
|
102
|
+
setError(normalizedError);
|
|
103
|
+
if (onFail)
|
|
104
|
+
onFail(normalizedError);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
finally {
|
|
108
|
+
clearTimeout(timeoutId);
|
|
109
|
+
if (requestId === requestIdRef.current) {
|
|
110
|
+
setPending(false);
|
|
111
|
+
if (onFinish)
|
|
112
|
+
onFinish();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}, [
|
|
116
|
+
ignoreRequest,
|
|
117
|
+
cancelRequest,
|
|
118
|
+
timeout,
|
|
119
|
+
urlString,
|
|
120
|
+
query,
|
|
121
|
+
params,
|
|
122
|
+
body,
|
|
123
|
+
parser,
|
|
124
|
+
onStart,
|
|
125
|
+
onSuccess,
|
|
126
|
+
onFail,
|
|
127
|
+
onFinish,
|
|
128
|
+
]);
|
|
129
|
+
(0, react_1.useEffect)(() => {
|
|
130
|
+
if (auto === true)
|
|
131
|
+
void sendRequest();
|
|
132
|
+
}, [auto, sendRequest]);
|
|
133
|
+
(0, useInterval_js_1.default)(() => {
|
|
134
|
+
void sendRequest();
|
|
135
|
+
}, poll);
|
|
136
|
+
(0, react_1.useEffect)(() => {
|
|
137
|
+
return () => {
|
|
138
|
+
if (ignoreCleanup !== true)
|
|
139
|
+
cancelRequest();
|
|
140
|
+
};
|
|
141
|
+
}, [ignoreCleanup, cancelRequest]);
|
|
142
|
+
return { pending, error, data, cancelRequest, sendRequest };
|
|
143
|
+
}
|
|
144
|
+
exports.default = useAsyncFetch;
|
|
145
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../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AI;QACJ,MAAM;QACN,OAAO;QACP,SAAS;QACT,MAAM;QACN,QAAQ;KACT,CAAC,CAAC;IAEH,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,IAAI,IAAI,KAAK,IAAI;YAAE,KAAK,WAAW,EAAE,CAAC;IACxC,CAAC,EAAE,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC,CAAC;IAExB,IAAA,wBAAW,EAAC,GAAG,EAAE;QACf,KAAK,WAAW,EAAE,CAAC;IACrB,CAAC,EAAE,IAAI,CAAC,CAAC;IAET,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,OAAO,GAAG,EAAE;YACV,IAAI,aAAa,KAAK,IAAI;gBAAE,aAAa,EAAE,CAAC;QAC9C,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,aAAa,EAAE,aAAa,CAAC,CAAC,CAAC;IAEnC,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,WAAW,EAAE,CAAC;AAC9D,CAAC;AAED,kBAAe,aAAa,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interfaces.js","sourceRoot":"","sources":["../../src/interfaces.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* Normalizes an unknown thrown value into a `FetchError`-shaped object.
|
|
5
|
+
* Handles `Error` instances, error-like objects, and primitive values.
|
|
6
|
+
* @example
|
|
7
|
+
* normalizeError(new Error("oops")) // { status: 500, statusText: "oops", response: "<stack>" }
|
|
8
|
+
* normalizeError({ status: 404, statusText: "Not Found", response: "" }) // { status: 404, statusText: "Not Found", response: "" }
|
|
9
|
+
* normalizeError({ status: 403 }) // { status: 403, statusText: "", response: "" }
|
|
10
|
+
* normalizeError("something went wrong") // { status: 500, statusText: "something went wrong", response: "" }
|
|
11
|
+
* normalizeError(null) // { status: 500, statusText: "null", response: "" }
|
|
12
|
+
*/
|
|
13
|
+
function normalizeError(err) {
|
|
14
|
+
if (err instanceof Error) {
|
|
15
|
+
return {
|
|
16
|
+
status: 500,
|
|
17
|
+
statusText: err.message,
|
|
18
|
+
response: err.stack ?? "",
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
if (typeof err === "object" && err !== null) {
|
|
22
|
+
const e = err;
|
|
23
|
+
return {
|
|
24
|
+
status: e.status ?? 500,
|
|
25
|
+
statusText: e.statusText ?? "",
|
|
26
|
+
response: e.response ?? "",
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
status: 500,
|
|
31
|
+
statusText: String(err),
|
|
32
|
+
response: "",
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
exports.default = normalizeError;
|
|
36
|
+
//# sourceMappingURL=normalizeError.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normalizeError.js","sourceRoot":"","sources":["../../src/normalizeError.ts"],"names":[],"mappings":";;AAEA;;;;;;;;;GASG;AACH,SAAS,cAAc,CAAiB,GAAY;IAClD,IAAI,GAAG,YAAY,KAAK,EAAE,CAAC;QACzB,OAAO;YACL,MAAM,EAAE,GAAG;YACX,UAAU,EAAE,GAAG,CAAC,OAAO;YACvB,QAAQ,EAAE,GAAG,CAAC,KAAK,IAAI,EAAE;SACrB,CAAC;IACT,CAAC;IAED,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,KAAK,IAAI,EAAE,CAAC;QAC5C,MAAM,CAAC,GAAG,GAA0B,CAAC;QACrC,OAAO;YACL,MAAM,EAAE,CAAC,CAAC,MAAM,IAAI,GAAG;YACvB,UAAU,EAAE,CAAC,CAAC,UAAU,IAAI,EAAE;YAC9B,QAAQ,EAAE,CAAC,CAAC,QAAQ,IAAI,EAAE;SACtB,CAAC;IACT,CAAC;IAED,OAAO;QACL,MAAM,EAAE,GAAG;QACX,UAAU,EAAE,MAAM,CAAC,GAAG,CAAC;QACvB,QAAQ,EAAE,EAAE;KACR,CAAC;AACT,CAAC;AAED,kBAAe,cAAc,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type FetchError, type RequestProps, type ResponseProps } from "./interfaces.js";
|
|
2
|
+
/**
|
|
3
|
+
* A React hook for managing async fetch requests with built-in state, cancellation, and polling.
|
|
4
|
+
* @example
|
|
5
|
+
* // Auto-fetch on mount:
|
|
6
|
+
* const { pending, error, data } = useAsyncFetch<User>("/api/user/1");
|
|
7
|
+
*
|
|
8
|
+
* // Manual trigger:
|
|
9
|
+
* const { pending, error, data, sendRequest } = useAsyncFetch<User>("/api/user/1", { auto: false });
|
|
10
|
+
* <button onClick={sendRequest}>Fetch</button>
|
|
11
|
+
*
|
|
12
|
+
* // With polling:
|
|
13
|
+
* const { data } = useAsyncFetch<Status>("/api/status", { poll: 5000 });
|
|
14
|
+
*
|
|
15
|
+
* // POST with JSON body:
|
|
16
|
+
* const { pending, error, data } = useAsyncFetch<Response>("/api/submit", {
|
|
17
|
+
* method: "POST",
|
|
18
|
+
* data: { name: "foo" },
|
|
19
|
+
* });
|
|
20
|
+
*
|
|
21
|
+
* // With callbacks:
|
|
22
|
+
* useAsyncFetch<User>("/api/user/1", {
|
|
23
|
+
* onStart: () => console.log("started"),
|
|
24
|
+
* onSuccess: (data) => console.log(data),
|
|
25
|
+
* onFail: (error) => console.error(error),
|
|
26
|
+
* onFinish: () => console.log("finished"),
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* // Cancel on demand:
|
|
30
|
+
* const { cancelRequest } = useAsyncFetch<User>("/api/user/1");
|
|
31
|
+
* <button onClick={cancelRequest}>Cancel</button>
|
|
32
|
+
*/
|
|
33
|
+
declare function useAsyncFetch<T = unknown, E = FetchError>(urlString: string, props?: RequestProps<T, E>): ResponseProps<T, E>;
|
|
34
|
+
export default useAsyncFetch;
|
|
35
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,UAAU,EACf,KAAK,YAAY,EACjB,KAAK,aAAa,EACnB,MAAM,iBAAiB,CAAC;AAKzB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,iBAAS,aAAa,CAAC,CAAC,GAAG,OAAO,EAAE,CAAC,GAAG,UAAU,EAChD,SAAS,EAAE,MAAM,EACjB,KAAK,GAAE,YAAY,CAAC,CAAC,EAAE,CAAC,CAAM,GAC7B,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC,CA+IrB;AAED,eAAe,aAAa,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export interface FetchError {
|
|
2
|
+
status: number;
|
|
3
|
+
statusText: string;
|
|
4
|
+
response: string;
|
|
5
|
+
}
|
|
6
|
+
export interface RequestProps<T, E> extends RequestInit {
|
|
7
|
+
initialPending?: boolean;
|
|
8
|
+
initialError?: E;
|
|
9
|
+
initialData?: T;
|
|
10
|
+
auto?: boolean;
|
|
11
|
+
poll?: number;
|
|
12
|
+
timeout?: number;
|
|
13
|
+
ignoreRequest?: boolean;
|
|
14
|
+
ignoreCleanup?: boolean;
|
|
15
|
+
query?: Record<string, string>;
|
|
16
|
+
params?: Record<string, string>;
|
|
17
|
+
data?: unknown;
|
|
18
|
+
parser?: "json" | "text" | "blob" | "formData" | "arrayBuffer";
|
|
19
|
+
onStart?: () => void;
|
|
20
|
+
onSuccess?: (data: T) => void;
|
|
21
|
+
onFail?: (error: E) => void;
|
|
22
|
+
onFinish?: () => void;
|
|
23
|
+
}
|
|
24
|
+
export interface ResponseProps<T, E> {
|
|
25
|
+
pending: boolean;
|
|
26
|
+
error?: E;
|
|
27
|
+
data?: T;
|
|
28
|
+
sendRequest: () => Promise<void>;
|
|
29
|
+
cancelRequest: () => void;
|
|
30
|
+
}
|
|
31
|
+
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C;IACT,WAAW,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IACjC,aAAa,EAAE,MAAM,IAAI,CAAC;CAC3B"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type FetchError } from "./interfaces.js";
|
|
2
|
+
/**
|
|
3
|
+
* Normalizes an unknown thrown value into a `FetchError`-shaped object.
|
|
4
|
+
* Handles `Error` instances, error-like objects, and primitive values.
|
|
5
|
+
* @example
|
|
6
|
+
* normalizeError(new Error("oops")) // { status: 500, statusText: "oops", response: "<stack>" }
|
|
7
|
+
* normalizeError({ status: 404, statusText: "Not Found", response: "" }) // { status: 404, statusText: "Not Found", response: "" }
|
|
8
|
+
* normalizeError({ status: 403 }) // { status: 403, statusText: "", response: "" }
|
|
9
|
+
* normalizeError("something went wrong") // { status: 500, statusText: "something went wrong", response: "" }
|
|
10
|
+
* normalizeError(null) // { status: 500, statusText: "null", response: "" }
|
|
11
|
+
*/
|
|
12
|
+
declare function normalizeError<E = FetchError>(err: unknown): E;
|
|
13
|
+
export default normalizeError;
|
|
14
|
+
//# sourceMappingURL=normalizeError.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normalizeError.d.ts","sourceRoot":"","sources":["../../../src/normalizeError.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAElD;;;;;;;;;GASG;AACH,iBAAS,cAAc,CAAC,CAAC,GAAG,UAAU,EAAE,GAAG,EAAE,OAAO,GAAG,CAAC,CAuBvD;AAED,eAAe,cAAc,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sets up a polling interval that calls a callback on a given delay.
|
|
3
|
+
* The callback ref is kept up to date so stale closures are avoided.
|
|
4
|
+
* No interval is created if `poll` is not a valid integer.
|
|
5
|
+
* @see https://github.com/Hermanya/use-interval/blob/master/src/index.tsx
|
|
6
|
+
* @example
|
|
7
|
+
* useInterval(() => fetchData(), 5000) // calls fetchData every 5 seconds
|
|
8
|
+
* useInterval(() => fetchData()) // no interval created
|
|
9
|
+
*/
|
|
10
|
+
declare function useInterval(callback: () => void, poll?: number): void;
|
|
11
|
+
export default useInterval;
|
|
12
|
+
//# sourceMappingURL=useInterval.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useInterval.d.ts","sourceRoot":"","sources":["../../../src/useInterval.ts"],"names":[],"mappings":"AAEA;;;;;;;;GAQG;AACH,iBAAS,WAAW,CAAC,QAAQ,EAAE,MAAM,IAAI,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAc9D;AAED,eAAe,WAAW,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const react_1 = require("react");
|
|
4
|
+
/**
|
|
5
|
+
* Sets up a polling interval that calls a callback on a given delay.
|
|
6
|
+
* The callback ref is kept up to date so stale closures are avoided.
|
|
7
|
+
* No interval is created if `poll` is not a valid integer.
|
|
8
|
+
* @see https://github.com/Hermanya/use-interval/blob/master/src/index.tsx
|
|
9
|
+
* @example
|
|
10
|
+
* useInterval(() => fetchData(), 5000) // calls fetchData every 5 seconds
|
|
11
|
+
* useInterval(() => fetchData()) // no interval created
|
|
12
|
+
*/
|
|
13
|
+
function useInterval(callback, poll) {
|
|
14
|
+
const callbackRef = (0, react_1.useRef)(callback);
|
|
15
|
+
(0, react_1.useEffect)(() => {
|
|
16
|
+
callbackRef.current = callback;
|
|
17
|
+
}, [callback]);
|
|
18
|
+
(0, react_1.useEffect)(() => {
|
|
19
|
+
if (!Number.isInteger(poll))
|
|
20
|
+
return;
|
|
21
|
+
const id = setInterval(() => callbackRef.current(), poll);
|
|
22
|
+
return () => clearInterval(id);
|
|
23
|
+
}, [poll]);
|
|
24
|
+
}
|
|
25
|
+
exports.default = useInterval;
|
|
26
|
+
//# sourceMappingURL=useInterval.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useInterval.js","sourceRoot":"","sources":["../../src/useInterval.ts"],"names":[],"mappings":";;AAAA,iCAA0C;AAE1C;;;;;;;;GAQG;AACH,SAAS,WAAW,CAAC,QAAoB,EAAE,IAAa;IACtD,MAAM,WAAW,GAAG,IAAA,cAAM,EAAa,QAAQ,CAAC,CAAC;IAEjD,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,WAAW,CAAC,OAAO,GAAG,QAAQ,CAAC;IACjC,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC;IAEf,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC;YAAE,OAAO;QAEpC,MAAM,EAAE,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC,WAAW,CAAC,OAAO,EAAE,EAAE,IAAI,CAAC,CAAC;QAE1D,OAAO,GAAG,EAAE,CAAC,aAAa,CAAC,EAAE,CAAC,CAAC;IACjC,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC;AACb,CAAC;AAED,kBAAe,WAAW,CAAC"}
|