@ahsan-iftikhar-114/api-toolkit-react-query 1.0.0 → 1.0.2

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.
Files changed (2) hide show
  1. package/README.md +364 -723
  2. package/package.json +37 -16
package/README.md CHANGED
@@ -1,396 +1,277 @@
1
1
  # @ahsan-iftikhar-114/api-toolkit-react-query
2
2
 
3
- **A simple and reusable API toolkit for React applications using Axios and TanStack Query.**
3
+ A reusable API toolkit for React applications using Axios and TanStack Query.
4
4
 
5
- This package makes it easier to connect your React application with your backend API.
5
+ The package provides a generic `createApi()` function for creating API queries and mutations without writing the same API setup repeatedly.
6
6
 
7
- You do not need to build API handling, loading states, error handling, authentication, caching, and request management from scratch.
7
+ ## Main Function
8
8
 
9
- The package provides these features in one reusable solution.
9
+ The main function provided by this package is:
10
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.
11
+ ```text
12
+ createApi(name, method, url)
13
+ ```
63
14
 
64
- ---
15
+ This is the primary function you will use.
65
16
 
66
- # What problem does it solve?
17
+ The same function can be used for all supported HTTP methods:
67
18
 
68
- Without an API toolkit, developers often need to manage several things separately:
19
+ ```text
20
+ GET
21
+ POST
22
+ PUT
23
+ PATCH
24
+ DELETE
25
+ ```
69
26
 
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
27
+ The package automatically handles the API as a TanStack Query query or mutation based on the HTTP method.
81
28
 
82
- This package brings these responsibilities together.
29
+ ```text
30
+ GET
31
+
32
+ Query
83
33
 
84
- ### Instead of building everything separately
34
+ POST / PUT / PATCH / DELETE
35
+
36
+ Mutation
37
+ ```
85
38
 
86
- You can use one consistent API system throughout your React application.
39
+ This provides a consistent way to manage API communication in React applications.
87
40
 
88
41
  ---
89
42
 
90
- # How does it work?
91
-
92
- The basic idea is very simple.
43
+ ## How `createApi()` works
93
44
 
94
- You define an API using three things:
45
+ `createApi()` accepts three basic parameters:
95
46
 
96
- **1. Name**
47
+ | Parameter | Description |
48
+ | --------- | ------------------------- |
49
+ | `name` | A unique name for the API |
50
+ | `method` | HTTP method |
51
+ | `url` | API endpoint |
97
52
 
98
- A name that identifies the API.
99
-
100
- **2. Method**
101
-
102
- The type of request:
53
+ For example:
103
54
 
104
- * GET
105
- * POST
106
- * PUT
107
- * PATCH
108
- * DELETE
55
+ ```text
56
+ createApi("users", "GET", "/users")
57
+ ```
109
58
 
110
- **3. URL**
59
+ This creates a GET API for `/users`.
111
60
 
112
- The backend API address.
61
+ For creating a user:
113
62
 
114
- That's it.
63
+ ```text
64
+ createApi("create-user", "POST", "/users")
65
+ ```
115
66
 
116
- The package then automatically decides how that API should work inside React.
67
+ The function remains the same. Only the API name, method, and URL change.
117
68
 
118
69
  ---
119
70
 
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
71
+ ## Installation
136
72
 
137
- ---
138
-
139
- ### POST
73
+ Install the package using npm:
140
74
 
141
- Used when you want to **create something new**.
75
+ ```bash
76
+ npm install @ahsan-iftikhar-114/api-toolkit-react-query
77
+ ```
142
78
 
143
- Examples:
79
+ ### Requirements
144
80
 
145
- * Create employee
146
- * Create user
147
- * Create invoice
148
- * Submit form
81
+ * React 18 or newer
82
+ * TanStack Query 5 or newer
83
+ * Axios 1 or newer
84
+ * TypeScript 5 or newer recommended
149
85
 
150
86
  ---
151
87
 
