maestro-bundle 1.3.1 → 1.4.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.
Files changed (116) hide show
  1. package/package.json +1 -1
  2. package/templates/bundle-ai-agents/skills/agent-orchestration/SKILL.md +107 -41
  3. package/templates/bundle-ai-agents/skills/agent-orchestration/references/graph-patterns.md +50 -0
  4. package/templates/bundle-ai-agents/skills/agent-orchestration/references/routing-strategies.md +47 -0
  5. package/templates/bundle-ai-agents/skills/api-design/SKILL.md +125 -16
  6. package/templates/bundle-ai-agents/skills/api-design/references/pydantic-patterns.md +72 -0
  7. package/templates/bundle-ai-agents/skills/api-design/references/rest-conventions.md +51 -0
  8. package/templates/bundle-ai-agents/skills/clean-architecture/SKILL.md +113 -21
  9. package/templates/bundle-ai-agents/skills/clean-architecture/references/dependency-injection.md +60 -0
  10. package/templates/bundle-ai-agents/skills/clean-architecture/references/layer-rules.md +56 -0
  11. package/templates/bundle-ai-agents/skills/context-engineering/SKILL.md +104 -36
  12. package/templates/bundle-ai-agents/skills/context-engineering/references/compression-techniques.md +76 -0
  13. package/templates/bundle-ai-agents/skills/context-engineering/references/context-budget-calculator.md +45 -0
  14. package/templates/bundle-ai-agents/skills/database-modeling/SKILL.md +146 -19
  15. package/templates/bundle-ai-agents/skills/database-modeling/references/index-strategies.md +48 -0
  16. package/templates/bundle-ai-agents/skills/database-modeling/references/naming-conventions.md +27 -0
  17. package/templates/bundle-ai-agents/skills/docker-containerization/SKILL.md +124 -15
  18. package/templates/bundle-ai-agents/skills/docker-containerization/references/compose-patterns.md +97 -0
  19. package/templates/bundle-ai-agents/skills/docker-containerization/references/dockerfile-checklist.md +37 -0
  20. package/templates/bundle-ai-agents/skills/eval-testing/SKILL.md +113 -25
  21. package/templates/bundle-ai-agents/skills/eval-testing/references/eval-types.md +52 -0
  22. package/templates/bundle-ai-agents/skills/eval-testing/references/golden-dataset-template.md +59 -0
  23. package/templates/bundle-ai-agents/skills/memory-management/SKILL.md +112 -28
  24. package/templates/bundle-ai-agents/skills/memory-management/references/memory-tiers.md +41 -0
  25. package/templates/bundle-ai-agents/skills/memory-management/references/namespace-conventions.md +41 -0
  26. package/templates/bundle-ai-agents/skills/prompt-engineering/SKILL.md +139 -47
  27. package/templates/bundle-ai-agents/skills/prompt-engineering/references/anti-patterns.md +59 -0
  28. package/templates/bundle-ai-agents/skills/prompt-engineering/references/prompt-templates.md +75 -0
  29. package/templates/bundle-ai-agents/skills/rag-pipeline/SKILL.md +104 -27
  30. package/templates/bundle-ai-agents/skills/rag-pipeline/references/chunking-strategies.md +27 -0
  31. package/templates/bundle-ai-agents/skills/rag-pipeline/references/embedding-models.md +31 -0
  32. package/templates/bundle-ai-agents/skills/rag-pipeline/references/rag-evaluation.md +39 -0
  33. package/templates/bundle-ai-agents/skills/testing-strategy/SKILL.md +127 -18
  34. package/templates/bundle-ai-agents/skills/testing-strategy/references/fixture-patterns.md +81 -0
  35. package/templates/bundle-ai-agents/skills/testing-strategy/references/naming-conventions.md +69 -0
  36. package/templates/bundle-base/skills/branch-strategy/SKILL.md +134 -21
  37. package/templates/bundle-base/skills/branch-strategy/references/branch-rules.md +40 -0
  38. package/templates/bundle-base/skills/code-review/SKILL.md +123 -38
  39. package/templates/bundle-base/skills/code-review/references/review-checklist.md +45 -0
  40. package/templates/bundle-base/skills/commit-pattern/SKILL.md +98 -39
  41. package/templates/bundle-base/skills/commit-pattern/references/conventional-commits.md +40 -0
  42. package/templates/bundle-data-pipeline/skills/data-preprocessing/SKILL.md +110 -19
  43. package/templates/bundle-data-pipeline/skills/data-preprocessing/references/pandas-cheatsheet.md +63 -0
  44. package/templates/bundle-data-pipeline/skills/data-preprocessing/references/pandera-schemas.md +44 -0
  45. package/templates/bundle-data-pipeline/skills/docker-containerization/SKILL.md +132 -16
  46. package/templates/bundle-data-pipeline/skills/docker-containerization/references/compose-patterns.md +82 -0
  47. package/templates/bundle-data-pipeline/skills/docker-containerization/references/dockerfile-best-practices.md +57 -0
  48. package/templates/bundle-data-pipeline/skills/feature-engineering/SKILL.md +143 -45
  49. package/templates/bundle-data-pipeline/skills/feature-engineering/references/encoding-guide.md +41 -0
  50. package/templates/bundle-data-pipeline/skills/feature-engineering/references/scaling-guide.md +38 -0
  51. package/templates/bundle-data-pipeline/skills/mlops-pipeline/SKILL.md +156 -37
  52. package/templates/bundle-data-pipeline/skills/mlops-pipeline/references/mlflow-commands.md +69 -0
  53. package/templates/bundle-data-pipeline/skills/model-training/SKILL.md +152 -33
  54. package/templates/bundle-data-pipeline/skills/model-training/references/evaluation-metrics.md +52 -0
  55. package/templates/bundle-data-pipeline/skills/model-training/references/model-selection-guide.md +41 -0
  56. package/templates/bundle-data-pipeline/skills/rag-pipeline/SKILL.md +127 -39
  57. package/templates/bundle-data-pipeline/skills/rag-pipeline/references/chunking-strategies.md +51 -0
  58. package/templates/bundle-data-pipeline/skills/rag-pipeline/references/embedding-models.md +49 -0
  59. package/templates/bundle-frontend-spa/skills/authentication/SKILL.md +196 -13
  60. package/templates/bundle-frontend-spa/skills/authentication/references/jwt-security.md +41 -0
  61. package/templates/bundle-frontend-spa/skills/component-design/SKILL.md +191 -41
  62. package/templates/bundle-frontend-spa/skills/component-design/references/accessibility-checklist.md +41 -0
  63. package/templates/bundle-frontend-spa/skills/component-design/references/tailwind-patterns.md +65 -0
  64. package/templates/bundle-frontend-spa/skills/e2e-testing/SKILL.md +241 -79
  65. package/templates/bundle-frontend-spa/skills/e2e-testing/references/playwright-selectors.md +66 -0
  66. package/templates/bundle-frontend-spa/skills/e2e-testing/references/test-patterns.md +82 -0
  67. package/templates/bundle-frontend-spa/skills/integration-api/SKILL.md +221 -31
  68. package/templates/bundle-frontend-spa/skills/integration-api/references/api-patterns.md +81 -0
  69. package/templates/bundle-frontend-spa/skills/react-patterns/SKILL.md +195 -70
  70. package/templates/bundle-frontend-spa/skills/react-patterns/references/component-checklist.md +22 -0
  71. package/templates/bundle-frontend-spa/skills/react-patterns/references/hook-patterns.md +63 -0
  72. package/templates/bundle-frontend-spa/skills/responsive-layout/SKILL.md +162 -22
  73. package/templates/bundle-frontend-spa/skills/responsive-layout/references/breakpoint-guide.md +63 -0
  74. package/templates/bundle-frontend-spa/skills/state-management/SKILL.md +158 -30
  75. package/templates/bundle-frontend-spa/skills/state-management/references/react-query-config.md +64 -0
  76. package/templates/bundle-frontend-spa/skills/state-management/references/state-patterns.md +78 -0
  77. package/templates/bundle-jhipster-microservices/skills/ci-cd-pipeline/SKILL.md +135 -45
  78. package/templates/bundle-jhipster-microservices/skills/ci-cd-pipeline/references/gitlab-ci-templates.md +93 -0
  79. package/templates/bundle-jhipster-microservices/skills/clean-architecture/SKILL.md +87 -21
  80. package/templates/bundle-jhipster-microservices/skills/clean-architecture/references/layer-rules.md +78 -0
  81. package/templates/bundle-jhipster-microservices/skills/ddd-tactical/SKILL.md +94 -25
  82. package/templates/bundle-jhipster-microservices/skills/ddd-tactical/references/ddd-patterns.md +48 -0
  83. package/templates/bundle-jhipster-microservices/skills/jhipster-angular/SKILL.md +63 -21
  84. package/templates/bundle-jhipster-microservices/skills/jhipster-angular/references/angular-microservices.md +40 -0
  85. package/templates/bundle-jhipster-microservices/skills/jhipster-angular/references/angular-structure.md +59 -0
  86. package/templates/bundle-jhipster-microservices/skills/jhipster-docker-k8s/SKILL.md +125 -91
  87. package/templates/bundle-jhipster-microservices/skills/jhipster-docker-k8s/references/docker-k8s-commands.md +68 -0
  88. package/templates/bundle-jhipster-microservices/skills/jhipster-entities/SKILL.md +72 -20
  89. package/templates/bundle-jhipster-microservices/skills/jhipster-entities/references/cross-service-entities.md +36 -0
  90. package/templates/bundle-jhipster-microservices/skills/jhipster-entities/references/jdl-types.md +56 -0
  91. package/templates/bundle-jhipster-microservices/skills/jhipster-gateway/SKILL.md +80 -8
  92. package/templates/bundle-jhipster-microservices/skills/jhipster-gateway/references/gateway-config.md +43 -0
  93. package/templates/bundle-jhipster-microservices/skills/jhipster-kafka/SKILL.md +115 -22
  94. package/templates/bundle-jhipster-microservices/skills/jhipster-kafka/references/kafka-events.md +39 -0
  95. package/templates/bundle-jhipster-microservices/skills/jhipster-registry/SKILL.md +92 -23
  96. package/templates/bundle-jhipster-microservices/skills/jhipster-registry/references/consul-config.md +61 -0
  97. package/templates/bundle-jhipster-microservices/skills/jhipster-service/SKILL.md +81 -18
  98. package/templates/bundle-jhipster-microservices/skills/jhipster-service/references/service-patterns.md +40 -0
  99. package/templates/bundle-jhipster-microservices/skills/testing-strategy/SKILL.md +101 -20
  100. package/templates/bundle-jhipster-microservices/skills/testing-strategy/references/test-naming.md +55 -0
  101. package/templates/bundle-jhipster-monorepo/skills/clean-architecture/SKILL.md +87 -21
  102. package/templates/bundle-jhipster-monorepo/skills/clean-architecture/references/layer-rules.md +78 -0
  103. package/templates/bundle-jhipster-monorepo/skills/ddd-tactical/SKILL.md +94 -25
  104. package/templates/bundle-jhipster-monorepo/skills/ddd-tactical/references/ddd-patterns.md +48 -0
  105. package/templates/bundle-jhipster-monorepo/skills/jhipster-angular/SKILL.md +99 -52
  106. package/templates/bundle-jhipster-monorepo/skills/jhipster-angular/references/angular-structure.md +59 -0
  107. package/templates/bundle-jhipster-monorepo/skills/jhipster-entities/SKILL.md +89 -36
  108. package/templates/bundle-jhipster-monorepo/skills/jhipster-entities/references/jdl-types.md +56 -0
  109. package/templates/bundle-jhipster-monorepo/skills/jhipster-liquibase/SKILL.md +123 -23
  110. package/templates/bundle-jhipster-monorepo/skills/jhipster-liquibase/references/liquibase-operations.md +95 -0
  111. package/templates/bundle-jhipster-monorepo/skills/jhipster-security/SKILL.md +106 -19
  112. package/templates/bundle-jhipster-monorepo/skills/jhipster-security/references/security-checklist.md +47 -0
  113. package/templates/bundle-jhipster-monorepo/skills/jhipster-spring/SKILL.md +84 -16
  114. package/templates/bundle-jhipster-monorepo/skills/jhipster-spring/references/spring-layers.md +41 -0
  115. package/templates/bundle-jhipster-monorepo/skills/testing-strategy/SKILL.md +101 -20
  116. package/templates/bundle-jhipster-monorepo/skills/testing-strategy/references/test-naming.md +55 -0
