create-huenei-template 0.2.2 → 0.2.4
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/dist/config/templates.js +3 -3
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-component/SKILL.md +288 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-component/references/component-patterns.md +358 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-di/SKILL.md +387 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-di/references/di-patterns.md +519 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-directives/SKILL.md +437 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-directives/references/directive-patterns.md +570 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-forms/SKILL.md +434 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-forms/references/form-patterns.md +398 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-http/SKILL.md +366 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-http/references/http-patterns.md +448 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-routing/SKILL.md +395 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-routing/references/routing-patterns.md +472 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-signals/SKILL.md +302 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-signals/references/signal-patterns.md +404 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-ssr/SKILL.md +435 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-ssr/references/ssr-patterns.md +497 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-testing/SKILL.md +660 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-testing/references/testing-patterns.md +707 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-tooling/SKILL.md +352 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/angular-tooling/references/tooling-patterns.md +448 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-accessibility/SKILL.md +780 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-components/SKILL.md +718 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-design-systems/SKILL.md +612 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-layouts/SKILL.md +594 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/SKILL.md +432 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/references/animation-patterns.md +427 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/SKILL.md +354 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/references/troubleshooting-guide.md +306 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/SKILL.md +578 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/references/react-patterns.md +353 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/SKILL.md +608 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/advanced-theme-patterns.md +215 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/custom-utilities-guide.md +281 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-mobile-first/SKILL.md +768 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/SKILL.md +374 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/references/bundle-optimization.md +244 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-plugins/SKILL.md +505 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/SKILL.md +601 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/references/advanced-responsive-patterns.md +485 -0
- package/dist/templates/frontend/angular-base/.cursor/skills/web-design-guidelines/SKILL.md +39 -0
- package/dist/templates/frontend/angular-base/package.json +0 -1
- package/dist/templates/frontend/angular-base/pnpm-lock.yaml +8183 -0
- package/package.json +1 -1
- package/templates/frontend/angular-base/.cursor/skills/angular-component/SKILL.md +288 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-component/references/component-patterns.md +358 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-di/SKILL.md +387 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-di/references/di-patterns.md +519 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-directives/SKILL.md +437 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-directives/references/directive-patterns.md +570 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-forms/SKILL.md +434 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-forms/references/form-patterns.md +398 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-http/SKILL.md +366 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-http/references/http-patterns.md +448 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-routing/SKILL.md +395 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-routing/references/routing-patterns.md +472 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-signals/SKILL.md +302 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-signals/references/signal-patterns.md +404 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-ssr/SKILL.md +435 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-ssr/references/ssr-patterns.md +497 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-testing/SKILL.md +660 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-testing/references/testing-patterns.md +707 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-tooling/SKILL.md +352 -0
- package/templates/frontend/angular-base/.cursor/skills/angular-tooling/references/tooling-patterns.md +448 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-accessibility/SKILL.md +780 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-components/SKILL.md +718 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-design-systems/SKILL.md +612 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-advanced-layouts/SKILL.md +594 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/SKILL.md +432 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-animations/references/animation-patterns.md +427 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/SKILL.md +354 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-debugging/references/troubleshooting-guide.md +306 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/SKILL.md +578 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-framework-integration/references/react-patterns.md +353 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/SKILL.md +608 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/advanced-theme-patterns.md +215 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-fundamentals-v4/references/custom-utilities-guide.md +281 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-mobile-first/SKILL.md +768 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/SKILL.md +374 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-performance/references/bundle-optimization.md +244 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-plugins/SKILL.md +505 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/SKILL.md +601 -0
- package/templates/frontend/angular-base/.cursor/skills/tailwindcss-responsive-darkmode/references/advanced-responsive-patterns.md +485 -0
- package/templates/frontend/angular-base/.cursor/skills/web-design-guidelines/SKILL.md +39 -0
- package/templates/frontend/angular-base/package.json +0 -1
- package/templates/frontend/angular-base/pnpm-lock.yaml +8183 -0
- package/templates/frontend/angular-base/package-lock.json +0 -13321
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: angular-http
|
|
3
|
+
description: Implement HTTP data fetching in Angular v20+ using resource(), httpResource(), and HttpClient. Use for API calls, data loading with signals, request/response handling, and interceptors. Triggers on data fetching, API integration, loading states, error handling, or converting Observable-based HTTP to signal-based patterns.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Angular HTTP & Data Fetching
|
|
7
|
+
|
|
8
|
+
Fetch data in Angular using signal-based `resource()`, `httpResource()`, and the traditional `HttpClient`.
|
|
9
|
+
|
|
10
|
+
## httpResource() - Signal-Based HTTP
|
|
11
|
+
|
|
12
|
+
`httpResource()` wraps HttpClient with signal-based state management:
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
import { Component, signal } from '@angular/core';
|
|
16
|
+
import { httpResource } from '@angular/common/http';
|
|
17
|
+
|
|
18
|
+
interface User {
|
|
19
|
+
id: number;
|
|
20
|
+
name: string;
|
|
21
|
+
email: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
@Component({
|
|
25
|
+
selector: 'app-user-profile',
|
|
26
|
+
template: `
|
|
27
|
+
@if (userResource.isLoading()) {
|
|
28
|
+
<p>Loading...</p>
|
|
29
|
+
} @else if (userResource.error()) {
|
|
30
|
+
<p>Error: {{ userResource.error()?.message }}</p>
|
|
31
|
+
<button (click)="userResource.reload()">Retry</button>
|
|
32
|
+
} @else if (userResource.hasValue()) {
|
|
33
|
+
<h1>{{ userResource.value().name }}</h1>
|
|
34
|
+
<p>{{ userResource.value().email }}</p>
|
|
35
|
+
}
|
|
36
|
+
`,
|
|
37
|
+
})
|
|
38
|
+
export class UserProfile {
|
|
39
|
+
userId = signal('123');
|
|
40
|
+
|
|
41
|
+
// Reactive HTTP resource - refetches when userId changes
|
|
42
|
+
userResource = httpResource<User>(() => `/api/users/${this.userId()}`);
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### httpResource Options
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
// Simple GET request
|
|
50
|
+
userResource = httpResource<User>(() => `/api/users/${this.userId()}`);
|
|
51
|
+
|
|
52
|
+
// With full request options
|
|
53
|
+
userResource = httpResource<User>(() => ({
|
|
54
|
+
url: `/api/users/${this.userId()}`,
|
|
55
|
+
method: 'GET',
|
|
56
|
+
headers: { 'Authorization': `Bearer ${this.token()}` },
|
|
57
|
+
params: { include: 'profile' },
|
|
58
|
+
}));
|
|
59
|
+
|
|
60
|
+
// With default value
|
|
61
|
+
usersResource = httpResource<User[]>(() => '/api/users', {
|
|
62
|
+
defaultValue: [],
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// Skip request when params undefined
|
|
66
|
+
userResource = httpResource<User>(() => {
|
|
67
|
+
const id = this.userId();
|
|
68
|
+
return id ? `/api/users/${id}` : undefined;
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Resource State
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
// Status signals
|
|
76
|
+
userResource.value() // Current value or undefined
|
|
77
|
+
userResource.hasValue() // Boolean - has resolved value
|
|
78
|
+
userResource.error() // Error or undefined
|
|
79
|
+
userResource.isLoading() // Boolean - currently loading
|
|
80
|
+
userResource.status() // 'idle' | 'loading' | 'reloading' | 'resolved' | 'error' | 'local'
|
|
81
|
+
|
|
82
|
+
// Actions
|
|
83
|
+
userResource.reload() // Manually trigger reload
|
|
84
|
+
userResource.set(value) // Set local value
|
|
85
|
+
userResource.update(fn) // Update local value
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## resource() - Generic Async Data
|
|
89
|
+
|
|
90
|
+
For non-HTTP async operations or custom fetch logic:
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
import { resource, signal } from '@angular/core';
|
|
94
|
+
|
|
95
|
+
@Component({...})
|
|
96
|
+
export class Search {
|
|
97
|
+
query = signal('');
|
|
98
|
+
|
|
99
|
+
searchResource = resource({
|
|
100
|
+
// Reactive params - triggers reload when changed
|
|
101
|
+
params: () => ({ q: this.query() }),
|
|
102
|
+
|
|
103
|
+
// Async loader function
|
|
104
|
+
loader: async ({ params, abortSignal }) => {
|
|
105
|
+
if (!params.q) return [];
|
|
106
|
+
|
|
107
|
+
const response = await fetch(`/api/search?q=${params.q}`, {
|
|
108
|
+
signal: abortSignal,
|
|
109
|
+
});
|
|
110
|
+
return response.json() as Promise<SearchResult[]>;
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Resource with Default Value
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
todosResource = resource({
|
|
120
|
+
defaultValue: [] as Todo[],
|
|
121
|
+
params: () => ({ filter: this.filter() }),
|
|
122
|
+
loader: async ({ params }) => {
|
|
123
|
+
const res = await fetch(`/api/todos?filter=${params.filter}`);
|
|
124
|
+
return res.json();
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
// value() returns Todo[] (never undefined)
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Conditional Loading
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
const userId = signal<string | null>(null);
|
|
135
|
+
|
|
136
|
+
userResource = resource({
|
|
137
|
+
params: () => {
|
|
138
|
+
const id = userId();
|
|
139
|
+
// Return undefined to skip loading
|
|
140
|
+
return id ? { id } : undefined;
|
|
141
|
+
},
|
|
142
|
+
loader: async ({ params }) => {
|
|
143
|
+
return fetch(`/api/users/${params.id}`).then(r => r.json());
|
|
144
|
+
},
|
|
145
|
+
});
|
|
146
|
+
// Status is 'idle' when params returns undefined
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## HttpClient - Traditional Approach
|
|
150
|
+
|
|
151
|
+
For complex scenarios or when you need Observable operators:
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
import { Component, inject } from '@angular/core';
|
|
155
|
+
import { HttpClient } from '@angular/common/http';
|
|
156
|
+
import { toSignal } from '@angular/core/rxjs-interop';
|
|
157
|
+
|
|
158
|
+
@Component({...})
|
|
159
|
+
export class Users {
|
|
160
|
+
private http = inject(HttpClient);
|
|
161
|
+
|
|
162
|
+
// Convert Observable to Signal
|
|
163
|
+
users = toSignal(
|
|
164
|
+
this.http.get<User[]>('/api/users'),
|
|
165
|
+
{ initialValue: [] }
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
// Or use Observable directly
|
|
169
|
+
users$ = this.http.get<User[]>('/api/users');
|
|
170
|
+
}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### HTTP Methods
|
|
174
|
+
|
|
175
|
+
```typescript
|
|
176
|
+
private http = inject(HttpClient);
|
|
177
|
+
|
|
178
|
+
// GET
|
|
179
|
+
getUser(id: string) {
|
|
180
|
+
return this.http.get<User>(`/api/users/${id}`);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// POST
|
|
184
|
+
createUser(user: CreateUserDto) {
|
|
185
|
+
return this.http.post<User>('/api/users', user);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// PUT
|
|
189
|
+
updateUser(id: string, user: UpdateUserDto) {
|
|
190
|
+
return this.http.put<User>(`/api/users/${id}`, user);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// PATCH
|
|
194
|
+
patchUser(id: string, changes: Partial<User>) {
|
|
195
|
+
return this.http.patch<User>(`/api/users/${id}`, changes);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// DELETE
|
|
199
|
+
deleteUser(id: string) {
|
|
200
|
+
return this.http.delete<void>(`/api/users/${id}`);
|
|
201
|
+
}
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
### Request Options
|
|
205
|
+
|
|
206
|
+
```typescript
|
|
207
|
+
this.http.get<User[]>('/api/users', {
|
|
208
|
+
headers: {
|
|
209
|
+
'Authorization': 'Bearer token',
|
|
210
|
+
'Content-Type': 'application/json',
|
|
211
|
+
},
|
|
212
|
+
params: {
|
|
213
|
+
page: '1',
|
|
214
|
+
limit: '10',
|
|
215
|
+
sort: 'name',
|
|
216
|
+
},
|
|
217
|
+
observe: 'response', // Get full HttpResponse
|
|
218
|
+
responseType: 'json',
|
|
219
|
+
});
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## Interceptors
|
|
223
|
+
|
|
224
|
+
### Functional Interceptor (Recommended)
|
|
225
|
+
|
|
226
|
+
```typescript
|
|
227
|
+
// auth.interceptor.ts
|
|
228
|
+
import { HttpInterceptorFn } from '@angular/common/http';
|
|
229
|
+
import { inject } from '@angular/core';
|
|
230
|
+
|
|
231
|
+
export const authInterceptor: HttpInterceptorFn = (req, next) => {
|
|
232
|
+
const authService = inject(Auth);
|
|
233
|
+
const token = authService.token();
|
|
234
|
+
|
|
235
|
+
if (token) {
|
|
236
|
+
req = req.clone({
|
|
237
|
+
setHeaders: { Authorization: `Bearer ${token}` },
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return next(req);
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
// error.interceptor.ts
|
|
245
|
+
export const errorInterceptor: HttpInterceptorFn = (req, next) => {
|
|
246
|
+
return next(req).pipe(
|
|
247
|
+
catchError((error: HttpErrorResponse) => {
|
|
248
|
+
if (error.status === 401) {
|
|
249
|
+
inject(Router).navigate(['/login']);
|
|
250
|
+
}
|
|
251
|
+
return throwError(() => error);
|
|
252
|
+
})
|
|
253
|
+
);
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
// logging.interceptor.ts
|
|
257
|
+
export const loggingInterceptor: HttpInterceptorFn = (req, next) => {
|
|
258
|
+
const started = Date.now();
|
|
259
|
+
return next(req).pipe(
|
|
260
|
+
tap({
|
|
261
|
+
next: () => console.log(`${req.method} ${req.url} - ${Date.now() - started}ms`),
|
|
262
|
+
error: (err) => console.error(`${req.method} ${req.url} failed`, err),
|
|
263
|
+
})
|
|
264
|
+
);
|
|
265
|
+
};
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
### Register Interceptors
|
|
269
|
+
|
|
270
|
+
```typescript
|
|
271
|
+
// app.config.ts
|
|
272
|
+
import { provideHttpClient, withInterceptors } from '@angular/common/http';
|
|
273
|
+
|
|
274
|
+
export const appConfig: ApplicationConfig = {
|
|
275
|
+
providers: [
|
|
276
|
+
provideHttpClient(
|
|
277
|
+
withInterceptors([
|
|
278
|
+
authInterceptor,
|
|
279
|
+
errorInterceptor,
|
|
280
|
+
loggingInterceptor,
|
|
281
|
+
])
|
|
282
|
+
),
|
|
283
|
+
],
|
|
284
|
+
};
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
## Error Handling
|
|
288
|
+
|
|
289
|
+
### With httpResource
|
|
290
|
+
|
|
291
|
+
```typescript
|
|
292
|
+
@Component({
|
|
293
|
+
template: `
|
|
294
|
+
@if (userResource.error(); as error) {
|
|
295
|
+
<div class="error">
|
|
296
|
+
<p>{{ getErrorMessage(error) }}</p>
|
|
297
|
+
<button (click)="userResource.reload()">Retry</button>
|
|
298
|
+
</div>
|
|
299
|
+
}
|
|
300
|
+
`,
|
|
301
|
+
})
|
|
302
|
+
export class UserCmpt {
|
|
303
|
+
userResource = httpResource<User>(() => `/api/users/${this.userId()}`);
|
|
304
|
+
|
|
305
|
+
getErrorMessage(error: unknown): string {
|
|
306
|
+
if (error instanceof HttpErrorResponse) {
|
|
307
|
+
return error.error?.message || `Error ${error.status}: ${error.statusText}`;
|
|
308
|
+
}
|
|
309
|
+
return 'An unexpected error occurred';
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
### With HttpClient
|
|
315
|
+
|
|
316
|
+
```typescript
|
|
317
|
+
import { catchError, retry } from 'rxjs';
|
|
318
|
+
|
|
319
|
+
getUser(id: string) {
|
|
320
|
+
return this.http.get<User>(`/api/users/${id}`).pipe(
|
|
321
|
+
retry(2), // Retry up to 2 times
|
|
322
|
+
catchError((error: HttpErrorResponse) => {
|
|
323
|
+
console.error('Error fetching user:', error);
|
|
324
|
+
return throwError(() => new Error('Failed to load user'));
|
|
325
|
+
})
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
## Loading States Pattern
|
|
331
|
+
|
|
332
|
+
```typescript
|
|
333
|
+
@Component({
|
|
334
|
+
template: `
|
|
335
|
+
@switch (dataResource.status()) {
|
|
336
|
+
@case ('idle') {
|
|
337
|
+
<p>Enter a search term</p>
|
|
338
|
+
}
|
|
339
|
+
@case ('loading') {
|
|
340
|
+
<app-spinner />
|
|
341
|
+
}
|
|
342
|
+
@case ('reloading') {
|
|
343
|
+
<app-data [data]="dataResource.value()" />
|
|
344
|
+
<app-spinner size="small" />
|
|
345
|
+
}
|
|
346
|
+
@case ('resolved') {
|
|
347
|
+
<app-data [data]="dataResource.value()" />
|
|
348
|
+
}
|
|
349
|
+
@case ('error') {
|
|
350
|
+
<app-error
|
|
351
|
+
[error]="dataResource.error()"
|
|
352
|
+
(retry)="dataResource.reload()"
|
|
353
|
+
/>
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
`,
|
|
357
|
+
})
|
|
358
|
+
export class Data {
|
|
359
|
+
query = signal('');
|
|
360
|
+
dataResource = httpResource<Data[]>(() =>
|
|
361
|
+
this.query() ? `/api/search?q=${this.query()}` : undefined
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
For advanced patterns, see [references/http-patterns.md](references/http-patterns.md).
|