152
- ### PUT
153
-
154
- Used when you want to **update existing information**.
88
+ ## Basic Setup
155
89
 
156
- Examples:
90
+ There are three main steps to start using the package.
157
91
 
158
- * Update employee
159
- * Update user
160
- * Update profile
92
+ ### 1. Configure the API
161
93
 
162
- ---
94
+ Configure your backend API URL and, if required, your authentication token.
163
95
 
164
- ### PATCH
96
+ The configuration is applied globally so that individual API definitions do not need to repeat the same settings.
165
97
 
166
- Also used to **update information**, usually when changing only part of it.
98
+ You can configure:
167
99
 
168
- Examples:
169
-
170
- * Change employee status
171
- * Update user name
172
- * Change invoice status
100
+ * Base API URL
101
+ * Request timeout
102
+ * Global headers
103
+ * Authentication token
173
104
 
174
105
  ---
175
106
 
176
- ### DELETE
177
-
178
- Used when you want to **remove something**.
179
-
180
- Examples:
181
-
182
- * Delete employee
183
- * Delete user
184
- * Delete product
107
+ ### 2. Configure TanStack Query
185
108
 
186
- ---
109
+ The package uses TanStack Query for managing server data.
187
110
 
188
- # How do I install it?
111
+ Your React application needs a `QueryClient` and `QueryClientProvider`.
189
112
 
190
- Open your React project's terminal and install the package:
113
+ You can create a QueryClient using the package helper:
191
114
 
192
115
  ```text
193
- npm install @ahsan-iftikhar-114/api-toolkit-react-query
116
+ createApiQueryClient()
194
117
  ```
195
118
 
196
- The package works with:
197
-
198
- * React
199
- * Axios
200
- * TanStack Query
119
+ or use your own TanStack Query `QueryClient`.
201
120
 
202
- If your project does not already have the required React Query package, install it as well.
121
+ The package does not create or force a global QueryClient.
203
122
 
204
123
  ---
205
124
 
206
- # What do I need before using it?
207
-
208
- You need a React application.
125
+ ### 3. Create APIs
209
126
 
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:
127
+ Create your APIs using:
235
128
 
236
129
  ```text
237
- https://api.example.com
130
+ createApi(name, method, url)
238
131
  ```
239
132
 
240
- You configure this once.
133
+ This is the main API creation function of the package.
241
134
 
242
- After that, you can use shorter API paths throughout your application.
135
+ ---
243
136
 
244
- For example:
137
+ # HTTP Methods
245
138
 
246
- ```text
247
- /users
248
- /products
249
- /orders
250
- /invoices
251
- ```
139
+ ## GET
252
140
 
253
- ---
141
+ GET requests are used to retrieve data.
254
142
 
255
- ## Step 3 — Connect TanStack Query
143
+ Examples:
256
144
 
257
- The package uses TanStack Query to manage server data.
145
+ * Get users
146
+ * Get employees
147
+ * Get products
148
+ * Get orders
149
+ * Get reports
258
150
 
259
- Your React application needs a Query Client.
151
+ When `GET` is used with `createApi()`, the package creates a TanStack Query.
260
152
 
261
- You can use the Query Client provided by the package or create your own TanStack Query client.
153
+ The query provides access to:
262
154
 
263
- This is normally configured once in your application.
155
+ * Response data
156
+ * Loading state
157
+ * Error state
158
+ * Fetching state
159
+ * Refetching
160
+ * Cached data
264
161
 
265
162
  ---
266
163
 
267
- ## Step 4 — Create your APIs
164
+ ## POST
268
165
 
269
- For every API, provide:
166
+ POST requests are generally used to create new data.
270
167
 
271
- * Name
272
- * Method
273
- * URL
274
-
275
- For example, conceptually:
276
-
277
- **Users**
168
+ Examples:
278
169
 
279
- **GET**
170
+ * Create user
171
+ * Create employee
172
+ * Create product
173
+ * Submit a form
280
174
 
