@ahsan-iftikhar-114/api-toolkit-react-query 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.md +21 -0
- package/README.md +1017 -0
- package/dist/config/config.d.ts +12 -0
- package/dist/config/config.js +29 -0
- package/dist/core/api.d.ts +26 -0
- package/dist/core/api.js +88 -0
- package/dist/core/axios.d.ts +10 -0
- package/dist/core/axios.js +38 -0
- package/dist/core/cache.d.ts +20 -0
- package/dist/core/cache.js +39 -0
- package/dist/core/client.d.ts +8 -0
- package/dist/core/client.js +10 -0
- package/dist/core/error.d.ts +6 -0
- package/dist/core/error.js +36 -0
- package/dist/core/mutation.d.ts +13 -0
- package/dist/core/mutation.js +27 -0
- package/dist/core/query.d.ts +10 -0
- package/dist/core/query.js +28 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +41 -0
- package/dist/types/index.d.ts +97 -0
- package/dist/types/index.js +1 -0
- package/package.json +59 -0
package/README.md
ADDED
|
@@ -0,0 +1,1017 @@
|
|
|
1
|
+
# @ahsan-iftikhar-114/api-toolkit-react-query
|
|
2
|
+
|
|
3
|
+
**A simple and reusable API toolkit for React applications using Axios and TanStack Query.**
|
|
4
|
+
|
|
5
|
+
This package makes it easier to connect your React application with your backend API.
|
|
6
|
+
|
|
7
|
+
You do not need to build API handling, loading states, error handling, authentication, caching, and request management from scratch.
|
|
8
|
+
|
|
9
|
+
The package provides these features in one reusable solution.
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## What is this package?
|
|
14
|
+
|
|
15
|
+
When a React application needs information from a backend server, it has to send an **API request**.
|
|
16
|
+
|
|
17
|
+
For example:
|
|
18
|
+
|
|
19
|
+
* Get users
|
|
20
|
+
* Create a user
|
|
21
|
+
* Update a user
|
|
22
|
+
* Delete a user
|
|
23
|
+
* Get products
|
|
24
|
+
* Submit a form
|
|
25
|
+
* Login a user
|
|
26
|
+
* Upload or send data
|
|
27
|
+
* Refresh information from the server
|
|
28
|
+
|
|
29
|
+
Normally, developers have to write a lot of code to manage these requests.
|
|
30
|
+
|
|
31
|
+
This package provides a simpler and more consistent way to manage them.
|
|
32
|
+
|
|
33
|
+
### In simple words
|
|
34
|
+
|
|
35
|
+
You tell the package:
|
|
36
|
+
|
|
37
|
+
**What is this API?**
|
|
38
|
+
|
|
39
|
+
**Which HTTP method should it use?**
|
|
40
|
+
|
|
41
|
+
**Which API URL should it call?**
|
|
42
|
+
|
|
43
|
+
The package handles the rest.
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
# Who is this package for?
|
|
48
|
+
|
|
49
|
+
This package is useful for:
|
|
50
|
+
|
|
51
|
+
* React developers
|
|
52
|
+
* Frontend developers
|
|
53
|
+
* Backend developers working with React
|
|
54
|
+
* Beginners learning API integration
|
|
55
|
+
* Developers building dashboards
|
|
56
|
+
* Developers building admin panels
|
|
57
|
+
* Developers building SaaS applications
|
|
58
|
+
* Developers building business applications
|
|
59
|
+
* Teams that want a consistent API structure
|
|
60
|
+
* Projects using Laravel, Node.js, PHP, .NET, Python or another backend
|
|
61
|
+
|
|
62
|
+
You do not need to understand every internal part of Axios or TanStack Query to use the basic functionality.
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
# What problem does it solve?
|
|
67
|
+
|
|
68
|
+
Without an API toolkit, developers often need to manage several things separately:
|
|
69
|
+
|
|
70
|
+
* API requests
|
|
71
|
+
* Axios configuration
|
|
72
|
+
* Authentication tokens
|
|
73
|
+
* Loading states
|
|
74
|
+
* Error states
|
|
75
|
+
* Request cancellation
|
|
76
|
+
* API caching
|
|
77
|
+
* Data refreshing
|
|
78
|
+
* Cache invalidation
|
|
79
|
+
* Query management
|
|
80
|
+
* Mutation management
|
|
81
|
+
|
|
82
|
+
This package brings these responsibilities together.
|
|
83
|
+
|
|
84
|
+
### Instead of building everything separately
|
|
85
|
+
|
|
86
|
+
You can use one consistent API system throughout your React application.
|
|
87
|
+
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
# How does it work?
|
|
91
|
+
|
|
92
|
+
The basic idea is very simple.
|
|
93
|
+
|
|
94
|
+
You define an API using three things:
|
|
95
|
+
|
|
96
|
+
**1. Name**
|
|
97
|
+
|
|
98
|
+
A name that identifies the API.
|
|
99
|
+
|
|
100
|
+
**2. Method**
|
|
101
|
+
|
|
102
|
+
The type of request:
|
|
103
|
+
|
|
104
|
+
* GET
|
|
105
|
+
* POST
|
|
106
|
+
* PUT
|
|
107
|
+
* PATCH
|
|
108
|
+
* DELETE
|
|
109
|
+
|
|
110
|
+
**3. URL**
|
|
111
|
+
|
|
112
|
+
The backend API address.
|
|
113
|
+
|
|
114
|
+
That's it.
|
|
115
|
+
|
|
116
|
+
The package then automatically decides how that API should work inside React.
|
|
117
|
+
|
|
118
|
+
---
|
|
119
|
+
|
|
120
|
+
# What are GET, POST, PUT, PATCH and DELETE?
|
|
121
|
+
|
|
122
|
+
You do not need to be an API expert to understand these.
|
|
123
|
+
|
|
124
|
+
Think about a simple employee management system.
|
|
125
|
+
|
|
126
|
+
### GET
|
|
127
|
+
|
|
128
|
+
Used when you want to **get information**.
|
|
129
|
+
|
|
130
|
+
Examples:
|
|
131
|
+
|
|
132
|
+
* Get employees
|
|
133
|
+
* Get users
|
|
134
|
+
* Get products
|
|
135
|
+
* Get invoices
|
|
136
|
+
|
|
137
|
+
---
|
|
138
|
+
|
|
139
|
+
### POST
|
|
140
|
+
|
|
141
|
+
Used when you want to **create something new**.
|
|
142
|
+
|
|
143
|
+
Examples:
|
|
144
|
+
|
|
145
|
+
* Create employee
|
|
146
|
+
* Create user
|
|
147
|
+
* Create invoice
|
|
148
|
+
* Submit form
|
|
149
|
+
|
|
150
|
+
---
|
|
151
|
+
|
|
152
|
+
### PUT
|
|
153
|
+
|
|
154
|
+
Used when you want to **update existing information**.
|
|
155
|
+
|
|
156
|
+
Examples:
|
|
157
|
+
|
|
158
|
+
* Update employee
|
|
159
|
+
* Update user
|
|
160
|
+
* Update profile
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
### PATCH
|
|
165
|
+
|
|
166
|
+
Also used to **update information**, usually when changing only part of it.
|
|
167
|
+
|
|
168
|
+
Examples:
|
|
169
|
+
|
|
170
|
+
* Change employee status
|
|
171
|
+
* Update user name
|
|
172
|
+
* Change invoice status
|
|
173
|
+
|
|
174
|
+
---
|
|
175
|
+
|
|
176
|
+
### DELETE
|
|
177
|
+
|
|
178
|
+
Used when you want to **remove something**.
|
|
179
|
+
|
|
180
|
+
Examples:
|
|
181
|
+
|
|
182
|
+
* Delete employee
|
|
183
|
+
* Delete user
|
|
184
|
+
* Delete product
|
|
185
|
+
|
|
186
|
+
---
|
|
187
|
+
|
|
188
|
+
# How do I install it?
|
|
189
|
+
|
|
190
|
+
Open your React project's terminal and install the package:
|
|
191
|
+
|
|
192
|
+
```text
|
|
193
|
+
npm install @ahsan-iftikhar-114/api-toolkit-react-query
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
The package works with:
|
|
197
|
+
|
|
198
|
+
* React
|
|
199
|
+
* Axios
|
|
200
|
+
* TanStack Query
|
|
201
|
+
|
|
202
|
+
If your project does not already have the required React Query package, install it as well.
|
|
203
|
+
|
|
204
|
+
---
|
|
205
|
+
|
|
206
|
+
# What do I need before using it?
|
|
207
|
+
|
|
208
|
+
You need a React application.
|
|
209
|
+
|
|
210
|
+
You also need:
|
|
211
|
+
|
|
212
|
+
* React 18 or newer
|
|
213
|
+
* TanStack Query 5 or newer
|
|
214
|
+
* Axios 1 or newer
|
|
215
|
+
|
|
216
|
+
TypeScript is recommended, but the package can also be used in JavaScript projects.
|
|
217
|
+
|
|
218
|
+
---
|
|
219
|
+
|
|
220
|
+
# Basic setup
|
|
221
|
+
|
|
222
|
+
The setup can be understood in four simple steps.
|
|
223
|
+
|
|
224
|
+
## Step 1 — Install the package
|
|
225
|
+
|
|
226
|
+
Install the package in your React project.
|
|
227
|
+
|
|
228
|
+
---
|
|
229
|
+
|
|
230
|
+
## Step 2 — Tell the package your API address
|
|
231
|
+
|
|
232
|
+
Your backend normally has a common address.
|
|
233
|
+
|
|
234
|
+
For example:
|
|
235
|
+
|
|
236
|
+
```text
|
|
237
|
+
https://api.example.com
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
You configure this once.
|
|
241
|
+
|
|
242
|
+
After that, you can use shorter API paths throughout your application.
|
|
243
|
+
|
|
244
|
+
For example:
|
|
245
|
+
|
|
246
|
+
```text
|
|
247
|
+
/users
|
|
248
|
+
/products
|
|
249
|
+
/orders
|
|
250
|
+
/invoices
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
---
|
|
254
|
+
|
|
255
|
+
## Step 3 — Connect TanStack Query
|
|
256
|
+
|
|
257
|
+
The package uses TanStack Query to manage server data.
|
|
258
|
+
|
|
259
|
+
Your React application needs a Query Client.
|
|
260
|
+
|
|
261
|
+
You can use the Query Client provided by the package or create your own TanStack Query client.
|
|
262
|
+
|
|
263
|
+
This is normally configured once in your application.
|
|
264
|
+
|
|
265
|
+
---
|
|
266
|
+
|
|
267
|
+
## Step 4 — Create your APIs
|
|
268
|
+
|
|
269
|
+
For every API, provide:
|
|
270
|
+
|
|
271
|
+
* Name
|
|
272
|
+
* Method
|
|
273
|
+
* URL
|
|
274
|
+
|
|
275
|
+
For example, conceptually:
|
|
276
|
+
|
|
277
|
+
**Users**
|
|
278
|
+
|
|
279
|
+
**GET**
|
|
280
|
+
|
|
281
|
+
**/users**
|
|
282
|
+
|
|
283
|
+
The package will treat this as a data-fetching API.
|
|
284
|
+
|
|
285
|
+
For a create-user API:
|
|
286
|
+
|
|
287
|
+
**Create User**
|
|
288
|
+
|
|
289
|
+
**POST**
|
|
290
|
+
|
|
291
|
+
**/users**
|
|
292
|
+
|
|
293
|
+
The package will treat this as a data-changing API.
|
|
294
|
+
|
|
295
|
+
---
|
|
296
|
+
|
|
297
|
+
# GET APIs
|
|
298
|
+
|
|
299
|
+
GET APIs are used when your application needs to **read information from the server**.
|
|
300
|
+
|
|
301
|
+
For example:
|
|
302
|
+
|
|
303
|
+
* Employee list
|
|
304
|
+
* Customer list
|
|
305
|
+
* Product list
|
|
306
|
+
* Dashboard statistics
|
|
307
|
+
* Notifications
|
|
308
|
+
* User profile
|
|
309
|
+
|
|
310
|
+
When you use a GET API, the package provides the information needed to manage the request, including:
|
|
311
|
+
|
|
312
|
+
* Data
|
|
313
|
+
* Loading state
|
|
314
|
+
* Error state
|
|
315
|
+
* Fetching state
|
|
316
|
+
* Refetching
|
|
317
|
+
* Cached data
|
|
318
|
+
|
|
319
|
+
This means your application can easily know whether information is loading, successfully received, or failed.
|
|
320
|
+
|
|
321
|
+
---
|
|
322
|
+
|
|
323
|
+
# POST, PUT, PATCH and DELETE APIs
|
|
324
|
+
|
|
325
|
+
These APIs are used when your application needs to **change information on the server**.
|
|
326
|
+
|
|
327
|
+
For example:
|
|
328
|
+
|
|
329
|
+
* Create employee
|
|
330
|
+
* Update employee
|
|
331
|
+
* Delete employee
|
|
332
|
+
* Create invoice
|
|
333
|
+
* Update invoice
|
|
334
|
+
* Submit request
|
|
335
|
+
|
|
336
|
+
The package manages these operations as mutations.
|
|
337
|
+
|
|
338
|
+
You can easily know:
|
|
339
|
+
|
|
340
|
+
* Whether the operation is running
|
|
341
|
+
* Whether it succeeded
|
|
342
|
+
* Whether it failed
|
|
343
|
+
* What data was returned
|
|
344
|
+
* What error occurred
|
|
345
|
+
|
|
346
|
+
---
|
|
347
|
+
|
|
348
|
+
# Sending information to the server
|
|
349
|
+
|
|
350
|
+
When creating or updating something, your application usually needs to send information.
|
|
351
|
+
|
|
352
|
+
For example, when creating an employee:
|
|
353
|
+
|
|
354
|
+
* Name
|
|
355
|
+
* Email
|
|
356
|
+
* Department
|
|
357
|
+
* Position
|
|
358
|
+
|
|
359
|
+
The package allows you to send this information to your backend API.
|
|
360
|
+
|
|
361
|
+
You can also send:
|
|
362
|
+
|
|
363
|
+
* Query parameters
|
|
364
|
+
* Request headers
|
|
365
|
+
* Authentication information
|
|
366
|
+
* Additional Axios configuration
|
|
367
|
+
|
|
368
|
+
---
|
|
369
|
+
|
|
370
|
+
# Authentication
|
|
371
|
+
|
|
372
|
+
Many APIs require users to log in before they can access protected information.
|
|
373
|
+
|
|
374
|
+
This package supports **Bearer Token authentication**.
|
|
375
|
+
|
|
376
|
+
You can configure an authentication token globally.
|
|
377
|
+
|
|
378
|
+
The token can be:
|
|
379
|
+
|
|
380
|
+
* A fixed token
|
|
381
|
+
* A dynamically retrieved token
|
|
382
|
+
|
|
383
|
+
A dynamic token is useful when the token is stored in browser storage or another authentication system.
|
|
384
|
+
|
|
385
|
+
The package automatically adds the token to API requests.
|
|
386
|
+
|
|
387
|
+
This means you do not need to manually add the authentication header to every request.
|
|
388
|
+
|
|
389
|
+
---
|
|
390
|
+
|
|
391
|
+
# API Base URL
|
|
392
|
+
|
|
393
|
+
You can configure one common API address for your application.
|
|
394
|
+
|
|
395
|
+
For example:
|
|
396
|
+
|
|
397
|
+
```text
|
|
398
|
+
https://api.example.com
|
|
399
|
+
```
|
|
400
|
+
|
|
401
|
+
Then individual APIs only need their own path.
|
|
402
|
+
|
|
403
|
+
For example:
|
|
404
|
+
|
|
405
|
+
```text
|
|
406
|
+
/users
|
|
407
|
+
/employees
|
|
408
|
+
/products
|
|
409
|
+
/orders
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
This makes API configuration easier to maintain.
|
|
413
|
+
|
|
414
|
+
If your backend address changes, you can update the main configuration instead of changing every API.
|
|
415
|
+
|
|
416
|
+
---
|
|
417
|
+
|
|
418
|
+
# Loading states
|
|
419
|
+
|
|
420
|
+
When your application requests information from a server, the server may take some time to respond.
|
|
421
|
+
|
|
422
|
+
The package provides request status information so your application can show things such as:
|
|
423
|
+
|
|
424
|
+
* Loading...
|
|
425
|
+
* Please wait...
|
|
426
|
+
* Loading spinner
|
|
427
|
+
* Refreshing...
|
|
428
|
+
* Saving...
|
|
429
|
+
* Deleting...
|
|
430
|
+
|
|
431
|
+
This makes it easier to create a better user experience.
|
|
432
|
+
|
|
433
|
+
---
|
|
434
|
+
|
|
435
|
+
# Error handling
|
|
436
|
+
|
|
437
|
+
API requests can fail for many reasons.
|
|
438
|
+
|
|
439
|
+
For example:
|
|
440
|
+
|
|
441
|
+
* Internet connection problem
|
|
442
|
+
* Server error
|
|
443
|
+
* Unauthorized request
|
|
444
|
+
* Validation error
|
|
445
|
+
* Not found
|
|
446
|
+
* Request timeout
|
|
447
|
+
|
|
448
|
+
The package provides a consistent error format so your application can handle API errors more easily.
|
|
449
|
+
|
|
450
|
+
Instead of every API returning a completely different error structure to your application, the package normalizes common API errors.
|
|
451
|
+
|
|
452
|
+
---
|
|
453
|
+
|
|
454
|
+
# Query parameters
|
|
455
|
+
|
|
456
|
+
Sometimes an API needs additional information in the URL.
|
|
457
|
+
|
|
458
|
+
For example:
|
|
459
|
+
|
|
460
|
+
* Page number
|
|
461
|
+
* Number of records
|
|
462
|
+
* Search text
|
|
463
|
+
* Sorting
|
|
464
|
+
* Filtering
|
|
465
|
+
* Status
|
|
466
|
+
|
|
467
|
+
The package supports query parameters.
|
|
468
|
+
|
|
469
|
+
This makes it suitable for:
|
|
470
|
+
|
|
471
|
+
* Search pages
|
|
472
|
+
* Data tables
|
|
473
|
+
* Pagination
|
|
474
|
+
* Filters
|
|
475
|
+
* Reports
|
|
476
|
+
* Admin dashboards
|
|
477
|
+
|
|
478
|
+
---
|
|
479
|
+
|
|
480
|
+
# Caching
|
|
481
|
+
|
|
482
|
+
One of the major features of this package is API data caching through TanStack Query.
|
|
483
|
+
|
|
484
|
+
Caching means your application can temporarily remember information it has already received.
|
|
485
|
+
|
|
486
|
+
This can reduce unnecessary API requests.
|
|
487
|
+
|
|
488
|
+
For example:
|
|
489
|
+
|
|
490
|
+
A user opens the employee page.
|
|
491
|
+
|
|
492
|
+
The application gets the employee list from the server.
|
|
493
|
+
|
|
494
|
+
If the same information is needed again, TanStack Query can use its cached data according to your configured caching rules.
|
|
495
|
+
|
|
496
|
+
This can make applications feel faster and reduce unnecessary network requests.
|
|
497
|
+
|
|
498
|
+
---
|
|
499
|
+
|
|
500
|
+
# Refetching
|
|
501
|
+
|
|
502
|
+
Sometimes information needs to be updated from the server.
|
|
503
|
+
|
|
504
|
+
For example:
|
|
505
|
+
|
|
506
|
+
* User clicks Refresh
|
|
507
|
+
* User returns to a page
|
|
508
|
+
* Application reconnects to the internet
|
|
509
|
+
* Data becomes stale
|
|
510
|
+
|
|
511
|
+
The package works with TanStack Query's refetching functionality.
|
|
512
|
+
|
|
513
|
+
This allows your application to keep server data up to date.
|
|
514
|
+
|
|
515
|
+
---
|
|
516
|
+
|
|
517
|
+
# Cache invalidation
|
|
518
|
+
|
|
519
|
+
Suppose you have an employee list.
|
|
520
|
+
|
|
521
|
+
A user creates a new employee.
|
|
522
|
+
|
|
523
|
+
The old employee list may no longer be correct.
|
|
524
|
+
|
|
525
|
+
The package provides cache invalidation utilities that can tell TanStack Query:
|
|
526
|
+
|
|
527
|
+
**"This data may have changed. Get the latest information."**
|
|
528
|
+
|
|
529
|
+
This is especially useful after:
|
|
530
|
+
|
|
531
|
+
* Creating records
|
|
532
|
+
* Updating records
|
|
533
|
+
* Deleting records
|
|
534
|
+
* Changing statuses
|
|
535
|
+
* Performing other server-side operations
|
|
536
|
+
|
|
537
|
+
---
|
|
538
|
+
|
|
539
|
+
# Request cancellation
|
|
540
|
+
|
|
541
|
+
Sometimes a request is no longer needed.
|
|
542
|
+
|
|
543
|
+
For example:
|
|
544
|
+
|
|
545
|
+
A user starts searching for employees.
|
|
546
|
+
|
|
547
|
+
Then immediately changes the search.
|
|
548
|
+
|
|
549
|
+
The previous request may no longer be useful.
|
|
550
|
+
|
|
551
|
+
The package supports request cancellation through `AbortSignal`.
|
|
552
|
+
|
|
553
|
+
This can help prevent unnecessary work and improve request management.
|
|
554
|
+
|
|
555
|
+
---
|
|
556
|
+
|
|
557
|
+
# Advanced configuration
|
|
558
|
+
|
|
559
|
+
Although the basic API is simple, the package also supports advanced configuration.
|
|
560
|
+
|
|
561
|
+
Developers can use TanStack Query options such as:
|
|
562
|
+
|
|
563
|
+
* Cache duration
|
|
564
|
+
* Stale time
|
|
565
|
+
* Retry behavior
|
|
566
|
+
* Automatic refetching
|
|
567
|
+
* Conditional requests
|
|
568
|
+
* Placeholder data
|
|
569
|
+
* Initial data
|
|
570
|
+
* Data selection
|
|
571
|
+
* Other compatible TanStack Query options
|
|
572
|
+
|
|
573
|
+
This means beginners can start with the simple functionality while experienced developers can customize the behavior when needed.
|
|
574
|
+
|
|
575
|
+
---
|
|
576
|
+
|
|
577
|
+
# TypeScript support
|
|
578
|
+
|
|
579
|
+
The package is designed with TypeScript support in mind.
|
|
580
|
+
|
|
581
|
+
You can define the type of:
|
|
582
|
+
|
|
583
|
+
* API response
|
|
584
|
+
* Request data
|
|
585
|
+
* Mutation context
|
|
586
|
+
* Selected data
|
|
587
|
+
|
|
588
|
+
This helps developers get:
|
|
589
|
+
|
|
590
|
+
* Better autocomplete
|
|
591
|
+
* Better editor support
|
|
592
|
+
* Better type checking
|
|
593
|
+
* Fewer mistakes
|
|
594
|
+
* Easier maintenance
|
|
595
|
+
|
|
596
|
+
TypeScript is especially useful for large applications.
|
|
597
|
+
|
|
598
|
+
---
|
|
599
|
+
|
|
600
|
+
# Works with different backends
|
|
601
|
+
|
|
602
|
+
This package is designed for the frontend API layer.
|
|
603
|
+
|
|
604
|
+
Your backend can be built using many different technologies.
|
|
605
|
+
|
|
606
|
+
For example:
|
|
607
|
+
|
|
608
|
+
* Laravel
|
|
609
|
+
* PHP
|
|
610
|
+
* Node.js
|
|
611
|
+
* Express
|
|
612
|
+
* NestJS
|
|
613
|
+
* .NET
|
|
614
|
+
* ASP.NET
|
|
615
|
+
* Django
|
|
616
|
+
* FastAPI
|
|
617
|
+
* Spring Boot
|
|
618
|
+
* Other REST APIs
|
|
619
|
+
|
|
620
|
+
As long as your backend provides an API that your React application can communicate with, this package can be used as the API management layer.
|
|
621
|
+
|
|
622
|
+
---
|
|
623
|
+
|
|
624
|
+
# What happens behind the scenes?
|
|
625
|
+
|
|
626
|
+
You do not need to manage all of these individually.
|
|
627
|
+
|
|
628
|
+
The package combines:
|
|
629
|
+
|
|
630
|
+
**React**
|
|
631
|
+
|
|
632
|
+
↓
|
|
633
|
+
|
|
634
|
+
**API Toolkit**
|
|
635
|
+
|
|
636
|
+
↓
|
|
637
|
+
|
|
638
|
+
**Axios**
|
|
639
|
+
|
|
640
|
+
↓
|
|
641
|
+
|
|
642
|
+
**Backend API**
|
|
643
|
+
|
|
644
|
+
And for server data management:
|
|
645
|
+
|
|
646
|
+
**TanStack Query**
|
|
647
|
+
|
|
648
|
+
↓
|
|
649
|
+
|
|
650
|
+
**Caching**
|
|
651
|
+
|
|
652
|
+
↓
|
|
653
|
+
|
|
654
|
+
**Refetching**
|
|
655
|
+
|
|
656
|
+
↓
|
|
657
|
+
|
|
658
|
+
**Loading & Error States**
|
|
659
|
+
|
|
660
|
+
↓
|
|
661
|
+
|
|
662
|
+
**Cache Invalidation**
|
|
663
|
+
|
|
664
|
+
The goal is to give your application one consistent way to communicate with APIs.
|
|
665
|
+
|
|
666
|
+
---
|
|
667
|
+
|
|
668
|
+
# Why use this package?
|
|
669
|
+
|
|
670
|
+
### Simple API creation
|
|
671
|
+
|
|
672
|
+
Define an API using:
|
|
673
|
+
|
|
674
|
+
* Name
|
|
675
|
+
* Method
|
|
676
|
+
* URL
|
|
677
|
+
|
|
678
|
+
---
|
|
679
|
+
|
|
680
|
+
### Less repeated code
|
|
681
|
+
|
|
682
|
+
You do not have to repeatedly create the same API request structure.
|
|
683
|
+
|
|
684
|
+
---
|
|
685
|
+
|
|
686
|
+
### Centralized configuration
|
|
687
|
+
|
|
688
|
+
Configure:
|
|
689
|
+
|
|
690
|
+
* API URL
|
|
691
|
+
* Timeout
|
|
692
|
+
* Headers
|
|
693
|
+
* Authentication
|
|
694
|
+
|
|
695
|
+
in one place.
|
|
696
|
+
|
|
697
|
+
---
|
|
698
|
+
|
|
699
|
+
### Built-in React Query support
|
|
700
|
+
|
|
701
|
+
Use the powerful server-state management capabilities of TanStack Query.
|
|
702
|
+
|
|
703
|
+
---
|
|
704
|
+
|
|
705
|
+
### Authentication support
|
|
706
|
+
|
|
707
|
+
Bearer token authentication can be configured centrally.
|
|
708
|
+
|
|
709
|
+
---
|
|
710
|
+
|
|
711
|
+
### Consistent errors
|
|
712
|
+
|
|
713
|
+
API errors are normalized into a common structure.
|
|
714
|
+
|
|
715
|
+
---
|
|
716
|
+
|
|
717
|
+
### Caching support
|
|
718
|
+
|
|
719
|
+
Use TanStack Query caching to reduce unnecessary requests.
|
|
720
|
+
|
|
721
|
+
---
|
|
722
|
+
|
|
723
|
+
### Request cancellation
|
|
724
|
+
|
|
725
|
+
Cancel requests when they are no longer required.
|
|
726
|
+
|
|
727
|
+
---
|
|
728
|
+
|
|
729
|
+
### TypeScript friendly
|
|
730
|
+
|
|
731
|
+
Use types for safer development and better editor support.
|
|
732
|
+
|
|
733
|
+
---
|
|
734
|
+
|
|
735
|
+
### Flexible
|
|
736
|
+
|
|
737
|
+
Beginners can use the simple API creation system, while experienced developers can use advanced Axios and TanStack Query options.
|
|
738
|
+
|
|
739
|
+
---
|
|
740
|
+
|
|
741
|
+
# Recommended for
|
|
742
|
+
|
|
743
|
+
This package is particularly useful for applications such as:
|
|
744
|
+
|
|
745
|
+
* Admin panels
|
|
746
|
+
* HRMS systems
|
|
747
|
+
* CRM systems
|
|
748
|
+
* ERP systems
|
|
749
|
+
* SaaS applications
|
|
750
|
+
* E-commerce applications
|
|
751
|
+
* Project management applications
|
|
752
|
+
* Business management systems
|
|
753
|
+
* Dashboards
|
|
754
|
+
* Customer portals
|
|
755
|
+
* Employee portals
|
|
756
|
+
* Internal company applications
|
|
757
|
+
* REST API based React applications
|
|
758
|
+
|
|
759
|
+
---
|
|
760
|
+
|
|
761
|
+
# Package Architecture
|
|
762
|
+
|
|
763
|
+
The package internally separates different responsibilities.
|
|
764
|
+
|
|
765
|
+
```text
|
|
766
|
+
API Configuration
|
|
767
|
+
↓
|
|
768
|
+
Axios
|
|
769
|
+
↓
|
|
770
|
+
API Request Layer
|
|
771
|
+
↓
|
|
772
|
+
TanStack Query
|
|
773
|
+
↓
|
|
774
|
+
React Application
|
|
775
|
+
```
|
|
776
|
+
|
|
777
|
+
This separation keeps the package reusable and easier to maintain.
|
|
778
|
+
|
|
779
|
+
---
|
|
780
|
+
|
|
781
|
+
# Supported HTTP Methods
|
|
782
|
+
|
|
783
|
+
The package supports:
|
|
784
|
+
|
|
785
|
+
| Method | Purpose |
|
|
786
|
+
| ------ | ---------------------------- |
|
|
787
|
+
| GET | Get information |
|
|
788
|
+
| POST | Create information |
|
|
789
|
+
| PUT | Update information |
|
|
790
|
+
| PATCH | Partially update information |
|
|
791
|
+
| DELETE | Delete information |
|
|
792
|
+
|
|
793
|
+
---
|
|
794
|
+
|
|
795
|
+
# Important terms explained simply
|
|
796
|
+
|
|
797
|
+
### API
|
|
798
|
+
|
|
799
|
+
An API allows your application to communicate with a backend server.
|
|
800
|
+
|
|
801
|
+
### Axios
|
|
802
|
+
|
|
803
|
+
Axios is the technology used to send HTTP requests to the backend.
|
|
804
|
+
|
|
805
|
+
### TanStack Query
|
|
806
|
+
|
|
807
|
+
TanStack Query manages server data inside your React application.
|
|
808
|
+
|
|
809
|
+
It helps with:
|
|
810
|
+
|
|
811
|
+
* Loading
|
|
812
|
+
* Errors
|
|
813
|
+
* Caching
|
|
814
|
+
* Refetching
|
|
815
|
+
* Mutations
|
|
816
|
+
* Server state
|
|
817
|
+
|
|
818
|
+
### Query
|
|
819
|
+
|
|
820
|
+
A query normally means:
|
|
821
|
+
|
|
822
|
+
**"Get information from the server."**
|
|
823
|
+
|
|
824
|
+
### Mutation
|
|
825
|
+
|
|
826
|
+
A mutation normally means:
|
|
827
|
+
|
|
828
|
+
**"Change something on the server."**
|
|
829
|
+
|
|
830
|
+
### Cache
|
|
831
|
+
|
|
832
|
+
Previously received server information that can temporarily be stored and reused.
|
|
833
|
+
|
|
834
|
+
### Query invalidation
|
|
835
|
+
|
|
836
|
+
Telling the application that previously cached information may now be outdated and should be refreshed.
|
|
837
|
+
|
|
838
|
+
### Bearer Token
|
|
839
|
+
|
|
840
|
+
A common way of sending an authentication token with an API request.
|
|
841
|
+
|
|
842
|
+
---
|
|
843
|
+
|
|
844
|
+
# Is this package difficult to use?
|
|
845
|
+
|
|
846
|
+
No.
|
|
847
|
+
|
|
848
|
+
The package is designed around a simple API creation concept.
|
|
849
|
+
|
|
850
|
+
You mainly need to understand three things:
|
|
851
|
+
|
|
852
|
+
**Name → Method → URL**
|
|
853
|
+
|
|
854
|
+
For example:
|
|
855
|
+
|
|
856
|
+
**Employees → GET → /employees**
|
|
857
|
+
|
|
858
|
+
Once you understand this concept, you can create APIs for almost any resource in your application.
|
|
859
|
+
|
|
860
|
+
---
|
|
861
|
+
|
|
862
|
+
# Typical application example
|
|
863
|
+
|
|
864
|
+
Imagine you are building an HRMS application.
|
|
865
|
+
|
|
866
|
+
You might have APIs for:
|
|
867
|
+
|
|
868
|
+
* Employees
|
|
869
|
+
* Attendance
|
|
870
|
+
* Leave
|
|
871
|
+
* Payroll
|
|
872
|
+
* Departments
|
|
873
|
+
* Performance
|
|
874
|
+
* Requests
|
|
875
|
+
* Notifications
|
|
876
|
+
|
|
877
|
+
You can use the same package structure for all of them.
|
|
878
|
+
|
|
879
|
+
This gives your application a consistent API architecture instead of creating a different API implementation for every module.
|
|
880
|
+
|
|
881
|
+
---
|
|
882
|
+
|
|
883
|
+
# Browser Support
|
|
884
|
+
|
|
885
|
+
This package is intended for modern React applications running in environments that support:
|
|
886
|
+
|
|
887
|
+
* Promises
|
|
888
|
+
* Fetch-compatible AbortSignal
|
|
889
|
+
* Modern JavaScript features
|
|
890
|
+
|
|
891
|
+
For older browser environments, appropriate polyfills may be required depending on the application's setup.
|
|
892
|
+
|
|
893
|
+
---
|
|
894
|
+
|
|
895
|
+
# Security Note
|
|
896
|
+
|
|
897
|
+
This package helps send authentication tokens, but it does not provide a complete authentication system.
|
|
898
|
+
|
|
899
|
+
Your application and backend are still responsible for:
|
|
900
|
+
|
|
901
|
+
* Login
|
|
902
|
+
* User registration
|
|
903
|
+
* Token generation
|
|
904
|
+
* Token expiration
|
|
905
|
+
* Token refresh
|
|
906
|
+
* Permissions
|
|
907
|
+
* Roles
|
|
908
|
+
* Backend authorization
|
|
909
|
+
* Secure token storage
|
|
910
|
+
|
|
911
|
+
Never expose sensitive secrets such as backend private keys or server credentials in a React frontend.
|
|
912
|
+
|
|
913
|
+
---
|
|
914
|
+
|
|
915
|
+
# Performance
|
|
916
|
+
|
|
917
|
+
The package uses TanStack Query for server-state management and caching.
|
|
918
|
+
|
|
919
|
+
Performance will depend on:
|
|
920
|
+
|
|
921
|
+
* API design
|
|
922
|
+
* Backend performance
|
|
923
|
+
* Database performance
|
|
924
|
+
* Network speed
|
|
925
|
+
* Query configuration
|
|
926
|
+
* Cache configuration
|
|
927
|
+
* Amount of requested data
|
|
928
|
+
|
|
929
|
+
For large applications, developers should also use appropriate backend pagination, filtering, indexing and optimized API responses.
|
|
930
|
+
|
|
931
|
+
---
|
|
932
|
+
|
|
933
|
+
# Package Philosophy
|
|
934
|
+
|
|
935
|
+
The main goal of this package is:
|
|
936
|
+
|
|
937
|
+
> **Make API integration simple, reusable and consistent.**
|
|
938
|
+
|
|
939
|
+
Instead of creating a separate API architecture for every React project, developers can use the same basic approach across different applications.
|
|
940
|
+
|
|
941
|
+
Beginners can start with the simple functionality.
|
|
942
|
+
|
|
943
|
+
Experienced developers can take advantage of advanced Axios and TanStack Query features.
|
|
944
|
+
|
|
945
|
+
---
|
|
946
|
+
|
|
947
|
+
# Technology
|
|
948
|
+
|
|
949
|
+
This package is built around:
|
|
950
|
+
|
|
951
|
+
* React
|
|
952
|
+
* TypeScript
|
|
953
|
+
* Axios
|
|
954
|
+
* TanStack Query v5
|
|
955
|
+
|
|
956
|
+
---
|
|
957
|
+
|
|
958
|
+
# Keywords
|
|
959
|
+
|
|
960
|
+
This package is useful for developers searching for:
|
|
961
|
+
|
|
962
|
+
* React API client
|
|
963
|
+
* React API toolkit
|
|
964
|
+
* React Query API
|
|
965
|
+
* TanStack Query API
|
|
966
|
+
* Axios React Query
|
|
967
|
+
* Axios API client
|
|
968
|
+
* React API hooks
|
|
969
|
+
* React Query hooks
|
|
970
|
+
* React mutation hooks
|
|
971
|
+
* React query hooks
|
|
972
|
+
* TypeScript API client
|
|
973
|
+
* React REST API client
|
|
974
|
+
* API caching
|
|
975
|
+
* React Query caching
|
|
976
|
+
* API cache invalidation
|
|
977
|
+
* Axios TypeScript
|
|
978
|
+
* TanStack Query TypeScript
|
|
979
|
+
* React API management
|
|
980
|
+
* Generic React API client
|
|
981
|
+
* Reusable React API hooks
|
|
982
|
+
* API request management
|
|
983
|
+
* React server state management
|
|
984
|
+
|
|
985
|
+
---
|
|
986
|
+
|
|
987
|
+
# Version
|
|
988
|
+
|
|
989
|
+
Current version:
|
|
990
|
+
|
|
991
|
+
**1.0.0**
|
|
992
|
+
|
|
993
|
+
---
|
|
994
|
+
|
|
995
|
+
# License
|
|
996
|
+
|
|
997
|
+
This package is released under the **MIT License**.
|
|
998
|
+
|
|
999
|
+
See the `LICENSE` file for more information.
|
|
1000
|
+
|
|
1001
|
+
---
|
|
1002
|
+
|
|
1003
|
+
# Author
|
|
1004
|
+
|
|
1005
|
+
**Ahsan Iftikhar**
|
|
1006
|
+
|
|
1007
|
+
---
|
|
1008
|
+
|
|
1009
|
+
# Final Note
|
|
1010
|
+
|
|
1011
|
+
If you are a beginner, start with the basic setup:
|
|
1012
|
+
|
|
1013
|
+
**Install → Configure → Connect Query Client → Create API → Use API**
|
|
1014
|
+
|
|
1015
|
+
You do not need to understand every advanced feature before starting.
|
|
1016
|
+
|
|
1017
|
+
Once your application grows, you can use the advanced configuration, caching, authentication, cancellation, TypeScript and React Query features provided by the package.
|