react-api-kit 1.0.5 → 1.0.7
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/README.md +18 -4
- package/dist/react-api-kit.mjs +2 -1
- package/dist/react-api-kit.umd.js +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@ A lightweight, production-ready API client built on **Axios** + **TanStack Query
|
|
|
9
9
|
✔ Custom static headers per project
|
|
10
10
|
✔ Global error handling with toast support
|
|
11
11
|
✔ Auto logout on 401
|
|
12
|
-
✔ React Query–ready hooks (GET, POST, PATCH, DELETE)
|
|
12
|
+
✔ React Query–ready hooks (GET, POST, PUT, PATCH, DELETE)
|
|
13
13
|
✔ No UI dependencies — works with any toast library
|
|
14
14
|
|
|
15
15
|
---
|
|
@@ -208,7 +208,7 @@ function UserList() {
|
|
|
208
208
|
```ts
|
|
209
209
|
const { mutate, mutateAsync, isPending, isError } = useApiMutation(
|
|
210
210
|
mutationKey, // unique key array
|
|
211
|
-
method, // "POST" | "PATCH" | "DELETE" | "GET"
|
|
211
|
+
method, // "POST" | "PUT" | "PATCH" | "DELETE" | "GET"
|
|
212
212
|
invalidateKey? // query keys to refetch after success (optional)
|
|
213
213
|
);
|
|
214
214
|
```
|
|
@@ -238,7 +238,18 @@ const handleCreate = () => {
|
|
|
238
238
|
};
|
|
239
239
|
```
|
|
240
240
|
|
|
241
|
-
####
|
|
241
|
+
#### PUT — Full Replace
|
|
242
|
+
|
|
243
|
+
```tsx
|
|
244
|
+
const { mutate } = useApiMutation(["replaceUser"], "PUT", ["users"]);
|
|
245
|
+
|
|
246
|
+
mutate({
|
|
247
|
+
endpoint: `/users/${id}`,
|
|
248
|
+
data: { name: "John", email: "john@example.com", role: "admin" },
|
|
249
|
+
});
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
#### PATCH — Partial Update
|
|
242
253
|
|
|
243
254
|
```tsx
|
|
244
255
|
const { mutate } = useApiMutation(["updateUser"], "PATCH", ["users"]);
|
|
@@ -304,6 +315,9 @@ const res = await api.get("/orders", { params: { status: "pending" } });
|
|
|
304
315
|
// POST
|
|
305
316
|
const res = await api.post("/users", { name: "John" });
|
|
306
317
|
|
|
318
|
+
// PUT
|
|
319
|
+
const res = await api.put(`/users/${id}`, { name: "John", email: "john@example.com" });
|
|
320
|
+
|
|
307
321
|
// PATCH
|
|
308
322
|
const res = await api.patch(`/users/${id}`, { name: "Updated" });
|
|
309
323
|
|
|
@@ -356,7 +370,7 @@ src/
|
|
|
356
370
|
├── api/
|
|
357
371
|
│ ├── axios.ts → Axios instance + request interceptor (auth + headers)
|
|
358
372
|
│ ├── interceptors.ts → Response interceptor (error handling + auto logout)
|
|
359
|
-
│ └── globalApi.ts → GET / POST / PATCH / DELETE wrappers
|
|
373
|
+
│ └── globalApi.ts → GET / POST / PUT / PATCH / DELETE wrappers
|
|
360
374
|
└── hooks/
|
|
361
375
|
├── useApiQuery.ts → useQuery wrapper
|
|
362
376
|
└── useApiMutation.ts → useMutation wrapper
|
package/dist/react-api-kit.mjs
CHANGED
|
@@ -27,12 +27,13 @@ a.interceptors.response.use(
|
|
|
27
27
|
422: "Validation error. Please check the form.",
|
|
28
28
|
500: "Server error. Please try again later."
|
|
29
29
|
}, o = e?.response?.data?.message || t && r?.errorMessages?.[t] || t && s[t] || e?.message || "Network Error";
|
|
30
|
-
return r?.
|
|
30
|
+
return r?.onError?.(e, o), t === 401 && r?.onLogout?.(), Promise.reject(e);
|
|
31
31
|
}
|
|
32
32
|
);
|
|
33
33
|
const u = {
|
|
34
34
|
GET: (e, t) => a.get(e, { params: t }).then((s) => s.data),
|
|
35
35
|
POST: (e, t) => a.post(e, t).then((s) => s.data),
|
|
36
|
+
PUT: (e, t) => a.put(e, t).then((s) => s.data),
|
|
36
37
|
PATCH: (e, t) => a.patch(e, t).then((s) => s.data),
|
|
37
38
|
DELETE: (e) => a.delete(e).then((t) => t.data)
|
|
38
39
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(n,i){typeof exports=="object"&&typeof module<"u"?i(exports,require("axios"),require("@tanstack/react-query")):typeof define=="function"&&define.amd?define(["exports","axios","@tanstack/react-query"],i):(n=typeof globalThis<"u"?globalThis:n||self,i(n.ReactApiKit={},n.axios,n.reactQuery))})(this,(function(n,i,u){"use strict";let a;const p=e=>{a=e},o=i.create();o.interceptors.request.use(e=>{a?.baseURL&&(e.baseURL=a.baseURL),a?.headers&&Object.entries(a.headers).forEach(([s,r])=>{e.headers[s]=r});const t=a?.getToken?.();if(t){const s=a?.tokenType??"Bearer";e.headers.Authorization=`${s} ${t}`}return e}),o.interceptors.response.use(e=>(a?.onSuccess?.(e.data),e),e=>{const t=e?.response?.status,s={400:"Bad request. Please check your input.",401:"Session expired. Please login again.",403:"You do not have permission to perform this action.",404:"Requested resource not found.",409:"Conflict occurred. Please try again.",422:"Validation error. Please check the form.",500:"Server error. Please try again later."},r=e?.response?.data?.message||t&&a?.errorMessages?.[t]||t&&s[t]||e?.message||"Network Error";return a?.onError?.(e,r),t===401&&a?.onLogout?.(),Promise.reject(e)});const d={GET:(e,t)=>o.get(e,{params:t}).then(s=>s.data),POST:(e,t)=>o.post(e,t).then(s=>s.data),PUT:(e,t)=>o.put(e,t).then(s=>s.data),PATCH:(e,t)=>o.patch(e,t).then(s=>s.data),DELETE:e=>o.delete(e).then(t=>t.data)};function f(e,t,s,r,c){return u.useQuery({queryKey:e,queryFn:()=>d[t](s,r),...c})}function h(e,t,s){const r=u.useQueryClient();return u.useMutation({mutationKey:e,mutationFn:({endpoint:c,data:l})=>d[t](c,l),onSuccess:()=>{s&&r.invalidateQueries({queryKey:s})}})}n.api=o,n.setupApi=p,n.useApiMutation=h,n.useApiQuery=f,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-api-kit",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
4
4
|
"description": "Reusable Axios + TanStack Query API hooks for React",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"author": "Ajay kammar",
|
|
@@ -28,4 +28,4 @@
|
|
|
28
28
|
"@vitejs/plugin-react": "^5.1.2",
|
|
29
29
|
"vite": "^7.3.0"
|
|
30
30
|
}
|
|
31
|
-
}
|
|
31
|
+
}
|