281
- **/users**
175
+ When `POST` is used, the package creates a TanStack Query mutation.
282
176
 
283
- The package will treat this as a data-fetching API.
177
+ ---
284
178
 
285
- For a create-user API:
179
+ ## PUT
286
180
 
287
- **Create User**
181
+ PUT requests are used to update existing data.
288
182
 
289
- **POST**
183
+ Examples:
290
184
 
291
- **/users**
185
+ * Update user
186
+ * Update employee
187
+ * Update product
292
188
 
293
- The package will treat this as a data-changing API.
189
+ PUT APIs are handled as mutations.
294
190
 
295
191
  ---
296
192
 
297
- # GET APIs
193
+ ## PATCH
298
194
 
299
- GET APIs are used when your application needs to **read information from the server**.
195
+ PATCH requests are used to partially update existing data.
300
196
 
301
- For example:
197
+ Examples:
302
198
 
303
- * Employee list
304
- * Customer list
305
- * Product list
306
- * Dashboard statistics
307
- * Notifications
308
- * User profile
199
+ * Change user status
200
+ * Update employee status
201
+ * Change order status
309
202
 
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.
203
+ PATCH APIs are handled as mutations.
320
204
 
321
205
  ---
322
206
 
323
- # POST, PUT, PATCH and DELETE APIs
207
+ ## DELETE
324
208
 
325
- These APIs are used when your application needs to **change information on the server**.
209
+ DELETE requests are used to remove data.
326
210
 
327
- For example:
211
+ Examples:
328
212
 
329
- * Create employee
330
- * Update employee
213
+ * Delete user
331
214
  * 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:
215
+ * Delete product
339
216
 
340
- * Whether the operation is running
341
- * Whether it succeeded
342
- * Whether it failed
343
- * What data was returned
344
- * What error occurred
217
+ DELETE APIs are handled as mutations.
345
218
 
346
219
  ---
347
220
 
348
- # Sending information to the server
221
+ # Request Data
349
222
 
350
- When creating or updating something, your application usually needs to send information.
223
+ POST, PUT, PATCH and DELETE requests can send data to the backend.
351
224
 
352
- For example, when creating an employee:
225
+ For example, an employee API may need:
353
226
 
354
227
  * Name
355
228
  * Email
356
229
  * Department
357
230
  * Position
358
231
 
359
- The package allows you to send this information to your backend API.
232
+ The package supports request payloads along with query parameters, headers and additional Axios configuration.
360
233
 
361
- You can also send:
234
+ ---
362
235
 
363
- * Query parameters
364
- * Request headers
365
- * Authentication information
366
- * Additional Axios configuration
236
+ # Query Parameters
367
237
 
368
- ---
238
+ GET requests can include query parameters for:
369
239
 
370
- # Authentication
240
+ * Search
241
+ * Pagination
242
+ * Filtering
243
+ * Sorting
244
+ * Page size
245
+ * Status
371
246
 
372
- Many APIs require users to log in before they can access protected information.
247
+ For example:
373
248
 
374
- This package supports **Bearer Token authentication**.
249
+ ```text
250
+ /users?page=1&search=ahsan
251
+ ```
375
252
 
376
- You can configure an authentication token globally.
253
+ Query parameters are also included in the query key, allowing TanStack Query to manage different parameter combinations separately.
377
254
 
378
- The token can be:
255
+ ---
379
256
 
380
- * A fixed token
381
- * A dynamically retrieved token
257
+ # Authentication
382
258
 
383
- A dynamic token is useful when the token is stored in browser storage or another authentication system.
259
+ The package supports Bearer token authentication.
384
260
 
385
- The package automatically adds the token to API requests.
261
+ You can configure either:
386
262
 
387
- This means you do not need to manually add the authentication header to every request.
263
+ * A static token
264
+ * A function that returns the current token
265
+
266
+ This allows applications to keep authentication configuration in one place instead of manually adding the token to every request.
267
+
268
+ The backend remains responsible for authentication and authorization.
388
269
 