@@ -1,26 +1,64 @@
1
1
  ---
2
2
  name: integration-api
3
- description: Integrar frontend React com backend API usando React Query, Axios e WebSocket. Use quando precisar conectar frontend com backend, consumir APIs, ou implementar real-time.
3
+ description: Integrate React frontend with backend APIs using Axios, React Query, and WebSocket for real-time updates. Use when you need to connect frontend to backend, consume REST APIs, set up HTTP clients, or implement real-time communication.
4
+ version: 1.0.0
5
+ author: Maestro
4
6
  ---
5
7
 
6
- # Integração Frontend ↔ Backend
8
+ # API Integration
7
9
 
8
- ## HTTP Client (Axios)
10
+ Connect a React frontend to backend APIs using Axios for HTTP, React Query for caching, and Socket.IO for real-time updates.
9
11
 
12
+ ## When to Use
13
+ - User needs to set up an HTTP client with Axios
14
+ - User wants to create a service layer for API calls
15
+ - User needs React Query hooks for data fetching and mutations
16
+ - User wants to add real-time WebSocket updates
17
+ - User needs to handle API errors, retries, and loading states
18
+
19
+ ## Available Operations
20
+ 1. Configure Axios client with base URL, timeouts, and interceptors
21
+ 2. Create a typed service layer for API endpoints
22
+ 3. Build React Query hooks with cache management
23
+ 4. Set up WebSocket connections with Socket.IO
24
+ 5. Handle error states, retries, and optimistic updates
25
+
26
+ ## Multi-Step Workflow
27
+
28
+ ### Step 1: Install Dependencies
29
+ ```bash
30
+ npm install axios @tanstack/react-query socket.io-client
31
+ npm install -D @tanstack/react-query-devtools
32
+ ```
33
+
34
+ ### Step 2: Configure Axios HTTP Client
10
35
  ```typescript
11
- // shared/lib/api.ts
36
+ // src/lib/api.ts
12
37
  import axios from 'axios';
13
38
 
14
39
  export const api = axios.create({
15
40
  baseURL: import.meta.env.VITE_API_URL || 'http://localhost:8000/api/v1',
16
41
  timeout: 10000,
42
+ headers: {
43
+ 'Content-Type': 'application/json',
44
+ },
17
45
  });
18
46
 
47
+ // Add auth token to every request
48
+ api.interceptors.request.use((config) => {
49
+ const token = localStorage.getItem('token');
50
+ if (token) {
51
+ config.headers.Authorization = `Bearer ${token}`;
52
+ }
53
+ return config;
54
+ });
55
+
56
+ // Unwrap response data and handle errors
19
57
  api.interceptors.response.use(
20
58
  (response) => response.data,
21
59
  (error) => {
22
60
  if (error.response?.status === 401) {
23
- useAuthStore.getState().logout();
61
+ localStorage.removeItem('token');
24
62
  window.location.href = '/login';
25
63
  }
26
64
  return Promise.reject(error.response?.data || error);
@@ -28,68 +66,220 @@ api.interceptors.response.use(
28
66
  );
29
67
  ```
