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
package/dist/templates/frontend/angular-base/.cursor/skills/angular-di/references/di-patterns.md
ADDED
|
@@ -0,0 +1,519 @@
|
|
|
1
|
+
# Angular Dependency Injection Patterns
|
|
2
|
+
|
|
3
|
+
## Table of Contents
|
|
4
|
+
- [Service Patterns](#service-patterns)
|
|
5
|
+
- [Abstract Classes as Tokens](#abstract-classes-as-tokens)
|
|
6
|
+
- [Hierarchical Injection](#hierarchical-injection)
|
|
7
|
+
- [Dynamic Providers](#dynamic-providers)
|
|
8
|
+
- [Testing with DI](#testing-with-di)
|
|
9
|
+
- [DestroyRef and Cleanup](#destroyref-and-cleanup)
|
|
10
|
+
|
|
11
|
+
## Service Patterns
|
|
12
|
+
|
|
13
|
+
### Facade Service
|
|
14
|
+
|
|
15
|
+
Combine multiple services into a single API:
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
@Injectable({ providedIn: 'root' })
|
|
19
|
+
export class ShopFacade {
|
|
20
|
+
private productService = inject(Product);
|
|
21
|
+
private cartService = inject(Cart);
|
|
22
|
+
private orderService = inject(Order);
|
|
23
|
+
|
|
24
|
+
// Expose combined state
|
|
25
|
+
readonly products = this.productService.products;
|
|
26
|
+
readonly cart = this.cartService.items;
|
|
27
|
+
readonly cartTotal = this.cartService.total;
|
|
28
|
+
|
|
29
|
+
// Unified actions
|
|
30
|
+
addToCart(productId: string, quantity: number) {
|
|
31
|
+
const product = this.productService.getById(productId);
|
|
32
|
+
if (product) {
|
|
33
|
+
this.cartService.add(product, quantity);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async checkout() {
|
|
38
|
+
const items = this.cartService.items();
|
|
39
|
+
const order = await this.orderService.create(items);
|
|
40
|
+
this.cartService.clear();
|
|
41
|
+
return order;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### State Service Pattern
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
interface UserState {
|
|
50
|
+
user: User | null;
|
|
51
|
+
loading: boolean;
|
|
52
|
+
error: string | null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
@Injectable({ providedIn: 'root' })
|
|
56
|
+
export class UserState {
|
|
57
|
+
private state = signal<UserState>({
|
|
58
|
+
user: null,
|
|
59
|
+
loading: false,
|
|
60
|
+
error: null,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// Selectors
|
|
64
|
+
readonly user = computed(() => this.state().user);
|
|
65
|
+
readonly loading = computed(() => this.state().loading);
|
|
66
|
+
readonly error = computed(() => this.state().error);
|
|
67
|
+
readonly isAuthenticated = computed(() => this.state().user !== null);
|
|
68
|
+
|
|
69
|
+
// Actions
|
|
70
|
+
setUser(user: User) {
|
|
71
|
+
this.state.update(s => ({ ...s, user, loading: false, error: null }));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
setLoading() {
|
|
75
|
+
this.state.update(s => ({ ...s, loading: true, error: null }));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
setError(error: string) {
|
|
79
|
+
this.state.update(s => ({ ...s, loading: false, error }));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
clear() {
|
|
83
|
+
this.state.set({ user: null, loading: false, error: null });
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Repository Pattern
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
// Generic repository interface
|
|
92
|
+
export abstract class Repository<T extends { id: string }> {
|
|
93
|
+
abstract getAll(): Promise<T[]>;
|
|
94
|
+
abstract getById(id: string): Promise<T | null>;
|
|
95
|
+
abstract create(item: Omit<T, 'id'>): Promise<T>;
|
|
96
|
+
abstract update(id: string, item: Partial<T>): Promise<T>;
|
|
97
|
+
abstract delete(id: string): Promise<void>;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// HTTP implementation
|
|
101
|
+
@Injectable()
|
|
102
|
+
export class HttpUserRepo extends Repository<User> {
|
|
103
|
+
private http = inject(HttpClient);
|
|
104
|
+
private apiUrl = inject(API_URL);
|
|
105
|
+
|
|
106
|
+
async getAll(): Promise<User[]> {
|
|
107
|
+
return firstValueFrom(this.http.get<User[]>(`${this.apiUrl}/users`));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async getById(id: string): Promise<User | null> {
|
|
111
|
+
return firstValueFrom(
|
|
112
|
+
this.http.get<User>(`${this.apiUrl}/users/${id}`).pipe(
|
|
113
|
+
catchError(() => of(null))
|
|
114
|
+
)
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async create(user: Omit<User, 'id'>): Promise<User> {
|
|
119
|
+
return firstValueFrom(this.http.post<User>(`${this.apiUrl}/users`, user));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async update(id: string, user: Partial<User>): Promise<User> {
|
|
123
|
+
return firstValueFrom(this.http.patch<User>(`${this.apiUrl}/users/${id}`, user));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async delete(id: string): Promise<void> {
|
|
127
|
+
await firstValueFrom(this.http.delete(`${this.apiUrl}/users/${id}`));
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Provide implementation
|
|
132
|
+
{ provide: Repository, useClass: HttpUserRepo }
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Abstract Classes as Tokens
|
|
136
|
+
|
|
137
|
+
Use abstract classes for better type safety:
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
// Abstract service definition
|
|
141
|
+
export abstract class Logger {
|
|
142
|
+
abstract log(message: string): void;
|
|
143
|
+
abstract error(message: string, error?: Error): void;
|
|
144
|
+
abstract warn(message: string): void;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Console implementation
|
|
148
|
+
@Injectable()
|
|
149
|
+
export class ConsoleLog extends Logger {
|
|
150
|
+
log(message: string) {
|
|
151
|
+
console.log(`[LOG] ${message}`);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
error(message: string, error?: Error) {
|
|
155
|
+
console.error(`[ERROR] ${message}`, error);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
warn(message: string) {
|
|
159
|
+
console.warn(`[WARN] ${message}`);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Remote implementation
|
|
164
|
+
@Injectable()
|
|
165
|
+
export class RemoteLog extends Logger {
|
|
166
|
+
private http = inject(HttpClient);
|
|
167
|
+
|
|
168
|
+
log(message: string) {
|
|
169
|
+
this.send('log', message);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
error(message: string, error?: Error) {
|
|
173
|
+
this.send('error', message, error);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
warn(message: string) {
|
|
177
|
+
this.send('warn', message);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
private send(level: string, message: string, error?: Error) {
|
|
181
|
+
this.http.post('/api/logs', { level, message, error: error?.message }).subscribe();
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Provide based on environment
|
|
186
|
+
{
|
|
187
|
+
provide: Logger,
|
|
188
|
+
useClass: environment.production ? RemoteLog : ConsoleLog,
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Inject using abstract class
|
|
192
|
+
@Injectable({ providedIn: 'root' })
|
|
193
|
+
export class User {
|
|
194
|
+
private logger = inject(Logger);
|
|
195
|
+
|
|
196
|
+
createUser(user: UserData) {
|
|
197
|
+
this.logger.log(`Creating user: ${user.email}`);
|
|
198
|
+
// ...
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
## Hierarchical Injection
|
|
204
|
+
|
|
205
|
+
### Component Tree Injection
|
|
206
|
+
|
|
207
|
+
```typescript
|
|
208
|
+
// Parent provides service
|
|
209
|
+
@Component({
|
|
210
|
+
selector: 'app-form-container',
|
|
211
|
+
providers: [FormState],
|
|
212
|
+
template: `
|
|
213
|
+
<app-form-header />
|
|
214
|
+
<app-form-body />
|
|
215
|
+
<app-form-footer />
|
|
216
|
+
`,
|
|
217
|
+
})
|
|
218
|
+
export class FormContainer {
|
|
219
|
+
private formState = inject(FormState);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// Children share same instance
|
|
223
|
+
@Component({
|
|
224
|
+
selector: 'app-form-body',
|
|
225
|
+
template: `...`,
|
|
226
|
+
})
|
|
227
|
+
export class FormBody {
|
|
228
|
+
// Gets same instance as parent
|
|
229
|
+
private formState = inject(FormState);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Grandchildren also share
|
|
233
|
+
@Component({
|
|
234
|
+
selector: 'app-form-field',
|
|
235
|
+
template: `...`,
|
|
236
|
+
})
|
|
237
|
+
export class FormField {
|
|
238
|
+
// Gets same instance from ancestor
|
|
239
|
+
private formState = inject(FormState);
|
|
240
|
+
}
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
### viewProviders vs providers
|
|
244
|
+
|
|
245
|
+
```typescript
|
|
246
|
+
@Component({
|
|
247
|
+
selector: 'app-tabs',
|
|
248
|
+
// providers: Available to component AND content children
|
|
249
|
+
providers: [TabsSvc],
|
|
250
|
+
|
|
251
|
+
// viewProviders: Available to component AND view children only
|
|
252
|
+
// NOT available to content children (<ng-content>)
|
|
253
|
+
viewProviders: [InternalTabs],
|
|
254
|
+
|
|
255
|
+
template: `
|
|
256
|
+
<div class="tabs">
|
|
257
|
+
<ng-content /> <!-- Content children can't access viewProviders -->
|
|
258
|
+
</div>
|
|
259
|
+
`,
|
|
260
|
+
})
|
|
261
|
+
export class Tabs {}
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
## Dynamic Providers
|
|
265
|
+
|
|
266
|
+
### Feature Flags
|
|
267
|
+
|
|
268
|
+
```typescript
|
|
269
|
+
export const FEATURE_FLAGS = new InjectionToken<FeatureFlags>('FeatureFlags');
|
|
270
|
+
|
|
271
|
+
interface FeatureFlags {
|
|
272
|
+
newDashboard: boolean;
|
|
273
|
+
betaFeatures: boolean;
|
|
274
|
+
experimentalApi: boolean;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// Load from API
|
|
278
|
+
{
|
|
279
|
+
provide: FEATURE_FLAGS,
|
|
280
|
+
useFactory: async () => {
|
|
281
|
+
const response = await fetch('/api/features');
|
|
282
|
+
return response.json();
|
|
283
|
+
},
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// Use in components
|
|
287
|
+
@Component({...})
|
|
288
|
+
export class Dashboard {
|
|
289
|
+
private features = inject(FEATURE_FLAGS);
|
|
290
|
+
|
|
291
|
+
showNewDashboard = this.features.newDashboard;
|
|
292
|
+
}
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
### Platform-Specific Services
|
|
296
|
+
|
|
297
|
+
```typescript
|
|
298
|
+
export abstract class Storage {
|
|
299
|
+
abstract get(key: string): string | null;
|
|
300
|
+
abstract set(key: string, value: string): void;
|
|
301
|
+
abstract remove(key: string): void;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
@Injectable()
|
|
305
|
+
export class BrowserStorage extends Storage {
|
|
306
|
+
get(key: string) { return localStorage.getItem(key); }
|
|
307
|
+
set(key: string, value: string) { localStorage.setItem(key, value); }
|
|
308
|
+
remove(key: string) { localStorage.removeItem(key); }
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
@Injectable()
|
|
312
|
+
export class ServerStorage extends Storage {
|
|
313
|
+
private store = new Map<string, string>();
|
|
314
|
+
|
|
315
|
+
get(key: string) { return this.store.get(key) ?? null; }
|
|
316
|
+
set(key: string, value: string) { this.store.set(key, value); }
|
|
317
|
+
remove(key: string) { this.store.delete(key); }
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// Provide based on platform
|
|
321
|
+
import { PLATFORM_ID, isPlatformBrowser } from '@angular/common';
|
|
322
|
+
|
|
323
|
+
{
|
|
324
|
+
provide: Storage,
|
|
325
|
+
useFactory: (platformId: object) => {
|
|
326
|
+
return isPlatformBrowser(platformId)
|
|
327
|
+
? new BrowserStorage()
|
|
328
|
+
: new ServerStorage();
|
|
329
|
+
},
|
|
330
|
+
deps: [PLATFORM_ID],
|
|
331
|
+
}
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
## Testing with DI
|
|
335
|
+
|
|
336
|
+
### Mocking Services
|
|
337
|
+
|
|
338
|
+
```typescript
|
|
339
|
+
describe('UserCmpt', () => {
|
|
340
|
+
let userServiceSpy: jasmine.SpyObj<User>;
|
|
341
|
+
|
|
342
|
+
beforeEach(async () => {
|
|
343
|
+
userServiceSpy = jasmine.createSpyObj('User', ['getUser', 'updateUser']);
|
|
344
|
+
userServiceSpy.getUser.and.returnValue(of({ id: '1', name: 'Test' }));
|
|
345
|
+
|
|
346
|
+
await TestBed.configureTestingModule({
|
|
347
|
+
imports: [UserCmpt],
|
|
348
|
+
providers: [
|
|
349
|
+
{ provide: User, useValue: userServiceSpy },
|
|
350
|
+
],
|
|
351
|
+
}).compileComponents();
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
it('should load user', () => {
|
|
355
|
+
const fixture = TestBed.createComponent(UserCmpt);
|
|
356
|
+
fixture.detectChanges();
|
|
357
|
+
|
|
358
|
+
expect(userServiceSpy.getUser).toHaveBeenCalled();
|
|
359
|
+
});
|
|
360
|
+
});
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
### Overriding Providers
|
|
364
|
+
|
|
365
|
+
```typescript
|
|
366
|
+
describe('with different config', () => {
|
|
367
|
+
beforeEach(async () => {
|
|
368
|
+
await TestBed.configureTestingModule({
|
|
369
|
+
imports: [App],
|
|
370
|
+
})
|
|
371
|
+
.overrideProvider(APP_CONFIG, {
|
|
372
|
+
useValue: { apiUrl: 'http://test-api.com' },
|
|
373
|
+
})
|
|
374
|
+
.compileComponents();
|
|
375
|
+
});
|
|
376
|
+
});
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
### Testing Injection Tokens
|
|
380
|
+
|
|
381
|
+
```typescript
|
|
382
|
+
describe('API_URL token', () => {
|
|
383
|
+
it('should provide correct URL', () => {
|
|
384
|
+
TestBed.configureTestingModule({
|
|
385
|
+
providers: [
|
|
386
|
+
{ provide: API_URL, useValue: 'https://api.test.com' },
|
|
387
|
+
],
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
const apiUrl = TestBed.inject(API_URL);
|
|
391
|
+
expect(apiUrl).toBe('https://api.test.com');
|
|
392
|
+
});
|
|
393
|
+
});
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
## DestroyRef and Cleanup
|
|
397
|
+
|
|
398
|
+
### Automatic Cleanup
|
|
399
|
+
|
|
400
|
+
```typescript
|
|
401
|
+
import { DestroyRef, inject } from '@angular/core';
|
|
402
|
+
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
|
|
403
|
+
|
|
404
|
+
@Component({...})
|
|
405
|
+
export class Data {
|
|
406
|
+
private destroyRef = inject(DestroyRef);
|
|
407
|
+
private dataService = inject(DataSvc);
|
|
408
|
+
|
|
409
|
+
constructor() {
|
|
410
|
+
// Auto-unsubscribe when component destroys
|
|
411
|
+
this.dataService.data$
|
|
412
|
+
.pipe(takeUntilDestroyed())
|
|
413
|
+
.subscribe(data => {
|
|
414
|
+
console.log(data);
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
// Or use DestroyRef directly
|
|
419
|
+
ngOnInit() {
|
|
420
|
+
const subscription = this.dataService.updates$.subscribe();
|
|
421
|
+
|
|
422
|
+
this.destroyRef.onDestroy(() => {
|
|
423
|
+
subscription.unsubscribe();
|
|
424
|
+
console.log('Cleaned up!');
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
```
|
|
429
|
+
|
|
430
|
+
### In Services
|
|
431
|
+
|
|
432
|
+
```typescript
|
|
433
|
+
@Injectable()
|
|
434
|
+
export class WebSocket {
|
|
435
|
+
private destroyRef = inject(DestroyRef);
|
|
436
|
+
private socket: WebSocket | null = null;
|
|
437
|
+
|
|
438
|
+
constructor() {
|
|
439
|
+
this.destroyRef.onDestroy(() => {
|
|
440
|
+
this.socket?.close();
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
connect(url: string) {
|
|
445
|
+
this.socket = new WebSocket(url);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
```
|
|
449
|
+
|
|
450
|
+
### takeUntilDestroyed Outside Constructor
|
|
451
|
+
|
|
452
|
+
```typescript
|
|
453
|
+
@Component({...})
|
|
454
|
+
export class My {
|
|
455
|
+
private destroyRef = inject(DestroyRef);
|
|
456
|
+
|
|
457
|
+
loadData() {
|
|
458
|
+
// Pass destroyRef when using outside constructor
|
|
459
|
+
this.http.get('/api/data')
|
|
460
|
+
.pipe(takeUntilDestroyed(this.destroyRef))
|
|
461
|
+
.subscribe();
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
```
|
|
465
|
+
|
|
466
|
+
## Injection Context Utilities
|
|
467
|
+
|
|
468
|
+
### assertInInjectionContext
|
|
469
|
+
|
|
470
|
+
```typescript
|
|
471
|
+
import { assertInInjectionContext, inject } from '@angular/core';
|
|
472
|
+
|
|
473
|
+
export function injectLogger(): Logger {
|
|
474
|
+
assertInInjectionContext(injectLogger);
|
|
475
|
+
return inject(Logger);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
// Usage - must be called in injection context
|
|
479
|
+
@Component({...})
|
|
480
|
+
export class My2 {
|
|
481
|
+
private logger = injectLogger(); // OK
|
|
482
|
+
|
|
483
|
+
someMethod() {
|
|
484
|
+
// injectLogger(); // ERROR - not in injection context
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
```
|
|
488
|
+
|
|
489
|
+
### Custom inject Functions
|
|
490
|
+
|
|
491
|
+
```typescript
|
|
492
|
+
// Create reusable injection utilities
|
|
493
|
+
export function injectRouteParam(param: string): Signal<string | null> {
|
|
494
|
+
assertInInjectionContext(injectRouteParam);
|
|
495
|
+
|
|
496
|
+
const route = inject(ActivatedRoute);
|
|
497
|
+
return toSignal(
|
|
498
|
+
route.paramMap.pipe(map(params => params.get(param))),
|
|
499
|
+
{ initialValue: null }
|
|
500
|
+
);
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
export function injectQueryParam(param: string): Signal<string | null> {
|
|
504
|
+
assertInInjectionContext(injectQueryParam);
|
|
505
|
+
|
|
506
|
+
const route = inject(ActivatedRoute);
|
|
507
|
+
return toSignal(
|
|
508
|
+
route.queryParamMap.pipe(map(params => params.get(param))),
|
|
509
|
+
{ initialValue: null }
|
|
510
|
+
);
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
// Usage
|
|
514
|
+
@Component({...})
|
|
515
|
+
export class UserCmpt {
|
|
516
|
+
userId = injectRouteParam('id');
|
|
517
|
+
tab = injectQueryParam('tab');
|
|
518
|
+
}
|
|
519
|
+
```
|