389
270
  ---
390
271
 
391
272
  # API Base URL
392
273
 
393
- You can configure one common API address for your application.
274
+ A global base URL can be configured for the application.
394
275
 
395
276
  For example:
396
277
 
@@ -398,9 +279,7 @@ For example:
398
279
  https://api.example.com
399
280
  ```
400
281
 
401
- Then individual APIs only need their own path.
402
-
403
- For example:
282
+ Individual APIs can then use relative paths:
404
283
 
405
284
  ```text
406
285
  /users
@@ -409,544 +288,302 @@ For example:
409
288
  /orders
410
289
  ```
411
290
 
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.
291
+ This keeps API definitions short and makes it easier to change the backend URL later.
415
292
 
416
293
  ---
417
294
 
418
- # Loading states
295
+ # Loading and Request States
419
296
 
420
- When your application requests information from a server, the server may take some time to respond.
297
+ TanStack Query provides request state information for GET requests and mutation state information for POST, PUT, PATCH and DELETE requests.
421
298
 
422
- The package provides request status information so your application can show things such as:
299
+ This can be used to display:
423
300
 
424
- * Loading...
425
- * Please wait...
426
- * Loading spinner
427
- * Refreshing...
428
- * Saving...
429
- * Deleting...
301
+ * Loading indicators
302
+ * Saving indicators
303
+ * Delete progress
304
+ * Error messages
305
+ * Refreshing states
430
306
 
431
- This makes it easier to create a better user experience.
307
+ The package does not require developers to create separate loading state variables for every API request.
432
308
 
433
309
  ---
434
310
 
435
- # Error handling
311
+ # Error Handling
436
312
 
437
- API requests can fail for many reasons.
313
+ The package normalizes API errors into a consistent `ApiError` structure.
438
314
 
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:
315
+ The error can include:
459
316
 
460
- * Page number
461
- * Number of records
462
- * Search text
463
- * Sorting
464
- * Filtering
465
- * Status
317
+ * Message
318
+ * HTTP status
319
+ * Status text
320
+ * Response data
321
+ * Validation errors
322
+ * Original Axios error
466
323
 
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
324
+ This provides a consistent way to handle API errors throughout the application.
477
325
 
478
326
  ---
479
327
 
480
328
  # Caching
481
329
 
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.
330
+ GET requests use TanStack Query's caching system.
491
331
 
492
- The application gets the employee list from the server.
332
+ Previously fetched data can be stored in the query cache according to the configured TanStack Query settings.
493
333
 
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.
334
+ Caching can reduce unnecessary API requests and improve the responsiveness of an application.
497
335
 
498
336
  ---
499
337
 
500
338
  # Refetching
501
339
 
502
- Sometimes information needs to be updated from the server.
340
+ TanStack Query provides refetching functionality for keeping server data up to date.
503
341
 
504
- For example:
342
+ Depending on your configuration, queries can be refetched when:
505
343
 
506
- * User clicks Refresh
507
- * User returns to a page
508
- * Application reconnects to the internet
344
+ * A user manually refreshes data
509
345
  * 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.
346
+ * The application reconnects
347
+ * A component mounts
348
+ * A configured interval is reached
514
349
 
515
350
  ---
516
351
 
517
- # Cache invalidation
518
-
519
- Suppose you have an employee list.
520
-
521
- A user creates a new employee.
352
+ # Cache Invalidation
522
353
 
523
- The old employee list may no longer be correct.
354
+ When data is created, updated or deleted, previously cached data may become outdated.
524
355
 
525
- The package provides cache invalidation utilities that can tell TanStack Query:
356
+ The package provides cache utilities for invalidating queries.
526
357
 
527
- **"This data may have changed. Get the latest information."**
358
+ Available utilities include:
528
359
 
529
- This is especially useful after:
360
+ ```text
361
+ invalidateApi()
362
+ invalidateApis()
363
+ invalidateAllApis()
364
+ ```
530
365
 
531
- * Creating records
532
- * Updating records
533
- * Deleting records
534
- * Changing statuses
535
- * Performing other server-side operations
366
+ You can use these after mutations to tell TanStack Query that related data should be refreshed.
536
367
 
537
368
  ---
538
369
 
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.
370
+ # Request Cancellation
550
371
 
551
372
  The package supports request cancellation through `AbortSignal`.
552
373
 
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
-
374
+ This is useful when a request is no longer required, such as when a user changes a search request before the previous request has completed.
657
375
 
658
- **Loading & Error States**
376
+ The package also provides:
659
377
 
660
-
661
-
662
- **Cache Invalidation**
378
+ ```text
379
+ cancelApi()
380
+ ```
663
381
 
664
- The goal is to give your application one consistent way to communicate with APIs.
382
+ for cancelling queries associated with an API.
665
383
 
666
384
  ---
667
385
 
668
- # Why use this package?
386
+ # React Query Options
669
387
 
670
- ### Simple API creation
671
-
672
- Define an API using:
673
-
674
- * Name
675
- * Method
676
- * URL
388
+ The package supports standard TanStack Query options.
677
389
 
678
- ---
390
+ These include:
679
391
 
680
- ### Less repeated code
392
+ * `staleTime`
393
+ * `gcTime`
394
+ * `enabled`
395
+ * `retry`
396
+ * `retryDelay`
397
+ * `refetchInterval`
398
+ * `refetchOnWindowFocus`
399
+ * `refetchOnReconnect`
400
+ * `refetchOnMount`
401
+ * `initialData`
402
+ * `placeholderData`
403
+ * `select`
681
404
 
682
- You do not have to repeatedly create the same API request structure.
405
+ This allows the basic API creation system to remain simple while still providing access to TanStack Query's configuration options.
683
406
 
684
407
  ---
685
408
 
686
- ### Centralized configuration
687
-
688
- Configure:
689
-
690
- * API URL
691
- * Timeout
692
- * Headers
693
- * Authentication
409
+ # Data Selection
694
410
 
695
- in one place.
411
+ The `select` option can be used when the component only needs a specific part or transformed version of the API response.
696
412
 
697
- ---
698
-
699
- ### Built-in React Query support
413
+ For example, an API may return a complete list of users while a component only needs the number of users.
700
414
 
701
- Use the powerful server-state management capabilities of TanStack Query.
415
+ TanStack Query's `select` functionality can be used for this transformation without changing the original cached data.
702
416
 
703
417
  ---
704
418
 
705
- ### Authentication support
419
+ # Axios Configuration
706
420
 
707
- Bearer token authentication can be configured centrally.
421
+ The package uses Axios for HTTP requests.
708
422
 
709
- ---
423
+ In addition to the global configuration, individual requests can use:
710
424
 
711
- ### Consistent errors
712
-
713
- API errors are normalized into a common structure.
714
-
715
- ---
716
-
717
- ### Caching support
425
+ * Request-specific headers
426
+ * Query parameters
427
+ * Request data
428
+ * Axios request configuration
429
+ * Request cancellation
718
430
 
719
- Use TanStack Query caching to reduce unnecessary requests.
431
+ This provides additional control when a specific API needs different settings.
720
432
 
721
433
  ---
722
434
 
723
- ### Request cancellation
724
-
725
- Cancel requests when they are no longer required.
435
+ # TypeScript
726
436
 
727
- ---
437
+ The package supports TypeScript generics for API responses, request data and selected data.
728
438
 
729
- ### TypeScript friendly
439
+ This provides type checking and editor support when working with API data.
730
440
 
731
- Use types for safer development and better editor support.
441
+ TypeScript is recommended for larger React applications.
732
442
 
733
443
  ---
734
444
 
735
- ### Flexible
445
+ # Query Client
736
446
 
737
- Beginners can use the simple API creation system, while experienced developers can use advanced Axios and TanStack Query options.
447
+ The package provides:
738
448
 
739
- ---
449
+ ```text
450
+ createApiQueryClient()
451
+ ```
740
452
 
741
- # Recommended for
453
+ as a helper for creating a TanStack Query `QueryClient`.
742
454
 
743
- This package is particularly useful for applications such as:
455
+ You can also create and configure your own `QueryClient`.
744
456
 
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
457
+ The package does not maintain a hidden global QueryClient. This allows the application to control its own QueryClient and cache lifecycle.
758
458
 
759
459
  ---
760
460
 
761
- # Package Architecture
461
+ # Complete API Flow
762
462
 
763
- The package internally separates different responsibilities.
463
+ The package follows this general flow:
764
464
 
765
465
  ```text