30
68
 
31
- ## Service Layer
69
+ Add the API URL to your environment:
70
+ ```bash
71
+ # .env.local
72
+ VITE_API_URL=http://localhost:8000/api/v1
73
+ ```
32
74
 
75
+ ### Step 3: Create Typed Service Layer
33
76
  ```typescript
34
- // features/demands/services/demandApi.ts
35
- export const demandApi = {
77
+ // src/services/itemApi.ts
78
+ import { api } from '@/lib/api';
79
+
80
+ export interface Item {
81
+ id: string;
82
+ title: string;
83
+ description: string;
84
+ status: 'pending' | 'in_progress' | 'completed';
85
+ priority: 'low' | 'medium' | 'high';
86
+ createdAt: string;
87
+ }
88
+
89
+ export interface PaginatedResponse<T> {
90
+ items: T[];
91
+ total: number;
92
+ page: number;
93
+ size: number;
94
+ }
95
+
96
+ export interface CreateItemDto {
97
+ title: string;
98
+ description: string;
99
+ priority: 'low' | 'medium' | 'high';
100
+ }
101
+
102
+ export const itemApi = {
36
103
  list: (params?: { page?: number; size?: number; status?: string }) =>
37
- api.get<PaginatedResponse<Demand>>('/demands', { params }),
104
+ api.get<PaginatedResponse<Item>>('/items', { params }),
38
105
 
39
106
  get: (id: string) =>
40
- api.get<Demand>(`/demands/${id}`),
107
+ api.get<Item>(`/items/${id}`),
41
108
 
42
- create: (data: CreateDemandDto) =>
43
- api.post<Demand>('/demands', data),
109
+ create: (data: CreateItemDto) =>
110
+ api.post<Item>('/items', data),
44
111
 
45
- update: (id: string, data: UpdateDemandDto) =>
46
- api.put<Demand>(`/demands/${id}`, data),
112
+ update: (id: string, data: Partial<CreateItemDto>) =>
113
+ api.put<Item>(`/items/${id}`, data),
47
114
 
48
115
  delete: (id: string) =>
49
- api.delete(`/demands/${id}`),
116
+ api.delete(`/items/${id}`),
50
117
  };
51
118
  ```
