use-fetch-state 0.1.0
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 +188 -0
- package/dist/index.cjs.js +60 -0
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.esm.js +58 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/useFetchState.d.ts +16 -0
- package/package.json +61 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alican Kahramaner
|
|
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
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
# useFetchState
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
`useFetchState` is a lightweight and type-safe React hook for managing API requests and keeping response data in state.
|
|
8
|
+
|
|
9
|
+
It provides:
|
|
10
|
+
|
|
11
|
+
- 🚀 Loading state management
|
|
12
|
+
- ❌ Error handling
|
|
13
|
+
- 🔄 Manual request triggering
|
|
14
|
+
- 🔁 Optional auto re-fetch on request change
|
|
15
|
+
- 🧠 Fully generic & TypeScript friendly
|
|
16
|
+
- 📦 Zero dependencies (except React)
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install use-fetch-state
|
|
24
|
+
# or
|
|
25
|
+
yarn add use-fetch-state
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
# Quick Start
|
|
31
|
+
|
|
32
|
+
```tsx
|
|
33
|
+
import { useEffect } from "react";
|
|
34
|
+
import { useFetchState } from "use-fetch-state";
|
|
35
|
+
|
|
36
|
+
function MyComponent() {
|
|
37
|
+
const { loading, response, error, fetchData } = useFetchState({
|
|
38
|
+
fetcher: async (request) => {
|
|
39
|
+
const res = await fetch("/api/data", {
|
|
40
|
+
method: "POST",
|
|
41
|
+
body: JSON.stringify(request),
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
if (!res.ok) return false;
|
|
45
|
+
|
|
46
|
+
return await res.json();
|
|
47
|
+
},
|
|
48
|
+
initialRequest: { id: 1 },
|
|
49
|
+
initialResponse: null,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
fetchData();
|
|
54
|
+
}, []);
|
|
55
|
+
|
|
56
|
+
if (loading) return <p>Loading...</p>;
|
|
57
|
+
if (error) return <p>Error: {error.message}</p>;
|
|
58
|
+
if (response) return <pre>{JSON.stringify(response, null, 2)}</pre>;
|
|
59
|
+
|
|
60
|
+
return <p>No data.</p>;
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
# Dynamic Request Example
|
|
67
|
+
|
|
68
|
+
```tsx
|
|
69
|
+
import { useEffect, useState } from "react";
|
|
70
|
+
import { useFetchState } from "use-fetch-state";
|
|
71
|
+
|
|
72
|
+
function MyComponent() {
|
|
73
|
+
const [id, setId] = useState(1);
|
|
74
|
+
|
|
75
|
+
const {
|
|
76
|
+
loading,
|
|
77
|
+
response,
|
|
78
|
+
error,
|
|
79
|
+
fetchData,
|
|
80
|
+
setRequest,
|
|
81
|
+
} = useFetchState({
|
|
82
|
+
fetcher: async (request) => {
|
|
83
|
+
const res = await fetch(`/api/data?id=${request.id}`);
|
|
84
|
+
if (!res.ok) return false;
|
|
85
|
+
return await res.json();
|
|
86
|
+
},
|
|
87
|
+
initialRequest: { id: 1 },
|
|
88
|
+
initialResponse: null,
|
|
89
|
+
reFetchChangeRequest: true,
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
useEffect(() => {
|
|
93
|
+
fetchData();
|
|
94
|
+
}, []);
|
|
95
|
+
|
|
96
|
+
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
97
|
+
const newId = parseInt(e.target.value);
|
|
98
|
+
setId(newId);
|
|
99
|
+
setRequest({ id: newId });
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
return (
|
|
103
|
+
<div>
|
|
104
|
+
<input type="number" value={id} onChange={handleChange} />
|
|
105
|
+
|
|
106
|
+
{loading && <p>Loading...</p>}
|
|
107
|
+
{error && <p>Error: {error.message}</p>}
|
|
108
|
+
{response && <pre>{JSON.stringify(response, null, 2)}</pre>}
|
|
109
|
+
</div>
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
---
|
|
115
|
+
|
|
116
|
+
# API Reference
|
|
117
|
+
|
|
118
|
+
## Returned Values
|
|
119
|
+
|
|
120
|
+
| Name | Type | Description |
|
|
121
|
+
|------|------|-------------|
|
|
122
|
+
| `loading` | `boolean` | Indicates whether the request is in progress |
|
|
123
|
+
| `response` | `T \| null` | The successful API response |
|
|
124
|
+
| `error` | `Error \| null` | Contains error if request fails |
|
|
125
|
+
| `request` | `Req` | Current request parameters |
|
|
126
|
+
| `fetchData()` | `() => Promise<void>` | Manually triggers the request |
|
|
127
|
+
| `setRequest(newRequest)` | `(Req) => void` | Updates request parameters |
|
|
128
|
+
|
|
129
|
+
---
|
|
130
|
+
|
|
131
|
+
# Options
|
|
132
|
+
|
|
133
|
+
| Option | Type | Default | Description |
|
|
134
|
+
|--------|------|----------|-------------|
|
|
135
|
+
| `fetcher` | `(request: Req) => Promise<T \| false>` | required | Function responsible for API request |
|
|
136
|
+
| `initialRequest` | `Req` | required | Initial request parameters |
|
|
137
|
+
| `initialResponse` | `T \| null` | required | Initial response state |
|
|
138
|
+
| `waitCallback` | `boolean` | `false` | If true, skips initial execution |
|
|
139
|
+
| `reFetchChangeRequest` | `boolean` | `false` | Automatically re-fetch when request changes |
|
|
140
|
+
|
|
141
|
+
---
|
|
142
|
+
|
|
143
|
+
# TypeScript Example
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
interface User {
|
|
147
|
+
id: number;
|
|
148
|
+
name: string;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const { response } = useFetchState<{ id: number }, User>({
|
|
152
|
+
fetcher: async (req) => {
|
|
153
|
+
const res = await fetch(`/api/user/${req.id}`);
|
|
154
|
+
return await res.json();
|
|
155
|
+
},
|
|
156
|
+
initialRequest: { id: 1 },
|
|
157
|
+
initialResponse: null,
|
|
158
|
+
});
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
---
|
|
162
|
+
|
|
163
|
+
# Error Handling
|
|
164
|
+
|
|
165
|
+
If `fetcher` returns `false`, the hook will treat it as a failed request and update the `error` state.
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
if (!res.ok) {
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
---
|
|
174
|
+
|
|
175
|
+
# When to Use
|
|
176
|
+
|
|
177
|
+
`useFetchState` is ideal when:
|
|
178
|
+
|
|
179
|
+
- You need lightweight request management
|
|
180
|
+
- You don't need full caching solutions like React Query
|
|
181
|
+
- You want a minimal abstraction over fetch logic
|
|
182
|
+
- You prefer full manual control
|
|
183
|
+
|
|
184
|
+
---
|
|
185
|
+
|
|
186
|
+
# License
|
|
187
|
+
|
|
188
|
+
MIT
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var react = require('react');
|
|
4
|
+
|
|
5
|
+
const useFetchState = function (options) {
|
|
6
|
+
const [request, setRequest] = react.useState(options.initialRequest);
|
|
7
|
+
const [response, setResponse] = react.useState(options.initialResponse);
|
|
8
|
+
const [loading, setLoading] = react.useState(true);
|
|
9
|
+
const [error, setError] = react.useState(null);
|
|
10
|
+
const fetchData = react.useCallback(async () => {
|
|
11
|
+
setError(null);
|
|
12
|
+
setLoading(true);
|
|
13
|
+
try {
|
|
14
|
+
const response = await options.fetcher(request);
|
|
15
|
+
if (response === false) {
|
|
16
|
+
setError(new Error("Fetch failed"));
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
setResponse(response);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
catch (err) {
|
|
23
|
+
setError(err);
|
|
24
|
+
}
|
|
25
|
+
finally {
|
|
26
|
+
setLoading(false);
|
|
27
|
+
}
|
|
28
|
+
}, [setLoading, setError, setResponse, options, request]);
|
|
29
|
+
react.useEffect(() => {
|
|
30
|
+
if (!options.reFetchChangeRequest)
|
|
31
|
+
return;
|
|
32
|
+
fetchData();
|
|
33
|
+
}, [request, fetchData]);
|
|
34
|
+
const clear = react.useCallback(() => {
|
|
35
|
+
setError(null);
|
|
36
|
+
setLoading(false);
|
|
37
|
+
setRequest(options.initialRequest);
|
|
38
|
+
setResponse(options.initialResponse);
|
|
39
|
+
}, [setError, setLoading, setRequest, setResponse, options]);
|
|
40
|
+
react.useEffect(() => {
|
|
41
|
+
if (!options.waitCallback) {
|
|
42
|
+
fetchData();
|
|
43
|
+
}
|
|
44
|
+
return () => {
|
|
45
|
+
clear();
|
|
46
|
+
};
|
|
47
|
+
}, []);
|
|
48
|
+
return {
|
|
49
|
+
setRequest,
|
|
50
|
+
loading,
|
|
51
|
+
response,
|
|
52
|
+
error,
|
|
53
|
+
request,
|
|
54
|
+
fetchData,
|
|
55
|
+
clear
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
exports.useFetchState = useFetchState;
|
|
60
|
+
//# sourceMappingURL=index.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs.js","sources":["../src/useFetchState.ts"],"sourcesContent":["import { useCallback, useEffect, useState } from \"react\";\n\nexport type UseFetchStateProps<Req, Res> = {\n fetcher: (request: Req) => Promise<Res | false>,\n initialResponse: Res;\n initialRequest: Req;\n waitCallback?: boolean;\n reFetchChangeRequest?: boolean;\n}\n\nexport const useFetchState = function <Request, Response>(options: UseFetchStateProps<Request, Response>) {\n\n const [request, setRequest] = useState(options.initialRequest);\n const [response, setResponse] = useState(options.initialResponse);\n const [loading, setLoading] = useState(true);\n const [error, setError] = useState<Error | null>(null);\n\n const fetchData = useCallback(async () => {\n setError(null);\n setLoading(true)\n try {\n const response = await options.fetcher(request)\n\n if (response === false) {\n setError(new Error(\"Fetch failed\"));\n } else {\n setResponse(response);\n }\n\n } catch (err) {\n setError(err as Error);\n } finally {\n setLoading(false);\n }\n }, [setLoading, setError, setResponse, options, request]);\n\n useEffect(() => {\n if (!options.reFetchChangeRequest) return;\n fetchData();\n }, [request, fetchData]);\n\n const clear = useCallback(() => {\n setError(null);\n setLoading(false);\n setRequest(options.initialRequest);\n setResponse(options.initialResponse);\n }, [setError, setLoading, setRequest, setResponse, options]);\n\n useEffect(() => {\n if (!options.waitCallback) {\n fetchData();\n }\n return () => {\n clear();\n };\n }, []);\n\n return {\n setRequest,\n loading,\n response,\n error,\n request,\n fetchData,\n clear\n };\n}"],"names":["useState","useCallback","useEffect"],"mappings":";;;;AAUO,MAAM,aAAa,GAAG,UAA6B,OAA8C,EAAA;AAEpG,IAAA,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAGA,cAAQ,CAAC,OAAO,CAAC,cAAc,CAAC;AAC9D,IAAA,MAAM,CAAC,QAAQ,EAAE,WAAW,CAAC,GAAGA,cAAQ,CAAC,OAAO,CAAC,eAAe,CAAC;IACjE,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAGA,cAAQ,CAAC,IAAI,CAAC;IAC5C,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAGA,cAAQ,CAAe,IAAI,CAAC;AAEtD,IAAA,MAAM,SAAS,GAAGC,iBAAW,CAAC,YAAW;QACrC,QAAQ,CAAC,IAAI,CAAC;QACd,UAAU,CAAC,IAAI,CAAC;AAChB,QAAA,IAAI;YACA,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC;AAE/C,YAAA,IAAI,QAAQ,KAAK,KAAK,EAAE;AACpB,gBAAA,QAAQ,CAAC,IAAI,KAAK,CAAC,cAAc,CAAC,CAAC;YACvC;iBAAO;gBACH,WAAW,CAAC,QAAQ,CAAC;YACzB;QAEJ;QAAE,OAAO,GAAG,EAAE;YACV,QAAQ,CAAC,GAAY,CAAC;QAC1B;gBAAU;YACN,UAAU,CAAC,KAAK,CAAC;QACrB;AACJ,IAAA,CAAC,EAAE,CAAC,UAAU,EAAE,QAAQ,EAAE,WAAW,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;IAEzDC,eAAS,CAAC,MAAK;QACX,IAAI,CAAC,OAAO,CAAC,oBAAoB;YAAE;AACnC,QAAA,SAAS,EAAE;AACf,IAAA,CAAC,EAAE,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;AAExB,IAAA,MAAM,KAAK,GAAGD,iBAAW,CAAC,MAAK;QAC3B,QAAQ,CAAC,IAAI,CAAC;QACd,UAAU,CAAC,KAAK,CAAC;AACjB,QAAA,UAAU,CAAC,OAAO,CAAC,cAAc,CAAC;AAClC,QAAA,WAAW,CAAC,OAAO,CAAC,eAAe,CAAC;AACxC,IAAA,CAAC,EAAE,CAAC,QAAQ,EAAE,UAAU,EAAE,UAAU,EAAE,WAAW,EAAE,OAAO,CAAC,CAAC;IAE5DC,eAAS,CAAC,MAAK;AACX,QAAA,IAAI,CAAC,OAAO,CAAC,YAAY,EAAE;AACvB,YAAA,SAAS,EAAE;QACf;AACA,QAAA,OAAO,MAAK;AACR,YAAA,KAAK,EAAE;AACX,QAAA,CAAC;IACL,CAAC,EAAE,EAAE,CAAC;IAEN,OAAO;QACH,UAAU;QACV,OAAO;QACP,QAAQ;QACR,KAAK;QACL,OAAO;QACP,SAAS;QACT;KACH;AACL;;;;"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
|
|
3
|
+
type UseFetchStateProps<Req, Res> = {
|
|
4
|
+
fetcher: (request: Req) => Promise<Res | false>;
|
|
5
|
+
initialResponse: Res;
|
|
6
|
+
initialRequest: Req;
|
|
7
|
+
waitCallback?: boolean;
|
|
8
|
+
reFetchChangeRequest?: boolean;
|
|
9
|
+
};
|
|
10
|
+
declare const useFetchState: <Request, Response>(options: UseFetchStateProps<Request, Response>) => {
|
|
11
|
+
setRequest: react.Dispatch<react.SetStateAction<Request>>;
|
|
12
|
+
loading: boolean;
|
|
13
|
+
response: Response;
|
|
14
|
+
error: Error | null;
|
|
15
|
+
request: Request;
|
|
16
|
+
fetchData: () => Promise<void>;
|
|
17
|
+
clear: () => void;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export { useFetchState };
|
|
21
|
+
export type { UseFetchStateProps };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { useState, useCallback, useEffect } from 'react';
|
|
2
|
+
|
|
3
|
+
const useFetchState = function (options) {
|
|
4
|
+
const [request, setRequest] = useState(options.initialRequest);
|
|
5
|
+
const [response, setResponse] = useState(options.initialResponse);
|
|
6
|
+
const [loading, setLoading] = useState(true);
|
|
7
|
+
const [error, setError] = useState(null);
|
|
8
|
+
const fetchData = useCallback(async () => {
|
|
9
|
+
setError(null);
|
|
10
|
+
setLoading(true);
|
|
11
|
+
try {
|
|
12
|
+
const response = await options.fetcher(request);
|
|
13
|
+
if (response === false) {
|
|
14
|
+
setError(new Error("Fetch failed"));
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
setResponse(response);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
catch (err) {
|
|
21
|
+
setError(err);
|
|
22
|
+
}
|
|
23
|
+
finally {
|
|
24
|
+
setLoading(false);
|
|
25
|
+
}
|
|
26
|
+
}, [setLoading, setError, setResponse, options, request]);
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
if (!options.reFetchChangeRequest)
|
|
29
|
+
return;
|
|
30
|
+
fetchData();
|
|
31
|
+
}, [request, fetchData]);
|
|
32
|
+
const clear = useCallback(() => {
|
|
33
|
+
setError(null);
|
|
34
|
+
setLoading(false);
|
|
35
|
+
setRequest(options.initialRequest);
|
|
36
|
+
setResponse(options.initialResponse);
|
|
37
|
+
}, [setError, setLoading, setRequest, setResponse, options]);
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
if (!options.waitCallback) {
|
|
40
|
+
fetchData();
|
|
41
|
+
}
|
|
42
|
+
return () => {
|
|
43
|
+
clear();
|
|
44
|
+
};
|
|
45
|
+
}, []);
|
|
46
|
+
return {
|
|
47
|
+
setRequest,
|
|
48
|
+
loading,
|
|
49
|
+
response,
|
|
50
|
+
error,
|
|
51
|
+
request,
|
|
52
|
+
fetchData,
|
|
53
|
+
clear
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export { useFetchState };
|
|
58
|
+
//# sourceMappingURL=index.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.esm.js","sources":["../src/useFetchState.ts"],"sourcesContent":["import { useCallback, useEffect, useState } from \"react\";\n\nexport type UseFetchStateProps<Req, Res> = {\n fetcher: (request: Req) => Promise<Res | false>,\n initialResponse: Res;\n initialRequest: Req;\n waitCallback?: boolean;\n reFetchChangeRequest?: boolean;\n}\n\nexport const useFetchState = function <Request, Response>(options: UseFetchStateProps<Request, Response>) {\n\n const [request, setRequest] = useState(options.initialRequest);\n const [response, setResponse] = useState(options.initialResponse);\n const [loading, setLoading] = useState(true);\n const [error, setError] = useState<Error | null>(null);\n\n const fetchData = useCallback(async () => {\n setError(null);\n setLoading(true)\n try {\n const response = await options.fetcher(request)\n\n if (response === false) {\n setError(new Error(\"Fetch failed\"));\n } else {\n setResponse(response);\n }\n\n } catch (err) {\n setError(err as Error);\n } finally {\n setLoading(false);\n }\n }, [setLoading, setError, setResponse, options, request]);\n\n useEffect(() => {\n if (!options.reFetchChangeRequest) return;\n fetchData();\n }, [request, fetchData]);\n\n const clear = useCallback(() => {\n setError(null);\n setLoading(false);\n setRequest(options.initialRequest);\n setResponse(options.initialResponse);\n }, [setError, setLoading, setRequest, setResponse, options]);\n\n useEffect(() => {\n if (!options.waitCallback) {\n fetchData();\n }\n return () => {\n clear();\n };\n }, []);\n\n return {\n setRequest,\n loading,\n response,\n error,\n request,\n fetchData,\n clear\n };\n}"],"names":[],"mappings":";;AAUO,MAAM,aAAa,GAAG,UAA6B,OAA8C,EAAA;AAEpG,IAAA,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,QAAQ,CAAC,OAAO,CAAC,cAAc,CAAC;AAC9D,IAAA,MAAM,CAAC,QAAQ,EAAE,WAAW,CAAC,GAAG,QAAQ,CAAC,OAAO,CAAC,eAAe,CAAC;IACjE,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC;IAC5C,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAe,IAAI,CAAC;AAEtD,IAAA,MAAM,SAAS,GAAG,WAAW,CAAC,YAAW;QACrC,QAAQ,CAAC,IAAI,CAAC;QACd,UAAU,CAAC,IAAI,CAAC;AAChB,QAAA,IAAI;YACA,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC;AAE/C,YAAA,IAAI,QAAQ,KAAK,KAAK,EAAE;AACpB,gBAAA,QAAQ,CAAC,IAAI,KAAK,CAAC,cAAc,CAAC,CAAC;YACvC;iBAAO;gBACH,WAAW,CAAC,QAAQ,CAAC;YACzB;QAEJ;QAAE,OAAO,GAAG,EAAE;YACV,QAAQ,CAAC,GAAY,CAAC;QAC1B;gBAAU;YACN,UAAU,CAAC,KAAK,CAAC;QACrB;AACJ,IAAA,CAAC,EAAE,CAAC,UAAU,EAAE,QAAQ,EAAE,WAAW,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;IAEzD,SAAS,CAAC,MAAK;QACX,IAAI,CAAC,OAAO,CAAC,oBAAoB;YAAE;AACnC,QAAA,SAAS,EAAE;AACf,IAAA,CAAC,EAAE,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;AAExB,IAAA,MAAM,KAAK,GAAG,WAAW,CAAC,MAAK;QAC3B,QAAQ,CAAC,IAAI,CAAC;QACd,UAAU,CAAC,KAAK,CAAC;AACjB,QAAA,UAAU,CAAC,OAAO,CAAC,cAAc,CAAC;AAClC,QAAA,WAAW,CAAC,OAAO,CAAC,eAAe,CAAC;AACxC,IAAA,CAAC,EAAE,CAAC,QAAQ,EAAE,UAAU,EAAE,UAAU,EAAE,WAAW,EAAE,OAAO,CAAC,CAAC;IAE5D,SAAS,CAAC,MAAK;AACX,QAAA,IAAI,CAAC,OAAO,CAAC,YAAY,EAAE;AACvB,YAAA,SAAS,EAAE;QACf;AACA,QAAA,OAAO,MAAK;AACR,YAAA,KAAK,EAAE;AACX,QAAA,CAAC;IACL,CAAC,EAAE,EAAE,CAAC;IAEN,OAAO;QACH,UAAU;QACV,OAAO;QACP,QAAQ;QACR,KAAK;QACL,OAAO;QACP,SAAS;QACT;KACH;AACL;;;;"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type UseFetchStateProps<Req, Res> = {
|
|
2
|
+
fetcher: (request: Req) => Promise<Res | false>;
|
|
3
|
+
initialResponse: Res;
|
|
4
|
+
initialRequest: Req;
|
|
5
|
+
waitCallback?: boolean;
|
|
6
|
+
reFetchChangeRequest?: boolean;
|
|
7
|
+
};
|
|
8
|
+
export declare const useFetchState: <Request, Response>(options: UseFetchStateProps<Request, Response>) => {
|
|
9
|
+
setRequest: import("react").Dispatch<import("react").SetStateAction<Request>>;
|
|
10
|
+
loading: boolean;
|
|
11
|
+
response: Response;
|
|
12
|
+
error: Error | null;
|
|
13
|
+
request: Request;
|
|
14
|
+
fetchData: () => Promise<void>;
|
|
15
|
+
clear: () => void;
|
|
16
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "use-fetch-state",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A lightweight React hook for managing async fetch state (data, loading, error) with automatic refetching.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"react",
|
|
7
|
+
"hook",
|
|
8
|
+
"fetch",
|
|
9
|
+
"async",
|
|
10
|
+
"state",
|
|
11
|
+
"loading",
|
|
12
|
+
"error",
|
|
13
|
+
"typescript"
|
|
14
|
+
],
|
|
15
|
+
"author": {
|
|
16
|
+
"name": "Alican Kahramaner",
|
|
17
|
+
"email": "alicankahramaner@gmail.com",
|
|
18
|
+
"url": "https://alicankahramaner.github.io/"
|
|
19
|
+
},
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/alicankahramaner/use-fetch-state.git"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/alicankahramaner/use-fetch-state#readme",
|
|
26
|
+
"bugs": {
|
|
27
|
+
"url": "https://github.com/alicankahramaner/use-fetch-state/issues"
|
|
28
|
+
},
|
|
29
|
+
"main": "dist/index.cjs.js",
|
|
30
|
+
"module": "dist/index.esm.js",
|
|
31
|
+
"types": "dist/index.d.ts",
|
|
32
|
+
"exports": {
|
|
33
|
+
".": {
|
|
34
|
+
"types": "./dist/index.d.ts",
|
|
35
|
+
"import": "./dist/index.esm.js",
|
|
36
|
+
"require": "./dist/index.cjs.js"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"dist"
|
|
41
|
+
],
|
|
42
|
+
"sideEffects": false,
|
|
43
|
+
"engines": {
|
|
44
|
+
"node": ">=16"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "rollup -c",
|
|
48
|
+
"prepublishOnly": "npm run build"
|
|
49
|
+
},
|
|
50
|
+
"peerDependencies": {
|
|
51
|
+
"react": "^17.0.0 || ^18.0.0 || ^19.0.0"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@rollup/plugin-typescript": "^11.1.6",
|
|
55
|
+
"@types/react": "^18.3.1",
|
|
56
|
+
"rollup": "^4.18.0",
|
|
57
|
+
"rollup-plugin-dts": "^6.1.1",
|
|
58
|
+
"tslib": "^2.6.3",
|
|
59
|
+
"typescript": "^5.5.3"
|
|
60
|
+
}
|
|
61
|
+
}
|