766
- API Configuration
767
-
768
- Axios
769
-
770
- API Request Layer
771
-
772
- TanStack Query
773
-
774
- React Application
466
+ createApi(name, method, url)
467
+ |
468
+ v
469
+ API Definition
470
+ |
471
+ v
472
+ Axios
473
+ |
474
+ v
475
+ Backend API
476
+ |
477
+ v
478
+ TanStack Query
479
+ |
480
+ v
481
+ Data / Cache / Loading
482
+ Errors / Refetching
483
+ Mutations / Invalidation
775
484
  ```
776
485
 
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 |
486
+ The developer mainly works with `createApi()` while Axios and TanStack Query handle the underlying API and server-state functionality.
792
487
 
793
488
  ---
794
489
 
795
- # Important terms explained simply
796
-
797
- ### API
798
-
799
- An API allows your application to communicate with a backend server.
800
-
801
- ### Axios
490
+ # Example Use Cases
802
491
 
803
- Axios is the technology used to send HTTP requests to the backend.
492
+ The same generic API function can be used in different types of applications.
804
493
 
805
- ### TanStack Query
494
+ For an HRMS:
806
495
 
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
496
+ ```text
497
+ Employees
498
+ Attendance
499
+ Leave
500
+ Payroll
501
+ Performance
502
+ Requests
503
+ Notifications
504
+ ```
831
505
 
832
- Previously received server information that can temporarily be stored and reused.
506
+ For a CRM:
833
507
 
834
- ### Query invalidation
508
+ ```text
509
+ Leads
510
+ Contacts
511
+ Companies
512
+ Opportunities
513
+ Deals
514
+ Customers
515
+ ```
835
516
 
836
- Telling the application that previously cached information may now be outdated and should be refreshed.
517
+ For an e-commerce application:
837
518
 
838
- ### Bearer Token
519
+ ```text
520
+ Products
521
+ Categories
522
+ Orders
523
+ Customers
524
+ Payments
525
+ ```
839
526
 
840
- A common way of sending an authentication token with an API request.
527
+ The API creation pattern remains the same.
841
528
 
842
529
  ---
843
530
 
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**
531
+ # Supported Methods
857
532
 
858
- Once you understand this concept, you can create APIs for almost any resource in your application.
533
+ | Method | Type | Purpose |
534
+ | ------ | -------- | --------------------- |
535
+ | GET | Query | Retrieve data |
536
+ | POST | Mutation | Create data |
537
+ | PUT | Mutation | Update data |
538
+ | PATCH | Mutation | Partially update data |
539
+ | DELETE | Mutation | Delete data |
859
540
 
860
541
  ---
861
542
 
862
- # Typical application example
543
+ # Backend Compatibility
863
544
 
864
- Imagine you are building an HRMS application.
545
+ The package can be used with any backend that provides a compatible HTTP API.
865
546
 
866
- You might have APIs for:
547
+ Examples include:
867
548
 
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
549
+ * Laravel
550
+ * PHP
551
+ * Node.js
552
+ * Express
553
+ * NestJS
554
+ * .NET
555
+ * ASP.NET
556
+ * Django
557
+ * FastAPI
558
+ * Spring Boot
559
+ * Other REST API backends
890
560
 
891
- For older browser environments, appropriate polyfills may be required depending on the application's setup.
561
+ The package is responsible for the React API layer. Backend authentication, authorization, validation and business logic remain the responsibility of the backend application.
892
562
 
893
563
  ---
894
564
 
895
- # Security Note
565
+ # Security
896
566
 
897
- This package helps send authentication tokens, but it does not provide a complete authentication system.
567
+ The package supports authentication tokens but does not provide an authentication system.
898
568
 
899
- Your application and backend are still responsible for:
569
+ Your application and backend are responsible for:
900
570
 
901
571
  * Login
902
- * User registration
572
+ * Registration
903
573
  * Token generation
904
- * Token expiration
905
574
  * Token refresh
906
- * Permissions
575
+ * Token expiration
907
576
  * 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.
577
+ * Permissions
578
+ * Authorization
942
579
 
943
- Experienced developers can take advantage of advanced Axios and TanStack Query features.
580
+ Do not expose private server credentials or secrets in a React application.
944
581
 
945
582
  ---
946
583
 
947
584
  # Technology
948
585
 
949
- This package is built around:
586
+ This package uses:
950
587
 
951
588
  * React
952
589
  * TypeScript
@@ -955,63 +592,67 @@ This package is built around:
955
592
 
956
593
  ---
957
594
 
958
- # Keywords
595
+ # Package Structure
959
596
 
960
- This package is useful for developers searching for:
597
+ The package separates its main responsibilities into:
961
598
 
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
599
+ ```text
600
+ Configuration
601
+ |
602
+ v
603
+ Axios
604
+ |
605
+ v
606
+ API Request
607
+ |
608
+ v
609
+ TanStack Query
610
+ |
611
+ v
612
+ React Application
613
+ ```
984
614
 
985
- ---
615
+ This keeps API communication and server-state management reusable across the application.
986
616
 
987
- # Version
617
+ ---
988
618
 
989
- Current version:
619
+ # Main API
990
620
 
991
- **1.0.0**
621
+ The main public function is:
992
622
 
993
- ---
623
+ ```text
624
+ createApi(name, method, url)
625
+ ```
994
626
 
995
- # License
627
+ Additional public utilities include:
996
628
 
997
- This package is released under the **MIT License**.
629
+ ```text
630
+ configureApi()
631
+ createApiQueryClient()
632
+ invalidateApi()
633
+ invalidateApis()
634
+ invalidateAllApis()
635
+ cancelApi()
636
+ ```
998
637
 
999
- See the `LICENSE` file for more information.
638
+ The package also provides TypeScript types for API configuration, requests, responses, errors and React Query options.
1000
639
 
1001
640
  ---
1002
641
 
1003
- # Author
642
+ # Keywords
1004
643
 
1005
- **Ahsan Iftikhar**
644
+ React API toolkit, React API client, React Query API, TanStack Query API, Axios React Query, Axios API client, React API hooks, React Query hooks, React mutation hooks, TypeScript API client, React REST API client, API caching, React Query caching, API cache invalidation, Axios TypeScript, TanStack Query TypeScript, React API management, generic React API client, reusable React API hooks, API request management, React server state management.
1006
645
 
1007
646
  ---
1008
647
 
1009
- # Final Note
648
+ # License
1010
649
 
1011
- If you are a beginner, start with the basic setup:
650
+ This package is released under the MIT License.
1012
651
 
1013
- **Install Configure Connect Query Client → Create API → Use API**
652
+ See the `LICENSE` file for complete license information.
1014
653
 
1015
- You do not need to understand every advanced feature before starting.
654
+ ---
655
+
656
+ # Author
1016
657
 
1017
- Once your application grows, you can use the advanced configuration, caching, authentication, cancellation, TypeScript and React Query features provided by the package.
658
+ Ahsan Iftikhar