@alistt69/create-api 0.2.2 → 0.2.3
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 +62 -17
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -32,6 +32,7 @@ It provides query, lazy query and mutation hooks, cache helpers and stale data s
|
|
|
32
32
|
| Query hooks | Fetch data with generated hooks |
|
|
33
33
|
| Lazy query hooks | Trigger queries manually when needed |
|
|
34
34
|
| Mutation hooks | Handle writes with generated hooks |
|
|
35
|
+
| fetchBaseQuery | Ready-to-use HTTP baseQuery built on fetch |
|
|
35
36
|
| Cache utils | Read and patch cached data manually |
|
|
36
37
|
| Stale time support | Reuse cached data before refetching |
|
|
37
38
|
| Keep unused data for | Control cache lifetime after unmount |
|
|
@@ -47,6 +48,7 @@ Do whatever you want and however you want with:
|
|
|
47
48
|
* predictable cache behavior
|
|
48
49
|
* simple manual cache updates
|
|
49
50
|
* a focused API without a larger state layer
|
|
51
|
+
* a built-in `fetchBaseQuery` for managing your HTTP requests
|
|
50
52
|
|
|
51
53
|
## 📦 Requirements
|
|
52
54
|
* Node.js 18 or higher
|
|
@@ -59,32 +61,23 @@ npm i @alistt69/create-api
|
|
|
59
61
|
|
|
60
62
|
## ⚡ Quick example
|
|
61
63
|
```typescript jsx
|
|
62
|
-
import { createApi } from '@alistt69/create-api';
|
|
64
|
+
import { createApi, fetchBaseQuery } from '@alistt69/create-api';
|
|
63
65
|
|
|
64
66
|
const api = createApi({
|
|
65
|
-
baseQuery:
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
signal,
|
|
69
|
-
headers: { 'Content-Type': 'application/json' },
|
|
70
|
-
body: body ? JSON.stringify(body) : undefined,
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
if (!res.ok) {
|
|
74
|
-
throw await res.json();
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return res.json();
|
|
78
|
-
},
|
|
67
|
+
baseQuery: fetchBaseQuery({
|
|
68
|
+
baseUrl: 'https://example.com/api',
|
|
69
|
+
}),
|
|
79
70
|
|
|
80
71
|
endpoints: (builder) => ({
|
|
81
72
|
getPost: builder.query({
|
|
82
|
-
query: (id) => ({
|
|
73
|
+
query: (id) => ({
|
|
74
|
+
url: `/posts/${id}`,
|
|
75
|
+
}),
|
|
83
76
|
}),
|
|
84
77
|
|
|
85
78
|
updatePost: builder.mutation({
|
|
86
79
|
query: ({ id, title }) => ({
|
|
87
|
-
url: `/
|
|
80
|
+
url: `/posts/${id}`,
|
|
88
81
|
method: 'PATCH',
|
|
89
82
|
body: { title },
|
|
90
83
|
}),
|
|
@@ -109,10 +102,62 @@ function Post() {
|
|
|
109
102
|
}
|
|
110
103
|
```
|
|
111
104
|
|
|
105
|
+
## 🌐 fetchBaseQuery
|
|
106
|
+
|
|
107
|
+
`fetchBaseQuery` is a small ready-to-use `baseQuery` built on top of the native `fetch` API.
|
|
108
|
+
|
|
109
|
+
It supports:
|
|
110
|
+
- `baseUrl`
|
|
111
|
+
- `params`
|
|
112
|
+
- `headers`
|
|
113
|
+
- `prepareHeaders`
|
|
114
|
+
- `timeout`
|
|
115
|
+
- `responseHandler`
|
|
116
|
+
- `validateStatus`
|
|
117
|
+
- `fetchFn`
|
|
118
|
+
|
|
119
|
+
### Example
|
|
120
|
+
|
|
121
|
+
```tsx
|
|
122
|
+
import { createApi, fetchBaseQuery } from '@alistt69/create-api';
|
|
123
|
+
|
|
124
|
+
const api = createApi({
|
|
125
|
+
baseQuery: fetchBaseQuery({
|
|
126
|
+
baseUrl: 'https://example.com/api',
|
|
127
|
+
prepareHeaders: (headers) => {
|
|
128
|
+
headers.set('authorization', 'Bearer token');
|
|
129
|
+
return headers;
|
|
130
|
+
},
|
|
131
|
+
}),
|
|
132
|
+
endpoints: (builder) => ({
|
|
133
|
+
getTickets: builder.query({
|
|
134
|
+
query: ({ page }) => ({
|
|
135
|
+
url: '/tickets',
|
|
136
|
+
params: { page },
|
|
137
|
+
}),
|
|
138
|
+
}),
|
|
139
|
+
}),
|
|
140
|
+
});
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Blob / custom response handling
|
|
144
|
+
```javascript
|
|
145
|
+
downloadFile: builder.query({
|
|
146
|
+
query: () => ({
|
|
147
|
+
url: '/report',
|
|
148
|
+
responseHandler: (response) => response.blob(),
|
|
149
|
+
}),
|
|
150
|
+
}),
|
|
151
|
+
```
|
|
152
|
+
|
|
112
153
|
## ⚙️ Cache utils
|
|
113
154
|
``` typescript
|
|
114
155
|
api.util.getQueryData('getPost', '1');
|
|
115
156
|
api.util.setQueryData('getPost', '1', { id: '1', title: 'Local title' });
|
|
157
|
+
api.util.updateQueryData('getPost', '1', (prev) => ({
|
|
158
|
+
...prev,
|
|
159
|
+
title: 'Patched title',
|
|
160
|
+
}));
|
|
116
161
|
```
|
|
117
162
|
|
|
118
163
|
## 📄 License
|