52
119
 
53
- ## React Query Hooks
54
-
120
+ ### Step 4: Build React Query Hooks
55
121
  ```typescript
56
- export function useDemands(params?: DemandFilters) {
122
+ // src/hooks/useItems.ts
123
+ import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
124
+ import { itemApi, type CreateItemDto } from '@/services/itemApi';
125
+ import { toast } from 'sonner';
126
+
127
+ export function useItems(params?: { page?: number; status?: string }) {
57
128
  return useQuery({
58
- queryKey: ['demands', params],
59
- queryFn: () => demandApi.list(params),
129
+ queryKey: ['items', params],
130
+ queryFn: () => itemApi.list(params),
60
131
  });
61
132
  }
62
133
 
63
- export function useDemand(id: string) {
134
+ export function useItem(id: string) {
64
135
  return useQuery({
65
- queryKey: ['demands', id],
66
- queryFn: () => demandApi.get(id),
136
+ queryKey: ['items', id],
137
+ queryFn: () => itemApi.get(id),
67
138
  enabled: !!id,
68
139
  });
69
140
  }
70
141
 
71
- export function useCreateDemand() {
142
+ export function useCreateItem() {
72
143
  const qc = useQueryClient();
73
144
  return useMutation({
74
- mutationFn: demandApi.create,
75
- onSuccess: () => qc.invalidateQueries({ queryKey: ['demands'] }),
145
+ mutationFn: (data: CreateItemDto) => itemApi.create(data),
146
+ onSuccess: () => {
147
+ qc.invalidateQueries({ queryKey: ['items'] });
148
+ toast.success('Item created successfully');
149
+ },
150
+ onError: (error: any) => {
151
+ toast.error(error.message || 'Failed to create item');
152
+ },
153
+ });
154
+ }
155
+
156
+ export function useUpdateItem() {
157
+ const qc = useQueryClient();
158
+ return useMutation({
159
+ mutationFn: ({ id, data }: { id: string; data: Partial<CreateItemDto> }) =>
160
+ itemApi.update(id, data),
161
+ onSuccess: (_, { id }) => {
162
+ qc.invalidateQueries({ queryKey: ['items'] });
163
+ qc.invalidateQueries({ queryKey: ['items', id] });
164
+ toast.success('Item updated');
165
+ },
166
+ });
167
+ }
168
+
169
+ export function useDeleteItem() {
170
+ const qc = useQueryClient();
171
+ return useMutation({
172
+ mutationFn: (id: string) => itemApi.delete(id),
173
+ onSuccess: () => {
174
+ qc.invalidateQueries({ queryKey: ['items'] });
175
+ toast.success('Item deleted');
176
+ },
76
177
  });
77
178
  }
78
179
  ```
