swr-resource 1.0.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 +224 -0
- package/dist/index.cjs +3002 -0
- package/dist/index.d.cts +21 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +2991 -0
- package/package.json +44 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Prabhat Kumar
|
|
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,224 @@
|
|
|
1
|
+
# ⚡ swr-resource
|
|
2
|
+
|
|
3
|
+
A zero-boilerplate, modern resource wrapper around **SWR** for lightning-fast React data fetching and CRUD operations.
|
|
4
|
+
|
|
5
|
+
Built for enterprise-scale applications, **swr-resource** eliminates repetitive API calls, loading state management, and manual cache updates. It keeps your UI synchronized automatically.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## ✨ Features
|
|
10
|
+
|
|
11
|
+
- 🚀 **Zero Boilerplate** – Perform complete CRUD operations with a single hook.
|
|
12
|
+
- 🔄 **Automatic Cache Sync** – UI updates automatically after `create`, `update`, and `remove` operations.
|
|
13
|
+
- 🔐 **Global Configuration** – Configure your API base URL and authentication token once.
|
|
14
|
+
- 📦 **Lightweight** – Built on top of the powerful `swr` library.
|
|
15
|
+
- 💙 **TypeScript Ready** – Fully typed for an excellent developer experience.
|
|
16
|
+
- ⚡ **Optimized Performance** – Leverages SWR's caching and revalidation features.
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
# 📦 Installation
|
|
21
|
+
|
|
22
|
+
Install **swr-resource** along with its peer dependency, **SWR**.
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install swr-resource swr
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
or
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
yarn add swr-resource swr
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
or
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
pnpm add swr-resource swr
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
# 🛠️ Setup
|
|
43
|
+
|
|
44
|
+
Wrap your application with the `ApiProvider`.
|
|
45
|
+
|
|
46
|
+
This allows you to configure a global API base URL and automatically inject authentication tokens into every request.
|
|
47
|
+
|
|
48
|
+
```tsx
|
|
49
|
+
// App.tsx
|
|
50
|
+
import React from "react";
|
|
51
|
+
import { ApiProvider } from "swr-resource";
|
|
52
|
+
import Dashboard from "./Dashboard";
|
|
53
|
+
|
|
54
|
+
export default function App() {
|
|
55
|
+
return (
|
|
56
|
+
<ApiProvider
|
|
57
|
+
baseURL="https://api.yourdomain.com/v1"
|
|
58
|
+
getAuthToken={() => localStorage.getItem("access_token")}
|
|
59
|
+
>
|
|
60
|
+
<Dashboard />
|
|
61
|
+
</ApiProvider>
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
# 💻 Usage
|
|
69
|
+
|
|
70
|
+
Once the provider is configured, use the `useResource` hook anywhere in your application.
|
|
71
|
+
|
|
72
|
+
```tsx
|
|
73
|
+
import React, { useState } from "react";
|
|
74
|
+
import { useResource } from "swr-resource";
|
|
75
|
+
|
|
76
|
+
interface User {
|
|
77
|
+
id: string;
|
|
78
|
+
name: string;
|
|
79
|
+
email: string;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export default function UserManagement() {
|
|
83
|
+
const {
|
|
84
|
+
data: users,
|
|
85
|
+
isLoading,
|
|
86
|
+
error,
|
|
87
|
+
create,
|
|
88
|
+
remove,
|
|
89
|
+
} = useResource<User[]>("/users");
|
|
90
|
+
|
|
91
|
+
const [newName, setNewName] = useState("");
|
|
92
|
+
|
|
93
|
+
if (isLoading) return <p>Loading users...</p>;
|
|
94
|
+
|
|
95
|
+
if (error) return <p>Failed to load users.</p>;
|
|
96
|
+
|
|
97
|
+
const handleAddUser = async () => {
|
|
98
|
+
await create({
|
|
99
|
+
name: newName,
|
|
100
|
+
email: "new@example.com",
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
setNewName("");
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
return (
|
|
107
|
+
<div>
|
|
108
|
+
<h2>User Management</h2>
|
|
109
|
+
|
|
110
|
+
<div>
|
|
111
|
+
<input
|
|
112
|
+
value={newName}
|
|
113
|
+
onChange={(e) => setNewName(e.target.value)}
|
|
114
|
+
placeholder="Enter user name"
|
|
115
|
+
/>
|
|
116
|
+
|
|
117
|
+
<button onClick={handleAddUser}>
|
|
118
|
+
Add User
|
|
119
|
+
</button>
|
|
120
|
+
</div>
|
|
121
|
+
|
|
122
|
+
<ul>
|
|
123
|
+
{users?.map((user) => (
|
|
124
|
+
<li key={user.id}>
|
|
125
|
+
{user.name} ({user.email})
|
|
126
|
+
|
|
127
|
+
<button
|
|
128
|
+
onClick={() => remove(user.id)}
|
|
129
|
+
>
|
|
130
|
+
Delete
|
|
131
|
+
</button>
|
|
132
|
+
</li>
|
|
133
|
+
))}
|
|
134
|
+
</ul>
|
|
135
|
+
</div>
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
---
|
|
141
|
+
|
|
142
|
+
# 📚 API Reference
|
|
143
|
+
|
|
144
|
+
## `<ApiProvider />`
|
|
145
|
+
|
|
146
|
+
### Props
|
|
147
|
+
|
|
148
|
+
| Prop | Type | Description |
|
|
149
|
+
|------|------|-------------|
|
|
150
|
+
| `baseURL` | `string` | Base URL of your API (e.g. `https://api.example.com`) |
|
|
151
|
+
| `getAuthToken` | `() => string \| null` | Function that returns the current Bearer token. It is evaluated before every request. |
|
|
152
|
+
|
|
153
|
+
---
|
|
154
|
+
|
|
155
|
+
## `useResource(endpoint)`
|
|
156
|
+
|
|
157
|
+
Returns the following object:
|
|
158
|
+
|
|
159
|
+
| Property | Type | Description |
|
|
160
|
+
|----------|------|-------------|
|
|
161
|
+
| `data` | `T` | Fetched resource data |
|
|
162
|
+
| `isLoading` | `boolean` | `true` while the initial request is loading |
|
|
163
|
+
| `isValidating` | `boolean` | `true` whenever SWR is revalidating |
|
|
164
|
+
| `error` | `Error` | Error object if the request fails |
|
|
165
|
+
| `create(payload)` | `Promise<any>` | Sends a `POST` request and automatically revalidates the cache |
|
|
166
|
+
| `update(id, payload)` | `Promise<any>` | Sends a `PUT` request to `endpoint/:id` and revalidates |
|
|
167
|
+
| `remove(id)` | `Promise<any>` | Sends a `DELETE` request to `endpoint/:id` and revalidates |
|
|
168
|
+
| `refresh()` | `() => void` | Manually re-fetches the current resource |
|
|
169
|
+
|
|
170
|
+
---
|
|
171
|
+
|
|
172
|
+
# Example
|
|
173
|
+
|
|
174
|
+
```tsx
|
|
175
|
+
const {
|
|
176
|
+
data,
|
|
177
|
+
create,
|
|
178
|
+
update,
|
|
179
|
+
remove,
|
|
180
|
+
refresh,
|
|
181
|
+
} = useResource("/products");
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
---
|
|
185
|
+
|
|
186
|
+
# Why swr-resource?
|
|
187
|
+
|
|
188
|
+
Instead of writing this:
|
|
189
|
+
|
|
190
|
+
```tsx
|
|
191
|
+
const { data, mutate } = useSWR("/users");
|
|
192
|
+
|
|
193
|
+
await axios.post("/users", payload);
|
|
194
|
+
|
|
195
|
+
mutate();
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
Simply write:
|
|
199
|
+
|
|
200
|
+
```tsx
|
|
201
|
+
const { data, create } = useResource("/users");
|
|
202
|
+
|
|
203
|
+
await create(payload);
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
No manual cache updates.
|
|
207
|
+
|
|
208
|
+
No repetitive Axios calls.
|
|
209
|
+
|
|
210
|
+
Just clean, declarative CRUD.
|
|
211
|
+
|
|
212
|
+
---
|
|
213
|
+
|
|
214
|
+
# Requirements
|
|
215
|
+
|
|
216
|
+
- React 18+
|
|
217
|
+
- SWR 2+
|
|
218
|
+
- TypeScript (optional but recommended)
|
|
219
|
+
|
|
220
|
+
---
|
|
221
|
+
|
|
222
|
+
# License
|
|
223
|
+
|
|
224
|
+
MIT
|