@benco112/use-req 1.0.0 → 1.0.1
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 +91 -0
- package/package.json +1 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 benco112
|
|
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,91 @@
|
|
|
1
|
+
# use-req
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@benco112/use-req)
|
|
4
|
+
[](https://react.dev/)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
|
|
7
|
+
Simple and lightweight React hook for making API requests with built-in `loading`, `error`, and `data` state.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- Lightweight and dependency-free
|
|
12
|
+
- Built for React Hooks (`>=16.8`)
|
|
13
|
+
- Simple request interface
|
|
14
|
+
- Supports custom methods, headers, and body
|
|
15
|
+
- Exposes request state in a clean API
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @benco112/use-req
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Quick Start
|
|
24
|
+
|
|
25
|
+
```jsx
|
|
26
|
+
import useReq from "@benco112/use-req";
|
|
27
|
+
|
|
28
|
+
function UsersList() {
|
|
29
|
+
const { data, loading, error, request } = useReq("https://api.example.com");
|
|
30
|
+
|
|
31
|
+
const loadUsers = async () => {
|
|
32
|
+
await request("/users");
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
if (loading) return <p>Loading...</p>;
|
|
36
|
+
if (error) return <p>Error: {error}</p>;
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
<div>
|
|
40
|
+
<button onClick={loadUsers}>Load users</button>
|
|
41
|
+
<pre>{JSON.stringify(data, null, 2)}</pre>
|
|
42
|
+
</div>
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## API
|
|
48
|
+
|
|
49
|
+
### `useReq(baseUrl)`
|
|
50
|
+
|
|
51
|
+
Creates a request client scoped to a base URL.
|
|
52
|
+
|
|
53
|
+
#### Parameters
|
|
54
|
+
|
|
55
|
+
- `baseUrl: string` — Base URL for all requests.
|
|
56
|
+
|
|
57
|
+
#### Returns
|
|
58
|
+
|
|
59
|
+
- `data: any` — Last successful response data.
|
|
60
|
+
- `loading: boolean` — Request state.
|
|
61
|
+
- `error: string | null` — Error message from the last failed request.
|
|
62
|
+
- `request(endpoint, options?)` — Function to execute a request.
|
|
63
|
+
|
|
64
|
+
### `request(endpoint, options?)`
|
|
65
|
+
|
|
66
|
+
- `endpoint: string` — Path appended to `baseUrl`.
|
|
67
|
+
- `options.method?: string` — HTTP method (default: `GET`).
|
|
68
|
+
- `options.headers?: Record<string, string>` — Additional request headers.
|
|
69
|
+
- `options.body?: unknown` — Request body. It is JSON-stringified when provided.
|
|
70
|
+
|
|
71
|
+
## Example: POST Request
|
|
72
|
+
|
|
73
|
+
```jsx
|
|
74
|
+
await request("/users", {
|
|
75
|
+
method: "POST",
|
|
76
|
+
body: { name: "Ben" },
|
|
77
|
+
headers: {
|
|
78
|
+
Authorization: "Bearer <token>",
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Build
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
npm run build
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## License
|
|
90
|
+
|
|
91
|
+
MIT
|