79
180
 
80
- ## WebSocket Real-time
181
+ ### Step 5: Use Hooks in Components
182
+ ```tsx
183
+ // src/features/items/ItemListPage.tsx
184
+ import { useItems, useDeleteItem } from '@/hooks/useItems';
185
+
186
+ export function ItemListPage() {
187
+ const [page, setPage] = useState(1);
188
+ const { data, isLoading, error } = useItems({ page });
189
+ const deleteItem = useDeleteItem();
190
+
191
+ if (isLoading) return <Skeleton />;
192
+ if (error) return <ErrorState message={error.message} />;
193
+
194
+ return (
195
+ <div>
196
+ <h1>Items</h1>
197
+ {data?.items.map((item) => (
198
+ <div key={item.id} className="flex justify-between p-4 border-b">
199
+ <div>
200
+ <h3>{item.title}</h3>
201
+ <p>{item.description}</p>
202
+ </div>
203
+ <button onClick={() => deleteItem.mutate(item.id)}>Delete</button>
204
+ </div>
205
+ ))}
206
+ <Pagination page={page} total={data?.total ?? 0} onChange={setPage} />
207
+ </div>
208
+ );
209
+ }
210
+ ```
81
211
 
212
+ ### Step 6: Add WebSocket for Real-Time Updates
82
213
  ```typescript
83
- // shared/hooks/useWebSocket.ts
84
- import { io, Socket } from 'socket.io-client';
214
+ // src/hooks/useWebSocket.ts
215
+ import { useEffect } from 'react';
216
+ import { io, type Socket } from 'socket.io-client';
85
217
 
86
218
  let socket: Socket | null = null;
87
219
 
88
220
  export function useAgentEvents(onEvent: (event: TrackingEvent) => void) {
89
221
  useEffect(() => {
90
- socket = io(import.meta.env.VITE_WS_URL);
222
+ socket = io(import.meta.env.VITE_WS_URL || 'http://localhost:8000');
91
223
  socket.on('tracking:event', onEvent);
92
- return () => { socket?.disconnect(); };
224
+
225
+ return () => {
226
+ socket?.disconnect();
227
+ socket = null;
228
+ };
93
229
  }, [onEvent]);
94
230
  }
231
+
232
+ // Usage:
233
+ // useAgentEvents((event) => {
234
+ // console.log('New event:', event);
235
+ // queryClient.invalidateQueries({ queryKey: ['items'] });
236
+ // });
237
+ ```
238
+
239
+ ### Step 7: Verify Integration
240
+ ```bash
241
+ # Start backend
242
+ uvicorn src.main:app --reload --port 8000
243
+
244
+ # Start frontend
245
+ npm run dev
246
+
247
+ # Verify in browser:
248
+ # 1. Open http://localhost:5173
249
+ # 2. Open DevTools Network tab
250
+ # 3. Check API calls are going to correct URL
251
+ # 4. Check Authorization header is present
252
+ # 5. Verify data loads and displays correctly
253
+ # 6. Test create/update/delete operations
254
+ # 7. Check error handling (stop backend, verify error states)
95
255
  ```
