create-huenei-template 0.2.3 → 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/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
package/dist/templates/frontend/angular-base/.cursor/skills/angular-http/references/http-patterns.md
ADDED
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
# Angular HTTP Patterns
|
|
2
|
+
|
|
3
|
+
## Table of Contents
|
|
4
|
+
- [Service Layer Pattern](#service-layer-pattern)
|
|
5
|
+
- [Caching Strategies](#caching-strategies)
|
|
6
|
+
- [Pagination](#pagination)
|
|
7
|
+
- [File Upload](#file-upload)
|
|
8
|
+
- [Request Cancellation](#request-cancellation)
|
|
9
|
+
- [Testing HTTP](#testing-http)
|
|
10
|
+
|
|
11
|
+
## Service Layer Pattern
|
|
12
|
+
|
|
13
|
+
Encapsulate HTTP logic in services:
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { Injectable, inject, signal, computed } from '@angular/core';
|
|
17
|
+
import { HttpClient } from '@angular/common/http';
|
|
18
|
+
import { httpResource } from '@angular/common/http';
|
|
19
|
+
|
|
20
|
+
export interface User {
|
|
21
|
+
id: string;
|
|
22
|
+
name: string;
|
|
23
|
+
email: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
@Injectable({ providedIn: 'root' })
|
|
27
|
+
export class User {
|
|
28
|
+
private http = inject(HttpClient);
|
|
29
|
+
private baseUrl = '/api/users';
|
|
30
|
+
|
|
31
|
+
// Current user ID for reactive fetching
|
|
32
|
+
private currentUserId = signal<string | null>(null);
|
|
33
|
+
|
|
34
|
+
// Reactive resource that updates when currentUserId changes
|
|
35
|
+
currentUser = httpResource<User>(() => {
|
|
36
|
+
const id = this.currentUserId();
|
|
37
|
+
return id ? `${this.baseUrl}/${id}` : undefined;
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// Set current user to fetch
|
|
41
|
+
selectUser(id: string) {
|
|
42
|
+
this.currentUserId.set(id);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// CRUD operations
|
|
46
|
+
getAll() {
|
|
47
|
+
return this.http.get<User[]>(this.baseUrl);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
getById(id: string) {
|
|
51
|
+
return this.http.get<User>(`${this.baseUrl}/${id}`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
create(user: Omit<User, 'id'>) {
|
|
55
|
+
return this.http.post<User>(this.baseUrl, user);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
update(id: string, user: Partial<User>) {
|
|
59
|
+
return this.http.patch<User>(`${this.baseUrl}/${id}`, user);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
delete(id: string) {
|
|
63
|
+
return this.http.delete<void>(`${this.baseUrl}/${id}`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Caching Strategies
|
|
69
|
+
|
|
70
|
+
### Simple In-Memory Cache
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
@Injectable({ providedIn: 'root' })
|
|
74
|
+
export class CachedUser {
|
|
75
|
+
private http = inject(HttpClient);
|
|
76
|
+
private cache = new Map<string, { data: User; timestamp: number }>();
|
|
77
|
+
private cacheDuration = 5 * 60 * 1000; // 5 minutes
|
|
78
|
+
|
|
79
|
+
getUser(id: string): Observable<User> {
|
|
80
|
+
const cached = this.cache.get(id);
|
|
81
|
+
|
|
82
|
+
if (cached && Date.now() - cached.timestamp < this.cacheDuration) {
|
|
83
|
+
return of(cached.data);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return this.http.get<User>(`/api/users/${id}`).pipe(
|
|
87
|
+
tap(user => {
|
|
88
|
+
this.cache.set(id, { data: user, timestamp: Date.now() });
|
|
89
|
+
})
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
invalidateCache(id?: string) {
|
|
94
|
+
if (id) {
|
|
95
|
+
this.cache.delete(id);
|
|
96
|
+
} else {
|
|
97
|
+
this.cache.clear();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Signal-Based Cache
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
@Injectable({ providedIn: 'root' })
|
|
107
|
+
export class UserCache {
|
|
108
|
+
private http = inject(HttpClient);
|
|
109
|
+
|
|
110
|
+
// Cache as signal
|
|
111
|
+
private usersCache = signal<Map<string, User>>(new Map());
|
|
112
|
+
|
|
113
|
+
// Computed for easy access
|
|
114
|
+
users = computed(() => Array.from(this.usersCache().values()));
|
|
115
|
+
|
|
116
|
+
getUser(id: string): User | undefined {
|
|
117
|
+
return this.usersCache().get(id);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async fetchUser(id: string): Promise<User> {
|
|
121
|
+
const cached = this.getUser(id);
|
|
122
|
+
if (cached) return cached;
|
|
123
|
+
|
|
124
|
+
const user = await firstValueFrom(
|
|
125
|
+
this.http.get<User>(`/api/users/${id}`)
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
this.usersCache.update(cache => {
|
|
129
|
+
const newCache = new Map(cache);
|
|
130
|
+
newCache.set(id, user);
|
|
131
|
+
return newCache;
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
return user;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Pagination
|
|
140
|
+
|
|
141
|
+
### Paginated Resource
|
|
142
|
+
|
|
143
|
+
```typescript
|
|
144
|
+
interface PaginatedResponse<T> {
|
|
145
|
+
data: T[];
|
|
146
|
+
total: number;
|
|
147
|
+
page: number;
|
|
148
|
+
pageSize: number;
|
|
149
|
+
totalPages: number;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
@Component({
|
|
153
|
+
template: `
|
|
154
|
+
@if (usersResource.isLoading()) {
|
|
155
|
+
<app-spinner />
|
|
156
|
+
} @else if (usersResource.hasValue()) {
|
|
157
|
+
<ul>
|
|
158
|
+
@for (user of usersResource.value().data; track user.id) {
|
|
159
|
+
<li>{{ user.name }}</li>
|
|
160
|
+
}
|
|
161
|
+
</ul>
|
|
162
|
+
|
|
163
|
+
<div class="pagination">
|
|
164
|
+
<button
|
|
165
|
+
(click)="prevPage()"
|
|
166
|
+
[disabled]="page() === 1"
|
|
167
|
+
>Previous</button>
|
|
168
|
+
|
|
169
|
+
<span>Page {{ page() }} of {{ usersResource.value().totalPages }}</span>
|
|
170
|
+
|
|
171
|
+
<button
|
|
172
|
+
(click)="nextPage()"
|
|
173
|
+
[disabled]="page() >= usersResource.value().totalPages"
|
|
174
|
+
>Next</button>
|
|
175
|
+
</div>
|
|
176
|
+
}
|
|
177
|
+
`,
|
|
178
|
+
})
|
|
179
|
+
export class UsersList {
|
|
180
|
+
page = signal(1);
|
|
181
|
+
pageSize = signal(10);
|
|
182
|
+
|
|
183
|
+
usersResource = httpResource<PaginatedResponse<User>>(() => ({
|
|
184
|
+
url: '/api/users',
|
|
185
|
+
params: {
|
|
186
|
+
page: this.page().toString(),
|
|
187
|
+
pageSize: this.pageSize().toString(),
|
|
188
|
+
},
|
|
189
|
+
}));
|
|
190
|
+
|
|
191
|
+
nextPage() {
|
|
192
|
+
this.page.update(p => p + 1);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
prevPage() {
|
|
196
|
+
this.page.update(p => Math.max(1, p - 1));
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
### Infinite Scroll
|
|
202
|
+
|
|
203
|
+
```typescript
|
|
204
|
+
@Component({
|
|
205
|
+
template: `
|
|
206
|
+
<ul>
|
|
207
|
+
@for (user of allUsers(); track user.id) {
|
|
208
|
+
<li>{{ user.name }}</li>
|
|
209
|
+
}
|
|
210
|
+
</ul>
|
|
211
|
+
|
|
212
|
+
@if (isLoading()) {
|
|
213
|
+
<app-spinner />
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
@if (hasMore()) {
|
|
217
|
+
<button (click)="loadMore()">Load More</button>
|
|
218
|
+
}
|
|
219
|
+
`,
|
|
220
|
+
})
|
|
221
|
+
export class InfiniteUsers {
|
|
222
|
+
private http = inject(HttpClient);
|
|
223
|
+
|
|
224
|
+
private page = signal(1);
|
|
225
|
+
private users = signal<User[]>([]);
|
|
226
|
+
private totalPages = signal(1);
|
|
227
|
+
|
|
228
|
+
allUsers = this.users.asReadonly();
|
|
229
|
+
isLoading = signal(false);
|
|
230
|
+
hasMore = computed(() => this.page() < this.totalPages());
|
|
231
|
+
|
|
232
|
+
constructor() {
|
|
233
|
+
this.loadPage(1);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
loadMore() {
|
|
237
|
+
this.loadPage(this.page() + 1);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
private async loadPage(page: number) {
|
|
241
|
+
this.isLoading.set(true);
|
|
242
|
+
|
|
243
|
+
try {
|
|
244
|
+
const response = await firstValueFrom(
|
|
245
|
+
this.http.get<PaginatedResponse<User>>('/api/users', {
|
|
246
|
+
params: { page: page.toString(), pageSize: '20' },
|
|
247
|
+
})
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
this.users.update(users => [...users, ...response.data]);
|
|
251
|
+
this.page.set(page);
|
|
252
|
+
this.totalPages.set(response.totalPages);
|
|
253
|
+
} finally {
|
|
254
|
+
this.isLoading.set(false);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
## File Upload
|
|
261
|
+
|
|
262
|
+
### Single File Upload
|
|
263
|
+
|
|
264
|
+
```typescript
|
|
265
|
+
@Component({
|
|
266
|
+
template: `
|
|
267
|
+
<input type="file" (change)="onFileSelected($event)" />
|
|
268
|
+
|
|
269
|
+
@if (uploadProgress() !== null) {
|
|
270
|
+
<progress [value]="uploadProgress()" max="100"></progress>
|
|
271
|
+
}
|
|
272
|
+
`,
|
|
273
|
+
})
|
|
274
|
+
export class FileUpload {
|
|
275
|
+
private http = inject(HttpClient);
|
|
276
|
+
|
|
277
|
+
uploadProgress = signal<number | null>(null);
|
|
278
|
+
|
|
279
|
+
onFileSelected(event: Event) {
|
|
280
|
+
const file = (event.target as HTMLInputElement).files?.[0];
|
|
281
|
+
if (!file) return;
|
|
282
|
+
|
|
283
|
+
const formData = new FormData();
|
|
284
|
+
formData.append('file', file);
|
|
285
|
+
|
|
286
|
+
this.http.post('/api/upload', formData, {
|
|
287
|
+
reportProgress: true,
|
|
288
|
+
observe: 'events',
|
|
289
|
+
}).subscribe(event => {
|
|
290
|
+
if (event.type === HttpEventType.UploadProgress && event.total) {
|
|
291
|
+
this.uploadProgress.set(Math.round(100 * event.loaded / event.total));
|
|
292
|
+
} else if (event.type === HttpEventType.Response) {
|
|
293
|
+
this.uploadProgress.set(null);
|
|
294
|
+
console.log('Upload complete:', event.body);
|
|
295
|
+
}
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
### Multiple Files
|
|
302
|
+
|
|
303
|
+
```typescript
|
|
304
|
+
uploadFiles(files: FileList) {
|
|
305
|
+
const formData = new FormData();
|
|
306
|
+
|
|
307
|
+
for (let i = 0; i < files.length; i++) {
|
|
308
|
+
formData.append('files', files[i]);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
return this.http.post<{ urls: string[] }>('/api/upload-multiple', formData);
|
|
312
|
+
}
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
## Request Cancellation
|
|
316
|
+
|
|
317
|
+
### With resource()
|
|
318
|
+
|
|
319
|
+
```typescript
|
|
320
|
+
// resource() automatically handles cancellation via abortSignal
|
|
321
|
+
searchResource = resource({
|
|
322
|
+
params: () => ({ q: this.query() }),
|
|
323
|
+
loader: async ({ params, abortSignal }) => {
|
|
324
|
+
const response = await fetch(`/api/search?q=${params.q}`, {
|
|
325
|
+
signal: abortSignal, // Cancels if params change
|
|
326
|
+
});
|
|
327
|
+
return response.json();
|
|
328
|
+
},
|
|
329
|
+
});
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
### With HttpClient
|
|
333
|
+
|
|
334
|
+
```typescript
|
|
335
|
+
@Component({...})
|
|
336
|
+
export class Search implements OnDestroy {
|
|
337
|
+
private http = inject(HttpClient);
|
|
338
|
+
private destroyRef = inject(DestroyRef);
|
|
339
|
+
|
|
340
|
+
query = signal('');
|
|
341
|
+
results = signal<Result[]>([]);
|
|
342
|
+
|
|
343
|
+
private searchSubscription?: Subscription;
|
|
344
|
+
|
|
345
|
+
search() {
|
|
346
|
+
// Cancel previous request
|
|
347
|
+
this.searchSubscription?.unsubscribe();
|
|
348
|
+
|
|
349
|
+
this.searchSubscription = this.http
|
|
350
|
+
.get<Result[]>(`/api/search?q=${this.query()}`)
|
|
351
|
+
.pipe(takeUntilDestroyed(this.destroyRef))
|
|
352
|
+
.subscribe(results => this.results.set(results));
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
### Debounced Search
|
|
358
|
+
|
|
359
|
+
```typescript
|
|
360
|
+
@Component({...})
|
|
361
|
+
export class SearchDebounced {
|
|
362
|
+
query = signal('');
|
|
363
|
+
|
|
364
|
+
private http = inject(HttpClient);
|
|
365
|
+
|
|
366
|
+
results = toSignal(
|
|
367
|
+
toObservable(this.query).pipe(
|
|
368
|
+
debounceTime(300),
|
|
369
|
+
distinctUntilChanged(),
|
|
370
|
+
filter(q => q.length >= 2),
|
|
371
|
+
switchMap(q => this.http.get<Result[]>(`/api/search?q=${q}`)),
|
|
372
|
+
catchError(() => of([]))
|
|
373
|
+
),
|
|
374
|
+
{ initialValue: [] }
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
## Testing HTTP
|
|
380
|
+
|
|
381
|
+
### Testing httpResource
|
|
382
|
+
|
|
383
|
+
```typescript
|
|
384
|
+
describe('UserCmpt', () => {
|
|
385
|
+
let component: UserCmpt;
|
|
386
|
+
let httpMock: HttpTestingController;
|
|
387
|
+
|
|
388
|
+
beforeEach(() => {
|
|
389
|
+
TestBed.configureTestingModule({
|
|
390
|
+
imports: [UserCmpt],
|
|
391
|
+
providers: [provideHttpClientTesting()],
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
component = TestBed.createComponent(UserCmpt).componentInstance;
|
|
395
|
+
httpMock = TestBed.inject(HttpTestingController);
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
it('should load user', () => {
|
|
399
|
+
component.userId.set('123');
|
|
400
|
+
|
|
401
|
+
const req = httpMock.expectOne('/api/users/123');
|
|
402
|
+
req.flush({ id: '123', name: 'Test User' });
|
|
403
|
+
|
|
404
|
+
expect(component.userResource.value()?.name).toBe('Test User');
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
afterEach(() => {
|
|
408
|
+
httpMock.verify();
|
|
409
|
+
});
|
|
410
|
+
});
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
### Testing Services
|
|
414
|
+
|
|
415
|
+
```typescript
|
|
416
|
+
describe('User', () => {
|
|
417
|
+
let service: User;
|
|
418
|
+
let httpMock: HttpTestingController;
|
|
419
|
+
|
|
420
|
+
beforeEach(() => {
|
|
421
|
+
TestBed.configureTestingModule({
|
|
422
|
+
providers: [
|
|
423
|
+
User,
|
|
424
|
+
provideHttpClient(),
|
|
425
|
+
provideHttpClientTesting(),
|
|
426
|
+
],
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
service = TestBed.inject(User);
|
|
430
|
+
httpMock = TestBed.inject(HttpTestingController);
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
it('should create user', () => {
|
|
434
|
+
const newUser = { name: 'Test', email: 'test@example.com' };
|
|
435
|
+
|
|
436
|
+
service.create(newUser).subscribe(user => {
|
|
437
|
+
expect(user.id).toBeDefined();
|
|
438
|
+
expect(user.name).toBe('Test');
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
const req = httpMock.expectOne('/api/users');
|
|
442
|
+
expect(req.request.method).toBe('POST');
|
|
443
|
+
expect(req.request.body).toEqual(newUser);
|
|
444
|
+
|
|
445
|
+
req.flush({ id: '1', ...newUser });
|
|
446
|
+
});
|
|
447
|
+
});
|
|
448
|
+
```
|