256
+
257
+ ## Resources
258
+ - `references/api-patterns.md` - REST API integration patterns and conventions
259
+
260
+ ## Examples
261
+ ### Example 1: Connect to a New API Endpoint
262
+ User asks: "Add API integration for the orders feature"
263
+ Response approach:
264
+ 1. Create `orderApi.ts` service with typed endpoints
265
+ 2. Create `useOrders()`, `useCreateOrder()`, `useUpdateOrder()` hooks
266
+ 3. Add toast notifications for mutations
267
+ 4. Show usage in an `OrderListPage` component
268
+ 5. Verify with `npm run dev`
269
+
270
+ ### Example 2: Add Real-Time Updates
271
+ User asks: "Show live updates when agents complete tasks"
272
+ Response approach:
273
+ 1. Set up Socket.IO connection with `useAgentEvents` hook
274
+ 2. Listen for `task:completed` events
275
+ 3. Invalidate relevant React Query caches on events
276
+ 4. Show a toast notification for each completed task
277
+ 5. Test by triggering events from the backend
278
+
279
+ ## Notes
280
+ - Always type API request/response DTOs with TypeScript interfaces
281
+ - Use React Query for all server state -- never store API data in useState or Zustand
282
+ - Add error handling to every mutation (onError with toast)
283
+ - Set up interceptors once in api.ts -- do not add auth headers in individual calls
284
+ - Use `enabled: !!id` to prevent queries with empty/null parameters
285
+ - Keep the service layer thin: just API call definitions, no business logic
@@ -0,0 +1,81 @@
1
+ # REST API Integration Patterns
2
+
3
+ ## Service Layer Structure
4
+ ```
5
+ src/
6
+ services/
7
+ itemApi.ts # API endpoint definitions
8
+ hooks/
9
+ useItems.ts # React Query hooks
10
+ lib/
11
+ api.ts # Axios instance + interceptors
12
+ types/
13
+ item.ts # TypeScript interfaces
14
+ ```
15
+
16
+ ## Naming Conventions
17
+ | API Method | Service Function | Hook Name |
18
+ |---|---|---|
19
+ | GET /items | `itemApi.list()` | `useItems()` |
20
+ | GET /items/:id | `itemApi.get(id)` | `useItem(id)` |
21
+ | POST /items | `itemApi.create(data)` | `useCreateItem()` |
22
+ | PUT /items/:id | `itemApi.update(id, data)` | `useUpdateItem()` |
23
+ | DELETE /items/:id | `itemApi.delete(id)` | `useDeleteItem()` |
24
+
25
+ ## Error Handling Pattern
26
+ ```typescript
27
+ // In mutation hook
28
+ useMutation({
29
+ mutationFn: api.create,
30
+ onSuccess: (data) => {
31
+ qc.invalidateQueries({ queryKey: ['items'] });
32
+ toast.success('Created successfully');
33
+ navigate(`/items/${data.id}`);
34
+ },
35
+ onError: (error: ApiError) => {
36
+ if (error.status === 422) {
37
+ toast.error('Validation failed: ' + error.detail);
38
+ } else {
39
+ toast.error('Something went wrong');
40
+ }
41
+ },
42
+ });
43
+ ```
44
+
45
+ ## Pagination Pattern
46
+ ```typescript
47
+ // Hook
48
+ function useItems(page: number, size = 20) {
49
+ return useQuery({
50
+ queryKey: ['items', { page, size }],
51
+ queryFn: () => itemApi.list({ page, size }),
52
+ keepPreviousData: true, // smooth pagination
53
+ });
54
+ }
55
+
56
+ // Component
57
+ const [page, setPage] = useState(1);
58
+ const { data } = useItems(page);
59
+ ```
60
+
61
+ ## Optimistic Update Pattern
62
+ ```typescript
63
+ useMutation({
64
+ mutationFn: api.update,
65
+ onMutate: async (updated) => {
66
+ await qc.cancelQueries({ queryKey: ['items'] });
67
+ const previous = qc.getQueryData(['items']);
68
+ qc.setQueryData(['items'], (old) =>
69
+ old?.map(item => item.id === updated.id ? { ...item, ...updated } : item)
70
+ );
71
+ return { previous };
72
+ },
73
+ onError: (_, __, context) => {
74
+ qc.setQueryData(['items'], context?.previous);
75
+ toast.error('Update failed, reverting changes');
76
+ },
77
+ onSettled: () => {
78
+ qc.invalidateQueries({ queryKey: ['items'] });
79
+ },
80
+ });
81
+ ```