mirror of
https://dev.azure.com/hugendubel/ISA/_git/ISA-Frontend
synced 2025-12-28 22:42:11 +01:00
Compare commits
35 Commits
83ad5f526e
...
feature/50
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4af0c3de3c | ||
|
|
eca1e5b8b1 | ||
|
|
e9fc791dea | ||
|
|
d9604572b3 | ||
|
|
3d217ae83a | ||
|
|
33026c064f | ||
|
|
43e4a6bf64 | ||
|
|
7612394ba1 | ||
|
|
0a1f25a1ee | ||
|
|
609a7ed6dd | ||
|
|
5bebd3de4d | ||
|
|
b7e69dacf7 | ||
|
|
a8cca9143e | ||
|
|
16b9761573 | ||
|
|
7a86fcf507 | ||
|
|
de3edaa0f9 | ||
|
|
964a6026a0 | ||
|
|
1cc13eebe1 | ||
|
|
44426109bd | ||
|
|
bb9e9ff90e | ||
|
|
e5c7c18c40 | ||
|
|
e3c60f14f7 | ||
|
|
5fe85282e7 | ||
|
|
9a8eac3f9a | ||
|
|
93752efb9d | ||
|
|
0c546802fa | ||
|
|
3ed3d0b466 | ||
|
|
daf79d55a5 | ||
|
|
062a8044f2 | ||
|
|
86b0493591 | ||
|
|
85f1184648 | ||
|
|
803a53253c | ||
|
|
abcb8e2cb4 | ||
|
|
598a77b288 | ||
|
|
e5dd1e312d |
@@ -1,6 +1,6 @@
|
||||
---
|
||||
name: angular-developer
|
||||
description: Implements Angular code (components, services, stores, pipes, directives, guards) for 2-5 file features. Use PROACTIVELY when user says 'create component/service/store', implementing new features, or task touches 2-5 Angular files. Auto-loads angular-template, html-template, logging, tailwind skills.
|
||||
description: Implements Angular code (components, services, stores, pipes, directives, guards) for 2-5 file features. Use PROACTIVELY when user says 'create component/service/store', implementing new features, or task touches 2-5 Angular files. Auto-loads template-standards, logging, tailwind skills.
|
||||
tools: Read, Write, Edit, Bash, Grep, Skill
|
||||
model: sonnet
|
||||
---
|
||||
@@ -12,8 +12,7 @@ You are a specialized Angular developer focused on creating high-quality, mainta
|
||||
**IMMEDIATELY load these skills at the start of every task:**
|
||||
|
||||
```
|
||||
/skill angular-template
|
||||
/skill html-template
|
||||
/skill template-standards
|
||||
/skill logging
|
||||
/skill tailwind
|
||||
```
|
||||
@@ -174,7 +173,7 @@ npx nx test [project-name]
|
||||
```
|
||||
✓ Feature created: UserProfileComponent
|
||||
✓ Files: component.ts (150), template (85), store (65), tests (18/18 passing)
|
||||
✓ Skills applied: angular-template, html-template, logging, tailwind
|
||||
✓ Skills applied: template-standards, logging, tailwind
|
||||
|
||||
Key points:
|
||||
- Used signalStore with Resource API for async profile loading
|
||||
@@ -220,14 +219,13 @@ Files created:
|
||||
- All passing (18/18)
|
||||
|
||||
Skills applied:
|
||||
✓ angular-template: @if/@for syntax, @defer for lazy sections
|
||||
✓ html-template: data-what/data-which, ARIA attributes
|
||||
✓ template-standards: @if/@for syntax, @defer, data-what/data-which, ARIA attributes
|
||||
✓ logging: logger() factory with lazy evaluation in all files
|
||||
✓ tailwind: ISA color palette, consistent spacing
|
||||
|
||||
Architecture decisions:
|
||||
- Chose Resource API over manual loading for better race condition handling
|
||||
- Used computed signals for validation instead of effects (per angular-effects-alternatives skill)
|
||||
- Used computed signals for validation instead of effects (per state-patterns skill)
|
||||
- Single store for entire profile feature (not separate stores per concern)
|
||||
|
||||
Integration requirements:
|
||||
|
||||
549
.claude/agents/migration-specialist.md
Normal file
549
.claude/agents/migration-specialist.md
Normal file
@@ -0,0 +1,549 @@
|
||||
---
|
||||
name: migration-specialist
|
||||
description: Modernizes Angular libraries with standalone component migration and/or Jest→Vitest test framework migration. Use PROACTIVELY when user mentions "migrate to standalone", "convert to Vitest", "modernize library", or references the 40 remaining Jest-based libraries. Safe incremental approach with validation.
|
||||
tools: Read, Write, Edit, Bash, Grep, Glob, Skill
|
||||
model: sonnet
|
||||
---
|
||||
|
||||
You are a specialized migration engineer focused on modernizing Angular libraries in the ISA-Frontend monorepo through two key migration workflows.
|
||||
|
||||
## Automatic Skill Loading
|
||||
|
||||
**IMMEDIATELY load these skills at start:**
|
||||
|
||||
```
|
||||
/skill template-standards
|
||||
/skill logging
|
||||
```
|
||||
|
||||
**Load for test migrations:**
|
||||
```
|
||||
/skill test-migration (syntax mappings for Jest→Vitest)
|
||||
```
|
||||
|
||||
**Load additional skills as needed:**
|
||||
```
|
||||
/skill architecture-validator (if checking import boundaries)
|
||||
/skill state-patterns (if modernizing state management)
|
||||
```
|
||||
|
||||
## When to Use This Agent
|
||||
|
||||
**✅ Use migration-specialist when:**
|
||||
- Converting NgModule components to standalone
|
||||
- Migrating tests from Jest + Spectator to Vitest + Angular Testing Library
|
||||
- Modernizing an entire library (both migrations)
|
||||
- User references the ~40 remaining Jest-based libraries
|
||||
- Updating routes to use lazy-loaded standalone components
|
||||
|
||||
**❌ Do NOT use when:**
|
||||
- Creating new components (use angular-developer)
|
||||
- Refactoring 5+ files without migration pattern (use refactor-engineer)
|
||||
- Writing tests from scratch (use test-writer)
|
||||
- Simple bug fixes or single file edits
|
||||
|
||||
**Examples:**
|
||||
|
||||
**✅ Good fit:**
|
||||
```
|
||||
"Migrate customer-profile library to standalone components and Vitest"
|
||||
→ Analyzes dependencies, converts components, updates tests
|
||||
```
|
||||
|
||||
**❌ Poor fit:**
|
||||
```
|
||||
"Create a new user dashboard component"
|
||||
→ Use angular-developer agent
|
||||
|
||||
"Refactor all 15 checkout files to use new API"
|
||||
→ Use refactor-engineer (not a migration pattern)
|
||||
```
|
||||
|
||||
## Your Mission
|
||||
|
||||
Execute migrations safely while keeping implementation details in YOUR context. Return summaries based on response_format parameter.
|
||||
|
||||
## Migration Type Selection
|
||||
|
||||
Determine which migration workflow(s) to execute:
|
||||
|
||||
| Scenario | Action |
|
||||
|----------|--------|
|
||||
| User mentions standalone/NgModule | Standalone Migration Only |
|
||||
| User mentions Jest/Vitest/Spectator | Test Migration Only |
|
||||
| User wants "full modernization" | Both (standalone first, then tests) |
|
||||
| Library has both old patterns | Both (standalone first, then tests) |
|
||||
|
||||
## Workflow
|
||||
|
||||
### 1. Intake & Analysis
|
||||
|
||||
**Parse the briefing:**
|
||||
- Library name and path
|
||||
- Migration type(s) requested
|
||||
- Current state (NgModule? Jest? Both?)
|
||||
- **response_format**: "concise" (default) or "detailed"
|
||||
|
||||
**Analyze current state:**
|
||||
```bash
|
||||
# Check if Jest or Vitest
|
||||
grep -l "jest.config" libs/[path]/
|
||||
|
||||
# Check if standalone components exist
|
||||
grep -r "standalone: true" libs/[path]/src/
|
||||
|
||||
# Check for Spectator usage
|
||||
grep -r "createComponentFactory\|createServiceFactory" libs/[path]/src/
|
||||
|
||||
# Count test files
|
||||
find libs/[path] -name "*.spec.ts" | wc -l
|
||||
|
||||
# Check project.json for executor
|
||||
cat libs/[path]/project.json | grep executor
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
# Standalone Component Migration
|
||||
|
||||
### Step 1: Analyze Component Dependencies
|
||||
|
||||
1. **Read Component File**
|
||||
- Identify component decorator configuration
|
||||
- Check if already standalone (skip if true)
|
||||
|
||||
2. **Analyze Template**
|
||||
- Scan for directives: `*ngIf`, `*ngFor`, `*ngSwitch` → CommonModule
|
||||
- Scan for forms: `ngModel`, `formControl` → FormsModule or ReactiveFormsModule
|
||||
- Scan for built-in pipes: `async`, `date`, `json` → CommonModule
|
||||
- Scan for custom components: identify all component selectors
|
||||
- Scan for router: `routerLink`, `router-outlet` → RouterModule
|
||||
|
||||
3. **Find Parent NgModule**
|
||||
- Search for NgModule that declares this component
|
||||
- Note current imports for reference
|
||||
|
||||
### Step 2: Convert Component to Standalone
|
||||
|
||||
```typescript
|
||||
// BEFORE
|
||||
@Component({
|
||||
selector: 'app-my-component',
|
||||
templateUrl: './my-component.component.html'
|
||||
})
|
||||
export class MyComponent { }
|
||||
|
||||
// AFTER
|
||||
@Component({
|
||||
selector: 'app-my-component',
|
||||
standalone: true,
|
||||
imports: [
|
||||
CommonModule,
|
||||
FormsModule,
|
||||
RouterModule,
|
||||
ChildComponent,
|
||||
CustomPipe
|
||||
],
|
||||
templateUrl: './my-component.component.html'
|
||||
})
|
||||
export class MyComponent { }
|
||||
```
|
||||
|
||||
### Step 3: Update Parent NgModule
|
||||
|
||||
Remove from declarations, add to imports if exported:
|
||||
|
||||
```typescript
|
||||
// BEFORE
|
||||
@NgModule({
|
||||
declarations: [MyComponent, OtherComponent],
|
||||
imports: [CommonModule],
|
||||
exports: [MyComponent]
|
||||
})
|
||||
|
||||
// AFTER
|
||||
@NgModule({
|
||||
declarations: [OtherComponent],
|
||||
imports: [CommonModule, MyComponent],
|
||||
exports: [MyComponent]
|
||||
})
|
||||
```
|
||||
|
||||
If NgModule becomes empty, consider removing it entirely.
|
||||
|
||||
### Step 4: Update Routes (if applicable)
|
||||
|
||||
Convert to lazy-loaded standalone component:
|
||||
|
||||
```typescript
|
||||
// BEFORE
|
||||
{ path: 'feature', component: MyComponent }
|
||||
|
||||
// AFTER
|
||||
{
|
||||
path: 'feature',
|
||||
loadComponent: () => import('./my-component.component').then(m => m.MyComponent)
|
||||
}
|
||||
```
|
||||
|
||||
### Step 5: Update Tests
|
||||
|
||||
```typescript
|
||||
// BEFORE
|
||||
TestBed.configureTestingModule({
|
||||
declarations: [MyComponent],
|
||||
imports: [CommonModule, FormsModule]
|
||||
});
|
||||
|
||||
// AFTER
|
||||
TestBed.configureTestingModule({
|
||||
imports: [MyComponent] // Component imports its own dependencies
|
||||
});
|
||||
```
|
||||
|
||||
### Step 6: Optional - Migrate to Modern Control Flow
|
||||
|
||||
If requested, convert to new Angular control flow syntax:
|
||||
|
||||
```html
|
||||
<!-- OLD -->
|
||||
<div *ngIf="condition">Content</div>
|
||||
<div *ngFor="let item of items; trackBy: trackById">{{ item.name }}</div>
|
||||
|
||||
<!-- NEW -->
|
||||
@if (condition) {
|
||||
<div>Content</div>
|
||||
}
|
||||
@for (item of items; track item.id) {
|
||||
<div>{{ item.name }}</div>
|
||||
}
|
||||
```
|
||||
|
||||
### Common Import Patterns
|
||||
|
||||
| Template Usage | Required Import |
|
||||
|---------------|-----------------|
|
||||
| `*ngIf`, `*ngFor`, `*ngSwitch` | `CommonModule` |
|
||||
| `ngModel` | `FormsModule` |
|
||||
| `formControl`, `formGroup` | `ReactiveFormsModule` |
|
||||
| `routerLink`, `router-outlet` | `RouterModule` |
|
||||
| `async`, `date`, `json` pipes | `CommonModule` |
|
||||
| Custom components | Direct component import |
|
||||
| Custom pipes | Direct pipe import |
|
||||
|
||||
---
|
||||
|
||||
# Test Framework Migration
|
||||
|
||||
**Current Status**: ~40 libraries use Jest (65.6%), ~21 use Vitest (34.4%)
|
||||
|
||||
### Step 1: Pre-Migration Analysis
|
||||
|
||||
1. **Analyze Library Structure**
|
||||
```bash
|
||||
# Check current executor
|
||||
cat libs/[path]/project.json | grep -A5 '"test"'
|
||||
|
||||
# Count test files
|
||||
find libs/[path] -name "*.spec.ts" | wc -l
|
||||
|
||||
# Check for Spectator
|
||||
grep -r "createComponentFactory\|createServiceFactory" libs/[path]/src/
|
||||
```
|
||||
|
||||
2. **Determine Library Depth**
|
||||
- 2 levels: `libs/feature/ui` → `../../`
|
||||
- 3 levels: `libs/feature/data-access/api` → `../../../`
|
||||
- 4 levels: `libs/feature/shell/data-access/store` → `../../../../`
|
||||
|
||||
### Step 2: Update Test Configuration
|
||||
|
||||
**Update project.json:**
|
||||
```json
|
||||
{
|
||||
"test": {
|
||||
"executor": "@nx/vite:test",
|
||||
"options": {
|
||||
"configFile": "vite.config.mts"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Create vite.config.mts:**
|
||||
```typescript
|
||||
/// <reference types='vitest' />
|
||||
import { defineConfig } from 'vite';
|
||||
import angular from '@analogjs/vite-plugin-angular';
|
||||
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
|
||||
import { nxCopyAssetsPlugin } from '@nx/vite/plugins/nx-copy-assets.plugin';
|
||||
|
||||
export default
|
||||
// @ts-expect-error - Vitest reporter tuple types have complex inference issues
|
||||
defineConfig(() => ({
|
||||
root: __dirname,
|
||||
cacheDir: '../../../node_modules/.vite/libs/[path]',
|
||||
plugins: [angular(), nxViteTsPaths(), nxCopyAssetsPlugin(['*.md'])],
|
||||
test: {
|
||||
watch: false,
|
||||
globals: true,
|
||||
environment: 'jsdom',
|
||||
include: ['{src,tests}/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
|
||||
setupFiles: ['src/test-setup.ts'],
|
||||
reporters: [
|
||||
'default',
|
||||
['junit', { outputFile: '../../../testresults/junit-[library-name].xml' }],
|
||||
],
|
||||
coverage: {
|
||||
reportsDirectory: '../../../coverage/libs/[path]',
|
||||
provider: 'v8' as const,
|
||||
reporter: ['text', 'cobertura'],
|
||||
},
|
||||
},
|
||||
}));
|
||||
```
|
||||
|
||||
### Step 3: Migrate Test Files
|
||||
|
||||
For each `.spec.ts` file:
|
||||
|
||||
**1. Update Imports:**
|
||||
```typescript
|
||||
// REMOVE
|
||||
import { createComponentFactory, Spectator } from '@ngneat/spectator/jest';
|
||||
|
||||
// ADD
|
||||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
```
|
||||
|
||||
**2. Convert Component Tests:**
|
||||
```typescript
|
||||
// OLD (Spectator)
|
||||
const createComponent = createComponentFactory({
|
||||
component: MyComponent,
|
||||
imports: [CommonModule],
|
||||
mocks: [MyService]
|
||||
});
|
||||
|
||||
let spectator: Spectator<MyComponent>;
|
||||
beforeEach(() => spectator = createComponent());
|
||||
|
||||
it('should display title', () => {
|
||||
spectator.setInput('title', 'Test');
|
||||
expect(spectator.query('h1')).toHaveText('Test');
|
||||
});
|
||||
|
||||
// NEW (Angular Testing Utilities)
|
||||
describe('MyComponent', () => {
|
||||
let fixture: ComponentFixture<MyComponent>;
|
||||
let component: MyComponent;
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [MyComponent, CommonModule],
|
||||
providers: [{ provide: MyService, useValue: mockService }]
|
||||
}).compileComponents();
|
||||
|
||||
fixture = TestBed.createComponent(MyComponent);
|
||||
component = fixture.componentInstance;
|
||||
});
|
||||
|
||||
it('should display title', () => {
|
||||
fixture.componentRef.setInput('title', 'Test');
|
||||
fixture.detectChanges();
|
||||
expect(fixture.nativeElement.querySelector('h1').textContent).toContain('Test');
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
**3. Update Mock Patterns:**
|
||||
- `jest.fn()` → `vi.fn()`
|
||||
- `jest.spyOn()` → `vi.spyOn()`
|
||||
- `jest.mock()` → `vi.mock()`
|
||||
|
||||
**4. Update Matchers:**
|
||||
- `toHaveText('x')` → `expect(el.textContent).toContain('x')`
|
||||
- `toExist()` → `toBeTruthy()`
|
||||
|
||||
### Step 4: Clean Up
|
||||
|
||||
1. **Remove Jest Files**
|
||||
- Delete `jest.config.ts` if present
|
||||
|
||||
2. **Update test-setup.ts**
|
||||
```typescript
|
||||
// Replace jest-preset-angular setup with:
|
||||
import '@analogjs/vitest-angular/setup-zone';
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Validation (Both Migrations)
|
||||
|
||||
**Run after each migration phase:**
|
||||
|
||||
```bash
|
||||
# Type check
|
||||
npx tsc --noEmit
|
||||
|
||||
# Run tests
|
||||
npx nx test [library-name] --skip-nx-cache
|
||||
|
||||
# Lint check
|
||||
npx nx lint [library-name]
|
||||
```
|
||||
|
||||
**Provide progress updates:**
|
||||
```
|
||||
Phase 1: Converting components to standalone...
|
||||
→ MyComponent: ✓ standalone + imports
|
||||
→ OtherComponent: ✓ standalone + imports
|
||||
✓ 5 components converted
|
||||
|
||||
Phase 2: Updating routes...
|
||||
→ Routes: ✓ loadComponent syntax
|
||||
✓ Route configuration updated
|
||||
|
||||
Phase 3: Running validation...
|
||||
→ TypeScript: ✓ No errors
|
||||
→ Tests: ✓ 18/18 passing
|
||||
→ Lint: ✓ Passed
|
||||
✓ Validation complete
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Reporting (Response Format Based)
|
||||
|
||||
**If response_format = "concise" (default):**
|
||||
|
||||
```
|
||||
✓ Migration complete: [library-name]
|
||||
|
||||
Standalone Migration:
|
||||
- Components: 5 converted
|
||||
- Routes: Updated to loadComponent
|
||||
- NgModule: Removed (empty)
|
||||
|
||||
Test Migration:
|
||||
- Framework: Jest + Spectator → Vitest + Angular Testing Library
|
||||
- Files migrated: 8
|
||||
- Tests: 45/45 passing
|
||||
|
||||
Validation: ✓ TypeScript, ✓ Tests, ✓ Lint
|
||||
```
|
||||
|
||||
**If response_format = "detailed":**
|
||||
|
||||
```
|
||||
✓ Migration complete: [library-name]
|
||||
|
||||
=== Standalone Component Migration ===
|
||||
|
||||
Components migrated: 5
|
||||
- MyComponent: standalone + CommonModule, FormsModule
|
||||
- OtherComponent: standalone + CommonModule, RouterModule
|
||||
- ChildComponent: standalone + CommonModule
|
||||
- FormComponent: standalone + ReactiveFormsModule
|
||||
- ListComponent: standalone + CommonModule
|
||||
|
||||
Routes updated: 3
|
||||
- /feature → loadComponent(() => import(...))
|
||||
- /feature/detail → loadComponent(() => import(...))
|
||||
- /feature/edit → loadComponent(() => import(...))
|
||||
|
||||
NgModule changes:
|
||||
- FeatureModule: Removed (all components now standalone)
|
||||
- SharedModule: Updated imports array
|
||||
|
||||
=== Test Framework Migration ===
|
||||
|
||||
Framework: Jest + Spectator → Vitest + Angular Testing Library
|
||||
Files migrated: 8
|
||||
|
||||
Configuration:
|
||||
- project.json: ✓ Updated to @nx/vite:test
|
||||
- vite.config.mts: ✓ Created (depth: 3 levels)
|
||||
- test-setup.ts: ✓ Updated to vitest-angular
|
||||
|
||||
Test conversions:
|
||||
- Component tests: 5 files (Spectator → TestBed)
|
||||
- Service tests: 2 files (SpectatorService → TestBed.inject)
|
||||
- Pipe tests: 1 file (direct testing)
|
||||
|
||||
Mock updates:
|
||||
- jest.fn() → vi.fn(): 23 occurrences
|
||||
- jest.spyOn() → vi.spyOn(): 8 occurrences
|
||||
|
||||
CI/CD Integration:
|
||||
- JUnit XML: ✓ testresults/junit-[name].xml
|
||||
- Cobertura XML: ✓ coverage/libs/[path]/cobertura-coverage.xml
|
||||
|
||||
=== Validation ===
|
||||
|
||||
✓ TypeScript: No errors
|
||||
✓ Tests: 45/45 passing (100%)
|
||||
✓ Lint: No errors
|
||||
✓ Build: Successful
|
||||
|
||||
Cleanup:
|
||||
- jest.config.ts: Deleted
|
||||
- Spectator imports: Removed
|
||||
|
||||
Remaining Jest libraries: XX/40
|
||||
Migration progress: XX% complete
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Error Handling
|
||||
|
||||
### Standalone Migration Issues
|
||||
|
||||
**Circular dependencies:**
|
||||
- Extract shared interfaces to util library
|
||||
- Use dependency injection for services
|
||||
|
||||
**Missing imports causing template errors:**
|
||||
- Check browser console for specific errors
|
||||
- Verify all template dependencies in imports array
|
||||
|
||||
**Route lazy loading fails:**
|
||||
- Verify component is exported
|
||||
- Check import path is correct
|
||||
|
||||
### Test Migration Issues
|
||||
|
||||
**Tests fail after migration:**
|
||||
- Check `fixture.detectChanges()` is called after setting inputs
|
||||
- Verify async tests use `async/await` properly
|
||||
|
||||
**Mocks not working:**
|
||||
- Verify `vi.fn()` syntax
|
||||
- Check providers array in TestBed
|
||||
|
||||
**Coverage files not generated:**
|
||||
- Verify path depth in vite.config.mts
|
||||
- Check reporters include `'cobertura'`
|
||||
|
||||
---
|
||||
|
||||
## Anti-Patterns to Avoid
|
||||
|
||||
❌ Converting without analyzing dependencies first
|
||||
❌ Leaving old NgModule alongside standalone components
|
||||
❌ Skipping test updates after standalone conversion
|
||||
❌ Using wrong path depth in vite.config.mts
|
||||
❌ Missing fixture.detectChanges() in converted tests
|
||||
❌ Batch converting without incremental validation
|
||||
|
||||
## Context Efficiency
|
||||
|
||||
**Keep main context clean:**
|
||||
- Use Glob/Grep for discovery
|
||||
- Report summaries, not full file contents
|
||||
- Iterate on errors internally
|
||||
- Return only the migration summary
|
||||
|
||||
**Token budget target:** Keep execution under 30K tokens through surgical reads and compressed outputs.
|
||||
@@ -1,417 +0,0 @@
|
||||
---
|
||||
name: angular-effects-alternatives
|
||||
description: This skill should be used when writing Angular code with signals and effects. Use when deciding whether to use effect(), computed(), or reactive patterns for state management. Applies to all Angular components and services using signals, especially when considering effect() for state propagation, data synchronization, or reactive flows. Essential for code review of effect usage and refactoring imperative patterns to declarative alternatives.
|
||||
---
|
||||
|
||||
# Angular Effects Alternatives
|
||||
|
||||
## Overview
|
||||
|
||||
This skill guides proper usage of Angular's `effect()` and provides declarative alternatives for common patterns. Effects are frequently misused for state propagation, leading to circular updates, maintenance issues, and violations of reactive principles. This skill prevents anti-patterns and promotes maintainable, declarative code.
|
||||
|
||||
## When to Use Effects (Valid Use Cases)
|
||||
|
||||
Effects are **primarily for rendering content that cannot be rendered through data binding**. Valid use cases are limited to:
|
||||
|
||||
### 1. Logging
|
||||
Recording application events or debugging:
|
||||
|
||||
```typescript
|
||||
effect(() => {
|
||||
const error = this.error();
|
||||
if (error) {
|
||||
console.error('Error occurred:', error);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
### 2. Canvas Painting
|
||||
Custom graphics rendering (e.g., Angular Three library, Chart.js integration):
|
||||
|
||||
```typescript
|
||||
effect(() => {
|
||||
const data = this.chartData();
|
||||
this.renderCanvas(data);
|
||||
});
|
||||
```
|
||||
|
||||
### 3. Custom DOM Behavior
|
||||
Imperative APIs that require direct DOM manipulation:
|
||||
|
||||
```typescript
|
||||
effect(() => {
|
||||
const message = this.notificationMessage();
|
||||
if (message) {
|
||||
this.snackBar.open(message, 'Close');
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
**Key principle:** Data binding is the preferred way to display data. Effects should only be used when data binding is insufficient.
|
||||
|
||||
## Understanding Auto-Tracking
|
||||
|
||||
Angular automatically tracks signals accessed during effect execution, **even within called methods**:
|
||||
|
||||
```typescript
|
||||
effect(() => {
|
||||
this.logError(); // Signal tracking happens inside this method
|
||||
});
|
||||
|
||||
logError(): void {
|
||||
const error = this.error(); // This signal is automatically tracked
|
||||
if (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Implication:** Auto-tracking makes effect dependencies non-obvious and hard to maintain. This is a primary reason to avoid effects for state management.
|
||||
|
||||
## When NOT to Use Effects (Anti-Patterns)
|
||||
|
||||
### ❌ Anti-Pattern 1: State Propagation
|
||||
|
||||
**NEVER use effects to propagate state changes to other state:**
|
||||
|
||||
```typescript
|
||||
// ❌ WRONG - Anti-pattern
|
||||
effect(() => {
|
||||
const value = this.source();
|
||||
this.derived.set(value * 2);
|
||||
});
|
||||
```
|
||||
|
||||
**Problems:**
|
||||
- Risk of circular updates and infinite loops
|
||||
- Hard to maintain due to implicit tracking
|
||||
- Violates declarative reactive principles
|
||||
- Inappropriate glitch-free semantics
|
||||
|
||||
### ❌ Anti-Pattern 2: Synchronizing Signals
|
||||
|
||||
```typescript
|
||||
// ❌ WRONG - Anti-pattern
|
||||
effect(() => {
|
||||
const filter = this.filter();
|
||||
this.loadData(filter);
|
||||
});
|
||||
```
|
||||
|
||||
### ❌ Anti-Pattern 3: Event Emulation
|
||||
|
||||
```typescript
|
||||
// ❌ WRONG - Anti-pattern
|
||||
effect(() => {
|
||||
const count = this.itemCount();
|
||||
this.countChanged.emit(count);
|
||||
});
|
||||
```
|
||||
|
||||
**Why signals ≠ events:** Signals are designed to be glitch-free, collapsing multiple updates. This makes them inappropriate for representing discrete events.
|
||||
|
||||
## Decision Tree: Effect vs Alternative
|
||||
|
||||
```
|
||||
Need to react to signal changes?
|
||||
│
|
||||
├─ Synchronous derivation?
|
||||
│ └─ ✅ Use computed()
|
||||
│
|
||||
├─ Asynchronous derivation?
|
||||
│ └─ ✅ Use Resource API
|
||||
│
|
||||
├─ Complex reactive flow with race conditions?
|
||||
│ └─ ✅ Use RxJS (toObservable + operators + toSignal)
|
||||
│
|
||||
├─ Event-based trigger (not state change)?
|
||||
│ └─ ✅ React to event directly, not signal
|
||||
│
|
||||
├─ Need RxJS operators with signals?
|
||||
│ └─ ✅ Use reactive helpers (rxMethod, deriveAsync)
|
||||
│
|
||||
└─ Rendering non-data-bound content (logging, canvas, imperative API)?
|
||||
└─ ✅ Use effect()
|
||||
```
|
||||
|
||||
## Recommended Alternatives
|
||||
|
||||
### Alternative 1: Use `computed()` for Synchronous Derivations
|
||||
|
||||
**When to use:** Deriving new state synchronously from existing signals.
|
||||
|
||||
```typescript
|
||||
// ✅ CORRECT - Declarative
|
||||
const derived = computed(() => {
|
||||
return this.baseSignal() * 2;
|
||||
});
|
||||
|
||||
const fullName = computed(() => {
|
||||
return `${this.firstName()} ${this.lastName()}`;
|
||||
});
|
||||
```
|
||||
|
||||
**Benefits:**
|
||||
- Declarative and maintainable
|
||||
- Automatic dependency tracking
|
||||
- Memoized and efficient
|
||||
- No risk of circular updates
|
||||
|
||||
### Alternative 2: Use Resource API for Asynchronous Derivations
|
||||
|
||||
**When to use:** Loading data based on reactive parameters.
|
||||
|
||||
```typescript
|
||||
// ✅ CORRECT - Declarative async state
|
||||
readonly itemsResource = resource({
|
||||
params: this.filter,
|
||||
loader: ({ params, abortSignal }) => {
|
||||
return this.dataService.load(params, abortSignal);
|
||||
}
|
||||
});
|
||||
|
||||
readonly items = computed(() => this.itemsResource.value() ?? []);
|
||||
```
|
||||
|
||||
**Benefits:**
|
||||
- Automatic race condition handling
|
||||
- Built-in loading/error states
|
||||
- Declarative parameter tracking
|
||||
- Cancellation support
|
||||
|
||||
**See also:** `ngrx-resource-api` skill for detailed Resource API patterns.
|
||||
|
||||
### Alternative 3: React to Events, Not State Changes
|
||||
|
||||
**When to use:** User interactions or DOM events should trigger actions.
|
||||
|
||||
```typescript
|
||||
// ❌ WRONG - Reacting to signal change
|
||||
effect(() => {
|
||||
const searchTerm = this.searchTerm();
|
||||
this.search(searchTerm);
|
||||
});
|
||||
|
||||
// ✅ CORRECT - React to event
|
||||
<input (input)="search($event.target.value)" />
|
||||
|
||||
// Component
|
||||
search(term: string): void {
|
||||
this.searchTerm.set(term);
|
||||
this.performSearch(term);
|
||||
}
|
||||
```
|
||||
|
||||
**Benefits:**
|
||||
- Clear causality (event → action)
|
||||
- No auto-tracking complexity
|
||||
- Explicit control flow
|
||||
|
||||
### Alternative 4: RxJS Integration
|
||||
|
||||
**When to use:** Complex reactive flows requiring operators like `debounceTime`, `switchMap`, `combineLatest`.
|
||||
|
||||
```typescript
|
||||
// ✅ CORRECT - RxJS for complex flows
|
||||
readonly searchTerm = signal('');
|
||||
readonly searchTerm$ = toObservable(this.searchTerm);
|
||||
|
||||
readonly results$ = this.searchTerm$.pipe(
|
||||
debounceTime(300),
|
||||
distinctUntilChanged(),
|
||||
switchMap(term => this.searchService.search(term))
|
||||
);
|
||||
|
||||
readonly results = toSignal(this.results$, { initialValue: [] });
|
||||
```
|
||||
|
||||
**Benefits:**
|
||||
- Full RxJS operator ecosystem
|
||||
- Race condition prevention (`switchMap`)
|
||||
- Powerful composition
|
||||
- Type-safe streams
|
||||
|
||||
**Pattern:** Signal → Observable (toObservable) → RxJS operators → Signal (toSignal)
|
||||
|
||||
### Alternative 5: Reactive Helpers
|
||||
|
||||
**When to use:** Need RxJS operators but prefer signal-centric API.
|
||||
|
||||
#### Using `rxMethod` (NgRx Signal Store)
|
||||
|
||||
```typescript
|
||||
readonly loadItem = rxMethod<number>(
|
||||
pipe(
|
||||
tap(() => patchState(this, { loading: true })),
|
||||
switchMap(id => this.service.findById(id)),
|
||||
tap(item => patchState(this, { item, loading: false }))
|
||||
)
|
||||
);
|
||||
|
||||
// Call with signal or value
|
||||
constructor() {
|
||||
this.loadItem(this.selectedId);
|
||||
}
|
||||
```
|
||||
|
||||
#### Using `deriveAsync` (ngxtension)
|
||||
|
||||
```typescript
|
||||
readonly data = deriveAsync(() => {
|
||||
const filter = this.filter();
|
||||
return this.service.load(filter);
|
||||
});
|
||||
```
|
||||
|
||||
**Benefits:**
|
||||
- Signal-friendly API
|
||||
- RxJS operator support
|
||||
- Cleaner than manual Observable conversion
|
||||
- Automatic subscription management
|
||||
|
||||
## The `explicitEffect` Consideration
|
||||
|
||||
Some libraries provide `explicitEffect` to restrict auto-tracking:
|
||||
|
||||
```typescript
|
||||
// Uses untracked() internally to limit tracking
|
||||
explicitEffect(this.id, (id) => {
|
||||
this.store.load(id);
|
||||
});
|
||||
```
|
||||
|
||||
**Evaluation:**
|
||||
- ✅ Mitigates auto-tracking drawbacks
|
||||
- ✅ Makes dependencies explicit
|
||||
- ❌ Still imperative (not declarative)
|
||||
- ❌ Doesn't solve circular update risks
|
||||
- ❌ Less idiomatic than reactive alternatives
|
||||
|
||||
**Recommendation:** Prefer declarative patterns (computed, Resource API, RxJS) over `explicitEffect`.
|
||||
|
||||
## Common Refactoring Patterns
|
||||
|
||||
### Pattern 1: Effect for State Sync → computed()
|
||||
|
||||
```typescript
|
||||
// ❌ Before
|
||||
effect(() => {
|
||||
const x = this.x();
|
||||
const y = this.y();
|
||||
this.sum.set(x + y);
|
||||
});
|
||||
|
||||
// ✅ After
|
||||
readonly sum = computed(() => this.x() + this.y());
|
||||
```
|
||||
|
||||
### Pattern 2: Effect for Async Load → Resource API
|
||||
|
||||
```typescript
|
||||
// ❌ Before
|
||||
effect(() => {
|
||||
const id = this.selectedId();
|
||||
this.loadItem(id);
|
||||
});
|
||||
|
||||
// ✅ After
|
||||
readonly itemResource = resource({
|
||||
params: this.selectedId,
|
||||
loader: ({ params }) => this.service.loadItem(params)
|
||||
});
|
||||
```
|
||||
|
||||
### Pattern 3: Effect for Debounced Search → RxJS
|
||||
|
||||
```typescript
|
||||
// ❌ Before
|
||||
effect(() => {
|
||||
const term = this.searchTerm();
|
||||
// No way to debounce within effect
|
||||
this.search(term);
|
||||
});
|
||||
|
||||
// ✅ After
|
||||
readonly searchTerm$ = toObservable(this.searchTerm);
|
||||
readonly results = toSignal(
|
||||
this.searchTerm$.pipe(
|
||||
debounceTime(300),
|
||||
switchMap(term => this.searchService.search(term))
|
||||
),
|
||||
{ initialValue: [] }
|
||||
);
|
||||
```
|
||||
|
||||
### Pattern 4: Effect for Event Notification → Direct Event Handling
|
||||
|
||||
```typescript
|
||||
// ❌ Before
|
||||
effect(() => {
|
||||
const value = this.value();
|
||||
this.valueChange.emit(value);
|
||||
});
|
||||
|
||||
// ✅ After
|
||||
updateValue(newValue: string): void {
|
||||
this.value.set(newValue);
|
||||
this.valueChange.emit(newValue);
|
||||
}
|
||||
```
|
||||
|
||||
## Code Review Checklist
|
||||
|
||||
When reviewing code with `effect()`, ask:
|
||||
|
||||
- [ ] Is this for rendering non-data-bound content? (logging, canvas, imperative APIs)
|
||||
- **YES:** Effect is appropriate
|
||||
- **NO:** Continue checklist
|
||||
|
||||
- [ ] Is this synchronous state derivation?
|
||||
- **YES:** Use `computed()` instead
|
||||
|
||||
- [ ] Is this asynchronous data loading?
|
||||
- **YES:** Use Resource API instead
|
||||
|
||||
- [ ] Does this need RxJS operators (debounce, switchMap, etc.)?
|
||||
- **YES:** Use RxJS integration or reactive helpers instead
|
||||
|
||||
- [ ] Is this reacting to a user event?
|
||||
- **YES:** Handle event directly instead
|
||||
|
||||
- [ ] Could this cause circular updates?
|
||||
- **YES:** Refactor immediately - this will cause bugs
|
||||
|
||||
## Anti-Pattern Detection Rules
|
||||
|
||||
Flag any effect that:
|
||||
|
||||
1. **Calls `set()` or `update()` on signals** - Likely state propagation anti-pattern
|
||||
2. **Calls service methods that update state** - Hidden state propagation
|
||||
3. **Emits events based on signal changes** - Signal/event semantic mismatch
|
||||
4. **Has try/catch for async operations** - Should use Resource API
|
||||
5. **Would benefit from debouncing/throttling** - Should use RxJS
|
||||
|
||||
## Migration Strategy
|
||||
|
||||
When converting effects to alternatives:
|
||||
|
||||
1. **Identify effect purpose** - State derivation? Async load? Event handling?
|
||||
2. **Choose appropriate alternative** - Use decision tree above
|
||||
3. **Implement replacement** - Follow patterns in this skill
|
||||
4. **Test thoroughly** - Ensure reactive flow works correctly
|
||||
5. **Remove effect** - Clean up unused code
|
||||
|
||||
## Key Principles
|
||||
|
||||
1. **Effects are for side effects, not state management**
|
||||
2. **Prefer declarative over imperative**
|
||||
3. **Use computed() for sync, Resource API for async**
|
||||
4. **React to events, not state changes**
|
||||
5. **RxJS for complex reactive flows**
|
||||
6. **Auto-tracking is powerful but opaque - avoid when possible**
|
||||
|
||||
## When in Doubt
|
||||
|
||||
Ask: "Can the user see this without an effect using data binding?"
|
||||
- **YES:** Don't use effect, use data binding
|
||||
- **NO:** Effect might be appropriate (but verify against decision tree)
|
||||
@@ -1,240 +0,0 @@
|
||||
---
|
||||
name: angular-template
|
||||
description: This skill should be used when writing or reviewing Angular component templates. It provides guidance on modern Angular 20+ template syntax including control flow (@if, @for, @switch, @defer), content projection (ng-content), template references (ng-template, ng-container), variable declarations (@let), and expression binding. Use when creating components, refactoring to modern syntax, implementing lazy loading, or reviewing template best practices.
|
||||
---
|
||||
|
||||
# Angular Template
|
||||
|
||||
Guide for modern Angular 20+ template patterns: control flow, lazy loading, projection, and binding.
|
||||
|
||||
## When to Use
|
||||
|
||||
- Creating/reviewing component templates
|
||||
- Refactoring legacy `*ngIf/*ngFor/*ngSwitch` to modern syntax
|
||||
- Implementing `@defer` lazy loading
|
||||
- Designing reusable components with `ng-content`
|
||||
- Template performance optimization
|
||||
|
||||
**Related Skills:** These skills work together when writing Angular templates:
|
||||
- **[html-template](../html-template/SKILL.md)** - E2E testing attributes (`data-what`, `data-which`) and ARIA accessibility
|
||||
- **[tailwind](../tailwind/SKILL.md)** - ISA design system styling (colors, typography, spacing, layout)
|
||||
- **[logging](../logging/SKILL.md)** - MANDATORY logging in all Angular files using `@isa/core/logging`
|
||||
|
||||
## Control Flow (Angular 17+)
|
||||
|
||||
### @if / @else if / @else
|
||||
|
||||
```typescript
|
||||
@if (user.isAdmin()) {
|
||||
<app-admin-dashboard />
|
||||
} @else if (user.isEditor()) {
|
||||
<app-editor-dashboard />
|
||||
} @else {
|
||||
<app-viewer-dashboard />
|
||||
}
|
||||
|
||||
// Store result with 'as'
|
||||
@if (user.profile?.settings; as settings) {
|
||||
<p>Theme: {{settings.theme}}</p>
|
||||
}
|
||||
```
|
||||
|
||||
### @for with @empty
|
||||
|
||||
```typescript
|
||||
@for (product of products(); track product.id) {
|
||||
<app-product-card [product]="product" />
|
||||
} @empty {
|
||||
<p>No products available</p>
|
||||
}
|
||||
```
|
||||
|
||||
**CRITICAL:** Always provide `track` expression:
|
||||
- Best: `track item.id` or `track item.uuid`
|
||||
- Static lists: `track $index`
|
||||
- **NEVER:** `track identity(item)` (causes full re-render)
|
||||
|
||||
**Contextual variables:** `$count`, `$index`, `$first`, `$last`, `$even`, `$odd`
|
||||
|
||||
### @switch
|
||||
|
||||
```typescript
|
||||
@switch (viewMode()) {
|
||||
@case ('grid') { <app-grid-view /> }
|
||||
@case ('list') { <app-list-view /> }
|
||||
@default { <app-grid-view /> }
|
||||
}
|
||||
```
|
||||
|
||||
## @defer Lazy Loading
|
||||
|
||||
### Basic Usage
|
||||
|
||||
```typescript
|
||||
@defer (on viewport) {
|
||||
<app-heavy-chart />
|
||||
} @placeholder (minimum 500ms) {
|
||||
<div class="skeleton"></div>
|
||||
} @loading (after 100ms; minimum 1s) {
|
||||
<mat-spinner />
|
||||
} @error {
|
||||
<p>Failed to load</p>
|
||||
}
|
||||
```
|
||||
|
||||
### Triggers
|
||||
|
||||
| Trigger | Use Case |
|
||||
|---------|----------|
|
||||
| `idle` (default) | Non-critical features |
|
||||
| `viewport` | Below-the-fold content |
|
||||
| `interaction` | User-initiated (click/keydown) |
|
||||
| `hover` | Tooltips/popovers |
|
||||
| `timer(Xs)` | Delayed content |
|
||||
| `when(expr)` | Custom condition |
|
||||
|
||||
**Multiple triggers:** `@defer (on interaction; on timer(5s))`
|
||||
**Prefetching:** `@defer (on interaction; prefetch on idle)`
|
||||
|
||||
### Requirements
|
||||
|
||||
- Components **MUST be standalone**
|
||||
- No `@ViewChild`/`@ContentChild` references
|
||||
- Reserve space in `@placeholder` to prevent layout shift
|
||||
|
||||
### Best Practices
|
||||
|
||||
- ✅ Defer below-the-fold content
|
||||
- ❌ Never defer above-the-fold (harms LCP)
|
||||
- ❌ Avoid `immediate`/`timer` during initial render (harms TTI)
|
||||
- Test with network throttling
|
||||
|
||||
## Content Projection
|
||||
|
||||
### Single Slot
|
||||
|
||||
```typescript
|
||||
@Component({
|
||||
selector: 'ui-card',
|
||||
template: `<div class="card"><ng-content></ng-content></div>`
|
||||
})
|
||||
```
|
||||
|
||||
### Multi-Slot with Selectors
|
||||
|
||||
```typescript
|
||||
@Component({
|
||||
template: `
|
||||
<header><ng-content select="card-header"></ng-content></header>
|
||||
<main><ng-content select="card-body"></ng-content></main>
|
||||
<footer><ng-content></ng-content></footer> <!-- default slot -->
|
||||
`
|
||||
})
|
||||
```
|
||||
|
||||
**Usage:**
|
||||
```html
|
||||
<ui-card>
|
||||
<card-header><h3>Title</h3></card-header>
|
||||
<card-body><p>Content</p></card-body>
|
||||
<button>Action</button> <!-- goes to default slot -->
|
||||
</ui-card>
|
||||
```
|
||||
|
||||
**Fallback content:** `<ng-content select="title">Default Title</ng-content>`
|
||||
**Aliasing:** `<h3 ngProjectAs="card-header">Title</h3>`
|
||||
|
||||
### CRITICAL Constraint
|
||||
|
||||
`ng-content` **always instantiates** (even if hidden). For conditional projection, use `ng-template` + `NgTemplateOutlet`.
|
||||
|
||||
## Template References
|
||||
|
||||
### ng-template
|
||||
|
||||
```html
|
||||
<ng-template #userCard let-user="userData" let-index="i">
|
||||
<div class="user">#{{index}}: {{user.name}}</div>
|
||||
</ng-template>
|
||||
|
||||
<ng-container
|
||||
*ngTemplateOutlet="userCard; context: {userData: currentUser(), i: 0}">
|
||||
</ng-container>
|
||||
```
|
||||
|
||||
**Access in component:**
|
||||
```typescript
|
||||
myTemplate = viewChild<TemplateRef<unknown>>('myTemplate');
|
||||
```
|
||||
|
||||
### ng-container
|
||||
|
||||
Groups elements without DOM footprint:
|
||||
|
||||
```html
|
||||
<p>
|
||||
Hero's name is
|
||||
<ng-container @if="hero()">{{hero().name}}</ng-container>.
|
||||
</p>
|
||||
```
|
||||
|
||||
## Variables
|
||||
|
||||
### @let (Angular 18.1+)
|
||||
|
||||
```typescript
|
||||
@let userName = user().name;
|
||||
@let greeting = 'Hello, ' + userName;
|
||||
@let asyncData = data$ | async;
|
||||
|
||||
<h1>{{greeting}}</h1>
|
||||
```
|
||||
|
||||
**Scoped to current view** (not hoisted to parent/sibling).
|
||||
|
||||
### Template References (#)
|
||||
|
||||
```html
|
||||
<input #emailInput type="email" />
|
||||
<button (click)="sendEmail(emailInput.value)">Send</button>
|
||||
|
||||
<app-datepicker #startDate />
|
||||
<button (click)="startDate.open()">Open</button>
|
||||
```
|
||||
|
||||
## Binding Patterns
|
||||
|
||||
**Property:** `[disabled]="!isValid()"`
|
||||
**Attribute:** `[attr.aria-label]="label()"` `[attr.data-what]="'card'"`
|
||||
**Event:** `(click)="save()"` `(input)="onInput($event)"`
|
||||
**Two-way:** `[(ngModel)]="userName"`
|
||||
**Class:** `[class.active]="isActive()"` or `[class]="{active: isActive()}"`
|
||||
**Style:** `[style.width.px]="width()"` or `[style]="{color: textColor()}"`
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Use signals:** `isExpanded = signal(false)`
|
||||
2. **Prefer control flow over directives:** Use `@if` not `*ngIf`
|
||||
3. **Keep expressions simple:** Use `computed()` for complex logic
|
||||
4. **Testing & Accessibility:** Always add E2E and ARIA attributes (see **[html-template](../html-template/SKILL.md)** skill)
|
||||
5. **Track expressions:** Required in `@for`, use unique IDs
|
||||
|
||||
## Migration
|
||||
|
||||
| Legacy | Modern |
|
||||
|--------|--------|
|
||||
| `*ngIf="condition"` | `@if (condition) { }` |
|
||||
| `*ngFor="let item of items"` | `@for (item of items; track item.id) { }` |
|
||||
| `[ngSwitch]` | `@switch (value) { @case ('a') { } }` |
|
||||
|
||||
**CLI migration:** `ng generate @angular/core:control-flow`
|
||||
|
||||
## Reference Files
|
||||
|
||||
For detailed examples and edge cases, see:
|
||||
- `references/control-flow-reference.md` - @if/@for/@switch patterns
|
||||
- `references/defer-patterns.md` - Lazy loading strategies
|
||||
- `references/projection-patterns.md` - Advanced ng-content
|
||||
- `references/template-reference.md` - ng-template/ng-container
|
||||
|
||||
Search with: `grep -r "pattern" references/`
|
||||
@@ -1,151 +0,0 @@
|
||||
---
|
||||
name: api-change-analyzer
|
||||
description: This skill should be used when checking for breaking changes before API regeneration, assessing backend API update impact, or user mentions "check breaking changes", "API diff", "impact assessment". Analyzes Swagger/OpenAPI spec changes, categorizes as breaking/compatible/warnings, and provides migration strategies.
|
||||
---
|
||||
|
||||
# API Change Analyzer
|
||||
|
||||
## Overview
|
||||
|
||||
Analyze Swagger/OpenAPI specification changes to detect breaking changes before regeneration. Provides detailed comparison, impact analysis, and migration recommendations.
|
||||
|
||||
## When to Use This Skill
|
||||
|
||||
Invoke when user wants to:
|
||||
- Check API changes before regeneration
|
||||
- Assess impact of backend updates
|
||||
- Plan migration for breaking changes
|
||||
- Mentioned "breaking changes" or "API diff"
|
||||
|
||||
## Analysis Workflow
|
||||
|
||||
### Step 1: Backup and Generate Temporarily
|
||||
|
||||
```bash
|
||||
cp -r generated/swagger/[api-name] /tmp/[api-name].backup
|
||||
npm run generate:swagger:[api-name]
|
||||
```
|
||||
|
||||
### Step 2: Compare Files
|
||||
|
||||
```bash
|
||||
diff -u /tmp/[api-name].backup/models.ts generated/swagger/[api-name]/models.ts
|
||||
diff -u /tmp/[api-name].backup/services.ts generated/swagger/[api-name]/services.ts
|
||||
```
|
||||
|
||||
### Step 3: Categorize Changes
|
||||
|
||||
**🔴 Breaking (Critical):**
|
||||
- Removed properties from response models
|
||||
- Changed property types (string → number)
|
||||
- Removed endpoints
|
||||
- Optional → required fields
|
||||
- Removed enum values
|
||||
|
||||
**✅ Compatible (Safe):**
|
||||
- Added properties (non-breaking)
|
||||
- New endpoints
|
||||
- Added optional parameters
|
||||
- New enum values
|
||||
|
||||
**⚠️ Warnings (Review):**
|
||||
- Property renamed (old removed + new added)
|
||||
- Changed default values
|
||||
- Changed validation rules
|
||||
- Added required request fields
|
||||
|
||||
### Step 4: Analyze Impact
|
||||
|
||||
For each breaking change, use `Explore` agent to find usages:
|
||||
|
||||
```bash
|
||||
# Example: Find usages of removed property
|
||||
grep -r "removedProperty" libs/*/data-access --include="*.ts"
|
||||
```
|
||||
|
||||
List:
|
||||
- Affected files
|
||||
- Services impacted
|
||||
- Estimated refactoring effort
|
||||
|
||||
### Step 5: Generate Migration Strategy
|
||||
|
||||
Based on severity:
|
||||
|
||||
**High Impact (multiple breaking changes):**
|
||||
1. Create migration branch
|
||||
2. Document all changes
|
||||
3. Update services incrementally
|
||||
4. Comprehensive testing
|
||||
|
||||
**Medium Impact:**
|
||||
1. Fix compilation errors
|
||||
2. Update affected tests
|
||||
3. Deploy with monitoring
|
||||
|
||||
**Low Impact:**
|
||||
1. Minor updates
|
||||
2. Deploy
|
||||
|
||||
### Step 6: Create Report
|
||||
|
||||
```
|
||||
API Breaking Changes Analysis
|
||||
==============================
|
||||
|
||||
API: [api-name]
|
||||
Analysis Date: [timestamp]
|
||||
|
||||
📊 Summary
|
||||
----------
|
||||
Breaking Changes: XX
|
||||
Warnings: XX
|
||||
Compatible Changes: XX
|
||||
|
||||
🔴 Breaking Changes
|
||||
-------------------
|
||||
1. Removed Property: OrderResponse.deliveryDate
|
||||
Files Affected: 2
|
||||
- libs/oms/data-access/src/lib/services/order.service.ts:45
|
||||
- libs/oms/feature/order-detail/src/lib/component.ts:78
|
||||
Impact: Medium
|
||||
Fix: Remove references or use alternativeDate
|
||||
|
||||
2. Type Changed: ProductResponse.price (string → number)
|
||||
Files Affected: 1
|
||||
- libs/catalogue/data-access/src/lib/services/product.service.ts:32
|
||||
Impact: High
|
||||
Fix: Update parsing logic
|
||||
|
||||
⚠️ Warnings
|
||||
-----------
|
||||
1. Possible Rename: CustomerResponse.customerName → fullName
|
||||
Action: Verify with backend team
|
||||
|
||||
✅ Compatible Changes
|
||||
---------------------
|
||||
1. Added Property: OrderResponse.estimatedDelivery
|
||||
2. New Endpoint: GET /api/v2/orders/bulk
|
||||
|
||||
💡 Migration Strategy
|
||||
---------------------
|
||||
Approach: [High/Medium/Low Impact]
|
||||
Estimated Effort: [hours]
|
||||
Steps: [numbered list]
|
||||
|
||||
🎯 Recommendation
|
||||
-----------------
|
||||
[Proceed with sync / Fix critical issues first / Coordinate with backend]
|
||||
```
|
||||
|
||||
### Step 7: Cleanup
|
||||
|
||||
```bash
|
||||
rm -rf /tmp/[api-name].backup
|
||||
# Or restore if needed
|
||||
```
|
||||
|
||||
## References
|
||||
|
||||
- CLAUDE.md API Integration
|
||||
- Semantic Versioning: https://semver.org
|
||||
362
.claude/skills/api-sync/SKILL.md
Normal file
362
.claude/skills/api-sync/SKILL.md
Normal file
@@ -0,0 +1,362 @@
|
||||
---
|
||||
name: api-sync
|
||||
description: This skill should be used when regenerating Swagger/OpenAPI TypeScript API clients with breaking change detection. Handles generation of all 10 API clients (or specific ones), pre-generation impact analysis, Unicode cleanup, TypeScript validation, and affected test execution. Use when user requests "API sync", "regenerate swagger", "check breaking changes", or indicates backend API changes.
|
||||
---
|
||||
|
||||
# API Sync Manager
|
||||
|
||||
## Overview
|
||||
|
||||
Automate the complete lifecycle of TypeScript API client regeneration from Swagger/OpenAPI specifications. Provides pre-generation breaking change detection, automatic post-processing, impact analysis, validation, and migration recommendations for all 10 API clients in the ISA-Frontend monorepo.
|
||||
|
||||
## Available APIs
|
||||
|
||||
availability-api, cat-search-api, checkout-api, crm-api, eis-api, inventory-api, isa-api, oms-api, print-api, wws-api
|
||||
|
||||
## Unified Sync Workflow
|
||||
|
||||
### Step 1: Pre-Generation Check
|
||||
|
||||
```bash
|
||||
# Check uncommitted changes
|
||||
git status generated/swagger/
|
||||
|
||||
# Verify no manual edits will be lost
|
||||
git diff generated/swagger/
|
||||
```
|
||||
|
||||
If uncommitted changes exist, warn user and ask to proceed. If manual edits detected, strongly recommend committing first.
|
||||
|
||||
### Step 2: Pre-Generation Breaking Change Detection
|
||||
|
||||
Generate to temporary location to compare without affecting working directory:
|
||||
|
||||
```bash
|
||||
# Backup current state
|
||||
cp -r generated/swagger/[api-name] /tmp/[api-name].backup
|
||||
|
||||
# Generate to temp location for comparison
|
||||
npm run generate:swagger:[api-name]
|
||||
```
|
||||
|
||||
**Compare Models and Services:**
|
||||
|
||||
```bash
|
||||
diff -u /tmp/[api-name].backup/models.ts generated/swagger/[api-name]/models.ts
|
||||
diff -u /tmp/[api-name].backup/services.ts generated/swagger/[api-name]/services.ts
|
||||
```
|
||||
|
||||
**Categorize Changes:**
|
||||
|
||||
**🔴 Breaking (Critical):**
|
||||
- Removed properties from response models
|
||||
- Changed property types (string → number, object → array)
|
||||
- Removed endpoints
|
||||
- Optional → required fields in request models
|
||||
- Removed enum values
|
||||
- Changed endpoint paths or HTTP methods
|
||||
|
||||
**⚠️ Warnings (Review Required):**
|
||||
- Property renamed (old removed + new added)
|
||||
- Changed default values
|
||||
- Changed validation rules (min/max length, pattern)
|
||||
- Added required request fields
|
||||
- Changed parameter locations (query → body)
|
||||
|
||||
**✅ Compatible (Safe):**
|
||||
- Added properties to response models
|
||||
- New endpoints
|
||||
- Added optional parameters
|
||||
- New enum values
|
||||
- Required → optional fields
|
||||
|
||||
### Step 3: Impact Analysis
|
||||
|
||||
For each breaking or warning change, analyze codebase impact:
|
||||
|
||||
```bash
|
||||
# Find all imports from affected API
|
||||
grep -r "from '@generated/swagger/[api-name]" libs/ --include="*.ts"
|
||||
|
||||
# Find usages of specific removed/changed items
|
||||
grep -r "[RemovedType|removedProperty|removedMethod]" libs/*/data-access --include="*.ts"
|
||||
```
|
||||
|
||||
**Document:**
|
||||
- Affected files (with line numbers)
|
||||
- Services impacted
|
||||
- Components/stores using affected services
|
||||
- Estimated refactoring effort (hours)
|
||||
|
||||
### Step 4: Generate Migration Strategy
|
||||
|
||||
Based on breaking change severity:
|
||||
|
||||
**High Impact (>5 breaking changes or critical endpoints):**
|
||||
1. Create dedicated migration branch from develop
|
||||
2. Document all changes in migration guide
|
||||
3. Update data-access services incrementally
|
||||
4. Update affected stores and components
|
||||
5. Comprehensive test coverage
|
||||
6. Coordinate deployment with backend team
|
||||
|
||||
**Medium Impact (2-5 breaking changes):**
|
||||
1. Fix TypeScript compilation errors
|
||||
2. Update affected data-access services
|
||||
3. Update tests
|
||||
4. Run affected test suite
|
||||
5. Deploy with monitoring
|
||||
|
||||
**Low Impact (1 breaking change, minor impact):**
|
||||
1. Apply minimal updates
|
||||
2. Verify tests pass
|
||||
3. Deploy normally
|
||||
|
||||
### Step 5: Execute Generation
|
||||
|
||||
If user approves proceeding after impact analysis:
|
||||
|
||||
```bash
|
||||
# All APIs
|
||||
npm run generate:swagger
|
||||
|
||||
# Specific API (if api-name provided)
|
||||
npm run generate:swagger:[api-name]
|
||||
```
|
||||
|
||||
### Step 6: Verify Unicode Cleanup
|
||||
|
||||
Automatic cleanup via `tools/fix-files.js` should execute during generation. Verify:
|
||||
|
||||
```bash
|
||||
# Scan for remaining Unicode issues
|
||||
grep -r "\\\\u00" generated/swagger/ || echo "✅ No Unicode issues"
|
||||
```
|
||||
|
||||
If issues remain, run manually:
|
||||
|
||||
```bash
|
||||
node tools/fix-files.js
|
||||
```
|
||||
|
||||
### Step 7: TypeScript Validation
|
||||
|
||||
```bash
|
||||
# Full TypeScript compilation check
|
||||
npx tsc --noEmit
|
||||
|
||||
# If errors, show affected files
|
||||
npx tsc --noEmit | head -20
|
||||
```
|
||||
|
||||
Document compilation errors:
|
||||
- Missing properties
|
||||
- Type mismatches
|
||||
- Incompatible signatures
|
||||
|
||||
### Step 8: Run Affected Tests
|
||||
|
||||
```bash
|
||||
# Run tests for affected projects
|
||||
npx nx affected:test --skip-nx-cache --base=HEAD~1
|
||||
|
||||
# Lint affected projects
|
||||
npx nx affected:lint --base=HEAD~1
|
||||
```
|
||||
|
||||
Monitor test failures and categorize:
|
||||
- Mock data mismatches (update fixtures)
|
||||
- Type assertion failures (update test types)
|
||||
- Integration test failures (API contract changes)
|
||||
|
||||
### Step 9: Generate Comprehensive Report
|
||||
|
||||
```
|
||||
API Sync Manager Report
|
||||
=======================
|
||||
|
||||
API: [api-name | all]
|
||||
Sync Date: [timestamp]
|
||||
Generation: ✅ Success / ❌ Failed
|
||||
|
||||
📊 Change Summary
|
||||
-----------------
|
||||
Breaking Changes: XX
|
||||
Warnings: XX
|
||||
Compatible Changes: XX
|
||||
Files Modified: XX
|
||||
|
||||
🔴 Breaking Changes
|
||||
-------------------
|
||||
1. [API Name] - Removed Property: OrderResponse.deliveryDate
|
||||
Impact: Medium (2 files affected)
|
||||
Files:
|
||||
- libs/oms/data-access/src/lib/services/order.service.ts:45
|
||||
- libs/oms/feature/order-detail/src/lib/component.ts:78
|
||||
Fix Strategy: Remove references or use alternativeDate field
|
||||
Estimated Effort: 30 minutes
|
||||
|
||||
2. [API Name] - Type Changed: ProductResponse.price (string → number)
|
||||
Impact: High (1 file + cascading changes)
|
||||
Files:
|
||||
- libs/catalogue/data-access/src/lib/services/product.service.ts:32
|
||||
Fix Strategy: Remove string parsing, update price calculations
|
||||
Estimated Effort: 1 hour
|
||||
|
||||
⚠️ Warnings (Review Required)
|
||||
------------------------------
|
||||
1. [API Name] - Possible Rename: CustomerResponse.customerName → fullName
|
||||
Action: Verify with backend team if intentional
|
||||
Migration: Update references if confirmed rename
|
||||
|
||||
2. [API Name] - New Required Field: CreateOrderRequest.taxId
|
||||
Action: Update order creation flows to provide taxId
|
||||
Impact: 3 order creation forms
|
||||
|
||||
✅ Compatible Changes
|
||||
---------------------
|
||||
1. [API Name] - Added Property: OrderResponse.estimatedDelivery
|
||||
2. [API Name] - New Endpoint: GET /api/v2/orders/bulk
|
||||
3. [API Name] - Added Optional Parameter: includeArchived to GET /orders
|
||||
|
||||
📊 Validation Results
|
||||
---------------------
|
||||
TypeScript Compilation: ✅ Pass / ❌ XX errors
|
||||
Unicode Cleanup: ✅ Complete
|
||||
Tests: XX/XX passing (YY affected)
|
||||
Lint: ✅ Pass / ⚠️ XX warnings
|
||||
|
||||
❌ Test Failures (if any)
|
||||
-------------------------
|
||||
1. order.service.spec.ts - Mock response missing deliveryDate
|
||||
Fix: Update mock fixture
|
||||
2. product.component.spec.ts - Price type assertion failed
|
||||
Fix: Change expect(price).toBe("10.99") → expect(price).toBe(10.99)
|
||||
|
||||
💡 Migration Strategy
|
||||
---------------------
|
||||
Approach: [High/Medium/Low Impact]
|
||||
Estimated Total Effort: [hours]
|
||||
|
||||
Steps:
|
||||
1. [Fix compilation errors in data-access services]
|
||||
2. [Update test mocks and fixtures]
|
||||
3. [Update components using affected properties]
|
||||
4. [Run full test suite]
|
||||
5. [Deploy with backend coordination]
|
||||
|
||||
🎯 Recommendation
|
||||
-----------------
|
||||
[One of the following:]
|
||||
✅ Safe to proceed - Only compatible changes detected
|
||||
⚠️ Proceed with caution - Fix breaking changes before deployment
|
||||
🔴 Coordinate with backend - High impact changes require migration plan
|
||||
🛑 Block regeneration - Critical breaking changes, backend rollback needed
|
||||
|
||||
📋 Next Steps
|
||||
-------------
|
||||
[Specific actions user should take]
|
||||
- [ ] Fix compilation errors in [files]
|
||||
- [ ] Update test mocks in [files]
|
||||
- [ ] Coordinate deployment timing with backend team
|
||||
- [ ] Monitor [specific endpoints] after deployment
|
||||
```
|
||||
|
||||
### Step 10: Cleanup
|
||||
|
||||
```bash
|
||||
# Remove temporary backup
|
||||
rm -rf /tmp/[api-name].backup
|
||||
|
||||
# Or restore if generation needs to be reverted
|
||||
# cp -r /tmp/[api-name].backup generated/swagger/[api-name]
|
||||
```
|
||||
|
||||
## Error Handling
|
||||
|
||||
**Generation Fails:**
|
||||
- Check OpenAPI spec URLs in package.json scripts
|
||||
- Verify backend API is accessible
|
||||
- Check for malformed OpenAPI specification
|
||||
|
||||
**Unicode Cleanup Fails:**
|
||||
- Run `node tools/fix-files.js` manually
|
||||
- Check for new Unicode patterns not covered by script
|
||||
|
||||
**TypeScript Compilation Errors:**
|
||||
- Review breaking changes section in report
|
||||
- Update affected data-access services first
|
||||
- Fix cascading type errors in consumers
|
||||
|
||||
**Test Failures:**
|
||||
- Update mock data to match new API contracts
|
||||
- Fix type assertions in tests
|
||||
- Update integration test expectations
|
||||
|
||||
**Diff Detection Issues:**
|
||||
- Ensure clean git state before running
|
||||
- Check file permissions on generated/ directory
|
||||
- Verify backup location is writable
|
||||
|
||||
## Advanced Usage
|
||||
|
||||
**Compare Specific Endpoints Only:**
|
||||
|
||||
```bash
|
||||
# Extract and compare specific interface
|
||||
grep -A 20 "interface OrderResponse" /tmp/[api-name].backup/models.ts
|
||||
grep -A 20 "interface OrderResponse" generated/swagger/[api-name]/models.ts
|
||||
```
|
||||
|
||||
**Bulk API Sync with Change Detection:**
|
||||
|
||||
Run pre-generation detection for all APIs before regenerating:
|
||||
|
||||
```bash
|
||||
for api in availability-api cat-search-api checkout-api crm-api eis-api inventory-api isa-api oms-api print-api wws-api; do
|
||||
echo "Checking $api..."
|
||||
# Run detection workflow for each
|
||||
done
|
||||
```
|
||||
|
||||
Generate consolidated report across all APIs before committing.
|
||||
|
||||
**Rollback Procedure:**
|
||||
|
||||
```bash
|
||||
# If sync causes critical issues
|
||||
git checkout generated/swagger/[api-name]
|
||||
git clean -fd generated/swagger/[api-name]
|
||||
|
||||
# Or restore from backup
|
||||
cp -r generated/swagger.backup.[timestamp]/* generated/swagger/
|
||||
```
|
||||
|
||||
## Integration with Git Workflow
|
||||
|
||||
**Recommended Commit Message:**
|
||||
|
||||
```
|
||||
feat(api): sync [api-name] API client [TASK-####]
|
||||
|
||||
Breaking changes:
|
||||
- Removed OrderResponse.deliveryDate (use estimatedDelivery)
|
||||
- Changed ProductResponse.price type (string → number)
|
||||
|
||||
Compatible changes:
|
||||
- Added OrderResponse.estimatedDelivery
|
||||
- New endpoint GET /api/v2/orders/bulk
|
||||
```
|
||||
|
||||
**Branch Strategy:**
|
||||
|
||||
- Low impact: Commit to feature branch directly
|
||||
- Medium impact: Create `sync/[api-name]-[date]` branch
|
||||
- High impact: Create `migration/[api-name]-[version]` branch with migration guide
|
||||
|
||||
## References
|
||||
|
||||
- CLAUDE.md API Integration section
|
||||
- package.json swagger generation scripts
|
||||
- tools/fix-files.js for Unicode cleanup logic
|
||||
- Semantic Versioning: https://semver.org
|
||||
@@ -1,171 +1,177 @@
|
||||
---
|
||||
name: architecture-documentation
|
||||
description: Generate architecture documentation (C4, Arc42, ADRs, PlantUML). Auto-invoke when user mentions "architecture docs", "C4 model", "ADR", "document architecture", "system design", or "create architecture diagram".
|
||||
---
|
||||
|
||||
# Architecture Documentation Skill
|
||||
|
||||
Generate comprehensive architecture documentation using modern frameworks and best practices.
|
||||
|
||||
## When to Use
|
||||
|
||||
- Creating or updating architecture documentation
|
||||
- Generating C4 model diagrams (Context, Container, Component, Code)
|
||||
- Writing Architecture Decision Records (ADRs)
|
||||
- Documenting system design and component relationships
|
||||
- Creating PlantUML or Mermaid diagrams
|
||||
|
||||
## Available Frameworks
|
||||
|
||||
### C4 Model
|
||||
Best for: Visualizing software architecture at different abstraction levels
|
||||
|
||||
Levels:
|
||||
1. **Context** - System landscape and external actors
|
||||
2. **Container** - High-level technology choices (apps, databases, etc.)
|
||||
3. **Component** - Internal structure of containers
|
||||
4. **Code** - Class/module level detail (optional)
|
||||
|
||||
See: `@references/c4-model.md` for patterns and examples
|
||||
|
||||
### Arc42 Template
|
||||
Best for: Comprehensive architecture documentation
|
||||
|
||||
Sections:
|
||||
1. Introduction and Goals
|
||||
2. Constraints
|
||||
3. Context and Scope
|
||||
4. Solution Strategy
|
||||
5. Building Block View
|
||||
6. Runtime View
|
||||
7. Deployment View
|
||||
8. Cross-cutting Concepts
|
||||
9. Architecture Decisions
|
||||
10. Quality Requirements
|
||||
11. Risks and Technical Debt
|
||||
12. Glossary
|
||||
|
||||
See: `@references/arc42.md` for template structure
|
||||
|
||||
### Architecture Decision Records (ADRs)
|
||||
Best for: Documenting individual architectural decisions
|
||||
|
||||
See: `@references/adr-template.md` for format and examples
|
||||
|
||||
## Workflow
|
||||
|
||||
### 1. Discovery Phase
|
||||
```bash
|
||||
# Find existing architecture files
|
||||
find . -name "*architecture*" -o -name "*.puml" -o -name "*.mmd"
|
||||
|
||||
# Identify service boundaries
|
||||
cat nx.json docker-compose.yml
|
||||
|
||||
# Check for existing ADRs
|
||||
ls -la docs/adr/ docs/decisions/
|
||||
```
|
||||
|
||||
### 2. Analysis Phase
|
||||
- Analyze codebase structure (`libs/`, `apps/`)
|
||||
- Identify dependencies from `tsconfig.base.json` paths
|
||||
- Review service boundaries from `project.json` tags
|
||||
- Map data flow from API definitions
|
||||
|
||||
### 3. Documentation Phase
|
||||
Based on the request, create appropriate documentation:
|
||||
|
||||
**For C4 diagrams:**
|
||||
```
|
||||
docs/architecture/
|
||||
├── c4-context.puml
|
||||
├── c4-container.puml
|
||||
└── c4-component-[name].puml
|
||||
```
|
||||
|
||||
**For ADRs:**
|
||||
```
|
||||
docs/adr/
|
||||
├── 0001-record-architecture-decisions.md
|
||||
├── 0002-[decision-title].md
|
||||
└── template.md
|
||||
```
|
||||
|
||||
**For Arc42:**
|
||||
```
|
||||
docs/architecture/
|
||||
└── arc42/
|
||||
├── 01-introduction.md
|
||||
├── 02-constraints.md
|
||||
└── ...
|
||||
```
|
||||
|
||||
## ISA-Frontend Specific Context
|
||||
|
||||
### Monorepo Structure
|
||||
- **apps/**: Angular applications
|
||||
- **libs/**: Shared libraries organized by domain
|
||||
- `libs/[domain]/feature/` - Feature modules
|
||||
- `libs/[domain]/data-access/` - State management
|
||||
- `libs/[domain]/ui/` - Presentational components
|
||||
- `libs/[domain]/util/` - Utilities
|
||||
|
||||
### Key Architectural Patterns
|
||||
- **Nx Monorepo** with strict module boundaries
|
||||
- **NgRx Signal Store** for state management
|
||||
- **Standalone Components** (Angular 20+)
|
||||
- **Domain-Driven Design** library organization
|
||||
|
||||
### Documentation Locations
|
||||
- ADRs: `docs/adr/`
|
||||
- Architecture diagrams: `docs/architecture/`
|
||||
- API documentation: Generated from Swagger/OpenAPI
|
||||
|
||||
## Output Standards
|
||||
|
||||
### PlantUML Format
|
||||
```plantuml
|
||||
@startuml C4_Context
|
||||
!include https://raw.githubusercontent.com/plantuml-stdlib/C4-PlantUML/master/C4_Context.puml
|
||||
|
||||
Person(user, "User", "System user")
|
||||
System(system, "ISA System", "Main application")
|
||||
System_Ext(external, "External API", "Third-party service")
|
||||
|
||||
Rel(user, system, "Uses")
|
||||
Rel(system, external, "Calls")
|
||||
@enduml
|
||||
```
|
||||
|
||||
### Mermaid Format
|
||||
```mermaid
|
||||
graph TD
|
||||
A[User] --> B[ISA App]
|
||||
B --> C[API Gateway]
|
||||
C --> D[Backend Services]
|
||||
```
|
||||
|
||||
### ADR Format
|
||||
```markdown
|
||||
# ADR-XXXX: [Title]
|
||||
|
||||
## Status
|
||||
[Proposed | Accepted | Deprecated | Superseded]
|
||||
|
||||
## Context
|
||||
[What is the issue?]
|
||||
|
||||
## Decision
|
||||
[What was decided?]
|
||||
|
||||
## Consequences
|
||||
[What are the results?]
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Start with Context** - Always begin with C4 Level 1 (System Context)
|
||||
2. **Use Consistent Notation** - Stick to one diagramming tool/format
|
||||
3. **Keep ADRs Atomic** - One decision per ADR
|
||||
4. **Version Control** - Commit documentation with code changes
|
||||
5. **Review Regularly** - Architecture docs decay; schedule reviews
|
||||
---
|
||||
name: arch-docs
|
||||
description: Generate architecture documentation (C4, Arc42, ADRs, PlantUML). Auto-invoke when user mentions "architecture docs", "C4 model", "ADR", "document architecture", "system design", or "create architecture diagram".
|
||||
---
|
||||
|
||||
# Architecture Documentation
|
||||
|
||||
Generate comprehensive architecture documentation using modern frameworks and best practices.
|
||||
|
||||
## Available Frameworks
|
||||
|
||||
### C4 Model
|
||||
Visualize software architecture at different abstraction levels.
|
||||
|
||||
**Levels:**
|
||||
1. **Context** - System landscape and external actors
|
||||
2. **Container** - High-level technology choices (apps, databases, etc.)
|
||||
3. **Component** - Internal structure of containers
|
||||
4. **Code** - Class/module level detail (optional)
|
||||
|
||||
**When to use:** Visualizing system architecture, creating diagrams for stakeholders at different technical levels.
|
||||
|
||||
See `references/c4-model.md` for detailed patterns and examples.
|
||||
|
||||
### Arc42 Template
|
||||
Comprehensive architecture documentation covering all aspects.
|
||||
|
||||
**Sections:**
|
||||
1. Introduction and Goals
|
||||
2. Constraints
|
||||
3. Context and Scope
|
||||
4. Solution Strategy
|
||||
5. Building Block View
|
||||
6. Runtime View
|
||||
7. Deployment View
|
||||
8. Cross-cutting Concepts
|
||||
9. Architecture Decisions
|
||||
10. Quality Requirements
|
||||
11. Risks and Technical Debt
|
||||
12. Glossary
|
||||
|
||||
**When to use:** Creating complete architecture documentation, documenting system-wide concerns, establishing quality goals.
|
||||
|
||||
See `references/arc42.md` for complete template structure.
|
||||
|
||||
### Architecture Decision Records (ADRs)
|
||||
Document individual architectural decisions with context and consequences.
|
||||
|
||||
**When to use:** Recording significant decisions, documenting trade-offs, tracking architectural evolution.
|
||||
|
||||
See `references/adr-template.md` for format and examples.
|
||||
|
||||
## Workflow
|
||||
|
||||
### 1. Discovery Phase
|
||||
Understand the system structure:
|
||||
|
||||
```bash
|
||||
# Find existing architecture files
|
||||
find . -name "*architecture*" -o -name "*.puml" -o -name "*.mmd"
|
||||
|
||||
# Identify service boundaries
|
||||
cat nx.json docker-compose.yml
|
||||
|
||||
# Check for existing ADRs
|
||||
ls -la docs/adr/ docs/decisions/
|
||||
```
|
||||
|
||||
### 2. Analysis Phase
|
||||
- Analyze codebase structure (`libs/`, `apps/`)
|
||||
- Identify dependencies from `tsconfig.base.json` paths
|
||||
- Review service boundaries from `project.json` tags
|
||||
- Map data flow from API definitions
|
||||
|
||||
### 3. Documentation Phase
|
||||
Choose appropriate output format based on request:
|
||||
|
||||
**For C4 diagrams:**
|
||||
```
|
||||
docs/architecture/
|
||||
├── c4-context.puml
|
||||
├── c4-container.puml
|
||||
└── c4-component-[name].puml
|
||||
```
|
||||
|
||||
**For ADRs:**
|
||||
```
|
||||
docs/adr/
|
||||
├── 0001-record-architecture-decisions.md
|
||||
├── 0002-[decision-title].md
|
||||
└── template.md
|
||||
```
|
||||
|
||||
**For Arc42:**
|
||||
```
|
||||
docs/architecture/
|
||||
└── arc42/
|
||||
├── 01-introduction.md
|
||||
├── 02-constraints.md
|
||||
└── ...
|
||||
```
|
||||
|
||||
## ISA-Frontend Context
|
||||
|
||||
### Monorepo Structure
|
||||
- **apps/**: Angular applications
|
||||
- **libs/**: Shared libraries organized by domain
|
||||
- `libs/[domain]/feature/` - Feature modules
|
||||
- `libs/[domain]/data-access/` - State management
|
||||
- `libs/[domain]/ui/` - Presentational components
|
||||
- `libs/[domain]/util/` - Utilities
|
||||
|
||||
### Key Architectural Patterns
|
||||
- **Nx Monorepo** with strict module boundaries
|
||||
- **NgRx Signal Store** for state management
|
||||
- **Standalone Components** (Angular 20+)
|
||||
- **Domain-Driven Design** library organization
|
||||
|
||||
### Documentation Locations
|
||||
- ADRs: `docs/adr/`
|
||||
- Architecture diagrams: `docs/architecture/`
|
||||
- API documentation: Generated from Swagger/OpenAPI
|
||||
|
||||
## Output Standards
|
||||
|
||||
### PlantUML Format
|
||||
```plantuml
|
||||
@startuml C4_Context
|
||||
!include https://raw.githubusercontent.com/plantuml-stdlib/C4-PlantUML/master/C4_Context.puml
|
||||
|
||||
Person(user, "User", "System user")
|
||||
System(system, "ISA System", "Main application")
|
||||
System_Ext(external, "External API", "Third-party service")
|
||||
|
||||
Rel(user, system, "Uses")
|
||||
Rel(system, external, "Calls")
|
||||
@enduml
|
||||
```
|
||||
|
||||
### Mermaid Format
|
||||
```mermaid
|
||||
graph TD
|
||||
A[User] --> B[ISA App]
|
||||
B --> C[API Gateway]
|
||||
C --> D[Backend Services]
|
||||
```
|
||||
|
||||
### ADR Format
|
||||
```markdown
|
||||
# ADR-XXXX: [Title]
|
||||
|
||||
## Status
|
||||
[Proposed | Accepted | Deprecated | Superseded]
|
||||
|
||||
## Context
|
||||
[What is the issue?]
|
||||
|
||||
## Decision
|
||||
[What was decided?]
|
||||
|
||||
## Consequences
|
||||
[What are the results?]
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Start with Context** - Always begin with C4 Level 1 (System Context)
|
||||
2. **Use Consistent Notation** - Stick to one diagramming tool/format
|
||||
3. **Keep ADRs Atomic** - One decision per ADR
|
||||
4. **Version Control** - Commit documentation with code changes
|
||||
5. **Review Regularly** - Architecture docs decay; schedule reviews
|
||||
|
||||
## References
|
||||
|
||||
- `references/c4-model.md` - C4 model patterns, templates, and ISA-Frontend domain structure
|
||||
- `references/arc42.md` - Complete Arc42 template with all 12 sections
|
||||
- `references/adr-template.md` - ADR format with examples and naming conventions
|
||||
@@ -1,208 +0,0 @@
|
||||
---
|
||||
name: architecture-enforcer
|
||||
description: This skill should be used when checking architecture compliance, validating layer boundaries (Feature→Feature violations), detecting circular dependencies, or user mentions "check architecture", "validate boundaries", "check imports". Validates import boundaries and architectural rules in ISA-Frontend monorepo.
|
||||
---
|
||||
|
||||
# Architecture Enforcer
|
||||
|
||||
## Overview
|
||||
|
||||
Validate and enforce architectural boundaries in the monorepo. Checks import rules, detects violations, generates dependency graphs, and suggests refactoring.
|
||||
|
||||
## When to Use This Skill
|
||||
|
||||
Invoke when user wants to:
|
||||
- Validate import boundaries
|
||||
- Check architectural rules
|
||||
- Find dependency violations
|
||||
- Mentioned "check architecture" or "validate imports"
|
||||
|
||||
## Architectural Rules
|
||||
|
||||
**✅ Allowed:**
|
||||
- Feature → Data Access
|
||||
- Feature → UI
|
||||
- Feature → Util
|
||||
- Data Access → Util
|
||||
|
||||
**❌ Forbidden:**
|
||||
- Feature → Feature
|
||||
- Data Access → Feature
|
||||
- UI → Feature
|
||||
- Cross-domain (OMS ↔ Remission)
|
||||
|
||||
## Enforcement Workflow
|
||||
|
||||
### Step 1: Run Nx Dependency Checks
|
||||
|
||||
```bash
|
||||
# Lint all (includes boundary checks)
|
||||
npx nx run-many --target=lint --all
|
||||
|
||||
# Or specific library
|
||||
npx nx lint [library-name]
|
||||
```
|
||||
|
||||
### Step 2: Generate Dependency Graph
|
||||
|
||||
```bash
|
||||
# Visual graph
|
||||
npx nx graph
|
||||
|
||||
# Focus on specific project
|
||||
npx nx graph --focus=[library-name]
|
||||
|
||||
# Affected projects
|
||||
npx nx affected:graph
|
||||
```
|
||||
|
||||
### Step 3: Scan for Violations
|
||||
|
||||
**Check for Circular Dependencies:**
|
||||
Use `Explore` agent to find A→B→A patterns.
|
||||
|
||||
**Check Layer Violations:**
|
||||
```bash
|
||||
# Find feature-to-feature imports
|
||||
grep -r "from '@isa/[^/]*/feature" libs/*/feature/ --include="*.ts"
|
||||
```
|
||||
|
||||
**Check Relative Imports:**
|
||||
```bash
|
||||
# Should use path aliases, not relative
|
||||
grep -r "from '\.\./\.\./\.\." libs/ --include="*.ts"
|
||||
```
|
||||
|
||||
**Check Direct Swagger Imports:**
|
||||
```bash
|
||||
# Should go through data-access
|
||||
grep -r "from '@generated/swagger" libs/*/feature/ --include="*.ts"
|
||||
```
|
||||
|
||||
### Step 4: Categorize Violations
|
||||
|
||||
**🔴 Critical:**
|
||||
- Circular dependencies
|
||||
- Feature → Feature
|
||||
- Data Access → Feature
|
||||
- Cross-domain dependencies
|
||||
|
||||
**⚠️ Warnings:**
|
||||
- Relative imports (should use aliases)
|
||||
- Missing tags in project.json
|
||||
- Deep import paths
|
||||
|
||||
**ℹ️ Info:**
|
||||
- Potential shared utilities
|
||||
|
||||
### Step 5: Generate Violation Report
|
||||
|
||||
For each violation:
|
||||
```
|
||||
📍 libs/oms/feature/return-search/src/lib/component.ts:12
|
||||
🔴 Layer Violation
|
||||
❌ Feature importing from another feature
|
||||
|
||||
Import: import { OrderList } from '@isa/oms/feature-order-list';
|
||||
Issue: Feature libraries should not depend on other features
|
||||
Fix: Move shared component to @isa/shared/* or @isa/ui/*
|
||||
```
|
||||
|
||||
### Step 6: Suggest Refactoring
|
||||
|
||||
**For repeated patterns:**
|
||||
- Create shared library for common components
|
||||
- Extract shared utilities to util library
|
||||
- Move API clients to data-access layer
|
||||
- Create facade services
|
||||
|
||||
### Step 7: Visualize Problems
|
||||
|
||||
```bash
|
||||
npx nx graph --focus=[problematic-library]
|
||||
```
|
||||
|
||||
### Step 8: Generate Report
|
||||
|
||||
```
|
||||
Import Boundary Analysis
|
||||
========================
|
||||
|
||||
Scope: [All | Specific library]
|
||||
|
||||
📊 Summary
|
||||
----------
|
||||
Total violations: XX
|
||||
🔴 Critical: XX
|
||||
⚠️ Warnings: XX
|
||||
ℹ️ Info: XX
|
||||
|
||||
🔍 Violations by Type
|
||||
---------------------
|
||||
Layer violations: XX
|
||||
Domain violations: XX
|
||||
Circular dependencies: XX
|
||||
Path alias violations: XX
|
||||
|
||||
🔴 Critical Violations
|
||||
----------------------
|
||||
1. [File:Line]
|
||||
Issue: Feature → Feature dependency
|
||||
Fix: Extract to @isa/shared/component-name
|
||||
|
||||
2. [File:Line]
|
||||
Issue: Circular dependency
|
||||
Fix: Extract interface to util library
|
||||
|
||||
💡 Refactoring Recommendations
|
||||
-------------------------------
|
||||
1. Create @isa/shared/order-components
|
||||
- Move: [list of shared components]
|
||||
- Benefits: Reusable, breaks circular deps
|
||||
|
||||
2. Extract interfaces to @isa/oms/util
|
||||
- Move: [list of interfaces]
|
||||
- Benefits: Breaks circular dependencies
|
||||
|
||||
📈 Dependency Graph
|
||||
-------------------
|
||||
npx nx graph --focus=[library]
|
||||
|
||||
🎯 Next Steps
|
||||
-------------
|
||||
1. Fix critical violations
|
||||
2. Update ESLint config
|
||||
3. Refactor shared components
|
||||
4. Re-run: architecture-enforcer
|
||||
```
|
||||
|
||||
## Common Fixes
|
||||
|
||||
**Circular Dependencies:**
|
||||
```typescript
|
||||
// Extract shared interface to util
|
||||
// @isa/oms/util
|
||||
export interface OrderId { id: string; }
|
||||
|
||||
// Both services import from util
|
||||
import { OrderId } from '@isa/oms/util';
|
||||
```
|
||||
|
||||
**Layer Violations:**
|
||||
```typescript
|
||||
// Move shared component from feature to ui
|
||||
// Before: @isa/oms/feature-shared
|
||||
// After: @isa/ui/order-components
|
||||
```
|
||||
|
||||
**Path Alias Usage:**
|
||||
```typescript
|
||||
// BEFORE: import { Service } from '../../../data-access/src/lib/service';
|
||||
// AFTER: import { Service } from '@isa/oms/data-access';
|
||||
```
|
||||
|
||||
## References
|
||||
|
||||
- CLAUDE.md Architecture section
|
||||
- Nx enforce-module-boundaries: https://nx.dev/nx-api/eslint-plugin/documents/enforce-module-boundaries
|
||||
- tsconfig.base.json (path aliases)
|
||||
590
.claude/skills/architecture-validator/SKILL.md
Normal file
590
.claude/skills/architecture-validator/SKILL.md
Normal file
@@ -0,0 +1,590 @@
|
||||
---
|
||||
name: architecture-validator
|
||||
description: This skill should be used when validating architecture compliance, checking import boundaries, detecting circular dependencies, finding layer violations (Feature→Feature), or user mentions "check architecture", "validate boundaries", "circular dependencies", "dependency cycles". Validates architectural rules, detects cycles using graph analysis, and provides fix strategies for the ISA-Frontend monorepo.
|
||||
---
|
||||
|
||||
# Architecture Validator
|
||||
|
||||
## Overview
|
||||
|
||||
Validate and enforce architectural boundaries in the ISA-Frontend monorepo. Detects import boundary violations, circular dependencies, layer violations, and cross-domain dependencies. Provides automated fix strategies using graph analysis, dependency injection, interface extraction, and shared code refactoring.
|
||||
|
||||
## Architectural Rules
|
||||
|
||||
**Allowed Dependencies:**
|
||||
- Feature → Data Access
|
||||
- Feature → UI
|
||||
- Feature → Util
|
||||
- Data Access → Util
|
||||
|
||||
**Forbidden Dependencies:**
|
||||
- Feature → Feature
|
||||
- Data Access → Feature
|
||||
- UI → Feature
|
||||
- Cross-domain (OMS ↔ Remission)
|
||||
|
||||
**Severity Levels:**
|
||||
|
||||
🔴 **Critical (Must Fix):**
|
||||
- Circular dependencies in services/data-access
|
||||
- Feature → Feature dependencies
|
||||
- Data Access → Feature dependencies
|
||||
- Cross-domain violations
|
||||
|
||||
⚠️ **Warning (Should Fix):**
|
||||
- Component-to-component cycles
|
||||
- Relative import paths (should use aliases)
|
||||
- Missing architectural tags in project.json
|
||||
- Deep import paths
|
||||
|
||||
ℹ️ **Info (Review):**
|
||||
- Type-only circular references (may be acceptable)
|
||||
- Potential shared utilities
|
||||
- Test file circular imports
|
||||
|
||||
## Validation Workflow
|
||||
|
||||
### Step 1: Run Automated Checks
|
||||
|
||||
**Nx Dependency Validation:**
|
||||
```bash
|
||||
# Lint all projects (includes boundary checks)
|
||||
npx nx run-many --target=lint --all
|
||||
|
||||
# Or specific library
|
||||
npx nx lint [library-name]
|
||||
|
||||
# Check for circular dependency warnings
|
||||
npx nx run-many --target=lint --all 2>&1 | grep -i "circular"
|
||||
```
|
||||
|
||||
**TypeScript Compilation Check:**
|
||||
```bash
|
||||
# Detect cycles through compilation
|
||||
npx tsc --noEmit --strict 2>&1 | grep -i "circular\|cycle"
|
||||
```
|
||||
|
||||
**Madge Analysis (if installed):**
|
||||
```bash
|
||||
# Install globally if needed
|
||||
npm install -g madge
|
||||
|
||||
# Detect circular dependencies
|
||||
madge --circular --extensions ts libs/
|
||||
|
||||
# Generate visual dependency graph
|
||||
madge --circular --image circular-deps.svg libs/
|
||||
```
|
||||
|
||||
### Step 2: Generate Dependency Graph
|
||||
|
||||
```bash
|
||||
# Visual interactive graph
|
||||
npx nx graph
|
||||
|
||||
# Focus on specific problematic project
|
||||
npx nx graph --focus=[library-name]
|
||||
|
||||
# Show only affected projects
|
||||
npx nx affected:graph
|
||||
```
|
||||
|
||||
### Step 3: Scan for Specific Violations
|
||||
|
||||
**Feature-to-Feature Imports:**
|
||||
```bash
|
||||
grep -r "from '@isa/[^/]*/feature" libs/*/feature/ --include="*.ts"
|
||||
```
|
||||
|
||||
**Relative Import Paths:**
|
||||
```bash
|
||||
# Should use path aliases, not relative paths
|
||||
grep -r "from '\.\./\.\./\.\." libs/ --include="*.ts"
|
||||
```
|
||||
|
||||
**Direct Swagger Imports in Features:**
|
||||
```bash
|
||||
# Features should go through data-access layer
|
||||
grep -r "from '@generated/swagger" libs/*/feature/ --include="*.ts"
|
||||
```
|
||||
|
||||
**Cross-Domain Imports:**
|
||||
```bash
|
||||
# OMS should not import from Remission and vice versa
|
||||
grep -r "from '@isa/remission" libs/oms/ --include="*.ts"
|
||||
grep -r "from '@isa/oms" libs/remission/ --include="*.ts"
|
||||
```
|
||||
|
||||
### Step 4: Analyze Each Violation
|
||||
|
||||
For each detected issue, categorize and document:
|
||||
|
||||
```
|
||||
📍 Violation Location
|
||||
libs/oms/feature-return-search/src/lib/component.ts:12
|
||||
|
||||
🔴 Violation Type: Feature → Feature Layer Violation
|
||||
|
||||
Import Statement:
|
||||
import { OrderList } from '@isa/oms/feature-order-list';
|
||||
|
||||
Issue Analysis:
|
||||
Feature libraries should not depend on other features.
|
||||
This creates tight coupling and prevents independent deployment.
|
||||
|
||||
Cycle Path (if circular):
|
||||
1. libs/oms/data-access/src/lib/services/order.service.ts
|
||||
→ imports OrderValidator
|
||||
2. libs/oms/data-access/src/lib/validators/order.validator.ts
|
||||
→ imports OrderService
|
||||
3. Back to order.service.ts (CYCLE)
|
||||
|
||||
Severity: 🔴 Critical
|
||||
Files Involved: 2
|
||||
```
|
||||
|
||||
### Step 5: Choose Fix Strategy
|
||||
|
||||
**Strategy 1: Extract to Shared Library**
|
||||
|
||||
Best for: Components, utilities, or types used across multiple features
|
||||
|
||||
```typescript
|
||||
// BEFORE (Violation)
|
||||
// @isa/oms/feature-return-search imports from @isa/oms/feature-order-list
|
||||
import { OrderList } from '@isa/oms/feature-order-list';
|
||||
|
||||
// AFTER (Fixed)
|
||||
// Create @isa/ui/order-components or @isa/oms/ui-order
|
||||
export { OrderList } from './order-list.component';
|
||||
|
||||
// Both features now import from shared UI library
|
||||
import { OrderList } from '@isa/ui/order-components';
|
||||
```
|
||||
|
||||
**Strategy 2: Interface Extraction**
|
||||
|
||||
Best for: Breaking circular dependencies with type-only references
|
||||
|
||||
```typescript
|
||||
// BEFORE (Circular: order.ts ↔ customer.ts)
|
||||
// order.ts
|
||||
import { Customer } from './customer';
|
||||
export class Order {
|
||||
customer: Customer;
|
||||
}
|
||||
|
||||
// customer.ts
|
||||
import { Order } from './order';
|
||||
export class Customer {
|
||||
orders: Order[];
|
||||
}
|
||||
|
||||
// AFTER (Fixed)
|
||||
// Create order.interface.ts
|
||||
export interface IOrder {
|
||||
id: string;
|
||||
customerId: string;
|
||||
}
|
||||
|
||||
// Create customer.interface.ts
|
||||
export interface ICustomer {
|
||||
id: string;
|
||||
orderIds: string[];
|
||||
}
|
||||
|
||||
// order.ts imports only ICustomer
|
||||
import { ICustomer } from './customer.interface';
|
||||
export class Order implements IOrder {
|
||||
customer: ICustomer;
|
||||
}
|
||||
|
||||
// customer.ts imports only IOrder
|
||||
import { IOrder } from './order.interface';
|
||||
export class Customer implements ICustomer {
|
||||
orders: IOrder[];
|
||||
}
|
||||
```
|
||||
|
||||
**Strategy 3: Dependency Injection (Lazy Loading)**
|
||||
|
||||
Best for: Service circular dependencies where both need each other
|
||||
|
||||
```typescript
|
||||
// BEFORE (Circular)
|
||||
import { ServiceB } from './service-b';
|
||||
export class ServiceA {
|
||||
constructor(private serviceB: ServiceB) {}
|
||||
}
|
||||
|
||||
// AFTER (Fixed)
|
||||
import { Injector } from '@angular/core';
|
||||
import type { ServiceB } from './service-b'; // type-only import
|
||||
|
||||
export class ServiceA {
|
||||
private serviceB!: ServiceB;
|
||||
|
||||
constructor(private injector: Injector) {
|
||||
// Lazy injection breaks the cycle
|
||||
setTimeout(() => {
|
||||
this.serviceB = this.injector.get(ServiceB);
|
||||
});
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Strategy 4: Extract to Util Library**
|
||||
|
||||
Best for: Shared types, interfaces, constants, or pure functions
|
||||
|
||||
```typescript
|
||||
// BEFORE (Circular)
|
||||
// order.service.ts imports validator.ts
|
||||
// validator.ts imports order.service.ts
|
||||
|
||||
// AFTER (Fixed)
|
||||
// Create @isa/oms/util/order-types.ts
|
||||
export interface OrderData {
|
||||
id: string;
|
||||
amount: number;
|
||||
}
|
||||
|
||||
export const ORDER_STATUS = {
|
||||
PENDING: 'pending',
|
||||
COMPLETED: 'completed'
|
||||
} as const;
|
||||
|
||||
// order.service.ts imports types
|
||||
import { OrderData, ORDER_STATUS } from '@isa/oms/util/order-types';
|
||||
|
||||
// validator.ts imports types
|
||||
import { OrderData } from '@isa/oms/util/order-types';
|
||||
|
||||
// No more cycle
|
||||
```
|
||||
|
||||
**Strategy 5: Forward References (Angular Components)**
|
||||
|
||||
Best for: Angular component circular references
|
||||
|
||||
```typescript
|
||||
// Use forwardRef for components
|
||||
import { forwardRef } from '@angular/core';
|
||||
|
||||
@Component({
|
||||
selector: 'parent-component',
|
||||
standalone: true,
|
||||
imports: [forwardRef(() => ChildComponent)]
|
||||
})
|
||||
export class ParentComponent {}
|
||||
```
|
||||
|
||||
**Strategy 6: Move to Data Access Layer**
|
||||
|
||||
Best for: Features directly importing API clients
|
||||
|
||||
```typescript
|
||||
// BEFORE (Violation)
|
||||
// Feature directly imports Swagger client
|
||||
import { OrderApi } from '@generated/swagger/oms';
|
||||
|
||||
// AFTER (Fixed)
|
||||
// Create data-access service
|
||||
// @isa/oms/data-access/order-data.service.ts
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class OrderDataService {
|
||||
private api = inject(OrderApi);
|
||||
|
||||
loadOrders() {
|
||||
return this.api.getOrders();
|
||||
}
|
||||
}
|
||||
|
||||
// Feature imports data-access
|
||||
import { OrderDataService } from '@isa/oms/data-access';
|
||||
```
|
||||
|
||||
### Step 6: Implement Fixes
|
||||
|
||||
Apply chosen strategy:
|
||||
|
||||
1. Create new files if needed (util library, interfaces, shared components)
|
||||
2. Update imports in all affected files
|
||||
3. Remove circular or violating imports
|
||||
4. Update architectural tags in project.json if needed
|
||||
5. Document the refactoring decision
|
||||
|
||||
### Step 7: Validate Fixes
|
||||
|
||||
Run complete validation suite:
|
||||
|
||||
```bash
|
||||
# Check cycles resolved
|
||||
madge --circular --extensions ts libs/
|
||||
|
||||
# TypeScript compilation
|
||||
npx tsc --noEmit
|
||||
|
||||
# Run affected tests
|
||||
npx nx affected:test --skip-nx-cache
|
||||
|
||||
# Lint affected projects
|
||||
npx nx affected:lint
|
||||
|
||||
# Visual verification
|
||||
npx nx graph --focus=[fixed-library]
|
||||
```
|
||||
|
||||
### Step 8: Generate Comprehensive Report
|
||||
|
||||
```
|
||||
Architecture Validation Report
|
||||
==============================
|
||||
|
||||
Analysis Date: [timestamp]
|
||||
Scope: [All | Specific library | Affected projects]
|
||||
|
||||
📊 Executive Summary
|
||||
--------------------
|
||||
Total violations: XX
|
||||
🔴 Critical: XX (must fix)
|
||||
⚠️ Warning: XX (should fix)
|
||||
ℹ️ Info: XX (review)
|
||||
|
||||
Fixed: XX/XX
|
||||
Remaining: XX
|
||||
|
||||
🔍 Violations by Category
|
||||
-------------------------
|
||||
Layer violations: XX
|
||||
- Feature → Feature: XX
|
||||
- Data Access → Feature: XX
|
||||
- UI → Feature: XX
|
||||
|
||||
Circular dependencies: XX
|
||||
- Service cycles: XX
|
||||
- Component cycles: XX
|
||||
- Model cycles: XX
|
||||
|
||||
Import violations: XX
|
||||
- Relative imports: XX
|
||||
- Direct Swagger imports: XX
|
||||
- Deep import paths: XX
|
||||
|
||||
Domain violations: XX
|
||||
- OMS ↔ Remission: XX
|
||||
|
||||
🔴 Critical Violations
|
||||
----------------------
|
||||
|
||||
1. Feature → Feature Dependency
|
||||
📍 libs/oms/feature-return-search/src/lib/component.ts:12
|
||||
❌ import { OrderList } from '@isa/oms/feature-order-list'
|
||||
|
||||
Issue: Feature libraries should not depend on other features
|
||||
Impact: Creates tight coupling, prevents independent deployment
|
||||
|
||||
Fix Strategy: Extract to Shared UI Library
|
||||
Action: Create @isa/ui/order-components
|
||||
Files to Move: OrderList, OrderDetails (2 components)
|
||||
Effort: ~30 minutes
|
||||
Status: ✅ FIXED
|
||||
|
||||
2. Circular Dependency (Service Layer)
|
||||
📍 Cycle detected in data-access layer
|
||||
|
||||
Path:
|
||||
1. libs/oms/data-access/src/lib/services/order.service.ts:8
|
||||
→ imports OrderValidator
|
||||
2. libs/oms/data-access/src/lib/validators/order.validator.ts:15
|
||||
→ imports OrderService
|
||||
3. Back to order.service.ts (CYCLE COMPLETES)
|
||||
|
||||
Issue: Service circular reference prevents proper initialization
|
||||
Impact: Runtime errors, initialization failures
|
||||
|
||||
Fix Strategy: Extract to Util Library
|
||||
Action: Create @isa/oms/util/order-types.ts
|
||||
Move: OrderData interface, ORDER_STATUS constants
|
||||
Effort: ~15 minutes
|
||||
Status: ✅ FIXED
|
||||
|
||||
⚠️ Warnings
|
||||
-----------
|
||||
|
||||
1. Relative Import Paths
|
||||
📍 libs/oms/feature-order/src/lib/component.ts:5
|
||||
❌ import { Service } from '../../../data-access/src/lib/service'
|
||||
✅ import { Service } from '@isa/oms/data-access'
|
||||
|
||||
Count: 12 occurrences
|
||||
Fix: Replace with path aliases from tsconfig.base.json
|
||||
Status: 🔄 IN PROGRESS
|
||||
|
||||
💡 Fix Strategies Applied
|
||||
--------------------------
|
||||
Strategy | Count | Success Rate
|
||||
----------------------------|-------|-------------
|
||||
Extract to Shared Library | 3 | 100%
|
||||
Interface Extraction | 2 | 100%
|
||||
Extract to Util Library | 4 | 100%
|
||||
Dependency Injection | 1 | 100%
|
||||
Move to Data Access | 2 | 100%
|
||||
|
||||
📈 Dependency Graph Analysis
|
||||
-----------------------------
|
||||
Before: XX dependencies (YY violations)
|
||||
After: XX dependencies (ZZ violations)
|
||||
Improvement: XX% reduction in violations
|
||||
|
||||
View graph: npx nx graph --focus=[library]
|
||||
|
||||
✅ Validation Results
|
||||
---------------------
|
||||
- Madge check: ✅ No circular dependencies
|
||||
- TypeScript: ✅ Compilation successful
|
||||
- Tests: ✅ 125/125 passing
|
||||
- Lint: ✅ All projects passing
|
||||
- Nx graph: ✅ No forbidden dependencies
|
||||
|
||||
🎯 Remaining Work
|
||||
-----------------
|
||||
1. Fix XX relative import warnings
|
||||
2. Add architectural tags to XX libraries
|
||||
3. Review XX type-only circular references
|
||||
|
||||
🛡️ Prevention Recommendations
|
||||
------------------------------
|
||||
1. Enable ESLint rule: import/no-cycle
|
||||
2. Add pre-commit hook for cycle detection
|
||||
3. Regular architecture reviews (monthly)
|
||||
4. Update onboarding docs with architecture rules
|
||||
5. Create library scaffolder with proper tags
|
||||
|
||||
📚 Architecture Documentation
|
||||
------------------------------
|
||||
- Nx boundaries: https://nx.dev/nx-api/eslint-plugin/documents/enforce-module-boundaries
|
||||
- ISA-Frontend architecture: docs/architecture.md
|
||||
- Path aliases: tsconfig.base.json
|
||||
- Library reference: docs/library-reference.md
|
||||
```
|
||||
|
||||
## Prevention Strategies
|
||||
|
||||
### ESLint Configuration
|
||||
|
||||
Add to `.eslintrc.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"rules": {
|
||||
"import/no-cycle": ["error", { "maxDepth": 1 }],
|
||||
"@nx/enforce-module-boundaries": [
|
||||
"error",
|
||||
{
|
||||
"enforceBuildableLibDependency": true,
|
||||
"allow": [],
|
||||
"depConstraints": [
|
||||
{
|
||||
"sourceTag": "type:feature",
|
||||
"onlyDependOnLibsWithTags": [
|
||||
"type:data-access",
|
||||
"type:ui",
|
||||
"type:util"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Pre-commit Hook
|
||||
|
||||
Create `.husky/pre-commit`:
|
||||
|
||||
```bash
|
||||
#!/bin/bash
|
||||
|
||||
echo "🔍 Checking for circular dependencies..."
|
||||
madge --circular --extensions ts libs/
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "❌ Circular dependencies detected"
|
||||
echo "Run 'npx nx run architecture-validator' to fix"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "✅ No circular dependencies found"
|
||||
```
|
||||
|
||||
### Nx Project Tags
|
||||
|
||||
Ensure all libraries have proper tags in `project.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"tags": [
|
||||
"domain:oms",
|
||||
"type:feature"
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
## Common Patterns and Solutions
|
||||
|
||||
### Pattern: Shared Components Between Features
|
||||
|
||||
**Problem:** Multiple features need the same component
|
||||
|
||||
**Solution:** Create shared UI library
|
||||
```bash
|
||||
npx nx g @nx/angular:library ui/order-components \
|
||||
--directory=libs/ui/order-components \
|
||||
--tags=type:ui,scope:shared
|
||||
```
|
||||
|
||||
### Pattern: Service Mutual Dependency
|
||||
|
||||
**Problem:** ServiceA needs ServiceB, ServiceB needs ServiceA
|
||||
|
||||
**Solution:** Use lazy injection with Injector or extract shared interface
|
||||
|
||||
### Pattern: Type Circular Reference
|
||||
|
||||
**Problem:** Model A references Model B, Model B references Model A
|
||||
|
||||
**Solution:** Extract interfaces to separate files, use type-only imports
|
||||
|
||||
### Pattern: Feature Needs API Client
|
||||
|
||||
**Problem:** Feature directly imports Swagger-generated client
|
||||
|
||||
**Solution:** Create data-access service as abstraction layer
|
||||
|
||||
## Tool Reference
|
||||
|
||||
**Nx Commands:**
|
||||
- `npx nx graph` - Visual dependency graph
|
||||
- `npx nx lint [project]` - Lint with boundary checks
|
||||
- `npx nx affected:graph` - Show affected projects
|
||||
|
||||
**Madge Commands:**
|
||||
- `madge --circular --extensions ts libs/` - Detect cycles
|
||||
- `madge --image graph.svg libs/` - Generate visual graph
|
||||
- `madge --json libs/ > deps.json` - Export dependency data
|
||||
|
||||
**Grep Patterns:**
|
||||
- Feature→Feature: `grep -r "from '@isa/[^/]*/feature" libs/*/feature/`
|
||||
- Relative imports: `grep -r "from '\.\./\.\./\.\." libs/`
|
||||
- Swagger direct: `grep -r "from '@generated/swagger" libs/*/feature/`
|
||||
|
||||
## References
|
||||
|
||||
- Nx enforce-module-boundaries: https://nx.dev/nx-api/eslint-plugin/documents/enforce-module-boundaries
|
||||
- Madge circular dependency detection: https://github.com/pahen/madge
|
||||
- ESLint import plugin: https://github.com/import-js/eslint-plugin-import
|
||||
- CLAUDE.md Architecture section
|
||||
- ISA-Frontend architecture docs: docs/architecture.md
|
||||
- Path aliases configuration: tsconfig.base.json
|
||||
@@ -1,249 +0,0 @@
|
||||
---
|
||||
name: circular-dependency-resolver
|
||||
description: This skill should be used when build fails with circular import warnings, user mentions "circular dependencies" or "dependency cycles", or fixing A→B→C→A import cycles. Detects and resolves circular dependencies using graph algorithms with DI, interface extraction, and shared code fix strategies.
|
||||
---
|
||||
|
||||
# Circular Dependency Resolver
|
||||
|
||||
## Overview
|
||||
|
||||
Detect and resolve circular dependencies using graph analysis, multiple fix strategies, and automated validation. Prevents runtime and build issues caused by dependency cycles.
|
||||
|
||||
## When to Use This Skill
|
||||
|
||||
Invoke when user:
|
||||
- Mentions "circular dependencies"
|
||||
- Has import cycle errors
|
||||
- Requests dependency analysis
|
||||
- Build fails with circular import warnings
|
||||
|
||||
## Resolution Workflow
|
||||
|
||||
### Step 1: Detect Circular Dependencies
|
||||
|
||||
**Using Nx:**
|
||||
```bash
|
||||
npx nx run-many --target=lint --all 2>&1 | grep -i "circular"
|
||||
```
|
||||
|
||||
**Using madge (if installed):**
|
||||
```bash
|
||||
npm install -g madge
|
||||
madge --circular --extensions ts libs/
|
||||
madge --circular --image circular-deps.svg libs/
|
||||
```
|
||||
|
||||
**Using TypeScript:**
|
||||
```bash
|
||||
npx tsc --noEmit --strict 2>&1 | grep -i "circular\|cycle"
|
||||
```
|
||||
|
||||
### Step 2: Analyze Each Cycle
|
||||
|
||||
For each cycle found:
|
||||
```
|
||||
📍 Circular Dependency Detected
|
||||
|
||||
Cycle Path:
|
||||
1. libs/oms/data-access/src/lib/services/order.service.ts
|
||||
→ imports OrderValidator
|
||||
2. libs/oms/data-access/src/lib/validators/order.validator.ts
|
||||
→ imports OrderService
|
||||
3. Back to order.service.ts
|
||||
|
||||
Type: Service-Validator circular reference
|
||||
Severity: 🔴 Critical
|
||||
Files Involved: 2
|
||||
```
|
||||
|
||||
### Step 3: Categorize by Severity
|
||||
|
||||
**🔴 Critical (Must Fix):**
|
||||
- Service-to-service cycles
|
||||
- Data-access layer cycles
|
||||
- Store dependencies creating cycles
|
||||
|
||||
**⚠️ Warning (Should Fix):**
|
||||
- Component-to-component cycles
|
||||
- Model cross-references
|
||||
- Utility function cycles
|
||||
|
||||
**ℹ️ Info (Review):**
|
||||
- Type-only circular references (may be acceptable)
|
||||
- Test file circular imports
|
||||
|
||||
### Step 4: Choose Fix Strategy
|
||||
|
||||
**Strategy 1: Extract to Shared Utility**
|
||||
```typescript
|
||||
// BEFORE (Circular)
|
||||
// order.service.ts imports validator.ts
|
||||
// validator.ts imports order.service.ts
|
||||
|
||||
// AFTER (Fixed)
|
||||
// Create @isa/oms/util/types.ts
|
||||
export interface OrderData { id: string; }
|
||||
|
||||
// order.service.ts imports types
|
||||
// validator.ts imports types
|
||||
// No more cycle
|
||||
```
|
||||
|
||||
**Strategy 2: Dependency Injection (Lazy)**
|
||||
```typescript
|
||||
// BEFORE
|
||||
import { ServiceB } from './service-b';
|
||||
export class ServiceA {
|
||||
constructor(private serviceB: ServiceB) {}
|
||||
}
|
||||
|
||||
// AFTER
|
||||
import { Injector } from '@angular/core';
|
||||
export class ServiceA {
|
||||
private serviceB!: ServiceB;
|
||||
|
||||
constructor(private injector: Injector) {
|
||||
setTimeout(() => {
|
||||
this.serviceB = this.injector.get(ServiceB);
|
||||
});
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Strategy 3: Interface Extraction**
|
||||
```typescript
|
||||
// BEFORE (Models with circular reference)
|
||||
// order.ts ↔ customer.ts
|
||||
|
||||
// AFTER
|
||||
// order.interface.ts - no imports
|
||||
export interface IOrder { customerId: string; }
|
||||
|
||||
// customer.interface.ts - no imports
|
||||
export interface ICustomer { orderIds: string[]; }
|
||||
|
||||
// order.ts imports only ICustomer
|
||||
// customer.ts imports only IOrder
|
||||
```
|
||||
|
||||
**Strategy 4: Move Shared Code**
|
||||
```typescript
|
||||
// BEFORE
|
||||
// feature-a imports feature-b
|
||||
// feature-b imports feature-a
|
||||
|
||||
// AFTER
|
||||
// Extract to @isa/shared/[name]
|
||||
// Both features import from shared
|
||||
```
|
||||
|
||||
**Strategy 5: Forward References (Angular)**
|
||||
```typescript
|
||||
// Use forwardRef for components
|
||||
import { forwardRef } from '@angular/core';
|
||||
|
||||
@Component({
|
||||
imports: [forwardRef(() => ChildComponent)]
|
||||
})
|
||||
```
|
||||
|
||||
### Step 5: Implement Fix
|
||||
|
||||
Apply chosen strategy:
|
||||
1. Create new files if needed (util library, interfaces)
|
||||
2. Update imports in both files
|
||||
3. Remove circular import
|
||||
|
||||
### Step 6: Validate Fix
|
||||
|
||||
```bash
|
||||
# Check cycle resolved
|
||||
madge --circular --extensions ts libs/
|
||||
|
||||
# TypeScript compilation
|
||||
npx tsc --noEmit
|
||||
|
||||
# Run tests
|
||||
npx nx affected:test --skip-nx-cache
|
||||
|
||||
# Lint
|
||||
npx nx affected:lint
|
||||
```
|
||||
|
||||
### Step 7: Generate Report
|
||||
|
||||
```
|
||||
Circular Dependency Resolution
|
||||
===============================
|
||||
|
||||
Analysis Date: [timestamp]
|
||||
|
||||
📊 Summary
|
||||
----------
|
||||
Circular dependencies found: XX
|
||||
🔴 Critical: XX
|
||||
⚠️ Warning: XX
|
||||
Fixed: XX
|
||||
|
||||
🔍 Detected Cycles
|
||||
------------------
|
||||
|
||||
🔴 Critical Cycle #1 (FIXED)
|
||||
Path: order.service → validator → order.service
|
||||
Strategy Used: Extract to Util Library
|
||||
Created: @isa/oms/util/order-types.ts
|
||||
Files Modified: 2
|
||||
Status: ✅ Resolved
|
||||
|
||||
⚠️ Warning Cycle #2 (FIXED)
|
||||
Path: component-a → component-b → component-a
|
||||
Strategy Used: Move Shared to @isa/ui
|
||||
Created: @isa/ui/shared-component
|
||||
Files Modified: 3
|
||||
Status: ✅ Resolved
|
||||
|
||||
💡 Fix Strategies Applied
|
||||
--------------------------
|
||||
1. Extract to Util: XX cycles
|
||||
2. Interface Extraction: XX cycles
|
||||
3. Move Shared Code: XX cycles
|
||||
4. Dependency Injection: XX cycles
|
||||
|
||||
✅ Validation
|
||||
-------------
|
||||
- madge check: ✅ No cycles
|
||||
- TypeScript: ✅ Compiles
|
||||
- Tests: ✅ XX/XX passing
|
||||
- Lint: ✅ Passed
|
||||
|
||||
🎯 Prevention Tips
|
||||
------------------
|
||||
1. Add ESLint rule: import/no-cycle
|
||||
2. Pre-commit hook for cycle detection
|
||||
3. Regular architecture reviews
|
||||
```
|
||||
|
||||
## Prevention
|
||||
|
||||
**ESLint Configuration:**
|
||||
```json
|
||||
{
|
||||
"import/no-cycle": ["error", { "maxDepth": 1 }]
|
||||
}
|
||||
```
|
||||
|
||||
**Pre-commit Hook:**
|
||||
```bash
|
||||
#!/bin/bash
|
||||
madge --circular --extensions ts libs/
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "❌ Circular dependencies detected"
|
||||
exit 1
|
||||
fi
|
||||
```
|
||||
|
||||
## References
|
||||
|
||||
- Madge: https://github.com/pahen/madge
|
||||
- Nx dependency graph: https://nx.dev/features/explore-graph
|
||||
- ESLint import plugin: https://github.com/import-js/eslint-plugin-import
|
||||
@@ -1,25 +1,14 @@
|
||||
---
|
||||
name: css-keyframes-animations
|
||||
name: css-animations
|
||||
description: This skill should be used when writing or reviewing CSS animations in Angular components. Use when creating entrance/exit animations, implementing @keyframes instead of @angular/animations, applying timing functions and fill modes, creating staggered animations, or ensuring GPU-accelerated performance. Essential for modern Angular 20+ components using animate.enter/animate.leave directives and converting legacy Angular animations to native CSS.
|
||||
---
|
||||
|
||||
# CSS @keyframes Animations
|
||||
# CSS Animations
|
||||
|
||||
## Overview
|
||||
|
||||
Implement native CSS @keyframes animations for Angular applications, replacing @angular/animations with GPU-accelerated, zero-bundle-size alternatives. This skill provides comprehensive guidance on creating performant entrance/exit animations, staggered effects, and proper timing configurations.
|
||||
|
||||
## When to Use This Skill
|
||||
|
||||
Apply this skill when:
|
||||
- **Writing Angular components** with entrance/exit animations
|
||||
- **Converting @angular/animations** to native CSS @keyframes
|
||||
- **Implementing animate.enter/animate.leave** in Angular 20+ templates
|
||||
- **Creating staggered animations** for lists or collections
|
||||
- **Debugging animation issues** (snap-back, wrong starting positions, choppy playback)
|
||||
- **Optimizing animation performance** for GPU acceleration
|
||||
- **Reviewing animation code** for accessibility and best practices
|
||||
|
||||
## Quick Start
|
||||
|
||||
### Basic Animation Setup
|
||||
@@ -311,41 +300,6 @@ element.addEventListener('animationend', (e) => {
|
||||
});
|
||||
```
|
||||
|
||||
## Resources
|
||||
|
||||
### references/keyframes-guide.md
|
||||
|
||||
Comprehensive deep-dive covering:
|
||||
- Complete @keyframes syntax reference
|
||||
- Detailed timing functions and cubic-bezier curves
|
||||
- Advanced techniques (direction, play-state, @starting-style)
|
||||
- Performance optimization strategies
|
||||
- Extensive common patterns library
|
||||
- Debugging techniques and troubleshooting
|
||||
|
||||
**When to reference:** Complex animation requirements, custom easing curves, advanced techniques, performance optimization, or learning comprehensive details.
|
||||
|
||||
### assets/animations.css
|
||||
|
||||
Ready-to-use CSS file with common animation patterns:
|
||||
- Fade animations (in/out)
|
||||
- Slide animations (up/down/left/right)
|
||||
- Scale animations (in/out)
|
||||
- Utility animations (spin, shimmer, shake, breathe, attention-pulse)
|
||||
- Toast/notification animations
|
||||
- Accessibility (@media prefers-reduced-motion)
|
||||
|
||||
**Usage:** Copy this file to project and import in component styles or global styles:
|
||||
|
||||
```css
|
||||
@import 'path/to/animations.css';
|
||||
```
|
||||
|
||||
Then use classes directly:
|
||||
```html
|
||||
<div animate.enter="fade-in" animate.leave="slide-out-down">
|
||||
```
|
||||
|
||||
## Migration from @angular/animations
|
||||
|
||||
### Before (Angular Animations)
|
||||
@@ -390,3 +344,38 @@ import { trigger, state, style, transition, animate } from '@angular/animations'
|
||||
- GPU hardware acceleration
|
||||
- Standard CSS (transferable skills)
|
||||
- Better performance
|
||||
|
||||
## Resources
|
||||
|
||||
### references/keyframes-guide.md
|
||||
|
||||
Comprehensive deep-dive covering:
|
||||
- Complete @keyframes syntax reference
|
||||
- Detailed timing functions and cubic-bezier curves
|
||||
- Advanced techniques (direction, play-state, @starting-style)
|
||||
- Performance optimization strategies
|
||||
- Extensive common patterns library
|
||||
- Debugging techniques and troubleshooting
|
||||
|
||||
**When to reference:** Complex animation requirements, custom easing curves, advanced techniques, performance optimization, or learning comprehensive details.
|
||||
|
||||
### assets/animations.css
|
||||
|
||||
Ready-to-use CSS file with common animation patterns:
|
||||
- Fade animations (in/out)
|
||||
- Slide animations (up/down/left/right)
|
||||
- Scale animations (in/out)
|
||||
- Utility animations (spin, shimmer, shake, breathe, attention-pulse)
|
||||
- Toast/notification animations
|
||||
- Accessibility (@media prefers-reduced-motion)
|
||||
|
||||
**Usage:** Copy this file to project and import in component styles or global styles:
|
||||
|
||||
```css
|
||||
@import 'path/to/animations.css';
|
||||
```
|
||||
|
||||
Then use classes directly:
|
||||
```html
|
||||
<div animate.enter="fade-in" animate.leave="slide-out-down">
|
||||
```
|
||||
@@ -1,352 +1,352 @@
|
||||
---
|
||||
name: git-workflow
|
||||
description: This skill should be used when creating branches, writing commits, or creating pull requests. Enforces ISA-Frontend Git conventions including feature/task-id-name branch format, conventional commits without co-author tags, and PRs targeting develop branch.
|
||||
---
|
||||
|
||||
# Git Workflow Skill
|
||||
|
||||
Enforces Git workflow conventions specific to the ISA-Frontend project.
|
||||
|
||||
## When to Use
|
||||
|
||||
- Creating new branches for features or bugfixes
|
||||
- Writing commit messages
|
||||
- Creating pull requests
|
||||
- Any Git operations requiring adherence to project conventions
|
||||
|
||||
## Core Principles
|
||||
|
||||
### 1. Default Branch is `develop` (NOT `main`)
|
||||
|
||||
- **All PRs target**: `develop` branch
|
||||
- **Feature branches from**: `develop`
|
||||
- **Never push directly to**: `develop` or `main`
|
||||
|
||||
### 2. Branch Naming Convention
|
||||
|
||||
**Format**: `<type>/{task-id}-{short-description}`
|
||||
|
||||
**Types**:
|
||||
- `feature/` - New features or enhancements
|
||||
- `bugfix/` - Bug fixes
|
||||
- `hotfix/` - Emergency production fixes
|
||||
|
||||
**Rules**:
|
||||
- Use English kebab-case for descriptions
|
||||
- Start with task/issue ID (e.g., `5391`)
|
||||
- Keep description concise - shorten if too long
|
||||
- Use hyphens to separate words
|
||||
|
||||
**Examples**:
|
||||
```bash
|
||||
# Good
|
||||
feature/5391-praemie-checkout-action-card-delivery-order
|
||||
bugfix/6123-fix-login-redirect-loop
|
||||
hotfix/7890-critical-payment-error
|
||||
|
||||
# Bad
|
||||
feature/praemie-checkout # Missing task ID
|
||||
feature/5391_praemie # Using underscores
|
||||
feature-5391-very-long-description-that-goes-on-forever # Too long
|
||||
```
|
||||
|
||||
### 3. Conventional Commits (WITHOUT Co-Author Tags)
|
||||
|
||||
**Format**: `<type>(<scope>): <description>`
|
||||
|
||||
**Types**:
|
||||
- `feat`: New feature
|
||||
- `fix`: Bug fix
|
||||
- `docs`: Documentation only
|
||||
- `style`: Code style (formatting, missing semicolons)
|
||||
- `refactor`: Code restructuring without feature changes
|
||||
- `perf`: Performance improvements
|
||||
- `test`: Adding or updating tests
|
||||
- `build`: Build system or dependencies
|
||||
- `ci`: CI configuration
|
||||
- `chore`: Maintenance tasks
|
||||
|
||||
**Rules**:
|
||||
- ❌ **NO** "Generated with Claude Code" tags
|
||||
- ❌ **NO** "Co-Authored-By: Claude" tags
|
||||
- ✅ Keep first line under 72 characters
|
||||
- ✅ Use imperative mood ("add" not "added")
|
||||
- ✅ Body optional but recommended for complex changes
|
||||
|
||||
**Examples**:
|
||||
```bash
|
||||
# Good
|
||||
feat(checkout): add bonus card selection for delivery orders
|
||||
|
||||
fix(crm): resolve customer search filter reset issue
|
||||
|
||||
refactor(oms): extract return validation logic into service
|
||||
|
||||
# Bad
|
||||
feat(checkout): add bonus card selection
|
||||
|
||||
Generated with Claude Code
|
||||
Co-Authored-By: Claude <noreply@anthropic.com>
|
||||
|
||||
# Also bad
|
||||
Added new feature # Wrong tense
|
||||
Fix bug # Missing scope
|
||||
```
|
||||
|
||||
### 4. Pull Request Creation
|
||||
|
||||
**Target Branch**: Always `develop`
|
||||
|
||||
**PR Title Format**: Same as conventional commit
|
||||
```
|
||||
feat(domain): concise description of changes
|
||||
```
|
||||
|
||||
**PR Body Structure**:
|
||||
```markdown
|
||||
## Summary
|
||||
- Brief bullet points of changes
|
||||
|
||||
## Related Tasks
|
||||
- Closes #{task-id}
|
||||
- Refs #{related-task}
|
||||
|
||||
## Test Plan
|
||||
- [ ] Unit tests added/updated
|
||||
- [ ] E2E attributes added
|
||||
- [ ] Manual testing completed
|
||||
|
||||
## Breaking Changes
|
||||
None / List breaking changes
|
||||
|
||||
## Screenshots (if UI changes)
|
||||
[Add screenshots]
|
||||
```
|
||||
|
||||
## Common Workflows
|
||||
|
||||
### Creating a Feature Branch
|
||||
|
||||
```bash
|
||||
# 1. Update develop
|
||||
git checkout develop
|
||||
git pull origin develop
|
||||
|
||||
# 2. Create feature branch
|
||||
git checkout -b feature/5391-praemie-checkout-action-card
|
||||
|
||||
# 3. Work and commit
|
||||
git add .
|
||||
git commit -m "feat(checkout): add primary bonus card selection logic"
|
||||
|
||||
# 4. Push to remote
|
||||
git push -u origin feature/5391-praemie-checkout-action-card
|
||||
|
||||
# 5. Create PR targeting develop (use gh CLI or web UI)
|
||||
```
|
||||
|
||||
### Creating a Bugfix Branch
|
||||
|
||||
```bash
|
||||
# From develop
|
||||
git checkout develop
|
||||
git pull origin develop
|
||||
git checkout -b bugfix/6123-login-redirect-loop
|
||||
|
||||
# Commit
|
||||
git commit -m "fix(auth): resolve infinite redirect on logout"
|
||||
```
|
||||
|
||||
### Creating a Hotfix Branch
|
||||
|
||||
```bash
|
||||
# From main (production)
|
||||
git checkout main
|
||||
git pull origin main
|
||||
git checkout -b hotfix/7890-payment-processing-error
|
||||
|
||||
# Commit
|
||||
git commit -m "fix(checkout): critical payment API timeout handling"
|
||||
|
||||
# Merge to both main and develop
|
||||
```
|
||||
|
||||
## Commit Message Guidelines
|
||||
|
||||
### Good Commit Messages
|
||||
|
||||
```bash
|
||||
feat(crm): add customer loyalty tier calculation
|
||||
|
||||
Implements three-tier loyalty system based on annual spend.
|
||||
Includes migration for existing customer data.
|
||||
|
||||
Refs #5234
|
||||
|
||||
---
|
||||
|
||||
fix(oms): prevent duplicate return submissions
|
||||
|
||||
Adds debouncing to return form submission and validates
|
||||
against existing returns in the last 60 seconds.
|
||||
|
||||
Closes #5891
|
||||
|
||||
---
|
||||
|
||||
refactor(catalogue): extract product search into dedicated service
|
||||
|
||||
Moves search logic from component to ProductSearchService
|
||||
for better testability and reusability.
|
||||
|
||||
---
|
||||
|
||||
perf(remission): optimize remission list query with pagination
|
||||
|
||||
Reduces initial load time from 3s to 800ms by implementing
|
||||
cursor-based pagination.
|
||||
|
||||
Closes #6234
|
||||
```
|
||||
|
||||
### Bad Commit Messages
|
||||
|
||||
```bash
|
||||
# Too vague
|
||||
fix: bug fixes
|
||||
|
||||
# Missing scope
|
||||
feat: new feature
|
||||
|
||||
# Wrong tense
|
||||
fixed the login issue
|
||||
|
||||
# Including banned tags
|
||||
feat(checkout): add feature
|
||||
|
||||
Generated with Claude Code
|
||||
Co-Authored-By: Claude <noreply@anthropic.com>
|
||||
```
|
||||
|
||||
## Git Configuration Checks
|
||||
|
||||
### Verify Git Setup
|
||||
|
||||
```bash
|
||||
# Check current branch
|
||||
git branch --show-current
|
||||
|
||||
# Verify remote
|
||||
git remote -v # Should show origin pointing to ISA-Frontend
|
||||
|
||||
# Check for uncommitted changes
|
||||
git status
|
||||
```
|
||||
|
||||
## Common Mistakes to Avoid
|
||||
|
||||
```bash
|
||||
# ❌ Creating PR against main
|
||||
gh pr create --base main # WRONG
|
||||
|
||||
# ✅ Always target develop
|
||||
gh pr create --base develop # CORRECT
|
||||
|
||||
# ❌ Using underscores in branch names
|
||||
git checkout -b feature/5391_my_feature # WRONG
|
||||
|
||||
# ✅ Use hyphens
|
||||
git checkout -b feature/5391-my-feature # CORRECT
|
||||
|
||||
# ❌ Adding co-author tags
|
||||
git commit -m "feat: something
|
||||
|
||||
Co-Authored-By: Claude <noreply@anthropic.com>" # WRONG
|
||||
|
||||
# ✅ Clean commit message
|
||||
git commit -m "feat(scope): something" # CORRECT
|
||||
|
||||
# ❌ Forgetting task ID in branch name
|
||||
git checkout -b feature/new-checkout-flow # WRONG
|
||||
|
||||
# ✅ Include task ID
|
||||
git checkout -b feature/5391-new-checkout-flow # CORRECT
|
||||
```
|
||||
|
||||
## Integration with Claude Code
|
||||
|
||||
When Claude Code creates commits or PRs:
|
||||
|
||||
### Commit Creation
|
||||
```bash
|
||||
# Claude uses conventional commits WITHOUT attribution
|
||||
git commit -m "feat(checkout): implement bonus card selection
|
||||
|
||||
Adds logic for selecting primary bonus card during checkout
|
||||
for delivery orders. Includes validation and error handling.
|
||||
|
||||
Refs #5391"
|
||||
```
|
||||
|
||||
### PR Creation
|
||||
```bash
|
||||
# Target develop by default
|
||||
gh pr create --base develop \
|
||||
--title "feat(checkout): implement bonus card selection" \
|
||||
--body "## Summary
|
||||
- Add primary bonus card selection logic
|
||||
- Implement validation for delivery orders
|
||||
- Add error handling for API failures
|
||||
|
||||
## Related Tasks
|
||||
- Closes #5391
|
||||
|
||||
## Test Plan
|
||||
- [x] Unit tests added
|
||||
- [x] E2E attributes added
|
||||
- [x] Manual testing completed"
|
||||
```
|
||||
|
||||
## Branch Cleanup
|
||||
|
||||
### After PR Merge
|
||||
```bash
|
||||
# Update develop
|
||||
git checkout develop
|
||||
git pull origin develop
|
||||
|
||||
# Delete local feature branch
|
||||
git branch -d feature/5391-praemie-checkout
|
||||
|
||||
# Delete remote branch (usually done by PR merge)
|
||||
git push origin --delete feature/5391-praemie-checkout
|
||||
```
|
||||
|
||||
## Quick Reference
|
||||
|
||||
```bash
|
||||
# Branch naming
|
||||
feature/{task-id}-{description}
|
||||
bugfix/{task-id}-{description}
|
||||
hotfix/{task-id}-{description}
|
||||
|
||||
# Commit format
|
||||
<type>(<scope>): <description>
|
||||
|
||||
# Common types
|
||||
feat, fix, docs, style, refactor, perf, test, build, ci, chore
|
||||
|
||||
# PR target
|
||||
Always: develop (NOT main)
|
||||
|
||||
# Banned in commits
|
||||
- "Generated with Claude Code"
|
||||
- "Co-Authored-By: Claude"
|
||||
- Any AI attribution
|
||||
```
|
||||
|
||||
## Resources
|
||||
|
||||
- [Conventional Commits](https://www.conventionalcommits.org/)
|
||||
- Project PR template: `.github/pull_request_template.md`
|
||||
- Code review standards: `.github/review-instructions.md`
|
||||
---
|
||||
name: git-workflow
|
||||
description: This skill should be used when creating branches, writing commits, or creating pull requests. Enforces ISA-Frontend Git conventions including feature/task-id-name branch format, conventional commits without co-author tags, and PRs targeting develop branch.
|
||||
---
|
||||
|
||||
# Git Workflow Skill
|
||||
|
||||
Enforces Git workflow conventions specific to the ISA-Frontend project.
|
||||
|
||||
## When to Use
|
||||
|
||||
- Creating new branches for features or bugfixes
|
||||
- Writing commit messages
|
||||
- Creating pull requests
|
||||
- Any Git operations requiring adherence to project conventions
|
||||
|
||||
## Core Principles
|
||||
|
||||
### 1. Default Branch is `develop` (NOT `main`)
|
||||
|
||||
- **All PRs target**: `develop` branch
|
||||
- **Feature branches from**: `develop`
|
||||
- **Never push directly to**: `develop` or `main`
|
||||
|
||||
### 2. Branch Naming Convention
|
||||
|
||||
**Format**: `<type>/{task-id}-{short-description}`
|
||||
|
||||
**Types**:
|
||||
- `feature/` - New features or enhancements
|
||||
- `bugfix/` - Bug fixes
|
||||
- `hotfix/` - Emergency production fixes
|
||||
|
||||
**Rules**:
|
||||
- Use English kebab-case for descriptions
|
||||
- Start with task/issue ID (e.g., `5391`)
|
||||
- Keep description concise - shorten if too long
|
||||
- Use hyphens to separate words
|
||||
|
||||
**Examples**:
|
||||
```bash
|
||||
# Good
|
||||
feature/5391-praemie-checkout-action-card-delivery-order
|
||||
bugfix/6123-fix-login-redirect-loop
|
||||
hotfix/7890-critical-payment-error
|
||||
|
||||
# Bad
|
||||
feature/praemie-checkout # Missing task ID
|
||||
feature/5391_praemie # Using underscores
|
||||
feature-5391-very-long-description-that-goes-on-forever # Too long
|
||||
```
|
||||
|
||||
### 3. Conventional Commits (WITHOUT Co-Author Tags)
|
||||
|
||||
**Format**: `<type>(<scope>): <description>`
|
||||
|
||||
**Types**:
|
||||
- `feat`: New feature
|
||||
- `fix`: Bug fix
|
||||
- `docs`: Documentation only
|
||||
- `style`: Code style (formatting, missing semicolons)
|
||||
- `refactor`: Code restructuring without feature changes
|
||||
- `perf`: Performance improvements
|
||||
- `test`: Adding or updating tests
|
||||
- `build`: Build system or dependencies
|
||||
- `ci`: CI configuration
|
||||
- `chore`: Maintenance tasks
|
||||
|
||||
**Rules**:
|
||||
- ❌ **NO** "Generated with Claude Code" tags
|
||||
- ❌ **NO** "Co-Authored-By: Claude" tags
|
||||
- ✅ Keep first line under 72 characters
|
||||
- ✅ Use imperative mood ("add" not "added")
|
||||
- ✅ Body optional but recommended for complex changes
|
||||
|
||||
**Examples**:
|
||||
```bash
|
||||
# Good
|
||||
feat(checkout): add bonus card selection for delivery orders
|
||||
|
||||
fix(crm): resolve customer search filter reset issue
|
||||
|
||||
refactor(oms): extract return validation logic into service
|
||||
|
||||
# Bad
|
||||
feat(checkout): add bonus card selection
|
||||
|
||||
Generated with Claude Code
|
||||
Co-Authored-By: Claude <noreply@anthropic.com>
|
||||
|
||||
# Also bad
|
||||
Added new feature # Wrong tense
|
||||
Fix bug # Missing scope
|
||||
```
|
||||
|
||||
### 4. Pull Request Creation
|
||||
|
||||
**Target Branch**: Always `develop`
|
||||
|
||||
**PR Title Format**: Same as conventional commit
|
||||
```
|
||||
feat(domain): concise description of changes
|
||||
```
|
||||
|
||||
**PR Body Structure**:
|
||||
```markdown
|
||||
## Summary
|
||||
- Brief bullet points of changes
|
||||
|
||||
## Related Tasks
|
||||
- Closes #{task-id}
|
||||
- Refs #{related-task}
|
||||
|
||||
## Test Plan
|
||||
- [ ] Unit tests added/updated
|
||||
- [ ] E2E attributes added
|
||||
- [ ] Manual testing completed
|
||||
|
||||
## Breaking Changes
|
||||
None / List breaking changes
|
||||
|
||||
## Screenshots (if UI changes)
|
||||
[Add screenshots]
|
||||
```
|
||||
|
||||
## Common Workflows
|
||||
|
||||
### Creating a Feature Branch
|
||||
|
||||
```bash
|
||||
# 1. Update develop
|
||||
git checkout develop
|
||||
git pull origin develop
|
||||
|
||||
# 2. Create feature branch
|
||||
git checkout -b feature/5391-praemie-checkout-action-card
|
||||
|
||||
# 3. Work and commit
|
||||
git add .
|
||||
git commit -m "feat(checkout): add primary bonus card selection logic"
|
||||
|
||||
# 4. Push to remote
|
||||
git push -u origin feature/5391-praemie-checkout-action-card
|
||||
|
||||
# 5. Create PR targeting develop (use gh CLI or web UI)
|
||||
```
|
||||
|
||||
### Creating a Bugfix Branch
|
||||
|
||||
```bash
|
||||
# From develop
|
||||
git checkout develop
|
||||
git pull origin develop
|
||||
git checkout -b bugfix/6123-login-redirect-loop
|
||||
|
||||
# Commit
|
||||
git commit -m "fix(auth): resolve infinite redirect on logout"
|
||||
```
|
||||
|
||||
### Creating a Hotfix Branch
|
||||
|
||||
```bash
|
||||
# From main (production)
|
||||
git checkout main
|
||||
git pull origin main
|
||||
git checkout -b hotfix/7890-payment-processing-error
|
||||
|
||||
# Commit
|
||||
git commit -m "fix(checkout): critical payment API timeout handling"
|
||||
|
||||
# Merge to both main and develop
|
||||
```
|
||||
|
||||
## Commit Message Guidelines
|
||||
|
||||
### Good Commit Messages
|
||||
|
||||
```bash
|
||||
feat(crm): add customer loyalty tier calculation
|
||||
|
||||
Implements three-tier loyalty system based on annual spend.
|
||||
Includes migration for existing customer data.
|
||||
|
||||
Refs #5234
|
||||
|
||||
---
|
||||
|
||||
fix(oms): prevent duplicate return submissions
|
||||
|
||||
Adds debouncing to return form submission and validates
|
||||
against existing returns in the last 60 seconds.
|
||||
|
||||
Closes #5891
|
||||
|
||||
---
|
||||
|
||||
refactor(catalogue): extract product search into dedicated service
|
||||
|
||||
Moves search logic from component to ProductSearchService
|
||||
for better testability and reusability.
|
||||
|
||||
---
|
||||
|
||||
perf(remission): optimize remission list query with pagination
|
||||
|
||||
Reduces initial load time from 3s to 800ms by implementing
|
||||
cursor-based pagination.
|
||||
|
||||
Closes #6234
|
||||
```
|
||||
|
||||
### Bad Commit Messages
|
||||
|
||||
```bash
|
||||
# Too vague
|
||||
fix: bug fixes
|
||||
|
||||
# Missing scope
|
||||
feat: new feature
|
||||
|
||||
# Wrong tense
|
||||
fixed the login issue
|
||||
|
||||
# Including banned tags
|
||||
feat(checkout): add feature
|
||||
|
||||
Generated with Claude Code
|
||||
Co-Authored-By: Claude <noreply@anthropic.com>
|
||||
```
|
||||
|
||||
## Git Configuration Checks
|
||||
|
||||
### Verify Git Setup
|
||||
|
||||
```bash
|
||||
# Check current branch
|
||||
git branch --show-current
|
||||
|
||||
# Verify remote
|
||||
git remote -v # Should show origin pointing to ISA-Frontend
|
||||
|
||||
# Check for uncommitted changes
|
||||
git status
|
||||
```
|
||||
|
||||
## Common Mistakes to Avoid
|
||||
|
||||
```bash
|
||||
# ❌ Creating PR against main
|
||||
gh pr create --base main # WRONG
|
||||
|
||||
# ✅ Always target develop
|
||||
gh pr create --base develop # CORRECT
|
||||
|
||||
# ❌ Using underscores in branch names
|
||||
git checkout -b feature/5391_my_feature # WRONG
|
||||
|
||||
# ✅ Use hyphens
|
||||
git checkout -b feature/5391-my-feature # CORRECT
|
||||
|
||||
# ❌ Adding co-author tags
|
||||
git commit -m "feat: something
|
||||
|
||||
Co-Authored-By: Claude <noreply@anthropic.com>" # WRONG
|
||||
|
||||
# ✅ Clean commit message
|
||||
git commit -m "feat(scope): something" # CORRECT
|
||||
|
||||
# ❌ Forgetting task ID in branch name
|
||||
git checkout -b feature/new-checkout-flow # WRONG
|
||||
|
||||
# ✅ Include task ID
|
||||
git checkout -b feature/5391-new-checkout-flow # CORRECT
|
||||
```
|
||||
|
||||
## Integration with Claude Code
|
||||
|
||||
When Claude Code creates commits or PRs:
|
||||
|
||||
### Commit Creation
|
||||
```bash
|
||||
# Claude uses conventional commits WITHOUT attribution
|
||||
git commit -m "feat(checkout): implement bonus card selection
|
||||
|
||||
Adds logic for selecting primary bonus card during checkout
|
||||
for delivery orders. Includes validation and error handling.
|
||||
|
||||
Refs #5391"
|
||||
```
|
||||
|
||||
### PR Creation
|
||||
```bash
|
||||
# Target develop by default
|
||||
gh pr create --base develop \
|
||||
--title "feat(checkout): implement bonus card selection" \
|
||||
--body "## Summary
|
||||
- Add primary bonus card selection logic
|
||||
- Implement validation for delivery orders
|
||||
- Add error handling for API failures
|
||||
|
||||
## Related Tasks
|
||||
- Closes #5391
|
||||
|
||||
## Test Plan
|
||||
- [x] Unit tests added
|
||||
- [x] E2E attributes added
|
||||
- [x] Manual testing completed"
|
||||
```
|
||||
|
||||
## Branch Cleanup
|
||||
|
||||
### After PR Merge
|
||||
```bash
|
||||
# Update develop
|
||||
git checkout develop
|
||||
git pull origin develop
|
||||
|
||||
# Delete local feature branch
|
||||
git branch -d feature/5391-praemie-checkout
|
||||
|
||||
# Delete remote branch (usually done by PR merge)
|
||||
git push origin --delete feature/5391-praemie-checkout
|
||||
```
|
||||
|
||||
## Quick Reference
|
||||
|
||||
```bash
|
||||
# Branch naming
|
||||
feature/{task-id}-{description}
|
||||
bugfix/{task-id}-{description}
|
||||
hotfix/{task-id}-{description}
|
||||
|
||||
# Commit format
|
||||
<type>(<scope>): <description>
|
||||
|
||||
# Common types
|
||||
feat, fix, docs, style, refactor, perf, test, build, ci, chore
|
||||
|
||||
# PR target
|
||||
Always: develop (NOT main)
|
||||
|
||||
# Banned in commits
|
||||
- "Generated with Claude Code"
|
||||
- "Co-Authored-By: Claude"
|
||||
- Any AI attribution
|
||||
```
|
||||
|
||||
## Resources
|
||||
|
||||
- [Conventional Commits](https://www.conventionalcommits.org/)
|
||||
- Project PR template: `.github/pull_request_template.md`
|
||||
- Code review standards: `.github/review-instructions.md`
|
||||
|
||||
@@ -1,299 +0,0 @@
|
||||
---
|
||||
name: html-template
|
||||
description: This skill should be used when writing or reviewing HTML templates to ensure proper E2E testing attributes (data-what, data-which) and ARIA accessibility attributes are included. Use when creating interactive elements like buttons, inputs, links, forms, dialogs, or any HTML markup requiring testing and accessibility compliance. Works seamlessly with the angular-template skill.
|
||||
---
|
||||
|
||||
# HTML Template - Testing & Accessibility Attributes
|
||||
|
||||
This skill should be used when writing or reviewing HTML templates to ensure proper testing and accessibility attributes are included.
|
||||
|
||||
## When to Use This Skill
|
||||
|
||||
Use this skill when:
|
||||
- Writing or modifying Angular component templates
|
||||
- Creating any HTML templates or markup
|
||||
- Reviewing code for testing and accessibility compliance
|
||||
- Adding interactive elements (buttons, inputs, links, etc.)
|
||||
- Implementing forms, lists, navigation, or dialogs
|
||||
|
||||
**Works seamlessly with:**
|
||||
- **[angular-template](../angular-template/SKILL.md)** - Angular template syntax, control flow, and modern patterns
|
||||
- **[tailwind](../tailwind/SKILL.md)** - ISA design system styling for visual design
|
||||
- **[logging](../logging/SKILL.md)** - MANDATORY logging in all Angular components using `@isa/core/logging`
|
||||
|
||||
## Overview
|
||||
|
||||
This skill provides comprehensive guidance for two critical HTML attribute categories:
|
||||
|
||||
### 1. E2E Testing Attributes
|
||||
Enable automated end-to-end testing by providing stable selectors for QA automation:
|
||||
- **`data-what`**: Semantic description of element's purpose
|
||||
- **`data-which`**: Unique identifier for specific instances
|
||||
- **`data-*`**: Additional contextual information
|
||||
|
||||
### 2. ARIA Accessibility Attributes
|
||||
Ensure web applications are accessible to all users, including those using assistive technologies:
|
||||
- **Roles**: Define element purpose (button, navigation, dialog, etc.)
|
||||
- **Properties**: Provide additional context (aria-label, aria-describedby)
|
||||
- **States**: Indicate dynamic states (aria-expanded, aria-disabled)
|
||||
- **Live Regions**: Announce dynamic content changes
|
||||
|
||||
## Why Both Are Essential
|
||||
|
||||
- **E2E Attributes**: Enable reliable automated testing without brittle CSS or XPath selectors
|
||||
- **ARIA Attributes**: Ensure compliance with WCAG standards and improve user experience for people with disabilities
|
||||
- **Together**: Create robust, testable, and accessible web applications
|
||||
|
||||
## Quick Reference
|
||||
|
||||
### Button Example
|
||||
```html
|
||||
<button
|
||||
type="button"
|
||||
(click)="onSubmit()"
|
||||
data-what="submit-button"
|
||||
data-which="registration-form"
|
||||
aria-label="Submit registration form">
|
||||
Submit
|
||||
</button>
|
||||
```
|
||||
|
||||
### Input Example
|
||||
```html
|
||||
<input
|
||||
type="text"
|
||||
[(ngModel)]="email"
|
||||
data-what="email-input"
|
||||
data-which="registration-form"
|
||||
aria-label="Email address"
|
||||
aria-describedby="email-hint"
|
||||
aria-required="true" />
|
||||
<span id="email-hint">We'll never share your email</span>
|
||||
```
|
||||
|
||||
### Dynamic List Example
|
||||
```html
|
||||
@for (item of items; track item.id) {
|
||||
<li
|
||||
(click)="selectItem(item)"
|
||||
data-what="list-item"
|
||||
[attr.data-which]="item.id"
|
||||
[attr.data-status]="item.status"
|
||||
[attr.aria-label]="'Select ' + item.name"
|
||||
role="button"
|
||||
tabindex="0">
|
||||
{{ item.name }}
|
||||
</li>
|
||||
}
|
||||
```
|
||||
|
||||
### Link Example
|
||||
```html
|
||||
<a
|
||||
[routerLink]="['/orders', orderId]"
|
||||
data-what="order-link"
|
||||
[attr.data-which]="orderId"
|
||||
[attr.aria-label]="'View order ' + orderNumber">
|
||||
View Order #{{ orderNumber }}
|
||||
</a>
|
||||
```
|
||||
|
||||
### Dialog Example
|
||||
```html
|
||||
<div
|
||||
class="dialog"
|
||||
data-what="confirmation-dialog"
|
||||
data-which="delete-item"
|
||||
role="dialog"
|
||||
aria-modal="true"
|
||||
aria-labelledby="dialog-title"
|
||||
aria-describedby="dialog-description">
|
||||
|
||||
<h2 id="dialog-title">Confirm Deletion</h2>
|
||||
<p id="dialog-description">Are you sure you want to delete this item?</p>
|
||||
|
||||
<button
|
||||
(click)="confirm()"
|
||||
data-what="confirm-button"
|
||||
data-which="delete-dialog"
|
||||
aria-label="Confirm deletion">
|
||||
Delete
|
||||
</button>
|
||||
|
||||
<button
|
||||
(click)="cancel()"
|
||||
data-what="cancel-button"
|
||||
data-which="delete-dialog"
|
||||
aria-label="Cancel deletion">
|
||||
Cancel
|
||||
</button>
|
||||
</div>
|
||||
```
|
||||
|
||||
## Common Patterns by Element Type
|
||||
|
||||
### Interactive Elements That Need Attributes
|
||||
|
||||
**Required attributes for:**
|
||||
- Buttons (`<button>`, `<ui-button>`, custom button components)
|
||||
- Form inputs (`<input>`, `<textarea>`, `<select>`)
|
||||
- Links (`<a>`, `[routerLink]`)
|
||||
- Clickable elements (elements with `(click)` handlers)
|
||||
- Custom interactive components
|
||||
- List items in dynamic lists
|
||||
- Navigation items
|
||||
- Dialog/modal controls
|
||||
|
||||
### Naming Conventions
|
||||
|
||||
**E2E `data-what` patterns:**
|
||||
- `*-button` (submit-button, cancel-button, delete-button)
|
||||
- `*-input` (email-input, search-input, quantity-input)
|
||||
- `*-link` (product-link, order-link, customer-link)
|
||||
- `*-item` (list-item, menu-item, card-item)
|
||||
- `*-dialog` (confirm-dialog, error-dialog, info-dialog)
|
||||
- `*-dropdown` (status-dropdown, category-dropdown)
|
||||
|
||||
**E2E `data-which` guidelines:**
|
||||
- Use unique identifiers: `data-which="primary"`, `data-which="customer-list"`
|
||||
- Bind dynamically for lists: `[attr.data-which]="item.id"`
|
||||
- Combine with context: `data-which="customer-{{ customerId }}-edit"`
|
||||
|
||||
**ARIA role patterns:**
|
||||
- Interactive elements: `button`, `link`, `menuitem`
|
||||
- Structural: `navigation`, `main`, `complementary`, `contentinfo`
|
||||
- Widget: `dialog`, `alertdialog`, `tooltip`, `tablist`, `tab`
|
||||
- Landmark: `banner`, `search`, `form`, `region`
|
||||
|
||||
## Best Practices
|
||||
|
||||
### E2E Attributes
|
||||
1. ✅ Add to ALL interactive elements
|
||||
2. ✅ Use kebab-case for `data-what` values
|
||||
3. ✅ Ensure `data-which` is unique within the view
|
||||
4. ✅ Use Angular binding for dynamic values: `[attr.data-*]`
|
||||
5. ✅ Avoid including sensitive data in attributes
|
||||
6. ✅ Document complex attribute patterns in template comments
|
||||
|
||||
### ARIA Attributes
|
||||
1. ✅ Use semantic HTML first (use `<button>` instead of `<div role="button">`)
|
||||
2. ✅ Provide text alternatives for all interactive elements
|
||||
3. ✅ Ensure proper keyboard navigation (tabindex, focus management)
|
||||
4. ✅ Use `aria-label` when visual label is missing
|
||||
5. ✅ Use `aria-labelledby` to reference existing visible labels
|
||||
6. ✅ Keep ARIA attributes in sync with visual states
|
||||
7. ✅ Test with screen readers (NVDA, JAWS, VoiceOver)
|
||||
|
||||
### Combined Best Practices
|
||||
1. ✅ Add both E2E and ARIA attributes to every interactive element
|
||||
2. ✅ Keep attributes close together in the HTML for readability
|
||||
3. ✅ Update tests to use `data-what` and `data-which` selectors
|
||||
4. ✅ Validate coverage: all interactive elements should have both types
|
||||
5. ✅ Review with QA and accessibility teams
|
||||
|
||||
## Detailed References
|
||||
|
||||
For comprehensive guides, examples, and patterns, see:
|
||||
|
||||
- **[E2E Testing Attributes](references/e2e-attributes.md)** - Complete E2E attribute patterns and conventions
|
||||
- **[ARIA Accessibility Attributes](references/aria-attributes.md)** - Comprehensive ARIA guidance and WCAG compliance
|
||||
- **[Combined Patterns](references/combined-patterns.md)** - Real-world examples with both attribute types
|
||||
|
||||
## Project-Specific Links
|
||||
|
||||
- **Testing Guidelines**: `docs/guidelines/testing.md` - Project testing standards including E2E attributes
|
||||
- **CLAUDE.md**: Project conventions and requirements
|
||||
- **Angular Template Skill**: `.claude/skills/angular-template` - For Angular-specific syntax
|
||||
|
||||
## Validation Checklist
|
||||
|
||||
Before considering template complete:
|
||||
- [ ] All buttons have `data-what`, `data-which`, and `aria-label`
|
||||
- [ ] All inputs have `data-what`, `data-which`, and appropriate ARIA attributes
|
||||
- [ ] All links have `data-what`, `data-which`, and descriptive ARIA labels
|
||||
- [ ] Dynamic lists use `[attr.data-*]` bindings with unique identifiers
|
||||
- [ ] Dialogs have proper ARIA roles and relationships
|
||||
- [ ] Forms have proper field associations and error announcements
|
||||
- [ ] Interactive elements are keyboard accessible (tabindex where needed)
|
||||
- [ ] No duplicate `data-which` values within the same view
|
||||
- [ ] Screen reader testing completed (if applicable)
|
||||
|
||||
## Example: Complete Form
|
||||
|
||||
```html
|
||||
<form
|
||||
data-what="registration-form"
|
||||
data-which="user-signup"
|
||||
role="form"
|
||||
aria-labelledby="form-title">
|
||||
|
||||
<h2 id="form-title">User Registration</h2>
|
||||
|
||||
<div class="form-field">
|
||||
<label for="username-input">Username</label>
|
||||
<input
|
||||
id="username-input"
|
||||
type="text"
|
||||
[(ngModel)]="username"
|
||||
data-what="username-input"
|
||||
data-which="registration-form"
|
||||
aria-required="true"
|
||||
aria-describedby="username-hint" />
|
||||
<span id="username-hint">Must be at least 3 characters</span>
|
||||
</div>
|
||||
|
||||
<div class="form-field">
|
||||
<label for="email-input">Email</label>
|
||||
<input
|
||||
id="email-input"
|
||||
type="email"
|
||||
[(ngModel)]="email"
|
||||
data-what="email-input"
|
||||
data-which="registration-form"
|
||||
aria-required="true"
|
||||
[attr.aria-invalid]="emailError ? 'true' : null"
|
||||
aria-describedby="email-error" />
|
||||
@if (emailError) {
|
||||
<span
|
||||
id="email-error"
|
||||
role="alert"
|
||||
aria-live="polite">
|
||||
{{ emailError }}
|
||||
</span>
|
||||
}
|
||||
</div>
|
||||
|
||||
<div class="form-actions">
|
||||
<button
|
||||
type="submit"
|
||||
(click)="onSubmit()"
|
||||
data-what="submit-button"
|
||||
data-which="registration-form"
|
||||
[attr.aria-disabled]="!isValid"
|
||||
aria-label="Submit registration form">
|
||||
Register
|
||||
</button>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
(click)="onCancel()"
|
||||
data-what="cancel-button"
|
||||
data-which="registration-form"
|
||||
aria-label="Cancel registration">
|
||||
Cancel
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
```
|
||||
|
||||
## Remember
|
||||
|
||||
- **Always use both E2E and ARIA attributes together**
|
||||
- **E2E attributes enable automated testing** - your QA team relies on them
|
||||
- **ARIA attributes enable accessibility** - legal requirement and right thing to do
|
||||
- **Test with real users and assistive technologies** - automated checks aren't enough
|
||||
- **Keep attributes up-to-date** - maintain as code changes
|
||||
|
||||
---
|
||||
|
||||
**This skill works automatically with Angular templates. Both E2E and ARIA attributes should be added to every interactive element.**
|
||||
@@ -1,50 +1,43 @@
|
||||
---
|
||||
name: library-scaffolder
|
||||
name: library-creator
|
||||
description: This skill should be used when creating feature/data-access/ui/util libraries or user says "create library", "new library", "scaffold library". Creates new Angular libraries in ISA-Frontend monorepo with proper Nx configuration, Vitest setup, architectural tags, and path aliases.
|
||||
---
|
||||
|
||||
# Library Scaffolder
|
||||
# Library Creator
|
||||
|
||||
## Overview
|
||||
|
||||
Automate the creation of new Angular libraries following ISA-Frontend conventions. This skill handles the complete scaffolding workflow including Nx generation, Vitest configuration with CI/CD integration, path alias verification, and initial validation.
|
||||
|
||||
## When to Use This Skill
|
||||
|
||||
Invoke this skill when:
|
||||
- User requests creating a new library
|
||||
- User mentions "new library", "scaffold library", or "create feature"
|
||||
- User wants to add a new domain/layer/feature to the monorepo
|
||||
Automate the creation of new Angular libraries following ISA-Frontend conventions. This skill handles the complete scaffolding workflow including Nx generation, Vitest configuration with CI/CD integration, architectural tags, path alias verification, and initial validation.
|
||||
|
||||
## Required Parameters
|
||||
|
||||
User must provide:
|
||||
Collect these parameters from the user:
|
||||
- **domain**: Domain name (oms, remission, checkout, ui, core, shared, utils)
|
||||
- **layer**: Layer type (feature, data-access, ui, util)
|
||||
- **name**: Library name in kebab-case
|
||||
|
||||
## Scaffolding Workflow
|
||||
## Library Creation Workflow
|
||||
|
||||
### Step 1: Validate Input
|
||||
|
||||
1. **Verify Domain**
|
||||
- Use `docs-researcher` to check `docs/library-reference.md`
|
||||
- Ensure domain follows existing patterns
|
||||
**Verify Domain:**
|
||||
- Use `docs-researcher` to check `docs/library-reference.md`
|
||||
- Ensure domain follows existing patterns
|
||||
|
||||
2. **Validate Layer**
|
||||
- Must be one of: feature, data-access, ui, util
|
||||
**Validate Layer:**
|
||||
- Must be one of: feature, data-access, ui, util
|
||||
|
||||
3. **Check Name**
|
||||
- Must be kebab-case
|
||||
- Must not conflict with existing libraries
|
||||
**Check Name:**
|
||||
- Must be kebab-case
|
||||
- Must not conflict with existing libraries
|
||||
|
||||
4. **Determine Path Depth**
|
||||
- 3 levels: `libs/domain/layer/name` → `../../../`
|
||||
- 4 levels: `libs/domain/type/layer/name` → `../../../../`
|
||||
**Determine Path Depth:**
|
||||
- 3 levels: `libs/domain/layer/name` → `../../../`
|
||||
- 4 levels: `libs/domain/type/layer/name` → `../../../../`
|
||||
|
||||
### Step 2: Run Dry-Run
|
||||
|
||||
Execute Nx generator with `--dry-run`:
|
||||
Execute Nx generator with `--dry-run` to preview changes:
|
||||
|
||||
```bash
|
||||
npx nx generate @nx/angular:library \
|
||||
@@ -118,7 +111,7 @@ cat libs/[domain]/[layer]/[name]/project.json | jq '.tags'
|
||||
|
||||
### Step 5: Configure Vitest with JUnit and Cobertura
|
||||
|
||||
Update `libs/[path]/vite.config.mts`:
|
||||
Update `libs/[path]/vite.config.mts` with this template:
|
||||
|
||||
```typescript
|
||||
/// <reference types='vitest' />
|
||||
@@ -152,22 +145,22 @@ defineConfig(() => ({
|
||||
}));
|
||||
```
|
||||
|
||||
**Critical**: Adjust path depth based on library location.
|
||||
**Critical**: Adjust path depth (`../../../` or `../../../../`) based on library location.
|
||||
|
||||
### Step 6: Verify Configuration
|
||||
|
||||
1. **Check Path Alias**
|
||||
- Verify `tsconfig.base.json` was updated
|
||||
- Should have: `"@isa/[domain]/[layer]/[name]": ["libs/[domain]/[layer]/[name]/src/index.ts"]`
|
||||
**Check Path Alias:**
|
||||
- Verify `tsconfig.base.json` was updated
|
||||
- Should have: `"@isa/[domain]/[layer]/[name]": ["libs/[domain]/[layer]/[name]/src/index.ts"]`
|
||||
|
||||
2. **Run Initial Test**
|
||||
```bash
|
||||
npx nx test [library-name] --coverage.enabled=true --skip-nx-cache
|
||||
```
|
||||
**Run Initial Test:**
|
||||
```bash
|
||||
npx nx test [library-name] --coverage.enabled=true --skip-nx-cache
|
||||
```
|
||||
|
||||
3. **Verify CI/CD Files Created**
|
||||
- JUnit XML: `testresults/junit-[library-name].xml`
|
||||
- Cobertura XML: `coverage/libs/[path]/cobertura-coverage.xml`
|
||||
**Verify CI/CD Files Created:**
|
||||
- JUnit XML: `testresults/junit-[library-name].xml`
|
||||
- Cobertura XML: `coverage/libs/[path]/cobertura-coverage.xml`
|
||||
|
||||
### Step 7: Create Library README
|
||||
|
||||
@@ -193,6 +186,8 @@ Add entry to `docs/library-reference.md` under appropriate domain:
|
||||
|
||||
### Step 9: Run Full Validation
|
||||
|
||||
Execute validation commands to ensure library is properly configured:
|
||||
|
||||
```bash
|
||||
# Lint (includes boundary checks)
|
||||
npx nx lint [library-name]
|
||||
@@ -209,6 +204,8 @@ npx nx graph --focus=[library-name]
|
||||
|
||||
### Step 10: Generate Creation Report
|
||||
|
||||
Provide this structured report to the user:
|
||||
|
||||
```
|
||||
Library Created Successfully
|
||||
============================
|
||||
@@ -251,18 +248,23 @@ Run lint to check for violations: npx nx lint [library-name]
|
||||
|
||||
## Error Handling
|
||||
|
||||
**Issue: Path depth mismatch**
|
||||
**Path depth mismatch:**
|
||||
- Count directory levels from workspace root
|
||||
- Adjust `../` in outputFile and reportsDirectory
|
||||
|
||||
**Issue: TypeScript errors in vite.config.mts**
|
||||
**TypeScript errors in vite.config.mts:**
|
||||
- Add `// @ts-expect-error` before `defineConfig()`
|
||||
|
||||
**Issue: Path alias not working**
|
||||
**Path alias not working:**
|
||||
- Check tsconfig.base.json
|
||||
- Run `npx nx reset`
|
||||
- Restart TypeScript server
|
||||
|
||||
**Library already exists:**
|
||||
- Check `tsconfig.base.json` for existing path alias
|
||||
- Use Grep to search for existing library name
|
||||
- Suggest alternative name to user
|
||||
|
||||
## References
|
||||
|
||||
- docs/guidelines/testing.md (Vitest, JUnit, Cobertura sections)
|
||||
@@ -270,6 +272,6 @@ Run lint to check for violations: npx nx lint [library-name]
|
||||
- CLAUDE.md (Library Organization, Testing Framework sections)
|
||||
- eslint.config.js (@nx/enforce-module-boundaries configuration)
|
||||
- scripts/add-library-tags.js (automatic tagging script)
|
||||
- .claude/skills/architecture-enforcer (boundary validation)
|
||||
- .claude/skills/architecture-validator (boundary validation)
|
||||
- Nx Angular Library Generator: https://nx.dev/nx-api/angular/generators/library
|
||||
- Nx Enforce Module Boundaries: https://nx.dev/nx-api/eslint-plugin/documents/enforce-module-boundaries
|
||||
@@ -1,272 +1,234 @@
|
||||
---
|
||||
name: logging
|
||||
description: This skill should be used when working with Angular components, directives, services, pipes, guards, or TypeScript classes. Logging is MANDATORY in all Angular files. Implements @isa/core/logging with logger() factory pattern, appropriate log levels, lazy evaluation for performance, error handling, and avoids console.log and common mistakes.
|
||||
---
|
||||
|
||||
# Logging Helper Skill
|
||||
|
||||
Ensures consistent and efficient logging using `@isa/core/logging` library.
|
||||
|
||||
## When to Use
|
||||
|
||||
- Adding logging to new components/services
|
||||
- Refactoring existing logging code
|
||||
- Reviewing code for proper logging patterns
|
||||
- Debugging logging issues
|
||||
|
||||
## Core Principles
|
||||
|
||||
### 1. Always Use Factory Pattern
|
||||
|
||||
```typescript
|
||||
import { logger } from '@isa/core/logging';
|
||||
|
||||
// ✅ DO
|
||||
#logger = logger();
|
||||
|
||||
// ❌ DON'T
|
||||
constructor(private loggingService: LoggingService) {}
|
||||
```
|
||||
|
||||
### 2. Choose Appropriate Log Levels
|
||||
|
||||
- **Trace**: Fine-grained debugging (method entry/exit)
|
||||
- **Debug**: Development debugging (variable states)
|
||||
- **Info**: Runtime information (user actions, events)
|
||||
- **Warn**: Potentially harmful situations
|
||||
- **Error**: Errors affecting functionality
|
||||
|
||||
### 3. Context Patterns
|
||||
|
||||
**Static Context** (component level):
|
||||
```typescript
|
||||
#logger = logger({ component: 'UserProfileComponent' });
|
||||
```
|
||||
|
||||
**Dynamic Context** (instance level):
|
||||
```typescript
|
||||
#logger = logger(() => ({
|
||||
userId: this.authService.currentUserId,
|
||||
storeId: this.config.storeId
|
||||
}));
|
||||
```
|
||||
|
||||
**Message Context** (use functions for performance):
|
||||
```typescript
|
||||
// ✅ Recommended - lazy evaluation
|
||||
this.#logger.info('Order processed', () => ({
|
||||
orderId: order.id,
|
||||
total: order.total,
|
||||
timestamp: Date.now()
|
||||
}));
|
||||
|
||||
// ✅ Acceptable - static values
|
||||
this.#logger.info('Order processed', {
|
||||
orderId: order.id,
|
||||
status: 'completed'
|
||||
});
|
||||
```
|
||||
|
||||
## Essential Patterns
|
||||
|
||||
### Component Logging
|
||||
```typescript
|
||||
@Component({
|
||||
selector: 'app-product-list',
|
||||
standalone: true,
|
||||
})
|
||||
export class ProductListComponent {
|
||||
#logger = logger({ component: 'ProductListComponent' });
|
||||
|
||||
ngOnInit(): void {
|
||||
this.#logger.info('Component initialized');
|
||||
}
|
||||
|
||||
onAction(id: string): void {
|
||||
this.#logger.debug('Action triggered', { id });
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Service Logging
|
||||
```typescript
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class DataService {
|
||||
#logger = logger({ service: 'DataService' });
|
||||
|
||||
fetchData(endpoint: string): Observable<Data> {
|
||||
this.#logger.debug('Fetching data', { endpoint });
|
||||
|
||||
return this.http.get<Data>(endpoint).pipe(
|
||||
tap((data) => this.#logger.info('Data fetched', () => ({
|
||||
endpoint,
|
||||
size: data.length
|
||||
}))),
|
||||
catchError((error) => {
|
||||
this.#logger.error('Fetch failed', error, () => ({
|
||||
endpoint,
|
||||
status: error.status
|
||||
}));
|
||||
return throwError(() => error);
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Error Handling
|
||||
```typescript
|
||||
try {
|
||||
await this.processOrder(orderId);
|
||||
} catch (error) {
|
||||
this.#logger.error('Order processing failed', error as Error, () => ({
|
||||
orderId,
|
||||
step: this.currentStep,
|
||||
attemptNumber: this.retryCount
|
||||
}));
|
||||
throw error;
|
||||
}
|
||||
```
|
||||
|
||||
### Hierarchical Context
|
||||
```typescript
|
||||
@Component({
|
||||
providers: [
|
||||
provideLoggerContext({ feature: 'checkout', module: 'sales' })
|
||||
]
|
||||
})
|
||||
export class CheckoutComponent {
|
||||
#logger = logger(() => ({ userId: this.userService.currentUserId }));
|
||||
|
||||
// Logs include: feature, module, userId + message context
|
||||
}
|
||||
```
|
||||
|
||||
## Common Mistakes to Avoid
|
||||
|
||||
```typescript
|
||||
// ❌ Don't use console.log
|
||||
console.log('User logged in');
|
||||
// ✅ Use logger
|
||||
this.#logger.info('User logged in');
|
||||
|
||||
// ❌ Don't create expensive context eagerly
|
||||
this.#logger.debug('Processing', {
|
||||
data: this.computeExpensive() // Always executes
|
||||
});
|
||||
// ✅ Use function for lazy evaluation
|
||||
this.#logger.debug('Processing', () => ({
|
||||
data: this.computeExpensive() // Only if debug enabled
|
||||
}));
|
||||
|
||||
// ❌ Don't log in tight loops
|
||||
for (const item of items) {
|
||||
this.#logger.debug('Item', { item });
|
||||
}
|
||||
// ✅ Log aggregates
|
||||
this.#logger.debug('Batch processed', () => ({
|
||||
count: items.length
|
||||
}));
|
||||
|
||||
// ❌ Don't log sensitive data
|
||||
this.#logger.info('User auth', { password: user.password });
|
||||
// ✅ Log safe identifiers only
|
||||
this.#logger.info('User auth', { userId: user.id });
|
||||
|
||||
// ❌ Don't miss error object
|
||||
this.#logger.error('Failed');
|
||||
// ✅ Include error object
|
||||
this.#logger.error('Failed', error as Error);
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
### App Configuration
|
||||
```typescript
|
||||
// app.config.ts
|
||||
import { ApplicationConfig, isDevMode } from '@angular/core';
|
||||
import {
|
||||
provideLogging, withLogLevel, withSink,
|
||||
LogLevel, ConsoleLogSink
|
||||
} from '@isa/core/logging';
|
||||
|
||||
export const appConfig: ApplicationConfig = {
|
||||
providers: [
|
||||
provideLogging(
|
||||
withLogLevel(isDevMode() ? LogLevel.Debug : LogLevel.Warn),
|
||||
withSink(ConsoleLogSink),
|
||||
withContext({ app: 'ISA', version: '1.0.0' })
|
||||
)
|
||||
]
|
||||
};
|
||||
```
|
||||
|
||||
## Testing
|
||||
|
||||
```typescript
|
||||
import { createComponentFactory, Spectator } from '@ngneat/spectator/jest';
|
||||
import { LoggingService } from '@isa/core/logging';
|
||||
|
||||
describe('MyComponent', () => {
|
||||
const createComponent = createComponentFactory({
|
||||
component: MyComponent,
|
||||
mocks: [LoggingService]
|
||||
});
|
||||
|
||||
it('should log error', () => {
|
||||
const spectator = createComponent();
|
||||
const loggingService = spectator.inject(LoggingService);
|
||||
|
||||
spectator.component.riskyOperation();
|
||||
|
||||
expect(loggingService.error).toHaveBeenCalledWith(
|
||||
expect.any(String),
|
||||
expect.any(Error),
|
||||
expect.any(Function)
|
||||
);
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
## Code Review Checklist
|
||||
|
||||
- [ ] Uses `logger()` factory, not `LoggingService` injection
|
||||
- [ ] Appropriate log level for each message
|
||||
- [ ] Context functions for expensive operations
|
||||
- [ ] No sensitive information (passwords, tokens, PII)
|
||||
- [ ] No `console.log` statements
|
||||
- [ ] Error logs include error object
|
||||
- [ ] No logging in tight loops
|
||||
- [ ] Component/service identified in context
|
||||
- [ ] E2E attributes on interactive elements
|
||||
|
||||
## Quick Reference
|
||||
|
||||
```typescript
|
||||
// Import
|
||||
import { logger, provideLoggerContext } from '@isa/core/logging';
|
||||
|
||||
// Create logger
|
||||
#logger = logger(); // Basic
|
||||
#logger = logger({ component: 'Name' }); // Static context
|
||||
#logger = logger(() => ({ id: this.id })); // Dynamic context
|
||||
|
||||
// Log messages
|
||||
this.#logger.trace('Detailed trace');
|
||||
this.#logger.debug('Debug info');
|
||||
this.#logger.info('General info', () => ({ key: value }));
|
||||
this.#logger.warn('Warning');
|
||||
this.#logger.error('Error', error, () => ({ context }));
|
||||
|
||||
// Component context
|
||||
@Component({
|
||||
providers: [provideLoggerContext({ feature: 'users' })]
|
||||
})
|
||||
```
|
||||
|
||||
## Additional Resources
|
||||
|
||||
- Full documentation: `libs/core/logging/README.md`
|
||||
- Examples: `.claude/skills/logging-helper/examples.md`
|
||||
- Quick reference: `.claude/skills/logging-helper/reference.md`
|
||||
- Troubleshooting: `.claude/skills/logging-helper/troubleshooting.md`
|
||||
---
|
||||
name: logging
|
||||
description: This skill should be used when working with Angular components, directives, services, pipes, guards, or TypeScript classes. Logging is MANDATORY in all Angular files. Implements @isa/core/logging with logger() factory pattern, appropriate log levels, lazy evaluation for performance, error handling, and avoids console.log and common mistakes.
|
||||
---
|
||||
|
||||
# Logging
|
||||
|
||||
Ensures consistent and efficient logging using `@isa/core/logging` library.
|
||||
|
||||
## Core Principles
|
||||
|
||||
### 1. Always Use Factory Pattern
|
||||
|
||||
```typescript
|
||||
import { logger } from '@isa/core/logging';
|
||||
|
||||
// ✅ DO
|
||||
#logger = logger();
|
||||
|
||||
// ❌ DON'T
|
||||
constructor(private loggingService: LoggingService) {}
|
||||
```
|
||||
|
||||
### 2. Choose Appropriate Log Levels
|
||||
|
||||
- **Trace**: Fine-grained debugging (method entry/exit)
|
||||
- **Debug**: Development debugging (variable states)
|
||||
- **Info**: Runtime information (user actions, events)
|
||||
- **Warn**: Potentially harmful situations
|
||||
- **Error**: Errors affecting functionality
|
||||
|
||||
### 3. Context Patterns
|
||||
|
||||
**Static Context** (component level):
|
||||
```typescript
|
||||
#logger = logger({ component: 'UserProfileComponent' });
|
||||
```
|
||||
|
||||
**Dynamic Context** (instance level):
|
||||
```typescript
|
||||
#logger = logger(() => ({
|
||||
userId: this.authService.currentUserId,
|
||||
storeId: this.config.storeId
|
||||
}));
|
||||
```
|
||||
|
||||
**Message Context** (use functions for performance):
|
||||
```typescript
|
||||
// ✅ Recommended - lazy evaluation
|
||||
this.#logger.info('Order processed', () => ({
|
||||
orderId: order.id,
|
||||
total: order.total,
|
||||
timestamp: Date.now()
|
||||
}));
|
||||
|
||||
// ✅ Acceptable - static values
|
||||
this.#logger.info('Order processed', {
|
||||
orderId: order.id,
|
||||
status: 'completed'
|
||||
});
|
||||
```
|
||||
|
||||
## Essential Patterns
|
||||
|
||||
### Component Logging
|
||||
```typescript
|
||||
@Component({
|
||||
selector: 'app-product-list',
|
||||
standalone: true,
|
||||
})
|
||||
export class ProductListComponent {
|
||||
#logger = logger({ component: 'ProductListComponent' });
|
||||
|
||||
ngOnInit(): void {
|
||||
this.#logger.info('Component initialized');
|
||||
}
|
||||
|
||||
onAction(id: string): void {
|
||||
this.#logger.debug('Action triggered', { id });
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Service Logging
|
||||
```typescript
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class DataService {
|
||||
#logger = logger({ service: 'DataService' });
|
||||
|
||||
fetchData(endpoint: string): Observable<Data> {
|
||||
this.#logger.debug('Fetching data', { endpoint });
|
||||
|
||||
return this.http.get<Data>(endpoint).pipe(
|
||||
tap((data) => this.#logger.info('Data fetched', () => ({
|
||||
endpoint,
|
||||
size: data.length
|
||||
}))),
|
||||
catchError((error) => {
|
||||
this.#logger.error('Fetch failed', error, () => ({
|
||||
endpoint,
|
||||
status: error.status
|
||||
}));
|
||||
return throwError(() => error);
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Error Handling
|
||||
```typescript
|
||||
try {
|
||||
await this.processOrder(orderId);
|
||||
} catch (error) {
|
||||
this.#logger.error('Order processing failed', error as Error, () => ({
|
||||
orderId,
|
||||
step: this.currentStep,
|
||||
attemptNumber: this.retryCount
|
||||
}));
|
||||
throw error;
|
||||
}
|
||||
```
|
||||
|
||||
### Hierarchical Context
|
||||
```typescript
|
||||
@Component({
|
||||
providers: [
|
||||
provideLoggerContext({ feature: 'checkout', module: 'sales' })
|
||||
]
|
||||
})
|
||||
export class CheckoutComponent {
|
||||
#logger = logger(() => ({ userId: this.userService.currentUserId }));
|
||||
|
||||
// Logs include: feature, module, userId + message context
|
||||
}
|
||||
```
|
||||
|
||||
## Common Mistakes to Avoid
|
||||
|
||||
```typescript
|
||||
// ❌ Don't use console.log
|
||||
console.log('User logged in');
|
||||
// ✅ Use logger
|
||||
this.#logger.info('User logged in');
|
||||
|
||||
// ❌ Don't create expensive context eagerly
|
||||
this.#logger.debug('Processing', {
|
||||
data: this.computeExpensive() // Always executes
|
||||
});
|
||||
// ✅ Use function for lazy evaluation
|
||||
this.#logger.debug('Processing', () => ({
|
||||
data: this.computeExpensive() // Only if debug enabled
|
||||
}));
|
||||
|
||||
// ❌ Don't log in tight loops
|
||||
for (const item of items) {
|
||||
this.#logger.debug('Item', { item });
|
||||
}
|
||||
// ✅ Log aggregates
|
||||
this.#logger.debug('Batch processed', () => ({
|
||||
count: items.length
|
||||
}));
|
||||
|
||||
// ❌ Don't log sensitive data
|
||||
this.#logger.info('User auth', { password: user.password });
|
||||
// ✅ Log safe identifiers only
|
||||
this.#logger.info('User auth', { userId: user.id });
|
||||
|
||||
// ❌ Don't miss error object
|
||||
this.#logger.error('Failed');
|
||||
// ✅ Include error object
|
||||
this.#logger.error('Failed', error as Error);
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
### App Configuration
|
||||
```typescript
|
||||
// app.config.ts
|
||||
import { ApplicationConfig, isDevMode } from '@angular/core';
|
||||
import {
|
||||
provideLogging, withLogLevel, withSink,
|
||||
LogLevel, ConsoleLogSink
|
||||
} from '@isa/core/logging';
|
||||
|
||||
export const appConfig: ApplicationConfig = {
|
||||
providers: [
|
||||
provideLogging(
|
||||
withLogLevel(isDevMode() ? LogLevel.Debug : LogLevel.Warn),
|
||||
withSink(ConsoleLogSink),
|
||||
withContext({ app: 'ISA', version: '1.0.0' })
|
||||
)
|
||||
]
|
||||
};
|
||||
```
|
||||
|
||||
## Testing
|
||||
|
||||
```typescript
|
||||
import { createComponentFactory, Spectator } from '@ngneat/spectator/jest';
|
||||
import { LoggingService } from '@isa/core/logging';
|
||||
|
||||
describe('MyComponent', () => {
|
||||
const createComponent = createComponentFactory({
|
||||
component: MyComponent,
|
||||
mocks: [LoggingService]
|
||||
});
|
||||
|
||||
it('should log error', () => {
|
||||
const spectator = createComponent();
|
||||
const loggingService = spectator.inject(LoggingService);
|
||||
|
||||
spectator.component.riskyOperation();
|
||||
|
||||
expect(loggingService.error).toHaveBeenCalledWith(
|
||||
expect.any(String),
|
||||
expect.any(Error),
|
||||
expect.any(Function)
|
||||
);
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
## Additional Resources
|
||||
|
||||
**For more detailed information:**
|
||||
|
||||
- **API signatures and patterns**: See [references/api-reference.md](references/api-reference.md) for complete API documentation
|
||||
- **Real-world examples**: See [references/examples.md](references/examples.md) for components, services, guards, interceptors, and more
|
||||
- **Troubleshooting**: See [references/troubleshooting.md](references/troubleshooting.md) for common issues and solutions
|
||||
|
||||
**Project documentation:**
|
||||
|
||||
- Full library documentation: `libs/core/logging/README.md`
|
||||
|
||||
@@ -1,192 +1,192 @@
|
||||
# Logging Quick Reference
|
||||
|
||||
## API Signatures
|
||||
|
||||
```typescript
|
||||
// Factory
|
||||
function logger(ctx?: MaybeLoggerContextFn): LoggerApi
|
||||
|
||||
// Logger API
|
||||
interface LoggerApi {
|
||||
trace(message: string, context?: MaybeLoggerContextFn): void;
|
||||
debug(message: string, context?: MaybeLoggerContextFn): void;
|
||||
info(message: string, context?: MaybeLoggerContextFn): void;
|
||||
warn(message: string, context?: MaybeLoggerContextFn): void;
|
||||
error(message: string, error?: Error, context?: MaybeLoggerContextFn): void;
|
||||
}
|
||||
|
||||
// Types
|
||||
type MaybeLoggerContextFn = LoggerContext | (() => LoggerContext);
|
||||
interface LoggerContext { [key: string]: unknown; }
|
||||
```
|
||||
|
||||
## Common Patterns
|
||||
|
||||
| Pattern | Code |
|
||||
|---------|------|
|
||||
| Basic logger | `#logger = logger()` |
|
||||
| Static context | `#logger = logger({ component: 'Name' })` |
|
||||
| Dynamic context | `#logger = logger(() => ({ id: this.id }))` |
|
||||
| Log info | `this.#logger.info('Message')` |
|
||||
| Log with context | `this.#logger.info('Message', () => ({ key: value }))` |
|
||||
| Log error | `this.#logger.error('Error', error)` |
|
||||
| Error with context | `this.#logger.error('Error', error, () => ({ id }))` |
|
||||
| Component context | `providers: [provideLoggerContext({ feature: 'x' })]` |
|
||||
|
||||
## Configuration
|
||||
|
||||
```typescript
|
||||
// app.config.ts
|
||||
import { provideLogging, withLogLevel, withSink, withContext,
|
||||
LogLevel, ConsoleLogSink } from '@isa/core/logging';
|
||||
|
||||
export const appConfig: ApplicationConfig = {
|
||||
providers: [
|
||||
provideLogging(
|
||||
withLogLevel(isDevMode() ? LogLevel.Debug : LogLevel.Warn),
|
||||
withSink(ConsoleLogSink),
|
||||
withContext({ app: 'ISA', version: '1.0.0' })
|
||||
)
|
||||
]
|
||||
};
|
||||
```
|
||||
|
||||
## Log Levels
|
||||
|
||||
| Level | Use Case | Example |
|
||||
|-------|----------|---------|
|
||||
| `Trace` | Method entry/exit | `this.#logger.trace('Entering processData')` |
|
||||
| `Debug` | Development info | `this.#logger.debug('Variable state', () => ({ x }))` |
|
||||
| `Info` | Runtime events | `this.#logger.info('User logged in', { userId })` |
|
||||
| `Warn` | Warnings | `this.#logger.warn('Deprecated API used')` |
|
||||
| `Error` | Errors | `this.#logger.error('Operation failed', error)` |
|
||||
| `Off` | Disable logging | `withLogLevel(LogLevel.Off)` |
|
||||
|
||||
## Decision Trees
|
||||
|
||||
### Context Type Decision
|
||||
```
|
||||
Value changes at runtime?
|
||||
├─ Yes → () => ({ value: this.getValue() })
|
||||
└─ No → { value: 'static' }
|
||||
|
||||
Computing value is expensive?
|
||||
├─ Yes → () => ({ data: this.compute() })
|
||||
└─ No → Either works
|
||||
```
|
||||
|
||||
### Log Level Decision
|
||||
```
|
||||
Method flow details? → Trace
|
||||
Development debug? → Debug
|
||||
Runtime information? → Info
|
||||
Potential problem? → Warn
|
||||
Error occurred? → Error
|
||||
```
|
||||
|
||||
## Performance Tips
|
||||
|
||||
```typescript
|
||||
// ✅ DO: Lazy evaluation
|
||||
this.#logger.debug('Data', () => ({
|
||||
result: this.expensive() // Only runs if debug enabled
|
||||
}));
|
||||
|
||||
// ❌ DON'T: Eager evaluation
|
||||
this.#logger.debug('Data', {
|
||||
result: this.expensive() // Always runs
|
||||
});
|
||||
|
||||
// ✅ DO: Log aggregates
|
||||
this.#logger.info('Batch done', () => ({ count: items.length }));
|
||||
|
||||
// ❌ DON'T: Log in loops
|
||||
for (const item of items) {
|
||||
this.#logger.debug('Item', { item }); // Performance hit
|
||||
}
|
||||
```
|
||||
|
||||
## Testing
|
||||
|
||||
```typescript
|
||||
import { createComponentFactory, Spectator } from '@ngneat/spectator/jest';
|
||||
import { LoggingService } from '@isa/core/logging';
|
||||
|
||||
describe('MyComponent', () => {
|
||||
const createComponent = createComponentFactory({
|
||||
component: MyComponent,
|
||||
mocks: [LoggingService]
|
||||
});
|
||||
|
||||
it('logs error', () => {
|
||||
const spectator = createComponent();
|
||||
const logger = spectator.inject(LoggingService);
|
||||
|
||||
spectator.component.operation();
|
||||
|
||||
expect(logger.error).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
## Custom Sink
|
||||
|
||||
```typescript
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Sink, LogLevel, LoggerContext } from '@isa/core/logging';
|
||||
|
||||
@Injectable()
|
||||
export class CustomSink implements Sink {
|
||||
log(level: LogLevel, message: string, context?: LoggerContext, error?: Error): void {
|
||||
// Implementation
|
||||
}
|
||||
}
|
||||
|
||||
// Register
|
||||
provideLogging(withSink(CustomSink))
|
||||
```
|
||||
|
||||
## Sink Function (with DI)
|
||||
|
||||
```typescript
|
||||
import { inject } from '@angular/core';
|
||||
import { SinkFn, LogLevel } from '@isa/core/logging';
|
||||
|
||||
export const remoteSink: SinkFn = () => {
|
||||
const http = inject(HttpClient);
|
||||
|
||||
return (level, message, context, error) => {
|
||||
if (level === LogLevel.Error) {
|
||||
http.post('/api/logs', { level, message, context, error }).subscribe();
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
// Register
|
||||
provideLogging(withSinkFn(remoteSink))
|
||||
```
|
||||
|
||||
## Common Imports
|
||||
|
||||
```typescript
|
||||
// Main imports
|
||||
import { logger, provideLoggerContext } from '@isa/core/logging';
|
||||
|
||||
// Configuration imports
|
||||
import {
|
||||
provideLogging,
|
||||
withLogLevel,
|
||||
withSink,
|
||||
withContext,
|
||||
LogLevel,
|
||||
ConsoleLogSink
|
||||
} from '@isa/core/logging';
|
||||
|
||||
// Type imports
|
||||
import {
|
||||
LoggerApi,
|
||||
Sink,
|
||||
SinkFn,
|
||||
LoggerContext
|
||||
} from '@isa/core/logging';
|
||||
```
|
||||
# Logging API Reference
|
||||
|
||||
## API Signatures
|
||||
|
||||
```typescript
|
||||
// Factory
|
||||
function logger(ctx?: MaybeLoggerContextFn): LoggerApi
|
||||
|
||||
// Logger API
|
||||
interface LoggerApi {
|
||||
trace(message: string, context?: MaybeLoggerContextFn): void;
|
||||
debug(message: string, context?: MaybeLoggerContextFn): void;
|
||||
info(message: string, context?: MaybeLoggerContextFn): void;
|
||||
warn(message: string, context?: MaybeLoggerContextFn): void;
|
||||
error(message: string, error?: Error, context?: MaybeLoggerContextFn): void;
|
||||
}
|
||||
|
||||
// Types
|
||||
type MaybeLoggerContextFn = LoggerContext | (() => LoggerContext);
|
||||
interface LoggerContext { [key: string]: unknown; }
|
||||
```
|
||||
|
||||
## Common Patterns
|
||||
|
||||
| Pattern | Code |
|
||||
|---------|------|
|
||||
| Basic logger | `#logger = logger()` |
|
||||
| Static context | `#logger = logger({ component: 'Name' })` |
|
||||
| Dynamic context | `#logger = logger(() => ({ id: this.id }))` |
|
||||
| Log info | `this.#logger.info('Message')` |
|
||||
| Log with context | `this.#logger.info('Message', () => ({ key: value }))` |
|
||||
| Log error | `this.#logger.error('Error', error)` |
|
||||
| Error with context | `this.#logger.error('Error', error, () => ({ id }))` |
|
||||
| Component context | `providers: [provideLoggerContext({ feature: 'x' })]` |
|
||||
|
||||
## Configuration
|
||||
|
||||
```typescript
|
||||
// app.config.ts
|
||||
import { provideLogging, withLogLevel, withSink, withContext,
|
||||
LogLevel, ConsoleLogSink } from '@isa/core/logging';
|
||||
|
||||
export const appConfig: ApplicationConfig = {
|
||||
providers: [
|
||||
provideLogging(
|
||||
withLogLevel(isDevMode() ? LogLevel.Debug : LogLevel.Warn),
|
||||
withSink(ConsoleLogSink),
|
||||
withContext({ app: 'ISA', version: '1.0.0' })
|
||||
)
|
||||
]
|
||||
};
|
||||
```
|
||||
|
||||
## Log Levels
|
||||
|
||||
| Level | Use Case | Example |
|
||||
|-------|----------|---------|
|
||||
| `Trace` | Method entry/exit | `this.#logger.trace('Entering processData')` |
|
||||
| `Debug` | Development info | `this.#logger.debug('Variable state', () => ({ x }))` |
|
||||
| `Info` | Runtime events | `this.#logger.info('User logged in', { userId })` |
|
||||
| `Warn` | Warnings | `this.#logger.warn('Deprecated API used')` |
|
||||
| `Error` | Errors | `this.#logger.error('Operation failed', error)` |
|
||||
| `Off` | Disable logging | `withLogLevel(LogLevel.Off)` |
|
||||
|
||||
## Decision Trees
|
||||
|
||||
### Context Type Decision
|
||||
```
|
||||
Value changes at runtime?
|
||||
├─ Yes → () => ({ value: this.getValue() })
|
||||
└─ No → { value: 'static' }
|
||||
|
||||
Computing value is expensive?
|
||||
├─ Yes → () => ({ data: this.compute() })
|
||||
└─ No → Either works
|
||||
```
|
||||
|
||||
### Log Level Decision
|
||||
```
|
||||
Method flow details? → Trace
|
||||
Development debug? → Debug
|
||||
Runtime information? → Info
|
||||
Potential problem? → Warn
|
||||
Error occurred? → Error
|
||||
```
|
||||
|
||||
## Performance Tips
|
||||
|
||||
```typescript
|
||||
// ✅ DO: Lazy evaluation
|
||||
this.#logger.debug('Data', () => ({
|
||||
result: this.expensive() // Only runs if debug enabled
|
||||
}));
|
||||
|
||||
// ❌ DON'T: Eager evaluation
|
||||
this.#logger.debug('Data', {
|
||||
result: this.expensive() // Always runs
|
||||
});
|
||||
|
||||
// ✅ DO: Log aggregates
|
||||
this.#logger.info('Batch done', () => ({ count: items.length }));
|
||||
|
||||
// ❌ DON'T: Log in loops
|
||||
for (const item of items) {
|
||||
this.#logger.debug('Item', { item }); // Performance hit
|
||||
}
|
||||
```
|
||||
|
||||
## Testing
|
||||
|
||||
```typescript
|
||||
import { createComponentFactory, Spectator } from '@ngneat/spectator/jest';
|
||||
import { LoggingService } from '@isa/core/logging';
|
||||
|
||||
describe('MyComponent', () => {
|
||||
const createComponent = createComponentFactory({
|
||||
component: MyComponent,
|
||||
mocks: [LoggingService]
|
||||
});
|
||||
|
||||
it('logs error', () => {
|
||||
const spectator = createComponent();
|
||||
const logger = spectator.inject(LoggingService);
|
||||
|
||||
spectator.component.operation();
|
||||
|
||||
expect(logger.error).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
## Custom Sink
|
||||
|
||||
```typescript
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Sink, LogLevel, LoggerContext } from '@isa/core/logging';
|
||||
|
||||
@Injectable()
|
||||
export class CustomSink implements Sink {
|
||||
log(level: LogLevel, message: string, context?: LoggerContext, error?: Error): void {
|
||||
// Implementation
|
||||
}
|
||||
}
|
||||
|
||||
// Register
|
||||
provideLogging(withSink(CustomSink))
|
||||
```
|
||||
|
||||
## Sink Function (with DI)
|
||||
|
||||
```typescript
|
||||
import { inject } from '@angular/core';
|
||||
import { SinkFn, LogLevel } from '@isa/core/logging';
|
||||
|
||||
export const remoteSink: SinkFn = () => {
|
||||
const http = inject(HttpClient);
|
||||
|
||||
return (level, message, context, error) => {
|
||||
if (level === LogLevel.Error) {
|
||||
http.post('/api/logs', { level, message, context, error }).subscribe();
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
// Register
|
||||
provideLogging(withSinkFn(remoteSink))
|
||||
```
|
||||
|
||||
## Common Imports
|
||||
|
||||
```typescript
|
||||
// Main imports
|
||||
import { logger, provideLoggerContext } from '@isa/core/logging';
|
||||
|
||||
// Configuration imports
|
||||
import {
|
||||
provideLogging,
|
||||
withLogLevel,
|
||||
withSink,
|
||||
withContext,
|
||||
LogLevel,
|
||||
ConsoleLogSink
|
||||
} from '@isa/core/logging';
|
||||
|
||||
// Type imports
|
||||
import {
|
||||
LoggerApi,
|
||||
Sink,
|
||||
SinkFn,
|
||||
LoggerContext
|
||||
} from '@isa/core/logging';
|
||||
```
|
||||
@@ -1,350 +1,350 @@
|
||||
# Logging Examples
|
||||
|
||||
Concise real-world examples of logging patterns.
|
||||
|
||||
## 1. Component with Observable
|
||||
|
||||
```typescript
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
import { logger } from '@isa/core/logging';
|
||||
|
||||
@Component({
|
||||
selector: 'app-product-list',
|
||||
standalone: true,
|
||||
})
|
||||
export class ProductListComponent implements OnInit {
|
||||
#logger = logger({ component: 'ProductListComponent' });
|
||||
|
||||
constructor(private productService: ProductService) {}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.#logger.info('Component initialized');
|
||||
this.loadProducts();
|
||||
}
|
||||
|
||||
private loadProducts(): void {
|
||||
this.productService.getProducts().subscribe({
|
||||
next: (products) => {
|
||||
this.#logger.info('Products loaded', () => ({ count: products.length }));
|
||||
},
|
||||
error: (error) => {
|
||||
this.#logger.error('Failed to load products', error);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 2. Service with HTTP
|
||||
|
||||
```typescript
|
||||
import { Injectable, inject } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { logger } from '@isa/core/logging';
|
||||
import { catchError, tap } from 'rxjs/operators';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class OrderService {
|
||||
private http = inject(HttpClient);
|
||||
#logger = logger({ service: 'OrderService' });
|
||||
|
||||
getOrder(id: string): Observable<Order> {
|
||||
this.#logger.debug('Fetching order', { id });
|
||||
|
||||
return this.http.get<Order>(`/api/orders/${id}`).pipe(
|
||||
tap((order) => this.#logger.info('Order fetched', () => ({
|
||||
id,
|
||||
status: order.status
|
||||
}))),
|
||||
catchError((error) => {
|
||||
this.#logger.error('Fetch failed', error, () => ({ id, status: error.status }));
|
||||
throw error;
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 3. Hierarchical Context
|
||||
|
||||
```typescript
|
||||
import { Component } from '@angular/core';
|
||||
import { logger, provideLoggerContext } from '@isa/core/logging';
|
||||
|
||||
@Component({
|
||||
selector: 'oms-return-process',
|
||||
standalone: true,
|
||||
providers: [
|
||||
provideLoggerContext({ feature: 'returns', module: 'oms' })
|
||||
],
|
||||
})
|
||||
export class ReturnProcessComponent {
|
||||
#logger = logger(() => ({
|
||||
processId: this.currentProcessId,
|
||||
step: this.currentStep
|
||||
}));
|
||||
|
||||
private currentProcessId = crypto.randomUUID();
|
||||
private currentStep = 1;
|
||||
|
||||
startProcess(orderId: string): void {
|
||||
// Logs include: feature, module, processId, step, orderId
|
||||
this.#logger.info('Process started', { orderId });
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 4. NgRx Effect
|
||||
|
||||
```typescript
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Actions, createEffect, ofType } from '@ngrx/effects';
|
||||
import { logger } from '@isa/core/logging';
|
||||
import { map, catchError, tap } from 'rxjs/operators';
|
||||
import { of } from 'rxjs';
|
||||
|
||||
@Injectable()
|
||||
export class OrdersEffects {
|
||||
#logger = logger({ effect: 'OrdersEffects' });
|
||||
|
||||
loadOrders$ = createEffect(() =>
|
||||
this.actions$.pipe(
|
||||
ofType(OrdersActions.loadOrders),
|
||||
tap((action) => this.#logger.debug('Loading orders', () => ({
|
||||
page: action.page
|
||||
}))),
|
||||
mergeMap((action) =>
|
||||
this.orderService.getOrders(action.filters).pipe(
|
||||
map((orders) => {
|
||||
this.#logger.info('Orders loaded', () => ({ count: orders.length }));
|
||||
return OrdersActions.loadOrdersSuccess({ orders });
|
||||
}),
|
||||
catchError((error) => {
|
||||
this.#logger.error('Load failed', error);
|
||||
return of(OrdersActions.loadOrdersFailure({ error }));
|
||||
})
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
constructor(
|
||||
private actions$: Actions,
|
||||
private orderService: OrderService
|
||||
) {}
|
||||
}
|
||||
```
|
||||
|
||||
## 5. Guard with Authorization
|
||||
|
||||
```typescript
|
||||
import { inject } from '@angular/core';
|
||||
import { CanActivateFn, Router } from '@angular/router';
|
||||
import { logger } from '@isa/core/logging';
|
||||
|
||||
export const authGuard: CanActivateFn = (route, state) => {
|
||||
const authService = inject(AuthService);
|
||||
const router = inject(Router);
|
||||
const log = logger({ guard: 'AuthGuard' });
|
||||
|
||||
if (authService.isAuthenticated()) {
|
||||
log.debug('Access granted', () => ({ route: state.url }));
|
||||
return true;
|
||||
}
|
||||
|
||||
log.warn('Access denied', () => ({
|
||||
attemptedRoute: state.url,
|
||||
redirectTo: '/login'
|
||||
}));
|
||||
return router.createUrlTree(['/login']);
|
||||
};
|
||||
```
|
||||
|
||||
## 6. HTTP Interceptor
|
||||
|
||||
```typescript
|
||||
import { HttpInterceptorFn } from '@angular/common/http';
|
||||
import { inject } from '@angular/core';
|
||||
import { tap, catchError } from 'rxjs/operators';
|
||||
import { LoggingService } from '@isa/core/logging';
|
||||
|
||||
export const loggingInterceptor: HttpInterceptorFn = (req, next) => {
|
||||
const loggingService = inject(LoggingService);
|
||||
const startTime = performance.now();
|
||||
|
||||
loggingService.debug('HTTP Request', () => ({
|
||||
method: req.method,
|
||||
url: req.url
|
||||
}));
|
||||
|
||||
return next(req).pipe(
|
||||
tap((event) => {
|
||||
if (event.type === HttpEventType.Response) {
|
||||
loggingService.info('HTTP Response', () => ({
|
||||
method: req.method,
|
||||
url: req.url,
|
||||
status: event.status,
|
||||
duration: `${(performance.now() - startTime).toFixed(2)}ms`
|
||||
}));
|
||||
}
|
||||
}),
|
||||
catchError((error) => {
|
||||
loggingService.error('HTTP Error', error, () => ({
|
||||
method: req.method,
|
||||
url: req.url,
|
||||
status: error.status
|
||||
}));
|
||||
return throwError(() => error);
|
||||
})
|
||||
);
|
||||
};
|
||||
```
|
||||
|
||||
## 7. Form Validation
|
||||
|
||||
```typescript
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
|
||||
import { logger } from '@isa/core/logging';
|
||||
|
||||
@Component({
|
||||
selector: 'shared-user-form',
|
||||
standalone: true,
|
||||
})
|
||||
export class UserFormComponent implements OnInit {
|
||||
#logger = logger({ component: 'UserFormComponent' });
|
||||
form!: FormGroup;
|
||||
|
||||
constructor(private fb: FormBuilder) {}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.form = this.fb.group({
|
||||
name: ['', Validators.required],
|
||||
email: ['', [Validators.required, Validators.email]]
|
||||
});
|
||||
}
|
||||
|
||||
onSubmit(): void {
|
||||
if (this.form.invalid) {
|
||||
this.#logger.warn('Invalid form submission', () => ({
|
||||
errors: this.getFormErrors()
|
||||
}));
|
||||
return;
|
||||
}
|
||||
|
||||
this.#logger.info('Form submitted');
|
||||
}
|
||||
|
||||
private getFormErrors(): Record<string, unknown> {
|
||||
const errors: Record<string, unknown> = {};
|
||||
Object.keys(this.form.controls).forEach((key) => {
|
||||
const control = this.form.get(key);
|
||||
if (control?.errors) errors[key] = control.errors;
|
||||
});
|
||||
return errors;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 8. Async Progress Tracking
|
||||
|
||||
```typescript
|
||||
import { Injectable } from '@angular/core';
|
||||
import { logger } from '@isa/core/logging';
|
||||
import { Observable } from 'rxjs';
|
||||
import { tap } from 'rxjs/operators';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class ImportService {
|
||||
#logger = logger({ service: 'ImportService' });
|
||||
|
||||
importData(file: File): Observable<number> {
|
||||
const importId = crypto.randomUUID();
|
||||
|
||||
this.#logger.info('Import started', () => ({
|
||||
importId,
|
||||
fileName: file.name,
|
||||
fileSize: file.size
|
||||
}));
|
||||
|
||||
return this.processImport(file).pipe(
|
||||
tap((progress) => {
|
||||
if (progress % 25 === 0) {
|
||||
this.#logger.debug('Import progress', () => ({
|
||||
importId,
|
||||
progress: `${progress}%`
|
||||
}));
|
||||
}
|
||||
}),
|
||||
tap({
|
||||
complete: () => this.#logger.info('Import completed', { importId }),
|
||||
error: (error) => this.#logger.error('Import failed', error, { importId })
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
private processImport(file: File): Observable<number> {
|
||||
// Implementation
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 9. Global Error Handler
|
||||
|
||||
```typescript
|
||||
import { Injectable, ErrorHandler } from '@angular/core';
|
||||
import { logger } from '@isa/core/logging';
|
||||
|
||||
@Injectable()
|
||||
export class GlobalErrorHandler implements ErrorHandler {
|
||||
#logger = logger({ handler: 'GlobalErrorHandler' });
|
||||
|
||||
handleError(error: Error): void {
|
||||
this.#logger.error('Uncaught error', error, () => ({
|
||||
url: window.location.href,
|
||||
userAgent: navigator.userAgent,
|
||||
timestamp: new Date().toISOString()
|
||||
}));
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 10. WebSocket Component
|
||||
|
||||
```typescript
|
||||
import { Component, OnInit, OnDestroy } from '@angular/core';
|
||||
import { logger } from '@isa/core/logging';
|
||||
import { Subject, takeUntil } from 'rxjs';
|
||||
|
||||
@Component({
|
||||
selector: 'oms-live-orders',
|
||||
standalone: true,
|
||||
})
|
||||
export class LiveOrdersComponent implements OnInit, OnDestroy {
|
||||
#logger = logger({ component: 'LiveOrdersComponent' });
|
||||
private destroy$ = new Subject<void>();
|
||||
|
||||
constructor(private wsService: WebSocketService) {}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.#logger.info('Connecting to WebSocket');
|
||||
|
||||
this.wsService.connect('orders').pipe(
|
||||
takeUntil(this.destroy$)
|
||||
).subscribe({
|
||||
next: (msg) => this.#logger.debug('Message received', () => ({
|
||||
type: msg.type,
|
||||
orderId: msg.orderId
|
||||
})),
|
||||
error: (error) => this.#logger.error('WebSocket error', error),
|
||||
complete: () => this.#logger.info('WebSocket closed')
|
||||
});
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
this.#logger.debug('Component destroyed');
|
||||
this.destroy$.next();
|
||||
this.destroy$.complete();
|
||||
}
|
||||
}
|
||||
```
|
||||
# Logging Examples
|
||||
|
||||
Concise real-world examples of logging patterns.
|
||||
|
||||
## 1. Component with Observable
|
||||
|
||||
```typescript
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
import { logger } from '@isa/core/logging';
|
||||
|
||||
@Component({
|
||||
selector: 'app-product-list',
|
||||
standalone: true,
|
||||
})
|
||||
export class ProductListComponent implements OnInit {
|
||||
#logger = logger({ component: 'ProductListComponent' });
|
||||
|
||||
constructor(private productService: ProductService) {}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.#logger.info('Component initialized');
|
||||
this.loadProducts();
|
||||
}
|
||||
|
||||
private loadProducts(): void {
|
||||
this.productService.getProducts().subscribe({
|
||||
next: (products) => {
|
||||
this.#logger.info('Products loaded', () => ({ count: products.length }));
|
||||
},
|
||||
error: (error) => {
|
||||
this.#logger.error('Failed to load products', error);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 2. Service with HTTP
|
||||
|
||||
```typescript
|
||||
import { Injectable, inject } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { logger } from '@isa/core/logging';
|
||||
import { catchError, tap } from 'rxjs/operators';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class OrderService {
|
||||
private http = inject(HttpClient);
|
||||
#logger = logger({ service: 'OrderService' });
|
||||
|
||||
getOrder(id: string): Observable<Order> {
|
||||
this.#logger.debug('Fetching order', { id });
|
||||
|
||||
return this.http.get<Order>(`/api/orders/${id}`).pipe(
|
||||
tap((order) => this.#logger.info('Order fetched', () => ({
|
||||
id,
|
||||
status: order.status
|
||||
}))),
|
||||
catchError((error) => {
|
||||
this.#logger.error('Fetch failed', error, () => ({ id, status: error.status }));
|
||||
throw error;
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 3. Hierarchical Context
|
||||
|
||||
```typescript
|
||||
import { Component } from '@angular/core';
|
||||
import { logger, provideLoggerContext } from '@isa/core/logging';
|
||||
|
||||
@Component({
|
||||
selector: 'oms-return-process',
|
||||
standalone: true,
|
||||
providers: [
|
||||
provideLoggerContext({ feature: 'returns', module: 'oms' })
|
||||
],
|
||||
})
|
||||
export class ReturnProcessComponent {
|
||||
#logger = logger(() => ({
|
||||
processId: this.currentProcessId,
|
||||
step: this.currentStep
|
||||
}));
|
||||
|
||||
private currentProcessId = crypto.randomUUID();
|
||||
private currentStep = 1;
|
||||
|
||||
startProcess(orderId: string): void {
|
||||
// Logs include: feature, module, processId, step, orderId
|
||||
this.#logger.info('Process started', { orderId });
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 4. NgRx Effect
|
||||
|
||||
```typescript
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Actions, createEffect, ofType } from '@ngrx/effects';
|
||||
import { logger } from '@isa/core/logging';
|
||||
import { map, catchError, tap } from 'rxjs/operators';
|
||||
import { of } from 'rxjs';
|
||||
|
||||
@Injectable()
|
||||
export class OrdersEffects {
|
||||
#logger = logger({ effect: 'OrdersEffects' });
|
||||
|
||||
loadOrders$ = createEffect(() =>
|
||||
this.actions$.pipe(
|
||||
ofType(OrdersActions.loadOrders),
|
||||
tap((action) => this.#logger.debug('Loading orders', () => ({
|
||||
page: action.page
|
||||
}))),
|
||||
mergeMap((action) =>
|
||||
this.orderService.getOrders(action.filters).pipe(
|
||||
map((orders) => {
|
||||
this.#logger.info('Orders loaded', () => ({ count: orders.length }));
|
||||
return OrdersActions.loadOrdersSuccess({ orders });
|
||||
}),
|
||||
catchError((error) => {
|
||||
this.#logger.error('Load failed', error);
|
||||
return of(OrdersActions.loadOrdersFailure({ error }));
|
||||
})
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
constructor(
|
||||
private actions$: Actions,
|
||||
private orderService: OrderService
|
||||
) {}
|
||||
}
|
||||
```
|
||||
|
||||
## 5. Guard with Authorization
|
||||
|
||||
```typescript
|
||||
import { inject } from '@angular/core';
|
||||
import { CanActivateFn, Router } from '@angular/router';
|
||||
import { logger } from '@isa/core/logging';
|
||||
|
||||
export const authGuard: CanActivateFn = (route, state) => {
|
||||
const authService = inject(AuthService);
|
||||
const router = inject(Router);
|
||||
const log = logger({ guard: 'AuthGuard' });
|
||||
|
||||
if (authService.isAuthenticated()) {
|
||||
log.debug('Access granted', () => ({ route: state.url }));
|
||||
return true;
|
||||
}
|
||||
|
||||
log.warn('Access denied', () => ({
|
||||
attemptedRoute: state.url,
|
||||
redirectTo: '/login'
|
||||
}));
|
||||
return router.createUrlTree(['/login']);
|
||||
};
|
||||
```
|
||||
|
||||
## 6. HTTP Interceptor
|
||||
|
||||
```typescript
|
||||
import { HttpInterceptorFn } from '@angular/common/http';
|
||||
import { inject } from '@angular/core';
|
||||
import { tap, catchError } from 'rxjs/operators';
|
||||
import { LoggingService } from '@isa/core/logging';
|
||||
|
||||
export const loggingInterceptor: HttpInterceptorFn = (req, next) => {
|
||||
const loggingService = inject(LoggingService);
|
||||
const startTime = performance.now();
|
||||
|
||||
loggingService.debug('HTTP Request', () => ({
|
||||
method: req.method,
|
||||
url: req.url
|
||||
}));
|
||||
|
||||
return next(req).pipe(
|
||||
tap((event) => {
|
||||
if (event.type === HttpEventType.Response) {
|
||||
loggingService.info('HTTP Response', () => ({
|
||||
method: req.method,
|
||||
url: req.url,
|
||||
status: event.status,
|
||||
duration: `${(performance.now() - startTime).toFixed(2)}ms`
|
||||
}));
|
||||
}
|
||||
}),
|
||||
catchError((error) => {
|
||||
loggingService.error('HTTP Error', error, () => ({
|
||||
method: req.method,
|
||||
url: req.url,
|
||||
status: error.status
|
||||
}));
|
||||
return throwError(() => error);
|
||||
})
|
||||
);
|
||||
};
|
||||
```
|
||||
|
||||
## 7. Form Validation
|
||||
|
||||
```typescript
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
|
||||
import { logger } from '@isa/core/logging';
|
||||
|
||||
@Component({
|
||||
selector: 'shared-user-form',
|
||||
standalone: true,
|
||||
})
|
||||
export class UserFormComponent implements OnInit {
|
||||
#logger = logger({ component: 'UserFormComponent' });
|
||||
form!: FormGroup;
|
||||
|
||||
constructor(private fb: FormBuilder) {}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.form = this.fb.group({
|
||||
name: ['', Validators.required],
|
||||
email: ['', [Validators.required, Validators.email]]
|
||||
});
|
||||
}
|
||||
|
||||
onSubmit(): void {
|
||||
if (this.form.invalid) {
|
||||
this.#logger.warn('Invalid form submission', () => ({
|
||||
errors: this.getFormErrors()
|
||||
}));
|
||||
return;
|
||||
}
|
||||
|
||||
this.#logger.info('Form submitted');
|
||||
}
|
||||
|
||||
private getFormErrors(): Record<string, unknown> {
|
||||
const errors: Record<string, unknown> = {};
|
||||
Object.keys(this.form.controls).forEach((key) => {
|
||||
const control = this.form.get(key);
|
||||
if (control?.errors) errors[key] = control.errors;
|
||||
});
|
||||
return errors;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 8. Async Progress Tracking
|
||||
|
||||
```typescript
|
||||
import { Injectable } from '@angular/core';
|
||||
import { logger } from '@isa/core/logging';
|
||||
import { Observable } from 'rxjs';
|
||||
import { tap } from 'rxjs/operators';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class ImportService {
|
||||
#logger = logger({ service: 'ImportService' });
|
||||
|
||||
importData(file: File): Observable<number> {
|
||||
const importId = crypto.randomUUID();
|
||||
|
||||
this.#logger.info('Import started', () => ({
|
||||
importId,
|
||||
fileName: file.name,
|
||||
fileSize: file.size
|
||||
}));
|
||||
|
||||
return this.processImport(file).pipe(
|
||||
tap((progress) => {
|
||||
if (progress % 25 === 0) {
|
||||
this.#logger.debug('Import progress', () => ({
|
||||
importId,
|
||||
progress: `${progress}%`
|
||||
}));
|
||||
}
|
||||
}),
|
||||
tap({
|
||||
complete: () => this.#logger.info('Import completed', { importId }),
|
||||
error: (error) => this.#logger.error('Import failed', error, { importId })
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
private processImport(file: File): Observable<number> {
|
||||
// Implementation
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 9. Global Error Handler
|
||||
|
||||
```typescript
|
||||
import { Injectable, ErrorHandler } from '@angular/core';
|
||||
import { logger } from '@isa/core/logging';
|
||||
|
||||
@Injectable()
|
||||
export class GlobalErrorHandler implements ErrorHandler {
|
||||
#logger = logger({ handler: 'GlobalErrorHandler' });
|
||||
|
||||
handleError(error: Error): void {
|
||||
this.#logger.error('Uncaught error', error, () => ({
|
||||
url: window.location.href,
|
||||
userAgent: navigator.userAgent,
|
||||
timestamp: new Date().toISOString()
|
||||
}));
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 10. WebSocket Component
|
||||
|
||||
```typescript
|
||||
import { Component, OnInit, OnDestroy } from '@angular/core';
|
||||
import { logger } from '@isa/core/logging';
|
||||
import { Subject, takeUntil } from 'rxjs';
|
||||
|
||||
@Component({
|
||||
selector: 'oms-live-orders',
|
||||
standalone: true,
|
||||
})
|
||||
export class LiveOrdersComponent implements OnInit, OnDestroy {
|
||||
#logger = logger({ component: 'LiveOrdersComponent' });
|
||||
private destroy$ = new Subject<void>();
|
||||
|
||||
constructor(private wsService: WebSocketService) {}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.#logger.info('Connecting to WebSocket');
|
||||
|
||||
this.wsService.connect('orders').pipe(
|
||||
takeUntil(this.destroy$)
|
||||
).subscribe({
|
||||
next: (msg) => this.#logger.debug('Message received', () => ({
|
||||
type: msg.type,
|
||||
orderId: msg.orderId
|
||||
})),
|
||||
error: (error) => this.#logger.error('WebSocket error', error),
|
||||
complete: () => this.#logger.info('WebSocket closed')
|
||||
});
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
this.#logger.debug('Component destroyed');
|
||||
this.destroy$.next();
|
||||
this.destroy$.complete();
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -1,235 +1,235 @@
|
||||
# Logging Troubleshooting
|
||||
|
||||
## 1. Logs Not Appearing
|
||||
|
||||
**Problem:** Logger called but nothing in console.
|
||||
|
||||
**Solutions:**
|
||||
```typescript
|
||||
// Check log level
|
||||
provideLogging(
|
||||
withLogLevel(isDevMode() ? LogLevel.Debug : LogLevel.Warn)
|
||||
)
|
||||
|
||||
// Add sink
|
||||
provideLogging(
|
||||
withLogLevel(LogLevel.Debug),
|
||||
withSink(ConsoleLogSink) // Required!
|
||||
)
|
||||
|
||||
// Verify configuration in app.config.ts
|
||||
export const appConfig: ApplicationConfig = {
|
||||
providers: [
|
||||
provideLogging(...) // Must be present
|
||||
]
|
||||
};
|
||||
```
|
||||
|
||||
## 2. NullInjectorError
|
||||
|
||||
**Error:** `NullInjectorError: No provider for LoggingService!`
|
||||
|
||||
**Solution:**
|
||||
```typescript
|
||||
// app.config.ts
|
||||
import { provideLogging, withLogLevel, withSink,
|
||||
LogLevel, ConsoleLogSink } from '@isa/core/logging';
|
||||
|
||||
export const appConfig: ApplicationConfig = {
|
||||
providers: [
|
||||
provideLogging(
|
||||
withLogLevel(LogLevel.Debug),
|
||||
withSink(ConsoleLogSink)
|
||||
)
|
||||
]
|
||||
};
|
||||
```
|
||||
|
||||
## 3. Context Not Showing
|
||||
|
||||
**Problem:** Context passed but doesn't appear.
|
||||
|
||||
**Check:**
|
||||
```typescript
|
||||
// ✅ Both work:
|
||||
this.#logger.info('Message', () => ({ id: '123' })); // Function
|
||||
this.#logger.info('Message', { id: '123' }); // Object
|
||||
|
||||
// ❌ Common mistake:
|
||||
const ctx = { id: '123' };
|
||||
this.#logger.info('Message', ctx); // Actually works!
|
||||
|
||||
// Verify hierarchical merge:
|
||||
// Global → Component → Instance → Message
|
||||
```
|
||||
|
||||
## 4. Performance Issues
|
||||
|
||||
**Problem:** Slow when debug logging enabled.
|
||||
|
||||
**Solutions:**
|
||||
```typescript
|
||||
// ✅ Use lazy evaluation
|
||||
this.#logger.debug('Data', () => ({
|
||||
expensive: this.compute() // Only if debug enabled
|
||||
}));
|
||||
|
||||
// ✅ Reduce log frequency
|
||||
this.#logger.debug('Batch', () => ({
|
||||
count: items.length // Not each item
|
||||
}));
|
||||
|
||||
// ✅ Increase production level
|
||||
provideLogging(
|
||||
withLogLevel(isDevMode() ? LogLevel.Debug : LogLevel.Warn)
|
||||
)
|
||||
```
|
||||
|
||||
## 5. Error Object Not Logged
|
||||
|
||||
**Problem:** Error shows as `[object Object]`.
|
||||
|
||||
**Solution:**
|
||||
```typescript
|
||||
// ❌ Wrong
|
||||
this.#logger.error('Failed', { error }); // Don't wrap in object
|
||||
|
||||
// ✅ Correct
|
||||
this.#logger.error('Failed', error as Error, () => ({
|
||||
additionalContext: 'value'
|
||||
}));
|
||||
```
|
||||
|
||||
## 6. TypeScript Errors
|
||||
|
||||
**Error:** `Type 'X' is not assignable to 'MaybeLoggerContextFn'`
|
||||
|
||||
**Solution:**
|
||||
```typescript
|
||||
// ❌ Wrong type
|
||||
this.#logger.info('Message', 'string'); // Invalid
|
||||
|
||||
// ✅ Correct types
|
||||
this.#logger.info('Message', { key: 'value' });
|
||||
this.#logger.info('Message', () => ({ key: 'value' }));
|
||||
```
|
||||
|
||||
## 7. Logs in Tests
|
||||
|
||||
**Problem:** Test output cluttered with logs.
|
||||
|
||||
**Solutions:**
|
||||
```typescript
|
||||
// Mock logging service
|
||||
import { createComponentFactory, Spectator } from '@ngneat/spectator/jest';
|
||||
import { LoggingService } from '@isa/core/logging';
|
||||
|
||||
const createComponent = createComponentFactory({
|
||||
component: MyComponent,
|
||||
mocks: [LoggingService] // Mocks all log methods
|
||||
});
|
||||
|
||||
// Or disable in tests
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
provideLogging(withLogLevel(LogLevel.Off))
|
||||
]
|
||||
});
|
||||
```
|
||||
|
||||
## 8. Undefined Property Error
|
||||
|
||||
**Error:** `Cannot read property 'X' of undefined`
|
||||
|
||||
**Problem:** Accessing uninitialized property in logger context.
|
||||
|
||||
**Solutions:**
|
||||
```typescript
|
||||
// ❌ Problem
|
||||
#logger = logger(() => ({
|
||||
userId: this.userService.currentUserId // May be undefined
|
||||
}));
|
||||
|
||||
// ✅ Solution 1: Optional chaining
|
||||
#logger = logger(() => ({
|
||||
userId: this.userService?.currentUserId ?? 'unknown'
|
||||
}));
|
||||
|
||||
// ✅ Solution 2: Delay access
|
||||
ngOnInit() {
|
||||
this.#logger.info('Init', () => ({
|
||||
userId: this.userService.currentUserId // Safe here
|
||||
}));
|
||||
}
|
||||
```
|
||||
|
||||
## 9. Circular Dependency
|
||||
|
||||
**Error:** `NG0200: Circular dependency in DI detected`
|
||||
|
||||
**Cause:** Service A ← → Service B both inject LoggingService.
|
||||
|
||||
**Solution:**
|
||||
```typescript
|
||||
// ❌ Creates circular dependency
|
||||
constructor(private loggingService: LoggingService) {}
|
||||
|
||||
// ✅ Use factory (no circular dependency)
|
||||
#logger = logger({ service: 'MyService' });
|
||||
```
|
||||
|
||||
## 10. Custom Sink Not Working
|
||||
|
||||
**Problem:** Sink registered but never called.
|
||||
|
||||
**Solutions:**
|
||||
```typescript
|
||||
// ✅ Correct registration
|
||||
provideLogging(
|
||||
withSink(MySink) // Add to config
|
||||
)
|
||||
|
||||
// ✅ Correct signature
|
||||
export class MySink implements Sink {
|
||||
log(
|
||||
level: LogLevel,
|
||||
message: string,
|
||||
context?: LoggerContext,
|
||||
error?: Error
|
||||
): void {
|
||||
// Implementation
|
||||
}
|
||||
}
|
||||
|
||||
// ✅ Sink function must return function
|
||||
export const mySinkFn: SinkFn = () => {
|
||||
const http = inject(HttpClient);
|
||||
return (level, message, context, error) => {
|
||||
// Implementation
|
||||
};
|
||||
};
|
||||
```
|
||||
|
||||
## Quick Diagnostics
|
||||
|
||||
```typescript
|
||||
// Enable all logs temporarily
|
||||
provideLogging(withLogLevel(LogLevel.Trace))
|
||||
|
||||
// Check imports
|
||||
import { logger } from '@isa/core/logging'; // ✅ Correct
|
||||
import { logger } from '@isa/core/logging/src/lib/logger.factory'; // ❌ Wrong
|
||||
|
||||
// Verify console filters in browser DevTools
|
||||
// Ensure Info, Debug, Warnings are enabled
|
||||
```
|
||||
|
||||
## Common Error Messages
|
||||
|
||||
| Error | Cause | Fix |
|
||||
|-------|-------|-----|
|
||||
| `NullInjectorError: LoggingService` | Missing config | Add `provideLogging()` |
|
||||
| `Type 'X' not assignable` | Wrong context type | Use object or function |
|
||||
| `Cannot read property 'X'` | Undefined property | Use optional chaining |
|
||||
| `Circular dependency` | Service injection | Use `logger()` factory |
|
||||
| Stack overflow | Infinite loop in context | Don't call logger in context |
|
||||
# Logging Troubleshooting
|
||||
|
||||
## 1. Logs Not Appearing
|
||||
|
||||
**Problem:** Logger called but nothing in console.
|
||||
|
||||
**Solutions:**
|
||||
```typescript
|
||||
// Check log level
|
||||
provideLogging(
|
||||
withLogLevel(isDevMode() ? LogLevel.Debug : LogLevel.Warn)
|
||||
)
|
||||
|
||||
// Add sink
|
||||
provideLogging(
|
||||
withLogLevel(LogLevel.Debug),
|
||||
withSink(ConsoleLogSink) // Required!
|
||||
)
|
||||
|
||||
// Verify configuration in app.config.ts
|
||||
export const appConfig: ApplicationConfig = {
|
||||
providers: [
|
||||
provideLogging(...) // Must be present
|
||||
]
|
||||
};
|
||||
```
|
||||
|
||||
## 2. NullInjectorError
|
||||
|
||||
**Error:** `NullInjectorError: No provider for LoggingService!`
|
||||
|
||||
**Solution:**
|
||||
```typescript
|
||||
// app.config.ts
|
||||
import { provideLogging, withLogLevel, withSink,
|
||||
LogLevel, ConsoleLogSink } from '@isa/core/logging';
|
||||
|
||||
export const appConfig: ApplicationConfig = {
|
||||
providers: [
|
||||
provideLogging(
|
||||
withLogLevel(LogLevel.Debug),
|
||||
withSink(ConsoleLogSink)
|
||||
)
|
||||
]
|
||||
};
|
||||
```
|
||||
|
||||
## 3. Context Not Showing
|
||||
|
||||
**Problem:** Context passed but doesn't appear.
|
||||
|
||||
**Check:**
|
||||
```typescript
|
||||
// ✅ Both work:
|
||||
this.#logger.info('Message', () => ({ id: '123' })); // Function
|
||||
this.#logger.info('Message', { id: '123' }); // Object
|
||||
|
||||
// ❌ Common mistake:
|
||||
const ctx = { id: '123' };
|
||||
this.#logger.info('Message', ctx); // Actually works!
|
||||
|
||||
// Verify hierarchical merge:
|
||||
// Global → Component → Instance → Message
|
||||
```
|
||||
|
||||
## 4. Performance Issues
|
||||
|
||||
**Problem:** Slow when debug logging enabled.
|
||||
|
||||
**Solutions:**
|
||||
```typescript
|
||||
// ✅ Use lazy evaluation
|
||||
this.#logger.debug('Data', () => ({
|
||||
expensive: this.compute() // Only if debug enabled
|
||||
}));
|
||||
|
||||
// ✅ Reduce log frequency
|
||||
this.#logger.debug('Batch', () => ({
|
||||
count: items.length // Not each item
|
||||
}));
|
||||
|
||||
// ✅ Increase production level
|
||||
provideLogging(
|
||||
withLogLevel(isDevMode() ? LogLevel.Debug : LogLevel.Warn)
|
||||
)
|
||||
```
|
||||
|
||||
## 5. Error Object Not Logged
|
||||
|
||||
**Problem:** Error shows as `[object Object]`.
|
||||
|
||||
**Solution:**
|
||||
```typescript
|
||||
// ❌ Wrong
|
||||
this.#logger.error('Failed', { error }); // Don't wrap in object
|
||||
|
||||
// ✅ Correct
|
||||
this.#logger.error('Failed', error as Error, () => ({
|
||||
additionalContext: 'value'
|
||||
}));
|
||||
```
|
||||
|
||||
## 6. TypeScript Errors
|
||||
|
||||
**Error:** `Type 'X' is not assignable to 'MaybeLoggerContextFn'`
|
||||
|
||||
**Solution:**
|
||||
```typescript
|
||||
// ❌ Wrong type
|
||||
this.#logger.info('Message', 'string'); // Invalid
|
||||
|
||||
// ✅ Correct types
|
||||
this.#logger.info('Message', { key: 'value' });
|
||||
this.#logger.info('Message', () => ({ key: 'value' }));
|
||||
```
|
||||
|
||||
## 7. Logs in Tests
|
||||
|
||||
**Problem:** Test output cluttered with logs.
|
||||
|
||||
**Solutions:**
|
||||
```typescript
|
||||
// Mock logging service
|
||||
import { createComponentFactory, Spectator } from '@ngneat/spectator/jest';
|
||||
import { LoggingService } from '@isa/core/logging';
|
||||
|
||||
const createComponent = createComponentFactory({
|
||||
component: MyComponent,
|
||||
mocks: [LoggingService] // Mocks all log methods
|
||||
});
|
||||
|
||||
// Or disable in tests
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
provideLogging(withLogLevel(LogLevel.Off))
|
||||
]
|
||||
});
|
||||
```
|
||||
|
||||
## 8. Undefined Property Error
|
||||
|
||||
**Error:** `Cannot read property 'X' of undefined`
|
||||
|
||||
**Problem:** Accessing uninitialized property in logger context.
|
||||
|
||||
**Solutions:**
|
||||
```typescript
|
||||
// ❌ Problem
|
||||
#logger = logger(() => ({
|
||||
userId: this.userService.currentUserId // May be undefined
|
||||
}));
|
||||
|
||||
// ✅ Solution 1: Optional chaining
|
||||
#logger = logger(() => ({
|
||||
userId: this.userService?.currentUserId ?? 'unknown'
|
||||
}));
|
||||
|
||||
// ✅ Solution 2: Delay access
|
||||
ngOnInit() {
|
||||
this.#logger.info('Init', () => ({
|
||||
userId: this.userService.currentUserId // Safe here
|
||||
}));
|
||||
}
|
||||
```
|
||||
|
||||
## 9. Circular Dependency
|
||||
|
||||
**Error:** `NG0200: Circular dependency in DI detected`
|
||||
|
||||
**Cause:** Service A ← → Service B both inject LoggingService.
|
||||
|
||||
**Solution:**
|
||||
```typescript
|
||||
// ❌ Creates circular dependency
|
||||
constructor(private loggingService: LoggingService) {}
|
||||
|
||||
// ✅ Use factory (no circular dependency)
|
||||
#logger = logger({ service: 'MyService' });
|
||||
```
|
||||
|
||||
## 10. Custom Sink Not Working
|
||||
|
||||
**Problem:** Sink registered but never called.
|
||||
|
||||
**Solutions:**
|
||||
```typescript
|
||||
// ✅ Correct registration
|
||||
provideLogging(
|
||||
withSink(MySink) // Add to config
|
||||
)
|
||||
|
||||
// ✅ Correct signature
|
||||
export class MySink implements Sink {
|
||||
log(
|
||||
level: LogLevel,
|
||||
message: string,
|
||||
context?: LoggerContext,
|
||||
error?: Error
|
||||
): void {
|
||||
// Implementation
|
||||
}
|
||||
}
|
||||
|
||||
// ✅ Sink function must return function
|
||||
export const mySinkFn: SinkFn = () => {
|
||||
const http = inject(HttpClient);
|
||||
return (level, message, context, error) => {
|
||||
// Implementation
|
||||
};
|
||||
};
|
||||
```
|
||||
|
||||
## Quick Diagnostics
|
||||
|
||||
```typescript
|
||||
// Enable all logs temporarily
|
||||
provideLogging(withLogLevel(LogLevel.Trace))
|
||||
|
||||
// Check imports
|
||||
import { logger } from '@isa/core/logging'; // ✅ Correct
|
||||
import { logger } from '@isa/core/logging/src/lib/logger.factory'; // ❌ Wrong
|
||||
|
||||
// Verify console filters in browser DevTools
|
||||
// Ensure Info, Debug, Warnings are enabled
|
||||
```
|
||||
|
||||
## Common Error Messages
|
||||
|
||||
| Error | Cause | Fix |
|
||||
|-------|-------|-----|
|
||||
| `NullInjectorError: LoggingService` | Missing config | Add `provideLogging()` |
|
||||
| `Type 'X' not assignable` | Wrong context type | Use object or function |
|
||||
| `Cannot read property 'X'` | Undefined property | Use optional chaining |
|
||||
| `Circular dependency` | Service injection | Use `logger()` factory |
|
||||
| Stack overflow | Infinite loop in context | Don't call logger in context |
|
||||
@@ -1,287 +0,0 @@
|
||||
---
|
||||
name: ngrx-resource-api
|
||||
description: This skill should be used when implementing Angular's Resource API with NgRx Signal Store for reactive data management. Use when creating signal stores that load data reactively, need automatic race condition prevention, or require declarative resource management without RxJS. Applies to data-access libraries, feature stores with API integration, and components needing reactive filtering or pagination.
|
||||
---
|
||||
|
||||
# NgRx Resource API
|
||||
|
||||
## Overview
|
||||
|
||||
This skill enables integration of Angular's Resource API with NgRx Signal Store to create reactive data flows without RxJS while automatically preventing race conditions. The Resource API handles concurrent request management declaratively, eliminating manual `switchMap` or `takeUntilDestroyed` patterns.
|
||||
|
||||
## Core Architectural Concepts
|
||||
|
||||
### Reactive Flow Graph
|
||||
|
||||
Establish three clear interaction points in the store:
|
||||
|
||||
1. **Filter signals trigger resource loading** - Parameter changes automatically reload resources
|
||||
2. **Methods explicitly invoke operations** - Use `signalMethod` for user-triggered actions
|
||||
3. **Computed signals derive view models** - Transform loaded data for component consumption
|
||||
|
||||
### The withProps Pattern for Dependency Injection
|
||||
|
||||
Inject services via `withProps` with underscore-prefixed properties to mark them as internal implementation details:
|
||||
|
||||
```typescript
|
||||
withProps(() => ({
|
||||
_dataService: inject(DataService),
|
||||
_notificationService: inject(ToastService),
|
||||
}))
|
||||
```
|
||||
|
||||
**Benefits:**
|
||||
- Centralizes dependency injection in one location
|
||||
- Clear distinction between internal (prefixed) and public properties
|
||||
- Services available to all subsequent feature sections
|
||||
|
||||
## Implementation Steps
|
||||
|
||||
### Step 1: Inject Services with withProps
|
||||
|
||||
Create the initial `withProps` section to inject required services:
|
||||
|
||||
```typescript
|
||||
export const MyStore = signalStore(
|
||||
withProps(() => ({
|
||||
_dataService: inject(DataService),
|
||||
_toastService: inject(ToastService),
|
||||
})),
|
||||
// ... additional features
|
||||
);
|
||||
```
|
||||
|
||||
**Naming convention:** Prefix all injected services with underscore (`_`) to indicate internal use.
|
||||
|
||||
### Step 2: Define Filter State
|
||||
|
||||
Add state properties that will serve as resource parameters:
|
||||
|
||||
```typescript
|
||||
withState({
|
||||
filter: {
|
||||
searchTerm: '',
|
||||
category: '',
|
||||
} as MyFilter,
|
||||
})
|
||||
```
|
||||
|
||||
### Step 3: Configure Resources
|
||||
|
||||
In a subsequent `withProps` section, create resources that reference the injected services and state:
|
||||
|
||||
```typescript
|
||||
withProps((store) => ({
|
||||
_itemsResource: resource({
|
||||
params: store.filter,
|
||||
loader: (loaderParams) => {
|
||||
const filter = loaderParams.params;
|
||||
const abortSignal = loaderParams.abortSignal;
|
||||
return store._dataService.loadItems(filter, abortSignal);
|
||||
}
|
||||
})
|
||||
}))
|
||||
```
|
||||
|
||||
**Key points:**
|
||||
- Resources automatically reload when `params` signal changes
|
||||
- `abortSignal` enables automatic cancellation of in-flight requests
|
||||
- Loader must return a Promise (use `.findPromise()` if service returns Observable)
|
||||
|
||||
### Step 4: Expose Read-Only Resources (Optional)
|
||||
|
||||
If the resource should be accessible to consumers, expose it as read-only:
|
||||
|
||||
```typescript
|
||||
withProps((store) => ({
|
||||
itemsResource: store._itemsResource.asReadonly(),
|
||||
}))
|
||||
```
|
||||
|
||||
**Pattern:** Internal resources use underscore prefix, public versions are read-only without prefix.
|
||||
|
||||
### Step 5: Create Signal Methods for Updates
|
||||
|
||||
Use `signalMethod` for actions that update state and trigger resource reloads:
|
||||
|
||||
```typescript
|
||||
withMethods((store) => ({
|
||||
updateFilter: signalMethod<MyFilter>((filter) => {
|
||||
patchState(store, { filter });
|
||||
}),
|
||||
|
||||
refresh: () => {
|
||||
store._itemsResource.reload();
|
||||
}
|
||||
}))
|
||||
```
|
||||
|
||||
**Important:** `signalMethod` implementations are **untracked by convention** - they don't re-execute when signals change. This provides explicit control flow.
|
||||
|
||||
### Step 6: Add Error Handling
|
||||
|
||||
Use `withHooks` to react to resource errors:
|
||||
|
||||
```typescript
|
||||
withHooks({
|
||||
onInit: (store) => {
|
||||
effect(() => {
|
||||
const error = store._itemsResource.error();
|
||||
if (error) {
|
||||
store._toastService.show('Error: ' + getMessage(error));
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
**Pattern:** Error effects should be read-only - they observe errors and trigger side effects, but don't modify state.
|
||||
|
||||
## Common Patterns
|
||||
|
||||
### Template Integration with linkedSignal
|
||||
|
||||
For two-way form binding that synchronizes with the store:
|
||||
|
||||
```typescript
|
||||
export class MyComponent {
|
||||
#store = inject(MyStore);
|
||||
|
||||
// Create linked signal for form field
|
||||
searchTerm = linkedSignal(() => this.#store.filter().searchTerm);
|
||||
|
||||
// Combine form fields into filter object
|
||||
#linkedFilter = computed(() => ({
|
||||
searchTerm: this.searchTerm(),
|
||||
// ... other fields
|
||||
}));
|
||||
|
||||
constructor() {
|
||||
// Sync form changes back to store
|
||||
this.#store.updateFilter(this.#linkedFilter);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Benefits:**
|
||||
- Two-way binding for forms
|
||||
- Automatic store synchronization
|
||||
- Type-safe filter construction
|
||||
|
||||
### Working with Resource Data
|
||||
|
||||
Access resource data through resource signals:
|
||||
|
||||
```typescript
|
||||
withComputed((store) => ({
|
||||
items: computed(() => store._itemsResource.value() ?? []),
|
||||
isLoading: computed(() => store._itemsResource.isLoading()),
|
||||
hasError: computed(() => store._itemsResource.hasError()),
|
||||
}))
|
||||
```
|
||||
|
||||
**Available signals:**
|
||||
- `value()` - The loaded data (undefined while loading)
|
||||
- `isLoading()` - Loading state boolean
|
||||
- `hasError()` - Error state boolean
|
||||
- `error()` - Error object if present
|
||||
- `status()` - Overall status: 'idle' | 'loading' | 'resolved' | 'error'
|
||||
|
||||
### Updating Resource Data Locally
|
||||
|
||||
For temporary working copies before server writes:
|
||||
|
||||
```typescript
|
||||
withMethods((store) => ({
|
||||
updateLocalItem: (id: string, changes: Partial<Item>) => {
|
||||
store._itemsResource.update((currentItems) => {
|
||||
return currentItems.map(item =>
|
||||
item.id === id ? { ...item, ...changes } : item
|
||||
);
|
||||
});
|
||||
}
|
||||
}))
|
||||
```
|
||||
|
||||
**Note:** This pattern feels unconventional but aligns with maintaining temporary working copies before server persistence.
|
||||
|
||||
### Multiple Resources in One Store
|
||||
|
||||
Combine multiple resources for complex data requirements:
|
||||
|
||||
```typescript
|
||||
withProps((store) => ({
|
||||
_itemsResource: resource({
|
||||
params: store.filter,
|
||||
loader: (params) => store._dataService.loadItems(params.params, params.abortSignal)
|
||||
}),
|
||||
|
||||
_detailsResource: resource({
|
||||
params: store.selectedId,
|
||||
loader: (params) => {
|
||||
if (!params.params) return Promise.resolve(null);
|
||||
return store._dataService.loadDetails(params.params, params.abortSignal);
|
||||
}
|
||||
})
|
||||
}))
|
||||
```
|
||||
|
||||
**Pattern:** Each resource has independent params and loading state, but can share service instances.
|
||||
|
||||
## Important Considerations
|
||||
|
||||
### Race Condition Prevention
|
||||
|
||||
The Resource API automatically handles race conditions:
|
||||
- New requests automatically cancel pending requests
|
||||
- No need for `switchMap`, `takeUntilDestroyed`, or manual abort handling
|
||||
- Declarative parameter changes trigger clean cancellation
|
||||
|
||||
### Untracked Signal Methods
|
||||
|
||||
`signalMethod` implementations deliberately skip reactive tracking:
|
||||
- Provides explicit, predictable control flow
|
||||
- Prevents unexpected re-executions from signal changes
|
||||
- Makes side effects obvious at call sites
|
||||
|
||||
### Loader Function Requirements
|
||||
|
||||
Loaders must:
|
||||
- Return a `Promise` (not Observable)
|
||||
- Accept and pass through the `abortSignal` to enable cancellation
|
||||
- Handle the signal in the underlying HTTP call
|
||||
|
||||
**Converting Observables:**
|
||||
```typescript
|
||||
loader: (params) => {
|
||||
return firstValueFrom(
|
||||
this._service.load$(params.params)
|
||||
.pipe(takeUntilDestroyed(this._destroyRef))
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
### Resource Lifecycle
|
||||
|
||||
Resources maintain their own state machine:
|
||||
1. **Idle** - Initial state before first load
|
||||
2. **Loading** - Request in progress
|
||||
3. **Resolved** - Data loaded successfully
|
||||
4. **Error** - Request failed
|
||||
|
||||
State transitions automatically trigger reactive updates to dependent computeds and effects.
|
||||
|
||||
## When to Use This Pattern
|
||||
|
||||
**Use Resource API with Signal Store when:**
|
||||
- Loading data based on reactive filter/search parameters
|
||||
- Need automatic race condition handling for concurrent requests
|
||||
- Want declarative data loading without RxJS subscriptions
|
||||
- Building stores with frequently changing query parameters
|
||||
- Implementing pagination, filtering, or search features
|
||||
|
||||
**Consider alternatives when:**
|
||||
- Simple one-time data loads (use `rxMethod` or direct service calls)
|
||||
- Complex Observable chains with multiple operators needed
|
||||
- Need fine-grained control over request timing/caching
|
||||
- Working with WebSocket or SSE streams (use `rxMethod` instead)
|
||||
@@ -1,209 +1,356 @@
|
||||
---
|
||||
name: skill-creator
|
||||
description: Guide for creating effective skills. This skill should be used when users want to create a new skill (or update an existing skill) that extends Claude's capabilities with specialized knowledge, workflows, or tool integrations.
|
||||
license: Complete terms in LICENSE.txt
|
||||
---
|
||||
|
||||
# Skill Creator
|
||||
|
||||
This skill provides guidance for creating effective skills.
|
||||
|
||||
## About Skills
|
||||
|
||||
Skills are modular, self-contained packages that extend Claude's capabilities by providing
|
||||
specialized knowledge, workflows, and tools. Think of them as "onboarding guides" for specific
|
||||
domains or tasks—they transform Claude from a general-purpose agent into a specialized agent
|
||||
equipped with procedural knowledge that no model can fully possess.
|
||||
|
||||
### What Skills Provide
|
||||
|
||||
1. Specialized workflows - Multi-step procedures for specific domains
|
||||
2. Tool integrations - Instructions for working with specific file formats or APIs
|
||||
3. Domain expertise - Company-specific knowledge, schemas, business logic
|
||||
4. Bundled resources - Scripts, references, and assets for complex and repetitive tasks
|
||||
|
||||
### Anatomy of a Skill
|
||||
|
||||
Every skill consists of a required SKILL.md file and optional bundled resources:
|
||||
|
||||
```
|
||||
skill-name/
|
||||
├── SKILL.md (required)
|
||||
│ ├── YAML frontmatter metadata (required)
|
||||
│ │ ├── name: (required)
|
||||
│ │ └── description: (required)
|
||||
│ └── Markdown instructions (required)
|
||||
└── Bundled Resources (optional)
|
||||
├── scripts/ - Executable code (Python/Bash/etc.)
|
||||
├── references/ - Documentation intended to be loaded into context as needed
|
||||
└── assets/ - Files used in output (templates, icons, fonts, etc.)
|
||||
```
|
||||
|
||||
#### SKILL.md (required)
|
||||
|
||||
**Metadata Quality:** The `name` and `description` in YAML frontmatter determine when Claude will use the skill. Be specific about what the skill does and when to use it. Use the third-person (e.g. "This skill should be used when..." instead of "Use this skill when...").
|
||||
|
||||
#### Bundled Resources (optional)
|
||||
|
||||
##### Scripts (`scripts/`)
|
||||
|
||||
Executable code (Python/Bash/etc.) for tasks that require deterministic reliability or are repeatedly rewritten.
|
||||
|
||||
- **When to include**: When the same code is being rewritten repeatedly or deterministic reliability is needed
|
||||
- **Example**: `scripts/rotate_pdf.py` for PDF rotation tasks
|
||||
- **Benefits**: Token efficient, deterministic, may be executed without loading into context
|
||||
- **Note**: Scripts may still need to be read by Claude for patching or environment-specific adjustments
|
||||
|
||||
##### References (`references/`)
|
||||
|
||||
Documentation and reference material intended to be loaded as needed into context to inform Claude's process and thinking.
|
||||
|
||||
- **When to include**: For documentation that Claude should reference while working
|
||||
- **Examples**: `references/finance.md` for financial schemas, `references/mnda.md` for company NDA template, `references/policies.md` for company policies, `references/api_docs.md` for API specifications
|
||||
- **Use cases**: Database schemas, API documentation, domain knowledge, company policies, detailed workflow guides
|
||||
- **Benefits**: Keeps SKILL.md lean, loaded only when Claude determines it's needed
|
||||
- **Best practice**: If files are large (>10k words), include grep search patterns in SKILL.md
|
||||
- **Avoid duplication**: Information should live in either SKILL.md or references files, not both. Prefer references files for detailed information unless it's truly core to the skill—this keeps SKILL.md lean while making information discoverable without hogging the context window. Keep only essential procedural instructions and workflow guidance in SKILL.md; move detailed reference material, schemas, and examples to references files.
|
||||
|
||||
##### Assets (`assets/`)
|
||||
|
||||
Files not intended to be loaded into context, but rather used within the output Claude produces.
|
||||
|
||||
- **When to include**: When the skill needs files that will be used in the final output
|
||||
- **Examples**: `assets/logo.png` for brand assets, `assets/slides.pptx` for PowerPoint templates, `assets/frontend-template/` for HTML/React boilerplate, `assets/font.ttf` for typography
|
||||
- **Use cases**: Templates, images, icons, boilerplate code, fonts, sample documents that get copied or modified
|
||||
- **Benefits**: Separates output resources from documentation, enables Claude to use files without loading them into context
|
||||
|
||||
### Progressive Disclosure Design Principle
|
||||
|
||||
Skills use a three-level loading system to manage context efficiently:
|
||||
|
||||
1. **Metadata (name + description)** - Always in context (~100 words)
|
||||
2. **SKILL.md body** - When skill triggers (<5k words)
|
||||
3. **Bundled resources** - As needed by Claude (Unlimited*)
|
||||
|
||||
*Unlimited because scripts can be executed without reading into context window.
|
||||
|
||||
## Skill Creation Process
|
||||
|
||||
To create a skill, follow the "Skill Creation Process" in order, skipping steps only if there is a clear reason why they are not applicable.
|
||||
|
||||
### Step 1: Understanding the Skill with Concrete Examples
|
||||
|
||||
Skip this step only when the skill's usage patterns are already clearly understood. It remains valuable even when working with an existing skill.
|
||||
|
||||
To create an effective skill, clearly understand concrete examples of how the skill will be used. This understanding can come from either direct user examples or generated examples that are validated with user feedback.
|
||||
|
||||
For example, when building an image-editor skill, relevant questions include:
|
||||
|
||||
- "What functionality should the image-editor skill support? Editing, rotating, anything else?"
|
||||
- "Can you give some examples of how this skill would be used?"
|
||||
- "I can imagine users asking for things like 'Remove the red-eye from this image' or 'Rotate this image'. Are there other ways you imagine this skill being used?"
|
||||
- "What would a user say that should trigger this skill?"
|
||||
|
||||
To avoid overwhelming users, avoid asking too many questions in a single message. Start with the most important questions and follow up as needed for better effectiveness.
|
||||
|
||||
Conclude this step when there is a clear sense of the functionality the skill should support.
|
||||
|
||||
### Step 2: Planning the Reusable Skill Contents
|
||||
|
||||
To turn concrete examples into an effective skill, analyze each example by:
|
||||
|
||||
1. Considering how to execute on the example from scratch
|
||||
2. Identifying what scripts, references, and assets would be helpful when executing these workflows repeatedly
|
||||
|
||||
Example: When building a `pdf-editor` skill to handle queries like "Help me rotate this PDF," the analysis shows:
|
||||
|
||||
1. Rotating a PDF requires re-writing the same code each time
|
||||
2. A `scripts/rotate_pdf.py` script would be helpful to store in the skill
|
||||
|
||||
Example: When designing a `frontend-webapp-builder` skill for queries like "Build me a todo app" or "Build me a dashboard to track my steps," the analysis shows:
|
||||
|
||||
1. Writing a frontend webapp requires the same boilerplate HTML/React each time
|
||||
2. An `assets/hello-world/` template containing the boilerplate HTML/React project files would be helpful to store in the skill
|
||||
|
||||
Example: When building a `big-query` skill to handle queries like "How many users have logged in today?" the analysis shows:
|
||||
|
||||
1. Querying BigQuery requires re-discovering the table schemas and relationships each time
|
||||
2. A `references/schema.md` file documenting the table schemas would be helpful to store in the skill
|
||||
|
||||
To establish the skill's contents, analyze each concrete example to create a list of the reusable resources to include: scripts, references, and assets.
|
||||
|
||||
### Step 3: Initializing the Skill
|
||||
|
||||
At this point, it is time to actually create the skill.
|
||||
|
||||
Skip this step only if the skill being developed already exists, and iteration or packaging is needed. In this case, continue to the next step.
|
||||
|
||||
When creating a new skill from scratch, always run the `init_skill.py` script. The script conveniently generates a new template skill directory that automatically includes everything a skill requires, making the skill creation process much more efficient and reliable.
|
||||
|
||||
Usage:
|
||||
|
||||
```bash
|
||||
scripts/init_skill.py <skill-name> --path <output-directory>
|
||||
```
|
||||
|
||||
The script:
|
||||
|
||||
- Creates the skill directory at the specified path
|
||||
- Generates a SKILL.md template with proper frontmatter and TODO placeholders
|
||||
- Creates example resource directories: `scripts/`, `references/`, and `assets/`
|
||||
- Adds example files in each directory that can be customized or deleted
|
||||
|
||||
After initialization, customize or remove the generated SKILL.md and example files as needed.
|
||||
|
||||
### Step 4: Edit the Skill
|
||||
|
||||
When editing the (newly-generated or existing) skill, remember that the skill is being created for another instance of Claude to use. Focus on including information that would be beneficial and non-obvious to Claude. Consider what procedural knowledge, domain-specific details, or reusable assets would help another Claude instance execute these tasks more effectively.
|
||||
|
||||
#### Start with Reusable Skill Contents
|
||||
|
||||
To begin implementation, start with the reusable resources identified above: `scripts/`, `references/`, and `assets/` files. Note that this step may require user input. For example, when implementing a `brand-guidelines` skill, the user may need to provide brand assets or templates to store in `assets/`, or documentation to store in `references/`.
|
||||
|
||||
Also, delete any example files and directories not needed for the skill. The initialization script creates example files in `scripts/`, `references/`, and `assets/` to demonstrate structure, but most skills won't need all of them.
|
||||
|
||||
#### Update SKILL.md
|
||||
|
||||
**Writing Style:** Write the entire skill using **imperative/infinitive form** (verb-first instructions), not second person. Use objective, instructional language (e.g., "To accomplish X, do Y" rather than "You should do X" or "If you need to do X"). This maintains consistency and clarity for AI consumption.
|
||||
|
||||
To complete SKILL.md, answer the following questions:
|
||||
|
||||
1. What is the purpose of the skill, in a few sentences?
|
||||
2. When should the skill be used?
|
||||
3. In practice, how should Claude use the skill? All reusable skill contents developed above should be referenced so that Claude knows how to use them.
|
||||
|
||||
### Step 5: Packaging a Skill
|
||||
|
||||
Once the skill is ready, it should be packaged into a distributable zip file that gets shared with the user. The packaging process automatically validates the skill first to ensure it meets all requirements:
|
||||
|
||||
```bash
|
||||
scripts/package_skill.py <path/to/skill-folder>
|
||||
```
|
||||
|
||||
Optional output directory specification:
|
||||
|
||||
```bash
|
||||
scripts/package_skill.py <path/to/skill-folder> ./dist
|
||||
```
|
||||
|
||||
The packaging script will:
|
||||
|
||||
1. **Validate** the skill automatically, checking:
|
||||
- YAML frontmatter format and required fields
|
||||
- Skill naming conventions and directory structure
|
||||
- Description completeness and quality
|
||||
- File organization and resource references
|
||||
|
||||
2. **Package** the skill if validation passes, creating a zip file named after the skill (e.g., `my-skill.zip`) that includes all files and maintains the proper directory structure for distribution.
|
||||
|
||||
If validation fails, the script will report the errors and exit without creating a package. Fix any validation errors and run the packaging command again.
|
||||
|
||||
### Step 6: Iterate
|
||||
|
||||
After testing the skill, users may request improvements. Often this happens right after using the skill, with fresh context of how the skill performed.
|
||||
|
||||
**Iteration workflow:**
|
||||
1. Use the skill on real tasks
|
||||
2. Notice struggles or inefficiencies
|
||||
3. Identify how SKILL.md or bundled resources should be updated
|
||||
4. Implement changes and test again
|
||||
---
|
||||
name: skill-creator
|
||||
description: Guide for creating effective skills. This skill should be used when users want to create a new skill (or update an existing skill) that extends Claude's capabilities with specialized knowledge, workflows, or tool integrations.
|
||||
license: Complete terms in LICENSE.txt
|
||||
---
|
||||
|
||||
# Skill Creator
|
||||
|
||||
This skill provides guidance for creating effective skills.
|
||||
|
||||
## About Skills
|
||||
|
||||
Skills are modular, self-contained packages that extend Claude's capabilities by providing
|
||||
specialized knowledge, workflows, and tools. Think of them as "onboarding guides" for specific
|
||||
domains or tasks—they transform Claude from a general-purpose agent into a specialized agent
|
||||
equipped with procedural knowledge that no model can fully possess.
|
||||
|
||||
### What Skills Provide
|
||||
|
||||
1. Specialized workflows - Multi-step procedures for specific domains
|
||||
2. Tool integrations - Instructions for working with specific file formats or APIs
|
||||
3. Domain expertise - Company-specific knowledge, schemas, business logic
|
||||
4. Bundled resources - Scripts, references, and assets for complex and repetitive tasks
|
||||
|
||||
## Core Principles
|
||||
|
||||
### Concise is Key
|
||||
|
||||
The context window is a public good. Skills share the context window with everything else Claude needs: system prompt, conversation history, other Skills' metadata, and the actual user request.
|
||||
|
||||
**Default assumption: Claude is already very smart.** Only add context Claude doesn't already have. Challenge each piece of information: "Does Claude really need this explanation?" and "Does this paragraph justify its token cost?"
|
||||
|
||||
Prefer concise examples over verbose explanations.
|
||||
|
||||
### Set Appropriate Degrees of Freedom
|
||||
|
||||
Match the level of specificity to the task's fragility and variability:
|
||||
|
||||
**High freedom (text-based instructions)**: Use when multiple approaches are valid, decisions depend on context, or heuristics guide the approach.
|
||||
|
||||
**Medium freedom (pseudocode or scripts with parameters)**: Use when a preferred pattern exists, some variation is acceptable, or configuration affects behavior.
|
||||
|
||||
**Low freedom (specific scripts, few parameters)**: Use when operations are fragile and error-prone, consistency is critical, or a specific sequence must be followed.
|
||||
|
||||
Think of Claude as exploring a path: a narrow bridge with cliffs needs specific guardrails (low freedom), while an open field allows many routes (high freedom).
|
||||
|
||||
### Anatomy of a Skill
|
||||
|
||||
Every skill consists of a required SKILL.md file and optional bundled resources:
|
||||
|
||||
```
|
||||
skill-name/
|
||||
├── SKILL.md (required)
|
||||
│ ├── YAML frontmatter metadata (required)
|
||||
│ │ ├── name: (required)
|
||||
│ │ └── description: (required)
|
||||
│ └── Markdown instructions (required)
|
||||
└── Bundled Resources (optional)
|
||||
├── scripts/ - Executable code (Python/Bash/etc.)
|
||||
├── references/ - Documentation intended to be loaded into context as needed
|
||||
└── assets/ - Files used in output (templates, icons, fonts, etc.)
|
||||
```
|
||||
|
||||
#### SKILL.md (required)
|
||||
|
||||
Every SKILL.md consists of:
|
||||
|
||||
- **Frontmatter** (YAML): Contains `name` and `description` fields. These are the only fields that Claude reads to determine when the skill gets used, thus it is very important to be clear and comprehensive in describing what the skill is, and when it should be used.
|
||||
- **Body** (Markdown): Instructions and guidance for using the skill. Only loaded AFTER the skill triggers (if at all).
|
||||
|
||||
#### Bundled Resources (optional)
|
||||
|
||||
##### Scripts (`scripts/`)
|
||||
|
||||
Executable code (Python/Bash/etc.) for tasks that require deterministic reliability or are repeatedly rewritten.
|
||||
|
||||
- **When to include**: When the same code is being rewritten repeatedly or deterministic reliability is needed
|
||||
- **Example**: `scripts/rotate_pdf.py` for PDF rotation tasks
|
||||
- **Benefits**: Token efficient, deterministic, may be executed without loading into context
|
||||
- **Note**: Scripts may still need to be read by Claude for patching or environment-specific adjustments
|
||||
|
||||
##### References (`references/`)
|
||||
|
||||
Documentation and reference material intended to be loaded as needed into context to inform Claude's process and thinking.
|
||||
|
||||
- **When to include**: For documentation that Claude should reference while working
|
||||
- **Examples**: `references/finance.md` for financial schemas, `references/mnda.md` for company NDA template, `references/policies.md` for company policies, `references/api_docs.md` for API specifications
|
||||
- **Use cases**: Database schemas, API documentation, domain knowledge, company policies, detailed workflow guides
|
||||
- **Benefits**: Keeps SKILL.md lean, loaded only when Claude determines it's needed
|
||||
- **Best practice**: If files are large (>10k words), include grep search patterns in SKILL.md
|
||||
- **Avoid duplication**: Information should live in either SKILL.md or references files, not both. Prefer references files for detailed information unless it's truly core to the skill—this keeps SKILL.md lean while making information discoverable without hogging the context window. Keep only essential procedural instructions and workflow guidance in SKILL.md; move detailed reference material, schemas, and examples to references files.
|
||||
|
||||
##### Assets (`assets/`)
|
||||
|
||||
Files not intended to be loaded into context, but rather used within the output Claude produces.
|
||||
|
||||
- **When to include**: When the skill needs files that will be used in the final output
|
||||
- **Examples**: `assets/logo.png` for brand assets, `assets/slides.pptx` for PowerPoint templates, `assets/frontend-template/` for HTML/React boilerplate, `assets/font.ttf` for typography
|
||||
- **Use cases**: Templates, images, icons, boilerplate code, fonts, sample documents that get copied or modified
|
||||
- **Benefits**: Separates output resources from documentation, enables Claude to use files without loading them into context
|
||||
|
||||
#### What to Not Include in a Skill
|
||||
|
||||
A skill should only contain essential files that directly support its functionality. Do NOT create extraneous documentation or auxiliary files, including:
|
||||
|
||||
- README.md
|
||||
- INSTALLATION_GUIDE.md
|
||||
- QUICK_REFERENCE.md
|
||||
- CHANGELOG.md
|
||||
- etc.
|
||||
|
||||
The skill should only contain the information needed for an AI agent to do the job at hand. It should not contain auxilary context about the process that went into creating it, setup and testing procedures, user-facing documentation, etc. Creating additional documentation files just adds clutter and confusion.
|
||||
|
||||
### Progressive Disclosure Design Principle
|
||||
|
||||
Skills use a three-level loading system to manage context efficiently:
|
||||
|
||||
1. **Metadata (name + description)** - Always in context (~100 words)
|
||||
2. **SKILL.md body** - When skill triggers (<5k words)
|
||||
3. **Bundled resources** - As needed by Claude (Unlimited because scripts can be executed without reading into context window)
|
||||
|
||||
#### Progressive Disclosure Patterns
|
||||
|
||||
Keep SKILL.md body to the essentials and under 500 lines to minimize context bloat. Split content into separate files when approaching this limit. When splitting out content into other files, it is very important to reference them from SKILL.md and describe clearly when to read them, to ensure the reader of the skill knows they exist and when to use them.
|
||||
|
||||
**Key principle:** When a skill supports multiple variations, frameworks, or options, keep only the core workflow and selection guidance in SKILL.md. Move variant-specific details (patterns, examples, configuration) into separate reference files.
|
||||
|
||||
**Pattern 1: High-level guide with references**
|
||||
|
||||
```markdown
|
||||
# PDF Processing
|
||||
|
||||
## Quick start
|
||||
|
||||
Extract text with pdfplumber:
|
||||
[code example]
|
||||
|
||||
## Advanced features
|
||||
|
||||
- **Form filling**: See [FORMS.md](FORMS.md) for complete guide
|
||||
- **API reference**: See [REFERENCE.md](REFERENCE.md) for all methods
|
||||
- **Examples**: See [EXAMPLES.md](EXAMPLES.md) for common patterns
|
||||
```
|
||||
|
||||
Claude loads FORMS.md, REFERENCE.md, or EXAMPLES.md only when needed.
|
||||
|
||||
**Pattern 2: Domain-specific organization**
|
||||
|
||||
For Skills with multiple domains, organize content by domain to avoid loading irrelevant context:
|
||||
|
||||
```
|
||||
bigquery-skill/
|
||||
├── SKILL.md (overview and navigation)
|
||||
└── reference/
|
||||
├── finance.md (revenue, billing metrics)
|
||||
├── sales.md (opportunities, pipeline)
|
||||
├── product.md (API usage, features)
|
||||
└── marketing.md (campaigns, attribution)
|
||||
```
|
||||
|
||||
When a user asks about sales metrics, Claude only reads sales.md.
|
||||
|
||||
Similarly, for skills supporting multiple frameworks or variants, organize by variant:
|
||||
|
||||
```
|
||||
cloud-deploy/
|
||||
├── SKILL.md (workflow + provider selection)
|
||||
└── references/
|
||||
├── aws.md (AWS deployment patterns)
|
||||
├── gcp.md (GCP deployment patterns)
|
||||
└── azure.md (Azure deployment patterns)
|
||||
```
|
||||
|
||||
When the user chooses AWS, Claude only reads aws.md.
|
||||
|
||||
**Pattern 3: Conditional details**
|
||||
|
||||
Show basic content, link to advanced content:
|
||||
|
||||
```markdown
|
||||
# DOCX Processing
|
||||
|
||||
## Creating documents
|
||||
|
||||
Use docx-js for new documents. See [DOCX-JS.md](DOCX-JS.md).
|
||||
|
||||
## Editing documents
|
||||
|
||||
For simple edits, modify the XML directly.
|
||||
|
||||
**For tracked changes**: See [REDLINING.md](REDLINING.md)
|
||||
**For OOXML details**: See [OOXML.md](OOXML.md)
|
||||
```
|
||||
|
||||
Claude reads REDLINING.md or OOXML.md only when the user needs those features.
|
||||
|
||||
**Important guidelines:**
|
||||
|
||||
- **Avoid deeply nested references** - Keep references one level deep from SKILL.md. All reference files should link directly from SKILL.md.
|
||||
- **Structure longer reference files** - For files longer than 100 lines, include a table of contents at the top so Claude can see the full scope when previewing.
|
||||
|
||||
## Skill Creation Process
|
||||
|
||||
Skill creation involves these steps:
|
||||
|
||||
1. Understand the skill with concrete examples
|
||||
2. Plan reusable skill contents (scripts, references, assets)
|
||||
3. Initialize the skill (run init_skill.py)
|
||||
4. Edit the skill (implement resources and write SKILL.md)
|
||||
5. Package the skill (run package_skill.py)
|
||||
6. Iterate based on real usage
|
||||
|
||||
Follow these steps in order, skipping only if there is a clear reason why they are not applicable.
|
||||
|
||||
### Step 1: Understanding the Skill with Concrete Examples
|
||||
|
||||
Skip this step only when the skill's usage patterns are already clearly understood. It remains valuable even when working with an existing skill.
|
||||
|
||||
To create an effective skill, clearly understand concrete examples of how the skill will be used. This understanding can come from either direct user examples or generated examples that are validated with user feedback.
|
||||
|
||||
For example, when building an image-editor skill, relevant questions include:
|
||||
|
||||
- "What functionality should the image-editor skill support? Editing, rotating, anything else?"
|
||||
- "Can you give some examples of how this skill would be used?"
|
||||
- "I can imagine users asking for things like 'Remove the red-eye from this image' or 'Rotate this image'. Are there other ways you imagine this skill being used?"
|
||||
- "What would a user say that should trigger this skill?"
|
||||
|
||||
To avoid overwhelming users, avoid asking too many questions in a single message. Start with the most important questions and follow up as needed for better effectiveness.
|
||||
|
||||
Conclude this step when there is a clear sense of the functionality the skill should support.
|
||||
|
||||
### Step 2: Planning the Reusable Skill Contents
|
||||
|
||||
To turn concrete examples into an effective skill, analyze each example by:
|
||||
|
||||
1. Considering how to execute on the example from scratch
|
||||
2. Identifying what scripts, references, and assets would be helpful when executing these workflows repeatedly
|
||||
|
||||
Example: When building a `pdf-editor` skill to handle queries like "Help me rotate this PDF," the analysis shows:
|
||||
|
||||
1. Rotating a PDF requires re-writing the same code each time
|
||||
2. A `scripts/rotate_pdf.py` script would be helpful to store in the skill
|
||||
|
||||
Example: When designing a `frontend-webapp-builder` skill for queries like "Build me a todo app" or "Build me a dashboard to track my steps," the analysis shows:
|
||||
|
||||
1. Writing a frontend webapp requires the same boilerplate HTML/React each time
|
||||
2. An `assets/hello-world/` template containing the boilerplate HTML/React project files would be helpful to store in the skill
|
||||
|
||||
Example: When building a `big-query` skill to handle queries like "How many users have logged in today?" the analysis shows:
|
||||
|
||||
1. Querying BigQuery requires re-discovering the table schemas and relationships each time
|
||||
2. A `references/schema.md` file documenting the table schemas would be helpful to store in the skill
|
||||
|
||||
To establish the skill's contents, analyze each concrete example to create a list of the reusable resources to include: scripts, references, and assets.
|
||||
|
||||
### Step 3: Initializing the Skill
|
||||
|
||||
At this point, it is time to actually create the skill.
|
||||
|
||||
Skip this step only if the skill being developed already exists, and iteration or packaging is needed. In this case, continue to the next step.
|
||||
|
||||
When creating a new skill from scratch, always run the `init_skill.py` script. The script conveniently generates a new template skill directory that automatically includes everything a skill requires, making the skill creation process much more efficient and reliable.
|
||||
|
||||
Usage:
|
||||
|
||||
```bash
|
||||
scripts/init_skill.py <skill-name> --path <output-directory>
|
||||
```
|
||||
|
||||
The script:
|
||||
|
||||
- Creates the skill directory at the specified path
|
||||
- Generates a SKILL.md template with proper frontmatter and TODO placeholders
|
||||
- Creates example resource directories: `scripts/`, `references/`, and `assets/`
|
||||
- Adds example files in each directory that can be customized or deleted
|
||||
|
||||
After initialization, customize or remove the generated SKILL.md and example files as needed.
|
||||
|
||||
### Step 4: Edit the Skill
|
||||
|
||||
When editing the (newly-generated or existing) skill, remember that the skill is being created for another instance of Claude to use. Include information that would be beneficial and non-obvious to Claude. Consider what procedural knowledge, domain-specific details, or reusable assets would help another Claude instance execute these tasks more effectively.
|
||||
|
||||
#### Learn Proven Design Patterns
|
||||
|
||||
Consult these helpful guides based on your skill's needs:
|
||||
|
||||
- **Multi-step processes**: See references/workflows.md for sequential workflows and conditional logic
|
||||
- **Specific output formats or quality standards**: See references/output-patterns.md for template and example patterns
|
||||
|
||||
These files contain established best practices for effective skill design.
|
||||
|
||||
#### Start with Reusable Skill Contents
|
||||
|
||||
To begin implementation, start with the reusable resources identified above: `scripts/`, `references/`, and `assets/` files. Note that this step may require user input. For example, when implementing a `brand-guidelines` skill, the user may need to provide brand assets or templates to store in `assets/`, or documentation to store in `references/`.
|
||||
|
||||
Added scripts must be tested by actually running them to ensure there are no bugs and that the output matches what is expected. If there are many similar scripts, only a representative sample needs to be tested to ensure confidence that they all work while balancing time to completion.
|
||||
|
||||
Any example files and directories not needed for the skill should be deleted. The initialization script creates example files in `scripts/`, `references/`, and `assets/` to demonstrate structure, but most skills won't need all of them.
|
||||
|
||||
#### Update SKILL.md
|
||||
|
||||
**Writing Guidelines:** Always use imperative/infinitive form.
|
||||
|
||||
##### Frontmatter
|
||||
|
||||
Write the YAML frontmatter with `name` and `description`:
|
||||
|
||||
- `name`: The skill name
|
||||
- `description`: This is the primary triggering mechanism for your skill, and helps Claude understand when to use the skill.
|
||||
- Include both what the Skill does and specific triggers/contexts for when to use it.
|
||||
- Include all "when to use" information here - Not in the body. The body is only loaded after triggering, so "When to Use This Skill" sections in the body are not helpful to Claude.
|
||||
- Example description for a `docx` skill: "Comprehensive document creation, editing, and analysis with support for tracked changes, comments, formatting preservation, and text extraction. Use when Claude needs to work with professional documents (.docx files) for: (1) Creating new documents, (2) Modifying or editing content, (3) Working with tracked changes, (4) Adding comments, or any other document tasks"
|
||||
|
||||
Do not include any other fields in YAML frontmatter.
|
||||
|
||||
##### Body
|
||||
|
||||
Write instructions for using the skill and its bundled resources.
|
||||
|
||||
### Step 5: Packaging a Skill
|
||||
|
||||
Once development of the skill is complete, it must be packaged into a distributable .skill file that gets shared with the user. The packaging process automatically validates the skill first to ensure it meets all requirements:
|
||||
|
||||
```bash
|
||||
scripts/package_skill.py <path/to/skill-folder>
|
||||
```
|
||||
|
||||
Optional output directory specification:
|
||||
|
||||
```bash
|
||||
scripts/package_skill.py <path/to/skill-folder> ./dist
|
||||
```
|
||||
|
||||
The packaging script will:
|
||||
|
||||
1. **Validate** the skill automatically, checking:
|
||||
|
||||
- YAML frontmatter format and required fields
|
||||
- Skill naming conventions and directory structure
|
||||
- Description completeness and quality
|
||||
- File organization and resource references
|
||||
|
||||
2. **Package** the skill if validation passes, creating a .skill file named after the skill (e.g., `my-skill.skill`) that includes all files and maintains the proper directory structure for distribution. The .skill file is a zip file with a .skill extension.
|
||||
|
||||
If validation fails, the script will report the errors and exit without creating a package. Fix any validation errors and run the packaging command again.
|
||||
|
||||
### Step 6: Iterate
|
||||
|
||||
After testing the skill, users may request improvements. Often this happens right after using the skill, with fresh context of how the skill performed.
|
||||
|
||||
**Iteration workflow:**
|
||||
|
||||
1. Use the skill on real tasks
|
||||
2. Notice struggles or inefficiencies
|
||||
3. Identify how SKILL.md or bundled resources should be updated
|
||||
4. Implement changes and test again
|
||||
|
||||
82
.claude/skills/skill-creator/references/output-patterns.md
Normal file
82
.claude/skills/skill-creator/references/output-patterns.md
Normal file
@@ -0,0 +1,82 @@
|
||||
# Output Patterns
|
||||
|
||||
Use these patterns when skills need to produce consistent, high-quality output.
|
||||
|
||||
## Template Pattern
|
||||
|
||||
Provide templates for output format. Match the level of strictness to your needs.
|
||||
|
||||
**For strict requirements (like API responses or data formats):**
|
||||
|
||||
```markdown
|
||||
## Report structure
|
||||
|
||||
ALWAYS use this exact template structure:
|
||||
|
||||
# [Analysis Title]
|
||||
|
||||
## Executive summary
|
||||
[One-paragraph overview of key findings]
|
||||
|
||||
## Key findings
|
||||
- Finding 1 with supporting data
|
||||
- Finding 2 with supporting data
|
||||
- Finding 3 with supporting data
|
||||
|
||||
## Recommendations
|
||||
1. Specific actionable recommendation
|
||||
2. Specific actionable recommendation
|
||||
```
|
||||
|
||||
**For flexible guidance (when adaptation is useful):**
|
||||
|
||||
```markdown
|
||||
## Report structure
|
||||
|
||||
Here is a sensible default format, but use your best judgment:
|
||||
|
||||
# [Analysis Title]
|
||||
|
||||
## Executive summary
|
||||
[Overview]
|
||||
|
||||
## Key findings
|
||||
[Adapt sections based on what you discover]
|
||||
|
||||
## Recommendations
|
||||
[Tailor to the specific context]
|
||||
|
||||
Adjust sections as needed for the specific analysis type.
|
||||
```
|
||||
|
||||
## Examples Pattern
|
||||
|
||||
For skills where output quality depends on seeing examples, provide input/output pairs:
|
||||
|
||||
```markdown
|
||||
## Commit message format
|
||||
|
||||
Generate commit messages following these examples:
|
||||
|
||||
**Example 1:**
|
||||
Input: Added user authentication with JWT tokens
|
||||
Output:
|
||||
```
|
||||
feat(auth): implement JWT-based authentication
|
||||
|
||||
Add login endpoint and token validation middleware
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
Input: Fixed bug where dates displayed incorrectly in reports
|
||||
Output:
|
||||
```
|
||||
fix(reports): correct date formatting in timezone conversion
|
||||
|
||||
Use UTC timestamps consistently across report generation
|
||||
```
|
||||
|
||||
Follow this style: type(scope): brief description, then detailed explanation.
|
||||
```
|
||||
|
||||
Examples help Claude understand the desired style and level of detail more clearly than descriptions alone.
|
||||
28
.claude/skills/skill-creator/references/workflows.md
Normal file
28
.claude/skills/skill-creator/references/workflows.md
Normal file
@@ -0,0 +1,28 @@
|
||||
# Workflow Patterns
|
||||
|
||||
## Sequential Workflows
|
||||
|
||||
For complex tasks, break operations into clear, sequential steps. It is often helpful to give Claude an overview of the process towards the beginning of SKILL.md:
|
||||
|
||||
```markdown
|
||||
Filling a PDF form involves these steps:
|
||||
|
||||
1. Analyze the form (run analyze_form.py)
|
||||
2. Create field mapping (edit fields.json)
|
||||
3. Validate mapping (run validate_fields.py)
|
||||
4. Fill the form (run fill_form.py)
|
||||
5. Verify output (run verify_output.py)
|
||||
```
|
||||
|
||||
## Conditional Workflows
|
||||
|
||||
For tasks with branching logic, guide Claude through decision points:
|
||||
|
||||
```markdown
|
||||
1. Determine the modification type:
|
||||
**Creating new content?** → Follow "Creation workflow" below
|
||||
**Editing existing content?** → Follow "Editing workflow" below
|
||||
|
||||
2. Creation workflow: [steps]
|
||||
3. Editing workflow: [steps]
|
||||
```
|
||||
@@ -1,303 +1,303 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Skill Initializer - Creates a new skill from template
|
||||
|
||||
Usage:
|
||||
init_skill.py <skill-name> --path <path>
|
||||
|
||||
Examples:
|
||||
init_skill.py my-new-skill --path skills/public
|
||||
init_skill.py my-api-helper --path skills/private
|
||||
init_skill.py custom-skill --path /custom/location
|
||||
"""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
SKILL_TEMPLATE = """---
|
||||
name: {skill_name}
|
||||
description: [TODO: Complete and informative explanation of what the skill does and when to use it. Include WHEN to use this skill - specific scenarios, file types, or tasks that trigger it.]
|
||||
---
|
||||
|
||||
# {skill_title}
|
||||
|
||||
## Overview
|
||||
|
||||
[TODO: 1-2 sentences explaining what this skill enables]
|
||||
|
||||
## Structuring This Skill
|
||||
|
||||
[TODO: Choose the structure that best fits this skill's purpose. Common patterns:
|
||||
|
||||
**1. Workflow-Based** (best for sequential processes)
|
||||
- Works well when there are clear step-by-step procedures
|
||||
- Example: DOCX skill with "Workflow Decision Tree" → "Reading" → "Creating" → "Editing"
|
||||
- Structure: ## Overview → ## Workflow Decision Tree → ## Step 1 → ## Step 2...
|
||||
|
||||
**2. Task-Based** (best for tool collections)
|
||||
- Works well when the skill offers different operations/capabilities
|
||||
- Example: PDF skill with "Quick Start" → "Merge PDFs" → "Split PDFs" → "Extract Text"
|
||||
- Structure: ## Overview → ## Quick Start → ## Task Category 1 → ## Task Category 2...
|
||||
|
||||
**3. Reference/Guidelines** (best for standards or specifications)
|
||||
- Works well for brand guidelines, coding standards, or requirements
|
||||
- Example: Brand styling with "Brand Guidelines" → "Colors" → "Typography" → "Features"
|
||||
- Structure: ## Overview → ## Guidelines → ## Specifications → ## Usage...
|
||||
|
||||
**4. Capabilities-Based** (best for integrated systems)
|
||||
- Works well when the skill provides multiple interrelated features
|
||||
- Example: Product Management with "Core Capabilities" → numbered capability list
|
||||
- Structure: ## Overview → ## Core Capabilities → ### 1. Feature → ### 2. Feature...
|
||||
|
||||
Patterns can be mixed and matched as needed. Most skills combine patterns (e.g., start with task-based, add workflow for complex operations).
|
||||
|
||||
Delete this entire "Structuring This Skill" section when done - it's just guidance.]
|
||||
|
||||
## [TODO: Replace with the first main section based on chosen structure]
|
||||
|
||||
[TODO: Add content here. See examples in existing skills:
|
||||
- Code samples for technical skills
|
||||
- Decision trees for complex workflows
|
||||
- Concrete examples with realistic user requests
|
||||
- References to scripts/templates/references as needed]
|
||||
|
||||
## Resources
|
||||
|
||||
This skill includes example resource directories that demonstrate how to organize different types of bundled resources:
|
||||
|
||||
### scripts/
|
||||
Executable code (Python/Bash/etc.) that can be run directly to perform specific operations.
|
||||
|
||||
**Examples from other skills:**
|
||||
- PDF skill: `fill_fillable_fields.py`, `extract_form_field_info.py` - utilities for PDF manipulation
|
||||
- DOCX skill: `document.py`, `utilities.py` - Python modules for document processing
|
||||
|
||||
**Appropriate for:** Python scripts, shell scripts, or any executable code that performs automation, data processing, or specific operations.
|
||||
|
||||
**Note:** Scripts may be executed without loading into context, but can still be read by Claude for patching or environment adjustments.
|
||||
|
||||
### references/
|
||||
Documentation and reference material intended to be loaded into context to inform Claude's process and thinking.
|
||||
|
||||
**Examples from other skills:**
|
||||
- Product management: `communication.md`, `context_building.md` - detailed workflow guides
|
||||
- BigQuery: API reference documentation and query examples
|
||||
- Finance: Schema documentation, company policies
|
||||
|
||||
**Appropriate for:** In-depth documentation, API references, database schemas, comprehensive guides, or any detailed information that Claude should reference while working.
|
||||
|
||||
### assets/
|
||||
Files not intended to be loaded into context, but rather used within the output Claude produces.
|
||||
|
||||
**Examples from other skills:**
|
||||
- Brand styling: PowerPoint template files (.pptx), logo files
|
||||
- Frontend builder: HTML/React boilerplate project directories
|
||||
- Typography: Font files (.ttf, .woff2)
|
||||
|
||||
**Appropriate for:** Templates, boilerplate code, document templates, images, icons, fonts, or any files meant to be copied or used in the final output.
|
||||
|
||||
---
|
||||
|
||||
**Any unneeded directories can be deleted.** Not every skill requires all three types of resources.
|
||||
"""
|
||||
|
||||
EXAMPLE_SCRIPT = '''#!/usr/bin/env python3
|
||||
"""
|
||||
Example helper script for {skill_name}
|
||||
|
||||
This is a placeholder script that can be executed directly.
|
||||
Replace with actual implementation or delete if not needed.
|
||||
|
||||
Example real scripts from other skills:
|
||||
- pdf/scripts/fill_fillable_fields.py - Fills PDF form fields
|
||||
- pdf/scripts/convert_pdf_to_images.py - Converts PDF pages to images
|
||||
"""
|
||||
|
||||
def main():
|
||||
print("This is an example script for {skill_name}")
|
||||
# TODO: Add actual script logic here
|
||||
# This could be data processing, file conversion, API calls, etc.
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
'''
|
||||
|
||||
EXAMPLE_REFERENCE = """# Reference Documentation for {skill_title}
|
||||
|
||||
This is a placeholder for detailed reference documentation.
|
||||
Replace with actual reference content or delete if not needed.
|
||||
|
||||
Example real reference docs from other skills:
|
||||
- product-management/references/communication.md - Comprehensive guide for status updates
|
||||
- product-management/references/context_building.md - Deep-dive on gathering context
|
||||
- bigquery/references/ - API references and query examples
|
||||
|
||||
## When Reference Docs Are Useful
|
||||
|
||||
Reference docs are ideal for:
|
||||
- Comprehensive API documentation
|
||||
- Detailed workflow guides
|
||||
- Complex multi-step processes
|
||||
- Information too lengthy for main SKILL.md
|
||||
- Content that's only needed for specific use cases
|
||||
|
||||
## Structure Suggestions
|
||||
|
||||
### API Reference Example
|
||||
- Overview
|
||||
- Authentication
|
||||
- Endpoints with examples
|
||||
- Error codes
|
||||
- Rate limits
|
||||
|
||||
### Workflow Guide Example
|
||||
- Prerequisites
|
||||
- Step-by-step instructions
|
||||
- Common patterns
|
||||
- Troubleshooting
|
||||
- Best practices
|
||||
"""
|
||||
|
||||
EXAMPLE_ASSET = """# Example Asset File
|
||||
|
||||
This placeholder represents where asset files would be stored.
|
||||
Replace with actual asset files (templates, images, fonts, etc.) or delete if not needed.
|
||||
|
||||
Asset files are NOT intended to be loaded into context, but rather used within
|
||||
the output Claude produces.
|
||||
|
||||
Example asset files from other skills:
|
||||
- Brand guidelines: logo.png, slides_template.pptx
|
||||
- Frontend builder: hello-world/ directory with HTML/React boilerplate
|
||||
- Typography: custom-font.ttf, font-family.woff2
|
||||
- Data: sample_data.csv, test_dataset.json
|
||||
|
||||
## Common Asset Types
|
||||
|
||||
- Templates: .pptx, .docx, boilerplate directories
|
||||
- Images: .png, .jpg, .svg, .gif
|
||||
- Fonts: .ttf, .otf, .woff, .woff2
|
||||
- Boilerplate code: Project directories, starter files
|
||||
- Icons: .ico, .svg
|
||||
- Data files: .csv, .json, .xml, .yaml
|
||||
|
||||
Note: This is a text placeholder. Actual assets can be any file type.
|
||||
"""
|
||||
|
||||
|
||||
def title_case_skill_name(skill_name):
|
||||
"""Convert hyphenated skill name to Title Case for display."""
|
||||
return ' '.join(word.capitalize() for word in skill_name.split('-'))
|
||||
|
||||
|
||||
def init_skill(skill_name, path):
|
||||
"""
|
||||
Initialize a new skill directory with template SKILL.md.
|
||||
|
||||
Args:
|
||||
skill_name: Name of the skill
|
||||
path: Path where the skill directory should be created
|
||||
|
||||
Returns:
|
||||
Path to created skill directory, or None if error
|
||||
"""
|
||||
# Determine skill directory path
|
||||
skill_dir = Path(path).resolve() / skill_name
|
||||
|
||||
# Check if directory already exists
|
||||
if skill_dir.exists():
|
||||
print(f"❌ Error: Skill directory already exists: {skill_dir}")
|
||||
return None
|
||||
|
||||
# Create skill directory
|
||||
try:
|
||||
skill_dir.mkdir(parents=True, exist_ok=False)
|
||||
print(f"✅ Created skill directory: {skill_dir}")
|
||||
except Exception as e:
|
||||
print(f"❌ Error creating directory: {e}")
|
||||
return None
|
||||
|
||||
# Create SKILL.md from template
|
||||
skill_title = title_case_skill_name(skill_name)
|
||||
skill_content = SKILL_TEMPLATE.format(
|
||||
skill_name=skill_name,
|
||||
skill_title=skill_title
|
||||
)
|
||||
|
||||
skill_md_path = skill_dir / 'SKILL.md'
|
||||
try:
|
||||
skill_md_path.write_text(skill_content)
|
||||
print("✅ Created SKILL.md")
|
||||
except Exception as e:
|
||||
print(f"❌ Error creating SKILL.md: {e}")
|
||||
return None
|
||||
|
||||
# Create resource directories with example files
|
||||
try:
|
||||
# Create scripts/ directory with example script
|
||||
scripts_dir = skill_dir / 'scripts'
|
||||
scripts_dir.mkdir(exist_ok=True)
|
||||
example_script = scripts_dir / 'example.py'
|
||||
example_script.write_text(EXAMPLE_SCRIPT.format(skill_name=skill_name))
|
||||
example_script.chmod(0o755)
|
||||
print("✅ Created scripts/example.py")
|
||||
|
||||
# Create references/ directory with example reference doc
|
||||
references_dir = skill_dir / 'references'
|
||||
references_dir.mkdir(exist_ok=True)
|
||||
example_reference = references_dir / 'api_reference.md'
|
||||
example_reference.write_text(EXAMPLE_REFERENCE.format(skill_title=skill_title))
|
||||
print("✅ Created references/api_reference.md")
|
||||
|
||||
# Create assets/ directory with example asset placeholder
|
||||
assets_dir = skill_dir / 'assets'
|
||||
assets_dir.mkdir(exist_ok=True)
|
||||
example_asset = assets_dir / 'example_asset.txt'
|
||||
example_asset.write_text(EXAMPLE_ASSET)
|
||||
print("✅ Created assets/example_asset.txt")
|
||||
except Exception as e:
|
||||
print(f"❌ Error creating resource directories: {e}")
|
||||
return None
|
||||
|
||||
# Print next steps
|
||||
print(f"\n✅ Skill '{skill_name}' initialized successfully at {skill_dir}")
|
||||
print("\nNext steps:")
|
||||
print("1. Edit SKILL.md to complete the TODO items and update the description")
|
||||
print("2. Customize or delete the example files in scripts/, references/, and assets/")
|
||||
print("3. Run the validator when ready to check the skill structure")
|
||||
|
||||
return skill_dir
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 4 or sys.argv[2] != '--path':
|
||||
print("Usage: init_skill.py <skill-name> --path <path>")
|
||||
print("\nSkill name requirements:")
|
||||
print(" - Hyphen-case identifier (e.g., 'data-analyzer')")
|
||||
print(" - Lowercase letters, digits, and hyphens only")
|
||||
print(" - Max 40 characters")
|
||||
print(" - Must match directory name exactly")
|
||||
print("\nExamples:")
|
||||
print(" init_skill.py my-new-skill --path skills/public")
|
||||
print(" init_skill.py my-api-helper --path skills/private")
|
||||
print(" init_skill.py custom-skill --path /custom/location")
|
||||
sys.exit(1)
|
||||
|
||||
skill_name = sys.argv[1]
|
||||
path = sys.argv[3]
|
||||
|
||||
print(f"🚀 Initializing skill: {skill_name}")
|
||||
print(f" Location: {path}")
|
||||
print()
|
||||
|
||||
result = init_skill(skill_name, path)
|
||||
|
||||
if result:
|
||||
sys.exit(0)
|
||||
else:
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Skill Initializer - Creates a new skill from template
|
||||
|
||||
Usage:
|
||||
init_skill.py <skill-name> --path <path>
|
||||
|
||||
Examples:
|
||||
init_skill.py my-new-skill --path skills/public
|
||||
init_skill.py my-api-helper --path skills/private
|
||||
init_skill.py custom-skill --path /custom/location
|
||||
"""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
SKILL_TEMPLATE = """---
|
||||
name: {skill_name}
|
||||
description: [TODO: Complete and informative explanation of what the skill does and when to use it. Include WHEN to use this skill - specific scenarios, file types, or tasks that trigger it.]
|
||||
---
|
||||
|
||||
# {skill_title}
|
||||
|
||||
## Overview
|
||||
|
||||
[TODO: 1-2 sentences explaining what this skill enables]
|
||||
|
||||
## Structuring This Skill
|
||||
|
||||
[TODO: Choose the structure that best fits this skill's purpose. Common patterns:
|
||||
|
||||
**1. Workflow-Based** (best for sequential processes)
|
||||
- Works well when there are clear step-by-step procedures
|
||||
- Example: DOCX skill with "Workflow Decision Tree" → "Reading" → "Creating" → "Editing"
|
||||
- Structure: ## Overview → ## Workflow Decision Tree → ## Step 1 → ## Step 2...
|
||||
|
||||
**2. Task-Based** (best for tool collections)
|
||||
- Works well when the skill offers different operations/capabilities
|
||||
- Example: PDF skill with "Quick Start" → "Merge PDFs" → "Split PDFs" → "Extract Text"
|
||||
- Structure: ## Overview → ## Quick Start → ## Task Category 1 → ## Task Category 2...
|
||||
|
||||
**3. Reference/Guidelines** (best for standards or specifications)
|
||||
- Works well for brand guidelines, coding standards, or requirements
|
||||
- Example: Brand styling with "Brand Guidelines" → "Colors" → "Typography" → "Features"
|
||||
- Structure: ## Overview → ## Guidelines → ## Specifications → ## Usage...
|
||||
|
||||
**4. Capabilities-Based** (best for integrated systems)
|
||||
- Works well when the skill provides multiple interrelated features
|
||||
- Example: Product Management with "Core Capabilities" → numbered capability list
|
||||
- Structure: ## Overview → ## Core Capabilities → ### 1. Feature → ### 2. Feature...
|
||||
|
||||
Patterns can be mixed and matched as needed. Most skills combine patterns (e.g., start with task-based, add workflow for complex operations).
|
||||
|
||||
Delete this entire "Structuring This Skill" section when done - it's just guidance.]
|
||||
|
||||
## [TODO: Replace with the first main section based on chosen structure]
|
||||
|
||||
[TODO: Add content here. See examples in existing skills:
|
||||
- Code samples for technical skills
|
||||
- Decision trees for complex workflows
|
||||
- Concrete examples with realistic user requests
|
||||
- References to scripts/templates/references as needed]
|
||||
|
||||
## Resources
|
||||
|
||||
This skill includes example resource directories that demonstrate how to organize different types of bundled resources:
|
||||
|
||||
### scripts/
|
||||
Executable code (Python/Bash/etc.) that can be run directly to perform specific operations.
|
||||
|
||||
**Examples from other skills:**
|
||||
- PDF skill: `fill_fillable_fields.py`, `extract_form_field_info.py` - utilities for PDF manipulation
|
||||
- DOCX skill: `document.py`, `utilities.py` - Python modules for document processing
|
||||
|
||||
**Appropriate for:** Python scripts, shell scripts, or any executable code that performs automation, data processing, or specific operations.
|
||||
|
||||
**Note:** Scripts may be executed without loading into context, but can still be read by Claude for patching or environment adjustments.
|
||||
|
||||
### references/
|
||||
Documentation and reference material intended to be loaded into context to inform Claude's process and thinking.
|
||||
|
||||
**Examples from other skills:**
|
||||
- Product management: `communication.md`, `context_building.md` - detailed workflow guides
|
||||
- BigQuery: API reference documentation and query examples
|
||||
- Finance: Schema documentation, company policies
|
||||
|
||||
**Appropriate for:** In-depth documentation, API references, database schemas, comprehensive guides, or any detailed information that Claude should reference while working.
|
||||
|
||||
### assets/
|
||||
Files not intended to be loaded into context, but rather used within the output Claude produces.
|
||||
|
||||
**Examples from other skills:**
|
||||
- Brand styling: PowerPoint template files (.pptx), logo files
|
||||
- Frontend builder: HTML/React boilerplate project directories
|
||||
- Typography: Font files (.ttf, .woff2)
|
||||
|
||||
**Appropriate for:** Templates, boilerplate code, document templates, images, icons, fonts, or any files meant to be copied or used in the final output.
|
||||
|
||||
---
|
||||
|
||||
**Any unneeded directories can be deleted.** Not every skill requires all three types of resources.
|
||||
"""
|
||||
|
||||
EXAMPLE_SCRIPT = '''#!/usr/bin/env python3
|
||||
"""
|
||||
Example helper script for {skill_name}
|
||||
|
||||
This is a placeholder script that can be executed directly.
|
||||
Replace with actual implementation or delete if not needed.
|
||||
|
||||
Example real scripts from other skills:
|
||||
- pdf/scripts/fill_fillable_fields.py - Fills PDF form fields
|
||||
- pdf/scripts/convert_pdf_to_images.py - Converts PDF pages to images
|
||||
"""
|
||||
|
||||
def main():
|
||||
print("This is an example script for {skill_name}")
|
||||
# TODO: Add actual script logic here
|
||||
# This could be data processing, file conversion, API calls, etc.
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
'''
|
||||
|
||||
EXAMPLE_REFERENCE = """# Reference Documentation for {skill_title}
|
||||
|
||||
This is a placeholder for detailed reference documentation.
|
||||
Replace with actual reference content or delete if not needed.
|
||||
|
||||
Example real reference docs from other skills:
|
||||
- product-management/references/communication.md - Comprehensive guide for status updates
|
||||
- product-management/references/context_building.md - Deep-dive on gathering context
|
||||
- bigquery/references/ - API references and query examples
|
||||
|
||||
## When Reference Docs Are Useful
|
||||
|
||||
Reference docs are ideal for:
|
||||
- Comprehensive API documentation
|
||||
- Detailed workflow guides
|
||||
- Complex multi-step processes
|
||||
- Information too lengthy for main SKILL.md
|
||||
- Content that's only needed for specific use cases
|
||||
|
||||
## Structure Suggestions
|
||||
|
||||
### API Reference Example
|
||||
- Overview
|
||||
- Authentication
|
||||
- Endpoints with examples
|
||||
- Error codes
|
||||
- Rate limits
|
||||
|
||||
### Workflow Guide Example
|
||||
- Prerequisites
|
||||
- Step-by-step instructions
|
||||
- Common patterns
|
||||
- Troubleshooting
|
||||
- Best practices
|
||||
"""
|
||||
|
||||
EXAMPLE_ASSET = """# Example Asset File
|
||||
|
||||
This placeholder represents where asset files would be stored.
|
||||
Replace with actual asset files (templates, images, fonts, etc.) or delete if not needed.
|
||||
|
||||
Asset files are NOT intended to be loaded into context, but rather used within
|
||||
the output Claude produces.
|
||||
|
||||
Example asset files from other skills:
|
||||
- Brand guidelines: logo.png, slides_template.pptx
|
||||
- Frontend builder: hello-world/ directory with HTML/React boilerplate
|
||||
- Typography: custom-font.ttf, font-family.woff2
|
||||
- Data: sample_data.csv, test_dataset.json
|
||||
|
||||
## Common Asset Types
|
||||
|
||||
- Templates: .pptx, .docx, boilerplate directories
|
||||
- Images: .png, .jpg, .svg, .gif
|
||||
- Fonts: .ttf, .otf, .woff, .woff2
|
||||
- Boilerplate code: Project directories, starter files
|
||||
- Icons: .ico, .svg
|
||||
- Data files: .csv, .json, .xml, .yaml
|
||||
|
||||
Note: This is a text placeholder. Actual assets can be any file type.
|
||||
"""
|
||||
|
||||
|
||||
def title_case_skill_name(skill_name):
|
||||
"""Convert hyphenated skill name to Title Case for display."""
|
||||
return ' '.join(word.capitalize() for word in skill_name.split('-'))
|
||||
|
||||
|
||||
def init_skill(skill_name, path):
|
||||
"""
|
||||
Initialize a new skill directory with template SKILL.md.
|
||||
|
||||
Args:
|
||||
skill_name: Name of the skill
|
||||
path: Path where the skill directory should be created
|
||||
|
||||
Returns:
|
||||
Path to created skill directory, or None if error
|
||||
"""
|
||||
# Determine skill directory path
|
||||
skill_dir = Path(path).resolve() / skill_name
|
||||
|
||||
# Check if directory already exists
|
||||
if skill_dir.exists():
|
||||
print(f"❌ Error: Skill directory already exists: {skill_dir}")
|
||||
return None
|
||||
|
||||
# Create skill directory
|
||||
try:
|
||||
skill_dir.mkdir(parents=True, exist_ok=False)
|
||||
print(f"✅ Created skill directory: {skill_dir}")
|
||||
except Exception as e:
|
||||
print(f"❌ Error creating directory: {e}")
|
||||
return None
|
||||
|
||||
# Create SKILL.md from template
|
||||
skill_title = title_case_skill_name(skill_name)
|
||||
skill_content = SKILL_TEMPLATE.format(
|
||||
skill_name=skill_name,
|
||||
skill_title=skill_title
|
||||
)
|
||||
|
||||
skill_md_path = skill_dir / 'SKILL.md'
|
||||
try:
|
||||
skill_md_path.write_text(skill_content)
|
||||
print("✅ Created SKILL.md")
|
||||
except Exception as e:
|
||||
print(f"❌ Error creating SKILL.md: {e}")
|
||||
return None
|
||||
|
||||
# Create resource directories with example files
|
||||
try:
|
||||
# Create scripts/ directory with example script
|
||||
scripts_dir = skill_dir / 'scripts'
|
||||
scripts_dir.mkdir(exist_ok=True)
|
||||
example_script = scripts_dir / 'example.py'
|
||||
example_script.write_text(EXAMPLE_SCRIPT.format(skill_name=skill_name))
|
||||
example_script.chmod(0o755)
|
||||
print("✅ Created scripts/example.py")
|
||||
|
||||
# Create references/ directory with example reference doc
|
||||
references_dir = skill_dir / 'references'
|
||||
references_dir.mkdir(exist_ok=True)
|
||||
example_reference = references_dir / 'api_reference.md'
|
||||
example_reference.write_text(EXAMPLE_REFERENCE.format(skill_title=skill_title))
|
||||
print("✅ Created references/api_reference.md")
|
||||
|
||||
# Create assets/ directory with example asset placeholder
|
||||
assets_dir = skill_dir / 'assets'
|
||||
assets_dir.mkdir(exist_ok=True)
|
||||
example_asset = assets_dir / 'example_asset.txt'
|
||||
example_asset.write_text(EXAMPLE_ASSET)
|
||||
print("✅ Created assets/example_asset.txt")
|
||||
except Exception as e:
|
||||
print(f"❌ Error creating resource directories: {e}")
|
||||
return None
|
||||
|
||||
# Print next steps
|
||||
print(f"\n✅ Skill '{skill_name}' initialized successfully at {skill_dir}")
|
||||
print("\nNext steps:")
|
||||
print("1. Edit SKILL.md to complete the TODO items and update the description")
|
||||
print("2. Customize or delete the example files in scripts/, references/, and assets/")
|
||||
print("3. Run the validator when ready to check the skill structure")
|
||||
|
||||
return skill_dir
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 4 or sys.argv[2] != '--path':
|
||||
print("Usage: init_skill.py <skill-name> --path <path>")
|
||||
print("\nSkill name requirements:")
|
||||
print(" - Hyphen-case identifier (e.g., 'data-analyzer')")
|
||||
print(" - Lowercase letters, digits, and hyphens only")
|
||||
print(" - Max 40 characters")
|
||||
print(" - Must match directory name exactly")
|
||||
print("\nExamples:")
|
||||
print(" init_skill.py my-new-skill --path skills/public")
|
||||
print(" init_skill.py my-api-helper --path skills/private")
|
||||
print(" init_skill.py custom-skill --path /custom/location")
|
||||
sys.exit(1)
|
||||
|
||||
skill_name = sys.argv[1]
|
||||
path = sys.argv[3]
|
||||
|
||||
print(f"🚀 Initializing skill: {skill_name}")
|
||||
print(f" Location: {path}")
|
||||
print()
|
||||
|
||||
result = init_skill(skill_name, path)
|
||||
|
||||
if result:
|
||||
sys.exit(0)
|
||||
else:
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
@@ -1,110 +1,110 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Skill Packager - Creates a distributable zip file of a skill folder
|
||||
|
||||
Usage:
|
||||
python utils/package_skill.py <path/to/skill-folder> [output-directory]
|
||||
|
||||
Example:
|
||||
python utils/package_skill.py skills/public/my-skill
|
||||
python utils/package_skill.py skills/public/my-skill ./dist
|
||||
"""
|
||||
|
||||
import sys
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
from quick_validate import validate_skill
|
||||
|
||||
|
||||
def package_skill(skill_path, output_dir=None):
|
||||
"""
|
||||
Package a skill folder into a zip file.
|
||||
|
||||
Args:
|
||||
skill_path: Path to the skill folder
|
||||
output_dir: Optional output directory for the zip file (defaults to current directory)
|
||||
|
||||
Returns:
|
||||
Path to the created zip file, or None if error
|
||||
"""
|
||||
skill_path = Path(skill_path).resolve()
|
||||
|
||||
# Validate skill folder exists
|
||||
if not skill_path.exists():
|
||||
print(f"❌ Error: Skill folder not found: {skill_path}")
|
||||
return None
|
||||
|
||||
if not skill_path.is_dir():
|
||||
print(f"❌ Error: Path is not a directory: {skill_path}")
|
||||
return None
|
||||
|
||||
# Validate SKILL.md exists
|
||||
skill_md = skill_path / "SKILL.md"
|
||||
if not skill_md.exists():
|
||||
print(f"❌ Error: SKILL.md not found in {skill_path}")
|
||||
return None
|
||||
|
||||
# Run validation before packaging
|
||||
print("🔍 Validating skill...")
|
||||
valid, message = validate_skill(skill_path)
|
||||
if not valid:
|
||||
print(f"❌ Validation failed: {message}")
|
||||
print(" Please fix the validation errors before packaging.")
|
||||
return None
|
||||
print(f"✅ {message}\n")
|
||||
|
||||
# Determine output location
|
||||
skill_name = skill_path.name
|
||||
if output_dir:
|
||||
output_path = Path(output_dir).resolve()
|
||||
output_path.mkdir(parents=True, exist_ok=True)
|
||||
else:
|
||||
output_path = Path.cwd()
|
||||
|
||||
zip_filename = output_path / f"{skill_name}.zip"
|
||||
|
||||
# Create the zip file
|
||||
try:
|
||||
with zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
||||
# Walk through the skill directory
|
||||
for file_path in skill_path.rglob('*'):
|
||||
if file_path.is_file():
|
||||
# Calculate the relative path within the zip
|
||||
arcname = file_path.relative_to(skill_path.parent)
|
||||
zipf.write(file_path, arcname)
|
||||
print(f" Added: {arcname}")
|
||||
|
||||
print(f"\n✅ Successfully packaged skill to: {zip_filename}")
|
||||
return zip_filename
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Error creating zip file: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: python utils/package_skill.py <path/to/skill-folder> [output-directory]")
|
||||
print("\nExample:")
|
||||
print(" python utils/package_skill.py skills/public/my-skill")
|
||||
print(" python utils/package_skill.py skills/public/my-skill ./dist")
|
||||
sys.exit(1)
|
||||
|
||||
skill_path = sys.argv[1]
|
||||
output_dir = sys.argv[2] if len(sys.argv) > 2 else None
|
||||
|
||||
print(f"📦 Packaging skill: {skill_path}")
|
||||
if output_dir:
|
||||
print(f" Output directory: {output_dir}")
|
||||
print()
|
||||
|
||||
result = package_skill(skill_path, output_dir)
|
||||
|
||||
if result:
|
||||
sys.exit(0)
|
||||
else:
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Skill Packager - Creates a distributable .skill file of a skill folder
|
||||
|
||||
Usage:
|
||||
python utils/package_skill.py <path/to/skill-folder> [output-directory]
|
||||
|
||||
Example:
|
||||
python utils/package_skill.py skills/public/my-skill
|
||||
python utils/package_skill.py skills/public/my-skill ./dist
|
||||
"""
|
||||
|
||||
import sys
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
from quick_validate import validate_skill
|
||||
|
||||
|
||||
def package_skill(skill_path, output_dir=None):
|
||||
"""
|
||||
Package a skill folder into a .skill file.
|
||||
|
||||
Args:
|
||||
skill_path: Path to the skill folder
|
||||
output_dir: Optional output directory for the .skill file (defaults to current directory)
|
||||
|
||||
Returns:
|
||||
Path to the created .skill file, or None if error
|
||||
"""
|
||||
skill_path = Path(skill_path).resolve()
|
||||
|
||||
# Validate skill folder exists
|
||||
if not skill_path.exists():
|
||||
print(f"❌ Error: Skill folder not found: {skill_path}")
|
||||
return None
|
||||
|
||||
if not skill_path.is_dir():
|
||||
print(f"❌ Error: Path is not a directory: {skill_path}")
|
||||
return None
|
||||
|
||||
# Validate SKILL.md exists
|
||||
skill_md = skill_path / "SKILL.md"
|
||||
if not skill_md.exists():
|
||||
print(f"❌ Error: SKILL.md not found in {skill_path}")
|
||||
return None
|
||||
|
||||
# Run validation before packaging
|
||||
print("🔍 Validating skill...")
|
||||
valid, message = validate_skill(skill_path)
|
||||
if not valid:
|
||||
print(f"❌ Validation failed: {message}")
|
||||
print(" Please fix the validation errors before packaging.")
|
||||
return None
|
||||
print(f"✅ {message}\n")
|
||||
|
||||
# Determine output location
|
||||
skill_name = skill_path.name
|
||||
if output_dir:
|
||||
output_path = Path(output_dir).resolve()
|
||||
output_path.mkdir(parents=True, exist_ok=True)
|
||||
else:
|
||||
output_path = Path.cwd()
|
||||
|
||||
skill_filename = output_path / f"{skill_name}.skill"
|
||||
|
||||
# Create the .skill file (zip format)
|
||||
try:
|
||||
with zipfile.ZipFile(skill_filename, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
||||
# Walk through the skill directory
|
||||
for file_path in skill_path.rglob('*'):
|
||||
if file_path.is_file():
|
||||
# Calculate the relative path within the zip
|
||||
arcname = file_path.relative_to(skill_path.parent)
|
||||
zipf.write(file_path, arcname)
|
||||
print(f" Added: {arcname}")
|
||||
|
||||
print(f"\n✅ Successfully packaged skill to: {skill_filename}")
|
||||
return skill_filename
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Error creating .skill file: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: python utils/package_skill.py <path/to/skill-folder> [output-directory]")
|
||||
print("\nExample:")
|
||||
print(" python utils/package_skill.py skills/public/my-skill")
|
||||
print(" python utils/package_skill.py skills/public/my-skill ./dist")
|
||||
sys.exit(1)
|
||||
|
||||
skill_path = sys.argv[1]
|
||||
output_dir = sys.argv[2] if len(sys.argv) > 2 else None
|
||||
|
||||
print(f"📦 Packaging skill: {skill_path}")
|
||||
if output_dir:
|
||||
print(f" Output directory: {output_dir}")
|
||||
print()
|
||||
|
||||
result = package_skill(skill_path, output_dir)
|
||||
|
||||
if result:
|
||||
sys.exit(0)
|
||||
else:
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
@@ -1,65 +1,95 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Quick validation script for skills - minimal version
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
def validate_skill(skill_path):
|
||||
"""Basic validation of a skill"""
|
||||
skill_path = Path(skill_path)
|
||||
|
||||
# Check SKILL.md exists
|
||||
skill_md = skill_path / 'SKILL.md'
|
||||
if not skill_md.exists():
|
||||
return False, "SKILL.md not found"
|
||||
|
||||
# Read and validate frontmatter
|
||||
content = skill_md.read_text()
|
||||
if not content.startswith('---'):
|
||||
return False, "No YAML frontmatter found"
|
||||
|
||||
# Extract frontmatter
|
||||
match = re.match(r'^---\n(.*?)\n---', content, re.DOTALL)
|
||||
if not match:
|
||||
return False, "Invalid frontmatter format"
|
||||
|
||||
frontmatter = match.group(1)
|
||||
|
||||
# Check required fields
|
||||
if 'name:' not in frontmatter:
|
||||
return False, "Missing 'name' in frontmatter"
|
||||
if 'description:' not in frontmatter:
|
||||
return False, "Missing 'description' in frontmatter"
|
||||
|
||||
# Extract name for validation
|
||||
name_match = re.search(r'name:\s*(.+)', frontmatter)
|
||||
if name_match:
|
||||
name = name_match.group(1).strip()
|
||||
# Check naming convention (hyphen-case: lowercase with hyphens)
|
||||
if not re.match(r'^[a-z0-9-]+$', name):
|
||||
return False, f"Name '{name}' should be hyphen-case (lowercase letters, digits, and hyphens only)"
|
||||
if name.startswith('-') or name.endswith('-') or '--' in name:
|
||||
return False, f"Name '{name}' cannot start/end with hyphen or contain consecutive hyphens"
|
||||
|
||||
# Extract and validate description
|
||||
desc_match = re.search(r'description:\s*(.+)', frontmatter)
|
||||
if desc_match:
|
||||
description = desc_match.group(1).strip()
|
||||
# Check for angle brackets
|
||||
if '<' in description or '>' in description:
|
||||
return False, "Description cannot contain angle brackets (< or >)"
|
||||
|
||||
return True, "Skill is valid!"
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) != 2:
|
||||
print("Usage: python quick_validate.py <skill_directory>")
|
||||
sys.exit(1)
|
||||
|
||||
valid, message = validate_skill(sys.argv[1])
|
||||
print(message)
|
||||
sys.exit(0 if valid else 1)
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Quick validation script for skills - minimal version
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
import re
|
||||
import yaml
|
||||
from pathlib import Path
|
||||
|
||||
def validate_skill(skill_path):
|
||||
"""Basic validation of a skill"""
|
||||
skill_path = Path(skill_path)
|
||||
|
||||
# Check SKILL.md exists
|
||||
skill_md = skill_path / 'SKILL.md'
|
||||
if not skill_md.exists():
|
||||
return False, "SKILL.md not found"
|
||||
|
||||
# Read and validate frontmatter
|
||||
content = skill_md.read_text()
|
||||
if not content.startswith('---'):
|
||||
return False, "No YAML frontmatter found"
|
||||
|
||||
# Extract frontmatter
|
||||
match = re.match(r'^---\n(.*?)\n---', content, re.DOTALL)
|
||||
if not match:
|
||||
return False, "Invalid frontmatter format"
|
||||
|
||||
frontmatter_text = match.group(1)
|
||||
|
||||
# Parse YAML frontmatter
|
||||
try:
|
||||
frontmatter = yaml.safe_load(frontmatter_text)
|
||||
if not isinstance(frontmatter, dict):
|
||||
return False, "Frontmatter must be a YAML dictionary"
|
||||
except yaml.YAMLError as e:
|
||||
return False, f"Invalid YAML in frontmatter: {e}"
|
||||
|
||||
# Define allowed properties
|
||||
ALLOWED_PROPERTIES = {'name', 'description', 'license', 'allowed-tools', 'metadata'}
|
||||
|
||||
# Check for unexpected properties (excluding nested keys under metadata)
|
||||
unexpected_keys = set(frontmatter.keys()) - ALLOWED_PROPERTIES
|
||||
if unexpected_keys:
|
||||
return False, (
|
||||
f"Unexpected key(s) in SKILL.md frontmatter: {', '.join(sorted(unexpected_keys))}. "
|
||||
f"Allowed properties are: {', '.join(sorted(ALLOWED_PROPERTIES))}"
|
||||
)
|
||||
|
||||
# Check required fields
|
||||
if 'name' not in frontmatter:
|
||||
return False, "Missing 'name' in frontmatter"
|
||||
if 'description' not in frontmatter:
|
||||
return False, "Missing 'description' in frontmatter"
|
||||
|
||||
# Extract name for validation
|
||||
name = frontmatter.get('name', '')
|
||||
if not isinstance(name, str):
|
||||
return False, f"Name must be a string, got {type(name).__name__}"
|
||||
name = name.strip()
|
||||
if name:
|
||||
# Check naming convention (hyphen-case: lowercase with hyphens)
|
||||
if not re.match(r'^[a-z0-9-]+$', name):
|
||||
return False, f"Name '{name}' should be hyphen-case (lowercase letters, digits, and hyphens only)"
|
||||
if name.startswith('-') or name.endswith('-') or '--' in name:
|
||||
return False, f"Name '{name}' cannot start/end with hyphen or contain consecutive hyphens"
|
||||
# Check name length (max 64 characters per spec)
|
||||
if len(name) > 64:
|
||||
return False, f"Name is too long ({len(name)} characters). Maximum is 64 characters."
|
||||
|
||||
# Extract and validate description
|
||||
description = frontmatter.get('description', '')
|
||||
if not isinstance(description, str):
|
||||
return False, f"Description must be a string, got {type(description).__name__}"
|
||||
description = description.strip()
|
||||
if description:
|
||||
# Check for angle brackets
|
||||
if '<' in description or '>' in description:
|
||||
return False, "Description cannot contain angle brackets (< or >)"
|
||||
# Check description length (max 1024 characters per spec)
|
||||
if len(description) > 1024:
|
||||
return False, f"Description is too long ({len(description)} characters). Maximum is 1024 characters."
|
||||
|
||||
return True, "Skill is valid!"
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) != 2:
|
||||
print("Usage: python quick_validate.py <skill_directory>")
|
||||
sys.exit(1)
|
||||
|
||||
valid, message = validate_skill(sys.argv[1])
|
||||
print(message)
|
||||
sys.exit(0 if valid else 1)
|
||||
|
||||
@@ -1,212 +0,0 @@
|
||||
---
|
||||
name: standalone-component-migrator
|
||||
description: This skill should be used when converting Angular NgModule-based components to standalone architecture. It handles dependency analysis, template scanning, route refactoring, and test updates. Use this skill when the user requests component migration to standalone, mentions "convert to standalone", or wants to modernize Angular components to the latest patterns.
|
||||
---
|
||||
|
||||
# Standalone Component Migrator
|
||||
|
||||
## Overview
|
||||
|
||||
Automate the conversion of Angular components from NgModule-based architecture to standalone components with explicit imports. This skill analyzes component dependencies, updates routing configurations, migrates tests, and optionally converts to modern Angular control flow syntax (@if, @for, @switch).
|
||||
|
||||
## When to Use This Skill
|
||||
|
||||
Invoke this skill when:
|
||||
- User requests component conversion to standalone
|
||||
- User mentions "migrate to standalone" or "modernize component"
|
||||
- User wants to remove NgModule declarations
|
||||
- User references Angular's standalone component architecture
|
||||
|
||||
## Migration Workflow
|
||||
|
||||
### Step 1: Analyze Component Dependencies
|
||||
|
||||
1. **Read Component File**
|
||||
- Identify component decorator configuration
|
||||
- Note selector, template path, style paths
|
||||
- Check if already standalone
|
||||
|
||||
2. **Analyze Template**
|
||||
- Read template file (HTML)
|
||||
- Scan for directives: `*ngIf`, `*ngFor`, `*ngSwitch` → requires CommonModule
|
||||
- Scan for forms: `ngModel`, `formControl` → requires FormsModule or ReactiveFormsModule
|
||||
- Scan for built-in pipes: `async`, `date`, `json` → CommonModule
|
||||
- Scan for custom components: identify all component selectors
|
||||
- Scan for router directives: `routerLink`, `router-outlet` → RouterModule
|
||||
|
||||
3. **Find Parent NgModule**
|
||||
- Search for NgModule that declares this component
|
||||
- Read NgModule file to understand current imports
|
||||
|
||||
### Step 2: Convert Component to Standalone
|
||||
|
||||
Add `standalone: true` and explicit imports array:
|
||||
|
||||
```typescript
|
||||
// BEFORE
|
||||
import { Component } from '@angular/core';
|
||||
|
||||
@Component({
|
||||
selector: 'app-my-component',
|
||||
templateUrl: './my-component.component.html'
|
||||
})
|
||||
export class MyComponent { }
|
||||
|
||||
// AFTER
|
||||
import { Component } from '@angular/core';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { FormsModule } from '@angular/forms';
|
||||
import { RouterModule } from '@angular/router';
|
||||
import { ChildComponent } from './child.component';
|
||||
import { CustomPipe } from '@isa/utils/pipes';
|
||||
|
||||
@Component({
|
||||
selector: 'app-my-component',
|
||||
standalone: true,
|
||||
imports: [
|
||||
CommonModule,
|
||||
FormsModule,
|
||||
RouterModule,
|
||||
ChildComponent,
|
||||
CustomPipe
|
||||
],
|
||||
templateUrl: './my-component.component.html'
|
||||
})
|
||||
export class MyComponent { }
|
||||
```
|
||||
|
||||
### Step 3: Update Parent NgModule
|
||||
|
||||
Remove component from declarations, add to imports if exported:
|
||||
|
||||
```typescript
|
||||
// BEFORE
|
||||
@NgModule({
|
||||
declarations: [MyComponent, OtherComponent],
|
||||
imports: [CommonModule],
|
||||
exports: [MyComponent]
|
||||
})
|
||||
|
||||
// AFTER
|
||||
@NgModule({
|
||||
declarations: [OtherComponent],
|
||||
imports: [CommonModule, MyComponent], // Import standalone component
|
||||
exports: [MyComponent]
|
||||
})
|
||||
```
|
||||
|
||||
If NgModule becomes empty (no declarations), consider removing it entirely.
|
||||
|
||||
### Step 4: Update Routes (if applicable)
|
||||
|
||||
Convert to lazy-loaded standalone component:
|
||||
|
||||
```typescript
|
||||
// BEFORE
|
||||
const routes: Routes = [
|
||||
{ path: 'feature', component: MyComponent }
|
||||
];
|
||||
|
||||
// AFTER (lazy loading)
|
||||
const routes: Routes = [
|
||||
{
|
||||
path: 'feature',
|
||||
loadComponent: () => import('./my-component.component').then(m => m.MyComponent)
|
||||
}
|
||||
];
|
||||
```
|
||||
|
||||
### Step 5: Update Tests
|
||||
|
||||
Convert test configuration:
|
||||
|
||||
```typescript
|
||||
// BEFORE
|
||||
TestBed.configureTestingModule({
|
||||
declarations: [MyComponent],
|
||||
imports: [CommonModule, FormsModule]
|
||||
});
|
||||
|
||||
// AFTER
|
||||
TestBed.configureTestingModule({
|
||||
imports: [MyComponent] // Component imports its own dependencies
|
||||
});
|
||||
```
|
||||
|
||||
### Step 6: Optional - Migrate to Modern Control Flow
|
||||
|
||||
If requested, convert to new Angular control flow syntax:
|
||||
|
||||
```typescript
|
||||
// OLD
|
||||
<div *ngIf="condition">Content</div>
|
||||
<div *ngFor="let item of items; trackBy: trackById">{{ item.name }}</div>
|
||||
<div [ngSwitch]="value">
|
||||
<div *ngSwitchCase="'a'">A</div>
|
||||
<div *ngSwitchDefault>Default</div>
|
||||
</div>
|
||||
|
||||
// NEW
|
||||
@if (condition) {
|
||||
<div>Content</div>
|
||||
}
|
||||
@for (item of items; track item.id) {
|
||||
<div>{{ item.name }}</div>
|
||||
}
|
||||
@switch (value) {
|
||||
@case ('a') { <div>A</div> }
|
||||
@default { <div>Default</div> }
|
||||
}
|
||||
```
|
||||
|
||||
### Step 7: Validate and Test
|
||||
|
||||
1. **Compile Check**
|
||||
```bash
|
||||
npx tsc --noEmit
|
||||
```
|
||||
|
||||
2. **Run Tests**
|
||||
```bash
|
||||
npx nx test [library-name] --skip-nx-cache
|
||||
```
|
||||
|
||||
3. **Lint Check**
|
||||
```bash
|
||||
npx nx lint [library-name]
|
||||
```
|
||||
|
||||
4. **Verify Application Runs**
|
||||
```bash
|
||||
npm start
|
||||
```
|
||||
|
||||
## Common Import Patterns
|
||||
|
||||
| Template Usage | Required Import |
|
||||
|---------------|-----------------|
|
||||
| `*ngIf`, `*ngFor`, `*ngSwitch` | `CommonModule` |
|
||||
| `ngModel` | `FormsModule` |
|
||||
| `formControl`, `formGroup` | `ReactiveFormsModule` |
|
||||
| `routerLink`, `router-outlet` | `RouterModule` |
|
||||
| `async`, `date`, `json` pipes | `CommonModule` |
|
||||
| Custom components | Direct component import |
|
||||
| Custom pipes | Direct pipe import |
|
||||
|
||||
## Error Handling
|
||||
|
||||
**Issue: Circular dependencies**
|
||||
- Extract shared interfaces to util library
|
||||
- Use dependency injection for services
|
||||
- Avoid component A importing component B when B imports A
|
||||
|
||||
**Issue: Missing imports causing template errors**
|
||||
- Check browser console for specific errors
|
||||
- Verify all template dependencies are in imports array
|
||||
- Use Angular Language Service in IDE for hints
|
||||
|
||||
## References
|
||||
|
||||
- Angular Standalone Components: https://angular.dev/guide/components/importing
|
||||
- Modern Control Flow: https://angular.dev/guide/templates/control-flow
|
||||
- CLAUDE.md Component Architecture section
|
||||
678
.claude/skills/state-patterns/SKILL.md
Normal file
678
.claude/skills/state-patterns/SKILL.md
Normal file
@@ -0,0 +1,678 @@
|
||||
---
|
||||
name: state-patterns
|
||||
description: This skill should be used when writing Angular code with signals, effects, and NgRx Signal Store. Use when deciding whether to use effect(), computed(), or reactive patterns for state management. Applies when implementing Resource API with Signal Store for reactive data loading, preventing race conditions, and creating declarative async flows. Essential for code review of effect usage, refactoring imperative patterns to declarative alternatives, and building stores with reactive filter/search parameters.
|
||||
---
|
||||
|
||||
# Reactive State Patterns
|
||||
|
||||
## Overview
|
||||
|
||||
This skill guides proper usage of Angular's `effect()`, provides declarative alternatives for common patterns, and enables integration of Angular's Resource API with NgRx Signal Store. Effects are frequently misused for state propagation, leading to circular updates and maintenance issues. The Resource API provides race-condition-free async data loading that integrates seamlessly with Signal Store.
|
||||
|
||||
## When to Use Effects (Valid Use Cases)
|
||||
|
||||
Effects are **primarily for rendering content that cannot be rendered through data binding**. Valid use cases are limited to:
|
||||
|
||||
### 1. Logging
|
||||
Recording application events or debugging:
|
||||
|
||||
```typescript
|
||||
effect(() => {
|
||||
const error = this.error();
|
||||
if (error) {
|
||||
console.error('Error occurred:', error);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
### 2. Canvas Painting
|
||||
Custom graphics rendering (e.g., Angular Three library, Chart.js integration):
|
||||
|
||||
```typescript
|
||||
effect(() => {
|
||||
const data = this.chartData();
|
||||
this.renderCanvas(data);
|
||||
});
|
||||
```
|
||||
|
||||
### 3. Custom DOM Behavior
|
||||
Imperative APIs that require direct DOM manipulation:
|
||||
|
||||
```typescript
|
||||
effect(() => {
|
||||
const message = this.notificationMessage();
|
||||
if (message) {
|
||||
this.snackBar.open(message, 'Close');
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
**Key principle:** Data binding is the preferred way to display data. Effects should only be used when data binding is insufficient.
|
||||
|
||||
## Understanding Auto-Tracking
|
||||
|
||||
Angular automatically tracks signals accessed during effect execution, **even within called methods**:
|
||||
|
||||
```typescript
|
||||
effect(() => {
|
||||
this.logError(); // Signal tracking happens inside this method
|
||||
});
|
||||
|
||||
logError(): void {
|
||||
const error = this.error(); // This signal is automatically tracked
|
||||
if (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Implication:** Auto-tracking makes effect dependencies non-obvious and hard to maintain. This is a primary reason to avoid effects for state management.
|
||||
|
||||
## Effect Anti-Patterns (NEVER Use)
|
||||
|
||||
### ❌ Anti-Pattern 1: State Propagation
|
||||
|
||||
**NEVER use effects to propagate state changes to other state:**
|
||||
|
||||
```typescript
|
||||
// ❌ WRONG - Anti-pattern
|
||||
effect(() => {
|
||||
const value = this.source();
|
||||
this.derived.set(value * 2);
|
||||
});
|
||||
```
|
||||
|
||||
**Problems:**
|
||||
- Risk of circular updates and infinite loops
|
||||
- Hard to maintain due to implicit tracking
|
||||
- Violates declarative reactive principles
|
||||
- Inappropriate glitch-free semantics
|
||||
|
||||
### ❌ Anti-Pattern 2: Synchronizing Signals
|
||||
|
||||
```typescript
|
||||
// ❌ WRONG - Anti-pattern
|
||||
effect(() => {
|
||||
const filter = this.filter();
|
||||
this.loadData(filter);
|
||||
});
|
||||
```
|
||||
|
||||
### ❌ Anti-Pattern 3: Event Emulation
|
||||
|
||||
```typescript
|
||||
// ❌ WRONG - Anti-pattern
|
||||
effect(() => {
|
||||
const count = this.itemCount();
|
||||
this.countChanged.emit(count);
|
||||
});
|
||||
```
|
||||
|
||||
**Why signals ≠ events:** Signals are designed to be glitch-free, collapsing multiple updates. This makes them inappropriate for representing discrete events.
|
||||
|
||||
## Decision Tree: Effect vs Alternative
|
||||
|
||||
```
|
||||
Need to react to signal changes?
|
||||
│
|
||||
├─ Synchronous derivation?
|
||||
│ └─ ✅ Use computed()
|
||||
│
|
||||
├─ Asynchronous derivation?
|
||||
│ └─ ✅ Use Resource API
|
||||
│
|
||||
├─ Complex reactive flow with race conditions?
|
||||
│ └─ ✅ Use RxJS (toObservable + operators + toSignal)
|
||||
│
|
||||
├─ Event-based trigger (not state change)?
|
||||
│ └─ ✅ React to event directly, not signal
|
||||
│
|
||||
├─ Need RxJS operators with signals?
|
||||
│ └─ ✅ Use reactive helpers (rxMethod, deriveAsync)
|
||||
│
|
||||
└─ Rendering non-data-bound content (logging, canvas, imperative API)?
|
||||
└─ ✅ Use effect()
|
||||
```
|
||||
|
||||
## Recommended Alternatives
|
||||
|
||||
### Alternative 1: Use `computed()` for Synchronous Derivations
|
||||
|
||||
**When to use:** Deriving new state synchronously from existing signals.
|
||||
|
||||
```typescript
|
||||
// ✅ CORRECT - Declarative
|
||||
const derived = computed(() => {
|
||||
return this.baseSignal() * 2;
|
||||
});
|
||||
|
||||
const fullName = computed(() => {
|
||||
return `${this.firstName()} ${this.lastName()}`;
|
||||
});
|
||||
```
|
||||
|
||||
**Benefits:**
|
||||
- Declarative and maintainable
|
||||
- Automatic dependency tracking
|
||||
- Memoized and efficient
|
||||
- No risk of circular updates
|
||||
|
||||
### Alternative 2: Use Resource API for Asynchronous Derivations
|
||||
|
||||
**When to use:** Loading data based on reactive parameters.
|
||||
|
||||
```typescript
|
||||
// ✅ CORRECT - Declarative async state
|
||||
readonly itemsResource = resource({
|
||||
params: this.filter,
|
||||
loader: ({ params, abortSignal }) => {
|
||||
return this.dataService.load(params, abortSignal);
|
||||
}
|
||||
});
|
||||
|
||||
readonly items = computed(() => this.itemsResource.value() ?? []);
|
||||
```
|
||||
|
||||
**Benefits:**
|
||||
- Automatic race condition handling
|
||||
- Built-in loading/error states
|
||||
- Declarative parameter tracking
|
||||
- Cancellation support
|
||||
|
||||
### Alternative 3: React to Events, Not State Changes
|
||||
|
||||
**When to use:** User interactions or DOM events should trigger actions.
|
||||
|
||||
```typescript
|
||||
// ❌ WRONG - Reacting to signal change
|
||||
effect(() => {
|
||||
const searchTerm = this.searchTerm();
|
||||
this.search(searchTerm);
|
||||
});
|
||||
|
||||
// ✅ CORRECT - React to event
|
||||
<input (input)="search($event.target.value)" />
|
||||
|
||||
// Component
|
||||
search(term: string): void {
|
||||
this.searchTerm.set(term);
|
||||
this.performSearch(term);
|
||||
}
|
||||
```
|
||||
|
||||
**Benefits:**
|
||||
- Clear causality (event → action)
|
||||
- No auto-tracking complexity
|
||||
- Explicit control flow
|
||||
|
||||
### Alternative 4: RxJS Integration
|
||||
|
||||
**When to use:** Complex reactive flows requiring operators like `debounceTime`, `switchMap`, `combineLatest`.
|
||||
|
||||
```typescript
|
||||
// ✅ CORRECT - RxJS for complex flows
|
||||
readonly searchTerm = signal('');
|
||||
readonly searchTerm$ = toObservable(this.searchTerm);
|
||||
|
||||
readonly results$ = this.searchTerm$.pipe(
|
||||
debounceTime(300),
|
||||
distinctUntilChanged(),
|
||||
switchMap(term => this.searchService.search(term))
|
||||
);
|
||||
|
||||
readonly results = toSignal(this.results$, { initialValue: [] });
|
||||
```
|
||||
|
||||
**Benefits:**
|
||||
- Full RxJS operator ecosystem
|
||||
- Race condition prevention (`switchMap`)
|
||||
- Powerful composition
|
||||
- Type-safe streams
|
||||
|
||||
**Pattern:** Signal → Observable (toObservable) → RxJS operators → Signal (toSignal)
|
||||
|
||||
### Alternative 5: Reactive Helpers
|
||||
|
||||
**When to use:** Need RxJS operators but prefer signal-centric API.
|
||||
|
||||
#### Using `rxMethod` (NgRx Signal Store)
|
||||
|
||||
```typescript
|
||||
readonly loadItem = rxMethod<number>(
|
||||
pipe(
|
||||
tap(() => patchState(this, { loading: true })),
|
||||
switchMap(id => this.service.findById(id)),
|
||||
tap(item => patchState(this, { item, loading: false }))
|
||||
)
|
||||
);
|
||||
|
||||
// Call with signal or value
|
||||
constructor() {
|
||||
this.loadItem(this.selectedId);
|
||||
}
|
||||
```
|
||||
|
||||
#### Using `deriveAsync` (ngxtension)
|
||||
|
||||
```typescript
|
||||
readonly data = deriveAsync(() => {
|
||||
const filter = this.filter();
|
||||
return this.service.load(filter);
|
||||
});
|
||||
```
|
||||
|
||||
**Benefits:**
|
||||
- Signal-friendly API
|
||||
- RxJS operator support
|
||||
- Cleaner than manual Observable conversion
|
||||
- Automatic subscription management
|
||||
|
||||
## Resource API with NgRx Signal Store
|
||||
|
||||
### Core Architectural Concepts
|
||||
|
||||
Establish three clear interaction points in the store:
|
||||
|
||||
1. **Filter signals trigger resource loading** - Parameter changes automatically reload resources
|
||||
2. **Methods explicitly invoke operations** - Use `signalMethod` for user-triggered actions
|
||||
3. **Computed signals derive view models** - Transform loaded data for component consumption
|
||||
|
||||
### The withProps Pattern for Dependency Injection
|
||||
|
||||
Inject services via `withProps` with underscore-prefixed properties to mark them as internal implementation details:
|
||||
|
||||
```typescript
|
||||
withProps(() => ({
|
||||
_dataService: inject(DataService),
|
||||
_notificationService: inject(ToastService),
|
||||
}))
|
||||
```
|
||||
|
||||
**Benefits:**
|
||||
- Centralizes dependency injection in one location
|
||||
- Clear distinction between internal (prefixed) and public properties
|
||||
- Services available to all subsequent feature sections
|
||||
|
||||
### Implementation Steps
|
||||
|
||||
#### Step 1: Inject Services with withProps
|
||||
|
||||
Create the initial `withProps` section to inject required services:
|
||||
|
||||
```typescript
|
||||
export const MyStore = signalStore(
|
||||
withProps(() => ({
|
||||
_dataService: inject(DataService),
|
||||
_toastService: inject(ToastService),
|
||||
})),
|
||||
// ... additional features
|
||||
);
|
||||
```
|
||||
|
||||
**Naming convention:** Prefix all injected services with underscore (`_`) to indicate internal use.
|
||||
|
||||
#### Step 2: Define Filter State
|
||||
|
||||
Add state properties that will serve as resource parameters:
|
||||
|
||||
```typescript
|
||||
withState({
|
||||
filter: {
|
||||
searchTerm: '',
|
||||
category: '',
|
||||
} as MyFilter,
|
||||
})
|
||||
```
|
||||
|
||||
#### Step 3: Configure Resources
|
||||
|
||||
In a subsequent `withProps` section, create resources that reference the injected services and state:
|
||||
|
||||
```typescript
|
||||
withProps((store) => ({
|
||||
_itemsResource: resource({
|
||||
params: store.filter,
|
||||
loader: (loaderParams) => {
|
||||
const filter = loaderParams.params;
|
||||
const abortSignal = loaderParams.abortSignal;
|
||||
return store._dataService.loadItems(filter, abortSignal);
|
||||
}
|
||||
})
|
||||
}))
|
||||
```
|
||||
|
||||
**Key points:**
|
||||
- Resources automatically reload when `params` signal changes
|
||||
- `abortSignal` enables automatic cancellation of in-flight requests
|
||||
- Loader must return a Promise (use `.findPromise()` if service returns Observable)
|
||||
|
||||
#### Step 4: Expose Read-Only Resources (Optional)
|
||||
|
||||
If the resource should be accessible to consumers, expose it as read-only:
|
||||
|
||||
```typescript
|
||||
withProps((store) => ({
|
||||
itemsResource: store._itemsResource.asReadonly(),
|
||||
}))
|
||||
```
|
||||
|
||||
**Pattern:** Internal resources use underscore prefix, public versions are read-only without prefix.
|
||||
|
||||
#### Step 5: Create Signal Methods for Updates
|
||||
|
||||
Use `signalMethod` for actions that update state and trigger resource reloads:
|
||||
|
||||
```typescript
|
||||
withMethods((store) => ({
|
||||
updateFilter: signalMethod<MyFilter>((filter) => {
|
||||
patchState(store, { filter });
|
||||
}),
|
||||
|
||||
refresh: () => {
|
||||
store._itemsResource.reload();
|
||||
}
|
||||
}))
|
||||
```
|
||||
|
||||
**Important:** `signalMethod` implementations are **untracked by convention** - they don't re-execute when signals change. This provides explicit control flow.
|
||||
|
||||
#### Step 6: Add Error Handling
|
||||
|
||||
Use `withHooks` to react to resource errors:
|
||||
|
||||
```typescript
|
||||
withHooks({
|
||||
onInit: (store) => {
|
||||
effect(() => {
|
||||
const error = store._itemsResource.error();
|
||||
if (error) {
|
||||
store._toastService.show('Error: ' + getMessage(error));
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
**Pattern:** Error effects should be read-only - they observe errors and trigger side effects, but don't modify state.
|
||||
|
||||
### Common Resource API Patterns
|
||||
|
||||
#### Template Integration with linkedSignal
|
||||
|
||||
For two-way form binding that synchronizes with the store:
|
||||
|
||||
```typescript
|
||||
export class MyComponent {
|
||||
#store = inject(MyStore);
|
||||
|
||||
// Create linked signal for form field
|
||||
searchTerm = linkedSignal(() => this.#store.filter().searchTerm);
|
||||
|
||||
// Combine form fields into filter object
|
||||
#linkedFilter = computed(() => ({
|
||||
searchTerm: this.searchTerm(),
|
||||
// ... other fields
|
||||
}));
|
||||
|
||||
constructor() {
|
||||
// Sync form changes back to store
|
||||
this.#store.updateFilter(this.#linkedFilter);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Benefits:**
|
||||
- Two-way binding for forms
|
||||
- Automatic store synchronization
|
||||
- Type-safe filter construction
|
||||
|
||||
#### Working with Resource Data
|
||||
|
||||
Access resource data through resource signals:
|
||||
|
||||
```typescript
|
||||
withComputed((store) => ({
|
||||
items: computed(() => store._itemsResource.value() ?? []),
|
||||
isLoading: computed(() => store._itemsResource.isLoading()),
|
||||
hasError: computed(() => store._itemsResource.hasError()),
|
||||
}))
|
||||
```
|
||||
|
||||
**Available signals:**
|
||||
- `value()` - The loaded data (undefined while loading)
|
||||
- `isLoading()` - Loading state boolean
|
||||
- `hasError()` - Error state boolean
|
||||
- `error()` - Error object if present
|
||||
- `status()` - Overall status: 'idle' | 'loading' | 'resolved' | 'error'
|
||||
|
||||
#### Updating Resource Data Locally
|
||||
|
||||
For temporary working copies before server writes:
|
||||
|
||||
```typescript
|
||||
withMethods((store) => ({
|
||||
updateLocalItem: (id: string, changes: Partial<Item>) => {
|
||||
store._itemsResource.update((currentItems) => {
|
||||
return currentItems.map(item =>
|
||||
item.id === id ? { ...item, ...changes } : item
|
||||
);
|
||||
});
|
||||
}
|
||||
}))
|
||||
```
|
||||
|
||||
**Note:** This pattern feels unconventional but aligns with maintaining temporary working copies before server persistence.
|
||||
|
||||
#### Multiple Resources in One Store
|
||||
|
||||
Combine multiple resources for complex data requirements:
|
||||
|
||||
```typescript
|
||||
withProps((store) => ({
|
||||
_itemsResource: resource({
|
||||
params: store.filter,
|
||||
loader: (params) => store._dataService.loadItems(params.params, params.abortSignal)
|
||||
}),
|
||||
|
||||
_detailsResource: resource({
|
||||
params: store.selectedId,
|
||||
loader: (params) => {
|
||||
if (!params.params) return Promise.resolve(null);
|
||||
return store._dataService.loadDetails(params.params, params.abortSignal);
|
||||
}
|
||||
})
|
||||
}))
|
||||
```
|
||||
|
||||
**Pattern:** Each resource has independent params and loading state, but can share service instances.
|
||||
|
||||
### Important Resource API Considerations
|
||||
|
||||
#### Race Condition Prevention
|
||||
|
||||
The Resource API automatically handles race conditions:
|
||||
- New requests automatically cancel pending requests
|
||||
- No need for `switchMap`, `takeUntilDestroyed`, or manual abort handling
|
||||
- Declarative parameter changes trigger clean cancellation
|
||||
|
||||
#### Untracked Signal Methods
|
||||
|
||||
`signalMethod` implementations deliberately skip reactive tracking:
|
||||
- Provides explicit, predictable control flow
|
||||
- Prevents unexpected re-executions from signal changes
|
||||
- Makes side effects obvious at call sites
|
||||
|
||||
#### Loader Function Requirements
|
||||
|
||||
Loaders must:
|
||||
- Return a `Promise` (not Observable)
|
||||
- Accept and pass through the `abortSignal` to enable cancellation
|
||||
- Handle the signal in the underlying HTTP call
|
||||
|
||||
**Converting Observables:**
|
||||
```typescript
|
||||
loader: (params) => {
|
||||
return firstValueFrom(
|
||||
this._service.load$(params.params)
|
||||
.pipe(takeUntilDestroyed(this._destroyRef))
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
#### Resource Lifecycle
|
||||
|
||||
Resources maintain their own state machine:
|
||||
1. **Idle** - Initial state before first load
|
||||
2. **Loading** - Request in progress
|
||||
3. **Resolved** - Data loaded successfully
|
||||
4. **Error** - Request failed
|
||||
|
||||
State transitions automatically trigger reactive updates to dependent computeds and effects.
|
||||
|
||||
## Common Refactoring Patterns
|
||||
|
||||
### Pattern 1: Effect for State Sync → computed()
|
||||
|
||||
```typescript
|
||||
// ❌ Before
|
||||
effect(() => {
|
||||
const x = this.x();
|
||||
const y = this.y();
|
||||
this.sum.set(x + y);
|
||||
});
|
||||
|
||||
// ✅ After
|
||||
readonly sum = computed(() => this.x() + this.y());
|
||||
```
|
||||
|
||||
### Pattern 2: Effect for Async Load → Resource API
|
||||
|
||||
```typescript
|
||||
// ❌ Before
|
||||
effect(() => {
|
||||
const id = this.selectedId();
|
||||
this.loadItem(id);
|
||||
});
|
||||
|
||||
// ✅ After
|
||||
readonly itemResource = resource({
|
||||
params: this.selectedId,
|
||||
loader: ({ params }) => this.service.loadItem(params)
|
||||
});
|
||||
```
|
||||
|
||||
### Pattern 3: Effect for Debounced Search → RxJS
|
||||
|
||||
```typescript
|
||||
// ❌ Before
|
||||
effect(() => {
|
||||
const term = this.searchTerm();
|
||||
// No way to debounce within effect
|
||||
this.search(term);
|
||||
});
|
||||
|
||||
// ✅ After
|
||||
readonly searchTerm$ = toObservable(this.searchTerm);
|
||||
readonly results = toSignal(
|
||||
this.searchTerm$.pipe(
|
||||
debounceTime(300),
|
||||
switchMap(term => this.searchService.search(term))
|
||||
),
|
||||
{ initialValue: [] }
|
||||
);
|
||||
```
|
||||
|
||||
### Pattern 4: Effect for Event Notification → Direct Event Handling
|
||||
|
||||
```typescript
|
||||
// ❌ Before
|
||||
effect(() => {
|
||||
const value = this.value();
|
||||
this.valueChange.emit(value);
|
||||
});
|
||||
|
||||
// ✅ After
|
||||
updateValue(newValue: string): void {
|
||||
this.value.set(newValue);
|
||||
this.valueChange.emit(newValue);
|
||||
}
|
||||
```
|
||||
|
||||
## Code Review Checklist
|
||||
|
||||
When reviewing code with `effect()`, ask:
|
||||
|
||||
- [ ] Is this for rendering non-data-bound content? (logging, canvas, imperative APIs)
|
||||
- **YES:** Effect is appropriate
|
||||
- **NO:** Continue checklist
|
||||
|
||||
- [ ] Is this synchronous state derivation?
|
||||
- **YES:** Use `computed()` instead
|
||||
|
||||
- [ ] Is this asynchronous data loading?
|
||||
- **YES:** Use Resource API instead
|
||||
|
||||
- [ ] Does this need RxJS operators (debounce, switchMap, etc.)?
|
||||
- **YES:** Use RxJS integration or reactive helpers instead
|
||||
|
||||
- [ ] Is this reacting to a user event?
|
||||
- **YES:** Handle event directly instead
|
||||
|
||||
- [ ] Could this cause circular updates?
|
||||
- **YES:** Refactor immediately - this will cause bugs
|
||||
|
||||
## Anti-Pattern Detection Rules
|
||||
|
||||
Flag any effect that:
|
||||
|
||||
1. **Calls `set()` or `update()` on signals** - Likely state propagation anti-pattern
|
||||
2. **Calls service methods that update state** - Hidden state propagation
|
||||
3. **Emits events based on signal changes** - Signal/event semantic mismatch
|
||||
4. **Has try/catch for async operations** - Should use Resource API
|
||||
5. **Would benefit from debouncing/throttling** - Should use RxJS
|
||||
|
||||
## When to Use Each Pattern
|
||||
|
||||
**Use computed() when:**
|
||||
- Synchronous state derivation
|
||||
- No async operations needed
|
||||
- Memoization desired
|
||||
|
||||
**Use Resource API with Signal Store when:**
|
||||
- Loading data based on reactive filter/search parameters
|
||||
- Need automatic race condition handling for concurrent requests
|
||||
- Want declarative data loading without RxJS subscriptions
|
||||
- Building stores with frequently changing query parameters
|
||||
- Implementing pagination, filtering, or search features
|
||||
|
||||
**Use RxJS integration when:**
|
||||
- Complex reactive flows with multiple operators
|
||||
- Need debouncing, throttling, or custom timing control
|
||||
- Working with multiple Observable sources
|
||||
- Require fine-grained control over subscription lifecycle
|
||||
|
||||
**Use reactive helpers when:**
|
||||
- Prefer signal-centric API but need RxJS power
|
||||
- Simple async operations with basic operators
|
||||
- Want automatic subscription management
|
||||
|
||||
**Consider alternatives to Resource API when:**
|
||||
- Simple one-time data loads (use `rxMethod` or direct service calls)
|
||||
- Complex Observable chains with multiple operators needed
|
||||
- Need fine-grained control over request timing/caching
|
||||
- Working with WebSocket or SSE streams (use `rxMethod` instead)
|
||||
|
||||
## Key Principles
|
||||
|
||||
1. **Effects are for side effects, not state management**
|
||||
2. **Prefer declarative over imperative**
|
||||
3. **Use computed() for sync, Resource API for async**
|
||||
4. **React to events, not state changes**
|
||||
5. **RxJS for complex reactive flows**
|
||||
6. **Auto-tracking is powerful but opaque - avoid when possible**
|
||||
|
||||
## When in Doubt
|
||||
|
||||
Ask: "Can the user see this without an effect using data binding?"
|
||||
- **YES:** Don't use effect, use data binding
|
||||
- **NO:** Effect might be appropriate (but verify against decision tree)
|
||||
@@ -1,134 +0,0 @@
|
||||
---
|
||||
name: swagger-sync-manager
|
||||
description: This skill should be used when regenerating Swagger/OpenAPI TypeScript API clients in the ISA-Frontend monorepo. It handles generation of all 10 API clients (or specific ones), Unicode cleanup, breaking change detection, TypeScript validation, and affected test execution. Use this skill when the user requests API sync, mentions "regenerate swagger", or indicates backend API changes.
|
||||
---
|
||||
|
||||
# Swagger Sync Manager
|
||||
|
||||
## Overview
|
||||
|
||||
Automate the regeneration of TypeScript API clients from Swagger/OpenAPI specifications. Handles 10 API clients with automatic post-processing, breaking change detection, impact analysis, and validation.
|
||||
|
||||
## When to Use This Skill
|
||||
|
||||
Invoke when user requests:
|
||||
- API client regeneration
|
||||
- "sync swagger" or "update API clients"
|
||||
- Backend API changes need frontend updates
|
||||
|
||||
## Available APIs
|
||||
|
||||
availability-api, cat-search-api, checkout-api, crm-api, eis-api, inventory-api, isa-api, oms-api, print-api, wws-api
|
||||
|
||||
## Sync Workflow
|
||||
|
||||
### Step 1: Pre-Generation Check
|
||||
|
||||
```bash
|
||||
# Check uncommitted changes
|
||||
git status generated/swagger/
|
||||
```
|
||||
|
||||
If changes exist, warn user and ask to proceed.
|
||||
|
||||
### Step 2: Backup Current State (Optional)
|
||||
|
||||
```bash
|
||||
cp -r generated/swagger generated/swagger.backup.$(date +%s)
|
||||
```
|
||||
|
||||
### Step 3: Run Generation
|
||||
|
||||
```bash
|
||||
# All APIs
|
||||
npm run generate:swagger
|
||||
|
||||
# Specific API (if api-name provided)
|
||||
npm run generate:swagger:[api-name]
|
||||
```
|
||||
|
||||
### Step 4: Verify Unicode Cleanup
|
||||
|
||||
Check `tools/fix-files.js` executed. Scan for remaining Unicode issues:
|
||||
|
||||
```bash
|
||||
grep -r "\\\\u00" generated/swagger/ || echo "✅ No Unicode issues"
|
||||
```
|
||||
|
||||
### Step 5: Detect Breaking Changes
|
||||
|
||||
For each modified API:
|
||||
|
||||
```bash
|
||||
git diff generated/swagger/[api-name]/
|
||||
```
|
||||
|
||||
Identify:
|
||||
- 🔴 Removed properties
|
||||
- 🔴 Changed types
|
||||
- 🔴 Removed endpoints
|
||||
- ✅ Added properties (safe)
|
||||
- ✅ New endpoints (safe)
|
||||
|
||||
### Step 6: Impact Analysis
|
||||
|
||||
Use `Explore` agent to find affected files:
|
||||
- Search for imports from `@generated/swagger/[api-name]`
|
||||
- List data-access services using changed APIs
|
||||
- Estimate refactoring scope
|
||||
|
||||
### Step 7: Validate
|
||||
|
||||
```bash
|
||||
# TypeScript compilation
|
||||
npx tsc --noEmit
|
||||
|
||||
# Run affected tests
|
||||
npx nx affected:test --skip-nx-cache
|
||||
|
||||
# Lint affected
|
||||
npx nx affected:lint
|
||||
```
|
||||
|
||||
### Step 8: Generate Report
|
||||
|
||||
```
|
||||
Swagger Sync Complete
|
||||
=====================
|
||||
|
||||
APIs Regenerated: [all | specific]
|
||||
Files Changed: XX
|
||||
Breaking Changes: XX
|
||||
|
||||
🔴 Breaking Changes
|
||||
-------------------
|
||||
- [API]: [Property removed/type changed]
|
||||
- Affected files: [list]
|
||||
|
||||
✅ Compatible Changes
|
||||
---------------------
|
||||
- [API]: [New properties/endpoints]
|
||||
|
||||
📊 Validation
|
||||
-------------
|
||||
TypeScript: ✅/❌
|
||||
Tests: XX/XX passing
|
||||
Lint: ✅/❌
|
||||
|
||||
💡 Next Steps
|
||||
-------------
|
||||
[Fix breaking changes / Deploy]
|
||||
```
|
||||
|
||||
## Error Handling
|
||||
|
||||
**Generation fails**: Check OpenAPI spec URLs in package.json
|
||||
|
||||
**Unicode cleanup fails**: Run `node tools/fix-files.js` manually
|
||||
|
||||
**TypeScript errors**: Review breaking changes, update affected services
|
||||
|
||||
## References
|
||||
|
||||
- CLAUDE.md API Integration section
|
||||
- package.json swagger generation scripts
|
||||
@@ -9,29 +9,6 @@ description: This skill should be used when working with Tailwind CSS styling in
|
||||
|
||||
Assist with applying the ISA-specific Tailwind CSS design system throughout the ISA-Frontend Angular monorepo. This skill provides comprehensive knowledge of custom utilities, color palettes, typography classes, button variants, and layout patterns specific to this project.
|
||||
|
||||
## When to Use This Skill
|
||||
|
||||
Invoke this skill when:
|
||||
- **After** checking `libs/ui/**` for existing components (always check first!)
|
||||
- Styling layout and spacing for components
|
||||
- Choosing appropriate color values for custom elements
|
||||
- Applying typography classes to text content
|
||||
- Determining spacing, layout, or responsive breakpoints
|
||||
- Customizing or extending existing UI components
|
||||
- Ensuring design system consistency
|
||||
- Questions about which Tailwind utility classes are available
|
||||
|
||||
**Important**: This skill provides Tailwind utilities. Always prefer using components from `@isa/ui/*` libraries before applying custom Tailwind styles.
|
||||
|
||||
**Works together with:**
|
||||
- **[angular-template](../angular-template/SKILL.md)** - Angular template syntax, control flow (@if, @for, @defer), and binding patterns
|
||||
- **[html-template](../html-template/SKILL.md)** - E2E testing attributes (`data-what`, `data-which`) and ARIA accessibility
|
||||
|
||||
When building Angular components, these three skills work together:
|
||||
1. Use **angular-template** for Angular syntax and control flow
|
||||
2. Use **html-template** for `data-*` and ARIA attributes
|
||||
3. Use **tailwind** (this skill) for styling with the ISA design system
|
||||
|
||||
## Core Design System Principles
|
||||
|
||||
### 0. Component Libraries First (Most Important)
|
||||
|
||||
472
.claude/skills/template-standards/SKILL.md
Normal file
472
.claude/skills/template-standards/SKILL.md
Normal file
@@ -0,0 +1,472 @@
|
||||
---
|
||||
name: template-standards
|
||||
description: This skill should be used when writing or reviewing Angular component templates. It provides comprehensive guidance on modern Angular 20+ template syntax (control flow, defer, projection, bindings), E2E testing attributes (data-what, data-which), and ARIA accessibility attributes. Use when creating components, refactoring to modern syntax, implementing lazy loading, adding testing attributes, ensuring accessibility compliance, or reviewing template best practices.
|
||||
---
|
||||
|
||||
# Template Standards
|
||||
|
||||
Comprehensive guide for Angular templates covering modern syntax, E2E testing attributes, and ARIA accessibility.
|
||||
|
||||
## Overview
|
||||
|
||||
This skill combines three essential aspects of Angular template development:
|
||||
|
||||
1. **Modern Angular Syntax** - Control flow (@if, @for, @switch), lazy loading (@defer), content projection, variable declarations, and bindings
|
||||
2. **E2E Testing Attributes** - Stable selectors (data-what, data-which) for automated testing
|
||||
3. **ARIA Accessibility** - Semantic roles, properties, and states for assistive technologies
|
||||
|
||||
**Every interactive element MUST include both E2E and ARIA attributes.**
|
||||
|
||||
**Related Skills:**
|
||||
- **tailwind** - ISA design system styling (colors, typography, spacing, layout)
|
||||
- **logging** - MANDATORY logging in all Angular files using `@isa/core/logging`
|
||||
|
||||
## Part 1: Angular Template Syntax
|
||||
|
||||
### Control Flow (Angular 17+)
|
||||
|
||||
#### @if / @else if / @else
|
||||
|
||||
```typescript
|
||||
@if (user.isAdmin()) {
|
||||
<app-admin-dashboard />
|
||||
} @else if (user.isEditor()) {
|
||||
<app-editor-dashboard />
|
||||
} @else {
|
||||
<app-viewer-dashboard />
|
||||
}
|
||||
|
||||
// Store result with 'as'
|
||||
@if (user.profile?.settings; as settings) {
|
||||
<p>Theme: {{settings.theme}}</p>
|
||||
}
|
||||
```
|
||||
|
||||
#### @for with @empty
|
||||
|
||||
```typescript
|
||||
@for (product of products(); track product.id) {
|
||||
<app-product-card [product]="product" />
|
||||
} @empty {
|
||||
<p>No products available</p>
|
||||
}
|
||||
```
|
||||
|
||||
**CRITICAL:** Always provide `track` expression:
|
||||
- Best: `track item.id` or `track item.uuid`
|
||||
- Static lists: `track $index`
|
||||
- **NEVER:** `track identity(item)` (causes full re-render)
|
||||
|
||||
**Contextual variables:** `$count`, `$index`, `$first`, `$last`, `$even`, `$odd`
|
||||
|
||||
#### @switch
|
||||
|
||||
```typescript
|
||||
@switch (viewMode()) {
|
||||
@case ('grid') { <app-grid-view /> }
|
||||
@case ('list') { <app-list-view /> }
|
||||
@default { <app-grid-view /> }
|
||||
}
|
||||
```
|
||||
|
||||
**See [control-flow-reference.md](references/control-flow-reference.md) for advanced patterns including nested loops, complex conditions, and filter strategies.**
|
||||
|
||||
### @defer Lazy Loading
|
||||
|
||||
#### Basic Usage
|
||||
|
||||
```typescript
|
||||
@defer (on viewport) {
|
||||
<app-heavy-chart />
|
||||
} @placeholder (minimum 500ms) {
|
||||
<div class="skeleton"></div>
|
||||
} @loading (after 100ms; minimum 1s) {
|
||||
<mat-spinner />
|
||||
} @error {
|
||||
<p>Failed to load</p>
|
||||
}
|
||||
```
|
||||
|
||||
#### Common Triggers
|
||||
|
||||
| Trigger | Use Case |
|
||||
|---------|----------|
|
||||
| `idle` (default) | Non-critical features |
|
||||
| `viewport` | Below-the-fold content |
|
||||
| `interaction` | User-initiated (click/keydown) |
|
||||
| `hover` | Tooltips/popovers |
|
||||
| `timer(Xs)` | Delayed content |
|
||||
| `when(expr)` | Custom condition |
|
||||
|
||||
**Multiple triggers:** `@defer (on interaction; on timer(5s))`
|
||||
**Prefetching:** `@defer (on interaction; prefetch on idle)`
|
||||
|
||||
#### Critical Requirements
|
||||
|
||||
- Components **MUST be standalone**
|
||||
- No `@ViewChild`/`@ContentChild` references
|
||||
- Reserve space in `@placeholder` to prevent layout shift
|
||||
- Never defer above-the-fold content (harms LCP)
|
||||
- Avoid `immediate`/`timer` during initial render (harms TTI)
|
||||
|
||||
**See [defer-patterns.md](references/defer-patterns.md) for performance optimization, Core Web Vitals impact, bundle size reduction strategies, and real-world examples.**
|
||||
|
||||
### Content Projection
|
||||
|
||||
#### Single Slot
|
||||
|
||||
```typescript
|
||||
@Component({
|
||||
selector: 'ui-card',
|
||||
template: `<div class="card"><ng-content></ng-content></div>`
|
||||
})
|
||||
```
|
||||
|
||||
#### Multi-Slot with Selectors
|
||||
|
||||
```typescript
|
||||
@Component({
|
||||
template: `
|
||||
<header><ng-content select="card-header"></ng-content></header>
|
||||
<main><ng-content select="card-body"></ng-content></main>
|
||||
<footer><ng-content></ng-content></footer> <!-- default slot -->
|
||||
`
|
||||
})
|
||||
```
|
||||
|
||||
**Usage:**
|
||||
```html
|
||||
<ui-card>
|
||||
<card-header><h3>Title</h3></card-header>
|
||||
<card-body><p>Content</p></card-body>
|
||||
<button>Action</button> <!-- goes to default slot -->
|
||||
</ui-card>
|
||||
```
|
||||
|
||||
**CRITICAL Constraint:** `ng-content` **always instantiates** (even if hidden). For conditional projection, use `ng-template` + `NgTemplateOutlet`.
|
||||
|
||||
**See [projection-patterns.md](references/projection-patterns.md) for conditional projection, template-based projection, querying projected content, and modal/form field examples.**
|
||||
|
||||
### Template References
|
||||
|
||||
#### ng-template
|
||||
|
||||
```html
|
||||
<ng-template #userCard let-user="userData" let-index="i">
|
||||
<div class="user">#{{index}}: {{user.name}}</div>
|
||||
</ng-template>
|
||||
|
||||
<ng-container
|
||||
*ngTemplateOutlet="userCard; context: {userData: currentUser(), i: 0}">
|
||||
</ng-container>
|
||||
```
|
||||
|
||||
**Access in component:**
|
||||
```typescript
|
||||
myTemplate = viewChild<TemplateRef<unknown>>('myTemplate');
|
||||
```
|
||||
|
||||
#### ng-container
|
||||
|
||||
Groups elements without DOM footprint:
|
||||
|
||||
```html
|
||||
<p>
|
||||
Hero's name is
|
||||
<ng-container @if="hero()">{{hero().name}}</ng-container>.
|
||||
</p>
|
||||
```
|
||||
|
||||
**See [template-reference.md](references/template-reference.md) for programmatic rendering, ViewContainerRef patterns, context scoping, and common pitfalls.**
|
||||
|
||||
### Variables
|
||||
|
||||
#### @let (Angular 18.1+)
|
||||
|
||||
```typescript
|
||||
@let userName = user().name;
|
||||
@let greeting = 'Hello, ' + userName;
|
||||
@let asyncData = data$ | async;
|
||||
|
||||
<h1>{{greeting}}</h1>
|
||||
```
|
||||
|
||||
**Scoped to current view** (not hoisted to parent/sibling).
|
||||
|
||||
#### Template References (#)
|
||||
|
||||
```html
|
||||
<input #emailInput type="email" />
|
||||
<button (click)="sendEmail(emailInput.value)">Send</button>
|
||||
|
||||
<app-datepicker #startDate />
|
||||
<button (click)="startDate.open()">Open</button>
|
||||
```
|
||||
|
||||
### Binding Patterns
|
||||
|
||||
**Property:** `[disabled]="!isValid()"`
|
||||
**Attribute:** `[attr.aria-label]="label()"` `[attr.data-what]="'card'"`
|
||||
**Event:** `(click)="save()"` `(input)="onInput($event)"`
|
||||
**Two-way:** `[(ngModel)]="userName"`
|
||||
**Class:** `[class.active]="isActive()"` or `[class]="{active: isActive()}"`
|
||||
**Style:** `[style.width.px]="width()"` or `[style]="{color: textColor()}"`
|
||||
|
||||
## Part 2: E2E Testing Attributes
|
||||
|
||||
### Purpose
|
||||
|
||||
Enable automated end-to-end testing by providing stable selectors for QA automation:
|
||||
- **`data-what`**: Semantic description of element's purpose (e.g., `submit-button`, `email-input`)
|
||||
- **`data-which`**: Unique identifier for specific instances (e.g., `registration-form`, `customer-123`)
|
||||
- **`data-*`**: Additional contextual information (e.g., `data-status="active"`)
|
||||
|
||||
### Naming Conventions
|
||||
|
||||
**`data-what` patterns:**
|
||||
- `*-button` (submit-button, cancel-button, delete-button)
|
||||
- `*-input` (email-input, search-input, quantity-input)
|
||||
- `*-link` (product-link, order-link, customer-link)
|
||||
- `*-item` (list-item, menu-item, card-item)
|
||||
- `*-dialog` (confirm-dialog, error-dialog, info-dialog)
|
||||
- `*-dropdown` (status-dropdown, category-dropdown)
|
||||
|
||||
**`data-which` guidelines:**
|
||||
- Use unique identifiers: `data-which="primary"`, `data-which="customer-list"`
|
||||
- Bind dynamically for lists: `[attr.data-which]="item.id"`
|
||||
- Combine with context: `data-which="customer-{{ customerId }}-edit"`
|
||||
|
||||
### Best Practices
|
||||
|
||||
1. ✅ Add to ALL interactive elements
|
||||
2. ✅ Use kebab-case for `data-what` values
|
||||
3. ✅ Ensure `data-which` is unique within the view
|
||||
4. ✅ Use Angular binding for dynamic values: `[attr.data-*]`
|
||||
5. ✅ Avoid including sensitive data in attributes
|
||||
|
||||
**See [e2e-attributes.md](references/e2e-attributes.md) for complete patterns by element type (buttons, inputs, links, lists, tables, dialogs), dynamic attribute bindings, testing integration examples, and validation strategies.**
|
||||
|
||||
## Part 3: ARIA Accessibility Attributes
|
||||
|
||||
### Purpose
|
||||
|
||||
Ensure web applications are accessible to all users, including those using assistive technologies:
|
||||
- **Roles**: Define element purpose (button, navigation, dialog, etc.)
|
||||
- **Properties**: Provide additional context (aria-label, aria-describedby)
|
||||
- **States**: Indicate dynamic states (aria-expanded, aria-disabled)
|
||||
- **Live Regions**: Announce dynamic content changes
|
||||
|
||||
### Role Patterns
|
||||
|
||||
- Interactive elements: `button`, `link`, `menuitem`
|
||||
- Structural: `navigation`, `main`, `complementary`, `contentinfo`
|
||||
- Widget: `dialog`, `alertdialog`, `tooltip`, `tablist`, `tab`
|
||||
- Landmark: `banner`, `search`, `form`, `region`
|
||||
|
||||
### Best Practices
|
||||
|
||||
1. ✅ Use semantic HTML first (use `<button>` instead of `<div role="button">`)
|
||||
2. ✅ Provide text alternatives for all interactive elements
|
||||
3. ✅ Ensure proper keyboard navigation (tabindex, focus management)
|
||||
4. ✅ Use `aria-label` when visual label is missing
|
||||
5. ✅ Use `aria-labelledby` to reference existing visible labels
|
||||
6. ✅ Keep ARIA attributes in sync with visual states
|
||||
7. ✅ Test with screen readers (NVDA, JAWS, VoiceOver)
|
||||
|
||||
**See [aria-attributes.md](references/aria-attributes.md) for comprehensive role reference, property and state attributes, live regions, keyboard navigation patterns, WCAG compliance requirements, and testing strategies.**
|
||||
|
||||
## Part 4: Combined Examples
|
||||
|
||||
### Button with All Attributes
|
||||
|
||||
```html
|
||||
<button
|
||||
type="button"
|
||||
(click)="onSubmit()"
|
||||
data-what="submit-button"
|
||||
data-which="registration-form"
|
||||
aria-label="Submit registration form"
|
||||
[attr.aria-disabled]="!isValid()">
|
||||
Submit
|
||||
</button>
|
||||
```
|
||||
|
||||
### Input with All Attributes
|
||||
|
||||
```html
|
||||
<input
|
||||
type="text"
|
||||
[(ngModel)]="email"
|
||||
data-what="email-input"
|
||||
data-which="registration-form"
|
||||
aria-label="Email address"
|
||||
aria-describedby="email-hint"
|
||||
aria-required="true"
|
||||
[attr.aria-invalid]="emailError ? 'true' : null" />
|
||||
<span id="email-hint">We'll never share your email</span>
|
||||
```
|
||||
|
||||
### Dynamic List with All Attributes
|
||||
|
||||
```typescript
|
||||
@for (item of items(); track item.id) {
|
||||
<li
|
||||
(click)="selectItem(item)"
|
||||
data-what="list-item"
|
||||
[attr.data-which]="item.id"
|
||||
[attr.data-status]="item.status"
|
||||
[attr.aria-label]="'Select ' + item.name"
|
||||
role="button"
|
||||
tabindex="0"
|
||||
(keydown.enter)="selectItem(item)"
|
||||
(keydown.space)="selectItem(item)">
|
||||
{{ item.name }}
|
||||
</li>
|
||||
}
|
||||
```
|
||||
|
||||
### Dialog with All Attributes
|
||||
|
||||
```html
|
||||
<div
|
||||
class="dialog"
|
||||
data-what="confirmation-dialog"
|
||||
data-which="delete-item"
|
||||
role="dialog"
|
||||
aria-modal="true"
|
||||
aria-labelledby="dialog-title"
|
||||
aria-describedby="dialog-description">
|
||||
|
||||
<h2 id="dialog-title">Confirm Deletion</h2>
|
||||
<p id="dialog-description">Are you sure you want to delete this item?</p>
|
||||
|
||||
<button
|
||||
(click)="confirm()"
|
||||
data-what="confirm-button"
|
||||
data-which="delete-dialog"
|
||||
aria-label="Confirm deletion">
|
||||
Delete
|
||||
</button>
|
||||
|
||||
<button
|
||||
(click)="cancel()"
|
||||
data-what="cancel-button"
|
||||
data-which="delete-dialog"
|
||||
aria-label="Cancel deletion">
|
||||
Cancel
|
||||
</button>
|
||||
</div>
|
||||
```
|
||||
|
||||
**See [combined-patterns.md](references/combined-patterns.md) for complete form examples, product listings, shopping carts, modal dialogs, navigation patterns, data tables, search interfaces, notifications, and multi-step forms with all attributes properly applied.**
|
||||
|
||||
## Validation Checklist
|
||||
|
||||
Before considering template complete:
|
||||
|
||||
### Angular Syntax
|
||||
- [ ] Using modern control flow (@if, @for, @switch) instead of *ngIf/*ngFor/*ngSwitch
|
||||
- [ ] All @for loops have proper `track` expressions (prefer item.id over $index)
|
||||
- [ ] Complex expressions moved to `computed()` in component
|
||||
- [ ] @defer used for below-the-fold or heavy components
|
||||
- [ ] Content projection using ng-content with proper selectors (if applicable)
|
||||
- [ ] Using @let for template-scoped variables (Angular 18.1+)
|
||||
|
||||
### E2E Attributes
|
||||
- [ ] All buttons have `data-what` and `data-which`
|
||||
- [ ] All inputs have `data-what` and `data-which`
|
||||
- [ ] All links have `data-what` and `data-which`
|
||||
- [ ] Dynamic lists use `[attr.data-*]` bindings with unique identifiers
|
||||
- [ ] No duplicate `data-which` values within the same view
|
||||
|
||||
### ARIA Accessibility
|
||||
- [ ] All interactive elements have appropriate ARIA labels
|
||||
- [ ] Proper roles assigned (button, navigation, dialog, etc.)
|
||||
- [ ] Form fields associated with labels (id/for or aria-labelledby)
|
||||
- [ ] Error messages use `role="alert"` and `aria-live="polite"`
|
||||
- [ ] Dialogs have `role="dialog"`, `aria-modal`, and label relationships
|
||||
- [ ] Dynamic state changes reflected in ARIA attributes
|
||||
- [ ] Keyboard accessibility (tabindex, enter/space handlers where needed)
|
||||
|
||||
### Combined Standards
|
||||
- [ ] Every interactive element has BOTH E2E and ARIA attributes
|
||||
- [ ] Attributes organized logically (Angular directives → data-* → aria-*)
|
||||
- [ ] Dynamic bindings use `[attr.*]` syntax correctly
|
||||
- [ ] No accessibility violations (semantic HTML preferred over ARIA)
|
||||
|
||||
## Migration Guide
|
||||
|
||||
### From Legacy Angular Syntax
|
||||
|
||||
| Legacy | Modern |
|
||||
|--------|--------|
|
||||
| `*ngIf="condition"` | `@if (condition) { }` |
|
||||
| `*ngFor="let item of items"` | `@for (item of items; track item.id) { }` |
|
||||
| `[ngSwitch]` | `@switch (value) { @case ('a') { } }` |
|
||||
|
||||
**CLI migration:** `ng generate @angular/core:control-flow`
|
||||
|
||||
### Adding E2E and ARIA to Existing Templates
|
||||
|
||||
1. **Identify interactive elements**: buttons, inputs, links, clickable divs
|
||||
2. **Add E2E attributes**: `data-what` (semantic type), `data-which` (unique identifier)
|
||||
3. **Add ARIA attributes**: appropriate role, label, and state attributes
|
||||
4. **Test**: verify selectors work in E2E tests, validate with screen readers
|
||||
|
||||
## Reference Files
|
||||
|
||||
For detailed examples and advanced patterns, see:
|
||||
|
||||
### Angular Syntax References
|
||||
- `references/control-flow-reference.md` - Advanced @if/@for/@switch patterns, nested loops, filtering
|
||||
- `references/defer-patterns.md` - Lazy loading strategies, Core Web Vitals, performance optimization
|
||||
- `references/projection-patterns.md` - Advanced ng-content, conditional projection, template-based patterns
|
||||
- `references/template-reference.md` - ng-template/ng-container, programmatic rendering, ViewContainerRef
|
||||
|
||||
### E2E Testing References
|
||||
- `references/e2e-attributes.md` - Complete E2E attribute patterns, naming conventions, testing integration
|
||||
|
||||
### ARIA Accessibility References
|
||||
- `references/aria-attributes.md` - Comprehensive ARIA guidance, roles, properties, states, WCAG compliance
|
||||
|
||||
### Combined References
|
||||
- `references/combined-patterns.md` - Real-world examples with Angular + E2E + ARIA integrated
|
||||
|
||||
Search with: `grep -r "pattern" references/`
|
||||
|
||||
## Quick Reference Summary
|
||||
|
||||
**Every interactive element needs:**
|
||||
|
||||
1. **Angular binding** (event, property, attribute)
|
||||
2. **`data-what`** (semantic type: submit-button, email-input)
|
||||
3. **`data-which`** (unique identifier: registration-form, user-123)
|
||||
4. **ARIA attribute** (role, aria-label, aria-describedby)
|
||||
|
||||
**Template Pattern:**
|
||||
```html
|
||||
<button
|
||||
(click)="action()"
|
||||
data-what="[type]-button"
|
||||
data-which="[context]"
|
||||
aria-label="[descriptive label]">
|
||||
Text
|
||||
</button>
|
||||
```
|
||||
|
||||
**Dynamic Pattern:**
|
||||
```typescript
|
||||
@for (item of items(); track item.id) {
|
||||
<div
|
||||
(click)="select(item)"
|
||||
data-what="item-card"
|
||||
[attr.data-which]="item.id"
|
||||
[attr.aria-label]="'Select ' + item.name">
|
||||
{{ item.name }}
|
||||
</div>
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
**This skill combines Angular template syntax, E2E testing attributes, and ARIA accessibility into a unified standard. Apply all three aspects to every template.**
|
||||
@@ -1,344 +0,0 @@
|
||||
---
|
||||
name: test-migration-specialist
|
||||
description: This skill should be used when migrating Angular libraries from Jest + Spectator to Vitest + Angular Testing Utilities. It handles test configuration updates, test file refactoring, mock pattern conversion, and validation. Use this skill when the user requests test framework migration, specifically for the 40 remaining Jest-based libraries in the ISA-Frontend monorepo.
|
||||
---
|
||||
|
||||
# Test Migration Specialist
|
||||
|
||||
## Overview
|
||||
|
||||
Automate the migration of Angular library tests from Jest + Spectator to Vitest + Angular Testing Utilities. This skill handles the complete migration workflow including configuration updates, test file refactoring, dependency management, and validation.
|
||||
|
||||
**Current Migration Status**: 40 libraries use Jest (65.6%), 21 libraries use Vitest (34.4%)
|
||||
|
||||
## When to Use This Skill
|
||||
|
||||
Invoke this skill when:
|
||||
- User requests test migration for a specific library
|
||||
- User mentions "migrate tests" or "Jest to Vitest"
|
||||
- User wants to update test framework for a library
|
||||
- User references the 40 remaining libraries to migrate
|
||||
|
||||
## Migration Workflow
|
||||
|
||||
### Step 1: Pre-Migration Analysis
|
||||
|
||||
Before making any changes, analyze the current state:
|
||||
|
||||
1. **Read Testing Guidelines**
|
||||
- Use `docs-researcher` agent to read `docs/guidelines/testing.md`
|
||||
- Understand migration patterns and best practices
|
||||
- Note JUnit and Cobertura configuration requirements
|
||||
|
||||
2. **Analyze Library Structure**
|
||||
- Read `libs/[path]/project.json` to identify current test executor
|
||||
- Count test files using Glob: `**/*.spec.ts`
|
||||
- Scan for Spectator usage patterns using Grep: `createComponentFactory|createServiceFactory|Spectator`
|
||||
- Identify complex mocking scenarios (ng-mocks, jest.mock patterns)
|
||||
|
||||
3. **Determine Library Depth**
|
||||
- Calculate directory levels from workspace root
|
||||
- This affects relative paths in vite.config.mts (../../../ vs ../../../../)
|
||||
|
||||
### Step 2: Update Test Configuration
|
||||
|
||||
Update the library's test configuration to use Vitest:
|
||||
|
||||
1. **Update project.json**
|
||||
Replace Jest executor with Vitest:
|
||||
```json
|
||||
{
|
||||
"test": {
|
||||
"executor": "@nx/vite:test",
|
||||
"options": {
|
||||
"configFile": "vite.config.mts"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
2. **Create vite.config.mts**
|
||||
Create configuration with JUnit and Cobertura reporters:
|
||||
```typescript
|
||||
/// <reference types='vitest' />
|
||||
import { defineConfig } from 'vite';
|
||||
import angular from '@analogjs/vite-plugin-angular';
|
||||
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
|
||||
import { nxCopyAssetsPlugin } from '@nx/vite/plugins/nx-copy-assets.plugin';
|
||||
|
||||
export default
|
||||
// @ts-expect-error - Vitest reporter tuple types have complex inference issues
|
||||
defineConfig(() => ({
|
||||
root: __dirname,
|
||||
cacheDir: '../../../node_modules/.vite/libs/[path]',
|
||||
plugins: [angular(), nxViteTsPaths(), nxCopyAssetsPlugin(['*.md'])],
|
||||
test: {
|
||||
watch: false,
|
||||
globals: true,
|
||||
environment: 'jsdom',
|
||||
include: ['{src,tests}/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
|
||||
setupFiles: ['src/test-setup.ts'],
|
||||
reporters: [
|
||||
'default',
|
||||
['junit', { outputFile: '../../../testresults/junit-[library-name].xml' }],
|
||||
],
|
||||
coverage: {
|
||||
reportsDirectory: '../../../coverage/libs/[path]',
|
||||
provider: 'v8' as const,
|
||||
reporter: ['text', 'cobertura'],
|
||||
},
|
||||
},
|
||||
}));
|
||||
```
|
||||
|
||||
**Critical**: Adjust `../../../` depth based on library location
|
||||
|
||||
### Step 3: Migrate Test Files
|
||||
|
||||
For each `.spec.ts` file, perform these conversions:
|
||||
|
||||
1. **Update Imports**
|
||||
```typescript
|
||||
// REMOVE
|
||||
import { createComponentFactory, Spectator } from '@ngneat/spectator/jest';
|
||||
|
||||
// ADD
|
||||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
import { describe, it, expect, beforeEach } from 'vitest';
|
||||
```
|
||||
|
||||
2. **Convert Component Tests**
|
||||
```typescript
|
||||
// OLD (Spectator)
|
||||
const createComponent = createComponentFactory({
|
||||
component: MyComponent,
|
||||
imports: [CommonModule],
|
||||
mocks: [MyService]
|
||||
});
|
||||
|
||||
let spectator: Spectator<MyComponent>;
|
||||
beforeEach(() => spectator = createComponent());
|
||||
|
||||
it('should display title', () => {
|
||||
spectator.setInput('title', 'Test');
|
||||
expect(spectator.query('h1')).toHaveText('Test');
|
||||
});
|
||||
|
||||
// NEW (Angular Testing Utilities)
|
||||
describe('MyComponent', () => {
|
||||
let fixture: ComponentFixture<MyComponent>;
|
||||
let component: MyComponent;
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [MyComponent, CommonModule],
|
||||
providers: [{ provide: MyService, useValue: mockService }]
|
||||
}).compileComponents();
|
||||
|
||||
fixture = TestBed.createComponent(MyComponent);
|
||||
component = fixture.componentInstance;
|
||||
});
|
||||
|
||||
it('should display title', () => {
|
||||
component.title = 'Test';
|
||||
fixture.detectChanges();
|
||||
expect(fixture.nativeElement.querySelector('h1').textContent).toContain('Test');
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
3. **Convert Service Tests**
|
||||
```typescript
|
||||
// OLD (Spectator)
|
||||
const createService = createServiceFactory({
|
||||
service: MyService,
|
||||
mocks: [HttpClient]
|
||||
});
|
||||
|
||||
// NEW (Angular Testing Utilities)
|
||||
describe('MyService', () => {
|
||||
let service: MyService;
|
||||
let httpMock: HttpTestingController;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [HttpClientTestingModule],
|
||||
providers: [MyService]
|
||||
});
|
||||
|
||||
service = TestBed.inject(MyService);
|
||||
httpMock = TestBed.inject(HttpTestingController);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
httpMock.verify();
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
4. **Update Mock Patterns**
|
||||
- Replace `jest.fn()` → `vi.fn()`
|
||||
- Replace `jest.spyOn()` → `vi.spyOn()`
|
||||
- Replace `jest.mock()` → `vi.mock()`
|
||||
- For complex mocks, use `ng-mocks` library if needed
|
||||
|
||||
5. **Update Matchers**
|
||||
- Replace Spectator matchers (`toHaveText`, `toExist`) with standard Jest/Vitest matchers
|
||||
- Use `expect().toBeTruthy()`, `expect().toContain()`, etc.
|
||||
|
||||
### Step 4: Verify E2E Attributes
|
||||
|
||||
Check component templates for E2E testing attributes:
|
||||
|
||||
1. **Scan Templates**
|
||||
Use Grep to find templates: `**/*.html`
|
||||
|
||||
2. **Validate Attributes**
|
||||
Ensure interactive elements have:
|
||||
- `data-what`: Semantic description (e.g., "submit-button")
|
||||
- `data-which`: Unique identifier (e.g., "form-primary")
|
||||
- Dynamic `data-*` for list items: `[attr.data-item-id]="item.id"`
|
||||
|
||||
3. **Add Missing Attributes**
|
||||
If missing, add them to components. See `dev:add-e2e-attrs` command or use that skill.
|
||||
|
||||
### Step 5: Run Tests and Validate
|
||||
|
||||
Execute tests to verify migration:
|
||||
|
||||
1. **Run Tests**
|
||||
```bash
|
||||
npx nx test [library-name] --skip-nx-cache
|
||||
```
|
||||
|
||||
2. **Run with Coverage**
|
||||
```bash
|
||||
npx nx test [library-name] --coverage.enabled=true --skip-nx-cache
|
||||
```
|
||||
|
||||
3. **Verify Output Files**
|
||||
Check that CI/CD integration files are created:
|
||||
- JUnit XML: `testresults/junit-[library-name].xml`
|
||||
- Cobertura XML: `coverage/libs/[path]/cobertura-coverage.xml`
|
||||
|
||||
4. **Address Failures**
|
||||
If tests fail:
|
||||
- Review test conversion (common issues: missing fixture.detectChanges(), incorrect selectors)
|
||||
- Check mock configurations
|
||||
- Verify imports are correct
|
||||
- Ensure async tests use proper patterns
|
||||
|
||||
### Step 6: Clean Up
|
||||
|
||||
Remove legacy configurations:
|
||||
|
||||
1. **Remove Jest Files**
|
||||
- Delete `jest.config.ts` or `jest.config.js` if present
|
||||
- Remove Jest-specific setup files
|
||||
|
||||
2. **Update Dependencies**
|
||||
- Note if Spectator can be removed (check if other libs still use it)
|
||||
- Note if Jest can be removed (check if other libs still use it)
|
||||
- Don't actually remove from package.json unless all libs migrated
|
||||
|
||||
3. **Update Documentation**
|
||||
Update library README.md with new test commands:
|
||||
```markdown
|
||||
## Testing
|
||||
|
||||
This library uses Vitest + Angular Testing Utilities.
|
||||
|
||||
```bash
|
||||
# Run tests
|
||||
npx nx test [library-name] --skip-nx-cache
|
||||
|
||||
# Run with coverage
|
||||
npx nx test [library-name] --coverage.enabled=true --skip-nx-cache
|
||||
```
|
||||
```
|
||||
|
||||
### Step 7: Generate Migration Report
|
||||
|
||||
Provide comprehensive migration summary:
|
||||
|
||||
```
|
||||
Test Migration Complete
|
||||
=======================
|
||||
|
||||
Library: [library-name]
|
||||
Framework: Jest + Spectator → Vitest + Angular Testing Utilities
|
||||
|
||||
📊 Migration Statistics
|
||||
-----------------------
|
||||
Test files migrated: XX
|
||||
Component tests: XX
|
||||
Service tests: XX
|
||||
Total test cases: XX
|
||||
|
||||
✅ Test Results
|
||||
---------------
|
||||
Passing: XX/XX (100%)
|
||||
Coverage: XX%
|
||||
|
||||
📝 Configuration
|
||||
----------------
|
||||
- project.json: ✅ Updated to @nx/vite:test
|
||||
- vite.config.mts: ✅ Created with JUnit + Cobertura
|
||||
- E2E attributes: ✅ Validated
|
||||
|
||||
📁 CI/CD Integration
|
||||
--------------------
|
||||
- JUnit XML: ✅ testresults/junit-[name].xml
|
||||
- Cobertura XML: ✅ coverage/libs/[path]/cobertura-coverage.xml
|
||||
|
||||
🧹 Cleanup
|
||||
----------
|
||||
- Jest config removed: ✅
|
||||
- README updated: ✅
|
||||
|
||||
💡 Next Steps
|
||||
-------------
|
||||
1. Verify tests in CI/CD pipeline
|
||||
2. Monitor for any edge cases
|
||||
3. Consider migrating related libraries
|
||||
|
||||
📚 Remaining Libraries
|
||||
----------------------
|
||||
Jest libraries remaining: XX/40
|
||||
Progress: XX% complete
|
||||
```
|
||||
|
||||
## Error Handling
|
||||
|
||||
### Common Migration Issues
|
||||
|
||||
**Issue 1: Tests fail after migration**
|
||||
- Check `fixture.detectChanges()` is called after setting inputs
|
||||
- Verify async tests use `async/await` properly
|
||||
- Check component imports are correct (standalone components)
|
||||
|
||||
**Issue 2: Mocks not working**
|
||||
- Verify `vi.fn()` syntax is correct
|
||||
- Check providers array in TestBed configuration
|
||||
- For complex mocks, consider using `ng-mocks`
|
||||
|
||||
**Issue 3: Coverage files not generated**
|
||||
- Verify path depth in vite.config.mts matches library location
|
||||
- Check reporters array includes `'cobertura'`
|
||||
- Ensure `provider: 'v8'` is set
|
||||
|
||||
**Issue 4: Type errors in vite.config.mts**
|
||||
- Add `// @ts-expect-error` comment before `defineConfig()`
|
||||
- This is expected due to Vitest reporter type complexity
|
||||
|
||||
## References
|
||||
|
||||
Use `docs-researcher` agent to access:
|
||||
- `docs/guidelines/testing.md` - Comprehensive migration guide with examples
|
||||
- `CLAUDE.md` - Testing Framework section for project conventions
|
||||
|
||||
**Key Documentation Sections:**
|
||||
- Vitest Configuration with JUnit and Cobertura
|
||||
- Angular Testing Utilities examples
|
||||
- Migration patterns and best practices
|
||||
- E2E attribute requirements
|
||||
@@ -1,346 +0,0 @@
|
||||
# Jest to Vitest Migration Patterns
|
||||
|
||||
## Overview
|
||||
|
||||
This reference provides syntax mappings and patterns for migrating tests from Jest (with Spectator) to Vitest (with Angular Testing Library).
|
||||
|
||||
## Configuration Migration
|
||||
|
||||
### Jest Config → Vitest Config
|
||||
|
||||
**Before (jest.config.ts):**
|
||||
```typescript
|
||||
export default {
|
||||
displayName: 'my-lib',
|
||||
preset: '../../jest.preset.js',
|
||||
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
|
||||
coverageDirectory: '../../coverage/libs/my-lib',
|
||||
transform: {
|
||||
'^.+\\.(ts|mjs|js|html)$': 'jest-preset-angular',
|
||||
},
|
||||
transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],
|
||||
snapshotSerializers: [
|
||||
'jest-preset-angular/build/serializers/no-ng-attributes',
|
||||
'jest-preset-angular/build/serializers/ng-snapshot',
|
||||
],
|
||||
};
|
||||
```
|
||||
|
||||
**After (vitest.config.ts):**
|
||||
```typescript
|
||||
import { defineConfig } from 'vitest/config';
|
||||
import angular from '@analogjs/vite-plugin-angular';
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [angular()],
|
||||
test: {
|
||||
globals: true,
|
||||
environment: 'jsdom',
|
||||
setupFiles: ['src/test-setup.ts'],
|
||||
include: ['**/*.spec.ts'],
|
||||
reporters: ['default'],
|
||||
coverage: {
|
||||
provider: 'v8',
|
||||
reporter: ['text', 'json', 'html'],
|
||||
},
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
### Test Setup Migration
|
||||
|
||||
**Before (test-setup.ts - Jest):**
|
||||
```typescript
|
||||
import 'jest-preset-angular/setup-jest';
|
||||
```
|
||||
|
||||
**After (test-setup.ts - Vitest):**
|
||||
```typescript
|
||||
import '@analogjs/vitest-angular/setup-zone';
|
||||
```
|
||||
|
||||
## Import Changes
|
||||
|
||||
### Test Function Imports
|
||||
|
||||
**Before (Jest):**
|
||||
```typescript
|
||||
import { describe, it, expect, beforeEach, afterEach } from '@jest/globals';
|
||||
```
|
||||
|
||||
**After (Vitest):**
|
||||
```typescript
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
||||
```
|
||||
|
||||
### Mock Imports
|
||||
|
||||
**Before (Jest):**
|
||||
```typescript
|
||||
jest.fn()
|
||||
jest.spyOn()
|
||||
jest.mock()
|
||||
jest.useFakeTimers()
|
||||
```
|
||||
|
||||
**After (Vitest):**
|
||||
```typescript
|
||||
vi.fn()
|
||||
vi.spyOn()
|
||||
vi.mock()
|
||||
vi.useFakeTimers()
|
||||
```
|
||||
|
||||
## Mock Migration Patterns
|
||||
|
||||
### Function Mocks
|
||||
|
||||
**Before (Jest):**
|
||||
```typescript
|
||||
const mockFn = jest.fn();
|
||||
const mockFnWithReturn = jest.fn().mockReturnValue('value');
|
||||
const mockFnWithAsync = jest.fn().mockResolvedValue('async value');
|
||||
```
|
||||
|
||||
**After (Vitest):**
|
||||
```typescript
|
||||
const mockFn = vi.fn();
|
||||
const mockFnWithReturn = vi.fn().mockReturnValue('value');
|
||||
const mockFnWithAsync = vi.fn().mockResolvedValue('async value');
|
||||
```
|
||||
|
||||
### Spy Migration
|
||||
|
||||
**Before (Jest):**
|
||||
```typescript
|
||||
const spy = jest.spyOn(service, 'method');
|
||||
spy.mockImplementation(() => 'mocked');
|
||||
```
|
||||
|
||||
**After (Vitest):**
|
||||
```typescript
|
||||
const spy = vi.spyOn(service, 'method');
|
||||
spy.mockImplementation(() => 'mocked');
|
||||
```
|
||||
|
||||
### Module Mocks
|
||||
|
||||
**Before (Jest):**
|
||||
```typescript
|
||||
jest.mock('@isa/core/logging', () => ({
|
||||
logger: jest.fn(() => ({
|
||||
info: jest.fn(),
|
||||
error: jest.fn(),
|
||||
})),
|
||||
}));
|
||||
```
|
||||
|
||||
**After (Vitest):**
|
||||
```typescript
|
||||
vi.mock('@isa/core/logging', () => ({
|
||||
logger: vi.fn(() => ({
|
||||
info: vi.fn(),
|
||||
error: vi.fn(),
|
||||
})),
|
||||
}));
|
||||
```
|
||||
|
||||
## Spectator → Angular Testing Library
|
||||
|
||||
### Component Testing
|
||||
|
||||
**Before (Spectator):**
|
||||
```typescript
|
||||
import { createComponentFactory, Spectator } from '@ngneat/spectator/jest';
|
||||
|
||||
describe('MyComponent', () => {
|
||||
let spectator: Spectator<MyComponent>;
|
||||
const createComponent = createComponentFactory({
|
||||
component: MyComponent,
|
||||
imports: [CommonModule],
|
||||
providers: [
|
||||
{ provide: MyService, useValue: mockService },
|
||||
],
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
spectator = createComponent();
|
||||
});
|
||||
|
||||
it('should render title', () => {
|
||||
expect(spectator.query('.title')).toHaveText('Hello');
|
||||
});
|
||||
|
||||
it('should handle click', () => {
|
||||
spectator.click('.button');
|
||||
expect(mockService.doSomething).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
**After (Angular Testing Library):**
|
||||
```typescript
|
||||
import { render, screen, fireEvent } from '@testing-library/angular';
|
||||
|
||||
describe('MyComponent', () => {
|
||||
it('should render title', async () => {
|
||||
await render(MyComponent, {
|
||||
imports: [CommonModule],
|
||||
providers: [
|
||||
{ provide: MyService, useValue: mockService },
|
||||
],
|
||||
});
|
||||
|
||||
expect(screen.getByText('Hello')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should handle click', async () => {
|
||||
await render(MyComponent, {
|
||||
providers: [{ provide: MyService, useValue: mockService }],
|
||||
});
|
||||
|
||||
fireEvent.click(screen.getByRole('button'));
|
||||
expect(mockService.doSomething).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
### Query Selectors
|
||||
|
||||
| Spectator | Angular Testing Library |
|
||||
|-----------|------------------------|
|
||||
| `spectator.query('.class')` | `screen.getByTestId()` or `screen.getByRole()` |
|
||||
| `spectator.queryAll('.class')` | `screen.getAllByRole()` |
|
||||
| `spectator.query('button')` | `screen.getByRole('button')` |
|
||||
| `spectator.query('[data-testid]')` | `screen.getByTestId()` |
|
||||
|
||||
### Events
|
||||
|
||||
| Spectator | Angular Testing Library |
|
||||
|-----------|------------------------|
|
||||
| `spectator.click(element)` | `fireEvent.click(element)` or `await userEvent.click(element)` |
|
||||
| `spectator.typeInElement(value, element)` | `await userEvent.type(element, value)` |
|
||||
| `spectator.blur(element)` | `fireEvent.blur(element)` |
|
||||
| `spectator.focus(element)` | `fireEvent.focus(element)` |
|
||||
|
||||
### Service Testing
|
||||
|
||||
**Before (Spectator):**
|
||||
```typescript
|
||||
import { createServiceFactory, SpectatorService } from '@ngneat/spectator/jest';
|
||||
|
||||
describe('MyService', () => {
|
||||
let spectator: SpectatorService<MyService>;
|
||||
const createService = createServiceFactory({
|
||||
service: MyService,
|
||||
providers: [
|
||||
{ provide: HttpClient, useValue: mockHttp },
|
||||
],
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
spectator = createService();
|
||||
});
|
||||
|
||||
it('should fetch data', () => {
|
||||
spectator.service.getData().subscribe(data => {
|
||||
expect(data).toEqual(expectedData);
|
||||
});
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
**After (TestBed):**
|
||||
```typescript
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
describe('MyService', () => {
|
||||
let service: MyService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
MyService,
|
||||
{ provide: HttpClient, useValue: mockHttp },
|
||||
],
|
||||
});
|
||||
service = TestBed.inject(MyService);
|
||||
});
|
||||
|
||||
it('should fetch data', () => {
|
||||
service.getData().subscribe(data => {
|
||||
expect(data).toEqual(expectedData);
|
||||
});
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
## Async Testing
|
||||
|
||||
### Observable Testing
|
||||
|
||||
**Before (Jest):**
|
||||
```typescript
|
||||
it('should emit values', (done) => {
|
||||
service.data$.subscribe({
|
||||
next: (value) => {
|
||||
expect(value).toBe(expected);
|
||||
done();
|
||||
},
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
**After (Vitest):**
|
||||
```typescript
|
||||
import { firstValueFrom } from 'rxjs';
|
||||
|
||||
it('should emit values', async () => {
|
||||
const value = await firstValueFrom(service.data$);
|
||||
expect(value).toBe(expected);
|
||||
});
|
||||
```
|
||||
|
||||
### Timer Mocks
|
||||
|
||||
**Before (Jest):**
|
||||
```typescript
|
||||
jest.useFakeTimers();
|
||||
service.startTimer();
|
||||
jest.advanceTimersByTime(1000);
|
||||
expect(callback).toHaveBeenCalled();
|
||||
jest.useRealTimers();
|
||||
```
|
||||
|
||||
**After (Vitest):**
|
||||
```typescript
|
||||
vi.useFakeTimers();
|
||||
service.startTimer();
|
||||
vi.advanceTimersByTime(1000);
|
||||
expect(callback).toHaveBeenCalled();
|
||||
vi.useRealTimers();
|
||||
```
|
||||
|
||||
## Common Matchers
|
||||
|
||||
| Jest | Vitest |
|
||||
|------|--------|
|
||||
| `expect(x).toBe(y)` | Same |
|
||||
| `expect(x).toEqual(y)` | Same |
|
||||
| `expect(x).toHaveBeenCalled()` | Same |
|
||||
| `expect(x).toHaveBeenCalledWith(y)` | Same |
|
||||
| `expect(x).toMatchSnapshot()` | `expect(x).toMatchSnapshot()` |
|
||||
| `expect(x).toHaveText('text')` | `expect(x).toHaveTextContent('text')` (with jest-dom) |
|
||||
|
||||
## Migration Checklist
|
||||
|
||||
1. [ ] Update `vitest.config.ts`
|
||||
2. [ ] Update `test-setup.ts`
|
||||
3. [ ] Replace `jest.fn()` with `vi.fn()`
|
||||
4. [ ] Replace `jest.spyOn()` with `vi.spyOn()`
|
||||
5. [ ] Replace `jest.mock()` with `vi.mock()`
|
||||
6. [ ] Replace Spectator with Angular Testing Library
|
||||
7. [ ] Update queries to use accessible selectors
|
||||
8. [ ] Update async patterns
|
||||
9. [ ] Run tests and fix any remaining issues
|
||||
10. [ ] Remove Jest dependencies from `package.json`
|
||||
192
.claude/skills/test-migration/SKILL.md
Normal file
192
.claude/skills/test-migration/SKILL.md
Normal file
@@ -0,0 +1,192 @@
|
||||
---
|
||||
name: test-migration
|
||||
description: Reference patterns for Jest to Vitest test migration. Contains syntax mappings for jest→vi, Spectator→Angular Testing Library, and common matcher conversions. Auto-loaded by migration-specialist agent.
|
||||
---
|
||||
|
||||
# Jest to Vitest Migration Patterns
|
||||
|
||||
Quick reference for test framework migration syntax.
|
||||
|
||||
## Import Changes
|
||||
|
||||
```typescript
|
||||
// REMOVE
|
||||
import { createComponentFactory, Spectator } from '@ngneat/spectator/jest';
|
||||
|
||||
// ADD
|
||||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
```
|
||||
|
||||
## Mock Migration
|
||||
|
||||
| Jest | Vitest |
|
||||
|------|--------|
|
||||
| `jest.fn()` | `vi.fn()` |
|
||||
| `jest.spyOn(obj, 'method')` | `vi.spyOn(obj, 'method')` |
|
||||
| `jest.mock('module')` | `vi.mock('module')` |
|
||||
| `jest.useFakeTimers()` | `vi.useFakeTimers()` |
|
||||
| `jest.advanceTimersByTime(ms)` | `vi.advanceTimersByTime(ms)` |
|
||||
| `jest.useRealTimers()` | `vi.useRealTimers()` |
|
||||
| `jest.clearAllMocks()` | `vi.clearAllMocks()` |
|
||||
| `jest.resetAllMocks()` | `vi.resetAllMocks()` |
|
||||
|
||||
## Spectator → TestBed
|
||||
|
||||
### Component Testing
|
||||
|
||||
```typescript
|
||||
// OLD (Spectator)
|
||||
const createComponent = createComponentFactory({
|
||||
component: MyComponent,
|
||||
imports: [CommonModule],
|
||||
mocks: [MyService]
|
||||
});
|
||||
let spectator: Spectator<MyComponent>;
|
||||
beforeEach(() => spectator = createComponent());
|
||||
|
||||
// NEW (TestBed)
|
||||
let fixture: ComponentFixture<MyComponent>;
|
||||
let component: MyComponent;
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [MyComponent],
|
||||
providers: [{ provide: MyService, useValue: mockService }]
|
||||
}).compileComponents();
|
||||
fixture = TestBed.createComponent(MyComponent);
|
||||
component = fixture.componentInstance;
|
||||
});
|
||||
```
|
||||
|
||||
### Service Testing
|
||||
|
||||
```typescript
|
||||
// OLD (Spectator)
|
||||
const createService = createServiceFactory({
|
||||
service: MyService,
|
||||
mocks: [HttpClient]
|
||||
});
|
||||
let spectator: SpectatorService<MyService>;
|
||||
beforeEach(() => spectator = createService());
|
||||
|
||||
// NEW (TestBed)
|
||||
let service: MyService;
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
MyService,
|
||||
{ provide: HttpClient, useValue: mockHttp }
|
||||
]
|
||||
});
|
||||
service = TestBed.inject(MyService);
|
||||
});
|
||||
```
|
||||
|
||||
## Query Selectors
|
||||
|
||||
| Spectator | Angular/Native |
|
||||
|-----------|---------------|
|
||||
| `spectator.query('.class')` | `fixture.nativeElement.querySelector('.class')` |
|
||||
| `spectator.queryAll('.class')` | `fixture.nativeElement.querySelectorAll('.class')` |
|
||||
| `spectator.query('button')` | `fixture.nativeElement.querySelector('button')` |
|
||||
|
||||
## Events
|
||||
|
||||
| Spectator | Native |
|
||||
|-----------|--------|
|
||||
| `spectator.click(element)` | `element.click()` + `fixture.detectChanges()` |
|
||||
| `spectator.typeInElement(value, el)` | `el.value = value; el.dispatchEvent(new Event('input'))` |
|
||||
| `spectator.blur(element)` | `element.dispatchEvent(new Event('blur'))` |
|
||||
|
||||
## Matchers
|
||||
|
||||
| Spectator | Standard |
|
||||
|-----------|----------|
|
||||
| `toHaveText('text')` | `expect(el.textContent).toContain('text')` |
|
||||
| `toExist()` | `toBeTruthy()` |
|
||||
| `toBeVisible()` | Check `!el.hidden && el.offsetParent !== null` |
|
||||
| `toHaveClass('class')` | `expect(el.classList.contains('class')).toBe(true)` |
|
||||
|
||||
## Async Patterns
|
||||
|
||||
```typescript
|
||||
// OLD (callback)
|
||||
it('should emit', (done) => {
|
||||
service.data$.subscribe(val => {
|
||||
expect(val).toBe(expected);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
// NEW (async/await)
|
||||
import { firstValueFrom } from 'rxjs';
|
||||
it('should emit', async () => {
|
||||
const val = await firstValueFrom(service.data$);
|
||||
expect(val).toBe(expected);
|
||||
});
|
||||
```
|
||||
|
||||
## Input/Output Testing
|
||||
|
||||
```typescript
|
||||
// Setting inputs (Angular 17.3+)
|
||||
fixture.componentRef.setInput('title', 'Test');
|
||||
fixture.detectChanges();
|
||||
|
||||
// Testing outputs
|
||||
const emitSpy = vi.fn();
|
||||
component.myOutput.subscribe(emitSpy);
|
||||
// trigger action...
|
||||
expect(emitSpy).toHaveBeenCalledWith(expectedValue);
|
||||
```
|
||||
|
||||
## Configuration Files
|
||||
|
||||
### vite.config.mts Template
|
||||
|
||||
```typescript
|
||||
/// <reference types='vitest' />
|
||||
import { defineConfig } from 'vite';
|
||||
import angular from '@analogjs/vite-plugin-angular';
|
||||
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
|
||||
import { nxCopyAssetsPlugin } from '@nx/vite/plugins/nx-copy-assets.plugin';
|
||||
|
||||
export default
|
||||
// @ts-expect-error - Vitest reporter tuple types have complex inference issues
|
||||
defineConfig(() => ({
|
||||
root: __dirname,
|
||||
cacheDir: '../../../node_modules/.vite/libs/[path]', // Adjust depth!
|
||||
plugins: [angular(), nxViteTsPaths(), nxCopyAssetsPlugin(['*.md'])],
|
||||
test: {
|
||||
watch: false,
|
||||
globals: true,
|
||||
environment: 'jsdom',
|
||||
include: ['{src,tests}/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
|
||||
setupFiles: ['src/test-setup.ts'],
|
||||
reporters: [
|
||||
'default',
|
||||
['junit', { outputFile: '../../../testresults/junit-[name].xml' }],
|
||||
],
|
||||
coverage: {
|
||||
reportsDirectory: '../../../coverage/libs/[path]',
|
||||
provider: 'v8' as const,
|
||||
reporter: ['text', 'cobertura'],
|
||||
},
|
||||
},
|
||||
}));
|
||||
```
|
||||
|
||||
### test-setup.ts
|
||||
|
||||
```typescript
|
||||
import '@analogjs/vitest-angular/setup-zone';
|
||||
```
|
||||
|
||||
## Path Depth Reference
|
||||
|
||||
| Library Location | Relative Path Prefix |
|
||||
|-----------------|---------------------|
|
||||
| `libs/feature/ui` | `../../` |
|
||||
| `libs/feature/data-access` | `../../` |
|
||||
| `libs/domain/feature/ui` | `../../../` |
|
||||
| `libs/domain/feature/data-access/store` | `../../../../` |
|
||||
@@ -1,199 +0,0 @@
|
||||
---
|
||||
name: type-safety-engineer
|
||||
description: This skill should be used when improving TypeScript type safety by removing `any` types, adding Zod schemas for runtime validation, creating type guards, and strengthening strictness. Use this skill when the user wants to enhance type safety, mentions "fix any types", "add Zod validation", or requests type improvements for better code quality.
|
||||
---
|
||||
|
||||
# Type Safety Engineer
|
||||
|
||||
## Overview
|
||||
|
||||
Enhance TypeScript type safety by eliminating `any` types, adding Zod schemas for runtime validation, creating type guards, and strengthening compiler strictness.
|
||||
|
||||
## When to Use This Skill
|
||||
|
||||
Invoke when user wants to:
|
||||
- Remove `any` types
|
||||
- Add runtime validation with Zod
|
||||
- Improve type safety
|
||||
- Mentioned "type safety" or "Zod schemas"
|
||||
|
||||
## Type Safety Workflow
|
||||
|
||||
### Step 1: Scan for Issues
|
||||
|
||||
```bash
|
||||
# Find explicit any
|
||||
grep -r ": any" libs/ --include="*.ts" | grep -v ".spec.ts"
|
||||
|
||||
# Find functions without return types
|
||||
grep -r "^.*function.*{$" libs/ --include="*.ts" | grep -v ": "
|
||||
|
||||
# TypeScript strict mode check
|
||||
npx tsc --noEmit --strict
|
||||
```
|
||||
|
||||
### Step 2: Categorize Issues
|
||||
|
||||
**🔴 Critical:**
|
||||
- `any` in API response handling
|
||||
- `any` in service methods
|
||||
- `any` in store state types
|
||||
|
||||
**⚠️ Important:**
|
||||
- Missing return types
|
||||
- Untyped parameters
|
||||
- Weak types (`object`, `Function`)
|
||||
|
||||
**ℹ️ Moderate:**
|
||||
- `any` in test files
|
||||
- Loose array types
|
||||
|
||||
### Step 3: Add Zod Schemas for API Responses
|
||||
|
||||
```typescript
|
||||
import { z } from 'zod';
|
||||
|
||||
// Define schema
|
||||
const OrderItemSchema = z.object({
|
||||
productId: z.string().uuid(),
|
||||
quantity: z.number().int().positive(),
|
||||
price: z.number().positive()
|
||||
});
|
||||
|
||||
const OrderResponseSchema = z.object({
|
||||
id: z.string().uuid(),
|
||||
status: z.enum(['pending', 'confirmed', 'shipped']),
|
||||
items: z.array(OrderItemSchema),
|
||||
createdAt: z.string().datetime()
|
||||
});
|
||||
|
||||
// Infer TypeScript type
|
||||
type OrderResponse = z.infer<typeof OrderResponseSchema>;
|
||||
|
||||
// Runtime validation
|
||||
const order = OrderResponseSchema.parse(apiResponse);
|
||||
```
|
||||
|
||||
### Step 4: Replace `any` with Specific Types
|
||||
|
||||
**Pattern 1: Unknown + Type Guards**
|
||||
```typescript
|
||||
// BEFORE
|
||||
function processData(data: any) {
|
||||
return data.value;
|
||||
}
|
||||
|
||||
// AFTER
|
||||
function processData(data: unknown): string {
|
||||
if (!isValidData(data)) {
|
||||
throw new Error('Invalid data');
|
||||
}
|
||||
return data.value;
|
||||
}
|
||||
|
||||
function isValidData(data: unknown): data is { value: string } {
|
||||
return typeof data === 'object' && data !== null && 'value' in data;
|
||||
}
|
||||
```
|
||||
|
||||
**Pattern 2: Generic Types**
|
||||
```typescript
|
||||
// BEFORE
|
||||
function findById(items: any[], id: string): any {
|
||||
return items.find(item => item.id === id);
|
||||
}
|
||||
|
||||
// AFTER
|
||||
function findById<T extends { id: string }>(items: T[], id: string): T | undefined {
|
||||
return items.find(item => item.id === id);
|
||||
}
|
||||
```
|
||||
|
||||
### Step 5: Add Type Guards for API Data
|
||||
|
||||
```typescript
|
||||
export function isOrderResponse(data: unknown): data is OrderResponse {
|
||||
try {
|
||||
OrderResponseSchema.parse(data);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Use in service
|
||||
getOrder(id: string): Observable<OrderResponse> {
|
||||
return this.http.get(`/api/orders/${id}`).pipe(
|
||||
map(response => {
|
||||
if (!isOrderResponse(response)) {
|
||||
throw new Error('Invalid API response');
|
||||
}
|
||||
return response;
|
||||
})
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
### Step 6: Validate Changes
|
||||
|
||||
```bash
|
||||
npx tsc --noEmit --strict
|
||||
npx nx affected:test --skip-nx-cache
|
||||
npx nx affected:lint
|
||||
```
|
||||
|
||||
### Step 7: Generate Report
|
||||
|
||||
```
|
||||
Type Safety Improvements
|
||||
========================
|
||||
|
||||
Path: [analyzed path]
|
||||
|
||||
🔍 Issues Found
|
||||
---------------
|
||||
`any` usages: XX → 0
|
||||
Missing return types: XX → 0
|
||||
Untyped parameters: XX → 0
|
||||
|
||||
✅ Improvements
|
||||
---------------
|
||||
- Added Zod schemas: XX
|
||||
- Created type guards: XX
|
||||
- Fixed `any` types: XX
|
||||
- Added return types: XX
|
||||
|
||||
📈 Type Safety Score
|
||||
--------------------
|
||||
Before: XX%
|
||||
After: XX% (+XX%)
|
||||
|
||||
💡 Recommendations
|
||||
------------------
|
||||
1. Enable stricter TypeScript options
|
||||
2. Add validation to remaining APIs
|
||||
```
|
||||
|
||||
## Common Patterns
|
||||
|
||||
**API Response Validation:**
|
||||
```typescript
|
||||
const schema = z.object({...});
|
||||
type Type = z.infer<typeof schema>;
|
||||
|
||||
return this.http.get<unknown>(url).pipe(
|
||||
map(response => schema.parse(response))
|
||||
);
|
||||
```
|
||||
|
||||
**Event Handlers:**
|
||||
```typescript
|
||||
// BEFORE: onClick(event: any)
|
||||
// AFTER: onClick(event: MouseEvent)
|
||||
```
|
||||
|
||||
## References
|
||||
|
||||
- Use `docs-researcher` for latest Zod documentation
|
||||
- Zod: https://zod.dev
|
||||
- TypeScript strict mode: https://www.typescriptlang.org/tsconfig#strict
|
||||
@@ -1,293 +0,0 @@
|
||||
# Zod Patterns Reference
|
||||
|
||||
## Overview
|
||||
|
||||
Zod is a TypeScript-first schema validation library. Use it for runtime validation at system boundaries (API responses, form inputs, external data).
|
||||
|
||||
## Basic Schema Patterns
|
||||
|
||||
### Primitive Types
|
||||
|
||||
```typescript
|
||||
import { z } from 'zod';
|
||||
|
||||
// Basic types
|
||||
const stringSchema = z.string();
|
||||
const numberSchema = z.number();
|
||||
const booleanSchema = z.boolean();
|
||||
const dateSchema = z.date();
|
||||
|
||||
// With constraints
|
||||
const emailSchema = z.string().email();
|
||||
const positiveNumber = z.number().positive();
|
||||
const nonEmptyString = z.string().min(1);
|
||||
const optionalString = z.string().optional();
|
||||
const nullableString = z.string().nullable();
|
||||
```
|
||||
|
||||
### Object Schemas
|
||||
|
||||
```typescript
|
||||
// Basic object
|
||||
const userSchema = z.object({
|
||||
id: z.string().uuid(),
|
||||
email: z.string().email(),
|
||||
name: z.string().min(1),
|
||||
age: z.number().int().positive().optional(),
|
||||
role: z.enum(['admin', 'user', 'guest']),
|
||||
createdAt: z.string().datetime(),
|
||||
});
|
||||
|
||||
// Infer TypeScript type from schema
|
||||
type User = z.infer<typeof userSchema>;
|
||||
|
||||
// Partial and Pick utilities
|
||||
const partialUser = userSchema.partial(); // All fields optional
|
||||
const requiredUser = userSchema.required(); // All fields required
|
||||
const pickedUser = userSchema.pick({ email: true, name: true });
|
||||
const omittedUser = userSchema.omit({ createdAt: true });
|
||||
```
|
||||
|
||||
### Array Schemas
|
||||
|
||||
```typescript
|
||||
// Basic array
|
||||
const stringArray = z.array(z.string());
|
||||
|
||||
// With constraints
|
||||
const nonEmptyArray = z.array(z.string()).nonempty();
|
||||
const limitedArray = z.array(z.number()).min(1).max(10);
|
||||
|
||||
// Tuple (fixed length, different types)
|
||||
const coordinate = z.tuple([z.number(), z.number()]);
|
||||
```
|
||||
|
||||
## API Response Validation
|
||||
|
||||
### Pattern: Validate API Responses
|
||||
|
||||
```typescript
|
||||
// Define response schema
|
||||
const apiResponseSchema = z.object({
|
||||
data: z.object({
|
||||
items: z.array(userSchema),
|
||||
total: z.number(),
|
||||
page: z.number(),
|
||||
pageSize: z.number(),
|
||||
}),
|
||||
meta: z.object({
|
||||
timestamp: z.string().datetime(),
|
||||
requestId: z.string().uuid(),
|
||||
}),
|
||||
});
|
||||
|
||||
// In Angular service
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class UserService {
|
||||
#http = inject(HttpClient);
|
||||
#logger = logger({ component: 'UserService' });
|
||||
|
||||
getUsers(): Observable<User[]> {
|
||||
return this.#http.get('/api/users').pipe(
|
||||
map((response) => {
|
||||
const result = apiResponseSchema.safeParse(response);
|
||||
if (!result.success) {
|
||||
this.#logger.error('Invalid API response', {
|
||||
errors: result.error.errors
|
||||
});
|
||||
throw new Error('Invalid API response');
|
||||
}
|
||||
return result.data.data.items;
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Pattern: Coerce Types
|
||||
|
||||
```typescript
|
||||
// API returns string IDs, coerce to number
|
||||
const productSchema = z.object({
|
||||
id: z.coerce.number(), // "123" -> 123
|
||||
price: z.coerce.number(), // "99.99" -> 99.99
|
||||
inStock: z.coerce.boolean(), // "true" -> true
|
||||
createdAt: z.coerce.date(), // "2024-01-01" -> Date
|
||||
});
|
||||
```
|
||||
|
||||
## Form Validation
|
||||
|
||||
### Pattern: Form Schema
|
||||
|
||||
```typescript
|
||||
// Define form schema with custom error messages
|
||||
const loginFormSchema = z.object({
|
||||
email: z.string()
|
||||
.email({ message: 'Invalid email address' }),
|
||||
password: z.string()
|
||||
.min(8, { message: 'Password must be at least 8 characters' })
|
||||
.regex(/[A-Z]/, { message: 'Must contain uppercase letter' })
|
||||
.regex(/[0-9]/, { message: 'Must contain number' }),
|
||||
rememberMe: z.boolean().default(false),
|
||||
});
|
||||
|
||||
// Use with Angular forms
|
||||
type LoginForm = z.infer<typeof loginFormSchema>;
|
||||
```
|
||||
|
||||
### Pattern: Cross-field Validation
|
||||
|
||||
```typescript
|
||||
const passwordFormSchema = z.object({
|
||||
password: z.string().min(8),
|
||||
confirmPassword: z.string(),
|
||||
}).refine(
|
||||
(data) => data.password === data.confirmPassword,
|
||||
{
|
||||
message: "Passwords don't match",
|
||||
path: ['confirmPassword'], // Error path
|
||||
}
|
||||
);
|
||||
```
|
||||
|
||||
## Type Guards
|
||||
|
||||
### Pattern: Create Type Guard from Schema
|
||||
|
||||
```typescript
|
||||
// Schema
|
||||
const customerSchema = z.object({
|
||||
type: z.literal('customer'),
|
||||
customerId: z.string(),
|
||||
loyaltyPoints: z.number(),
|
||||
});
|
||||
|
||||
// Type guard function
|
||||
function isCustomer(value: unknown): value is z.infer<typeof customerSchema> {
|
||||
return customerSchema.safeParse(value).success;
|
||||
}
|
||||
|
||||
// Usage
|
||||
if (isCustomer(data)) {
|
||||
console.log(data.loyaltyPoints); // Type-safe access
|
||||
}
|
||||
```
|
||||
|
||||
### Pattern: Discriminated Unions
|
||||
|
||||
```typescript
|
||||
const customerSchema = z.object({
|
||||
type: z.literal('customer'),
|
||||
customerId: z.string(),
|
||||
});
|
||||
|
||||
const guestSchema = z.object({
|
||||
type: z.literal('guest'),
|
||||
sessionId: z.string(),
|
||||
});
|
||||
|
||||
const userSchema = z.discriminatedUnion('type', [
|
||||
customerSchema,
|
||||
guestSchema,
|
||||
]);
|
||||
|
||||
type User = z.infer<typeof userSchema>;
|
||||
// User = { type: 'customer'; customerId: string } | { type: 'guest'; sessionId: string }
|
||||
```
|
||||
|
||||
## Replacing `any` Types
|
||||
|
||||
### Before (unsafe)
|
||||
|
||||
```typescript
|
||||
function processOrder(order: any) {
|
||||
// No type safety
|
||||
console.log(order.items.length);
|
||||
console.log(order.customer.name);
|
||||
}
|
||||
```
|
||||
|
||||
### After (with Zod)
|
||||
|
||||
```typescript
|
||||
const orderSchema = z.object({
|
||||
id: z.string().uuid(),
|
||||
items: z.array(z.object({
|
||||
productId: z.string(),
|
||||
quantity: z.number().positive(),
|
||||
price: z.number().nonnegative(),
|
||||
})),
|
||||
customer: z.object({
|
||||
name: z.string(),
|
||||
email: z.string().email(),
|
||||
}),
|
||||
status: z.enum(['pending', 'confirmed', 'shipped', 'delivered']),
|
||||
});
|
||||
|
||||
type Order = z.infer<typeof orderSchema>;
|
||||
|
||||
function processOrder(input: unknown): Order {
|
||||
const order = orderSchema.parse(input); // Throws if invalid
|
||||
console.log(order.items.length); // Type-safe
|
||||
console.log(order.customer.name); // Type-safe
|
||||
return order;
|
||||
}
|
||||
```
|
||||
|
||||
## Error Handling
|
||||
|
||||
### Pattern: Structured Error Handling
|
||||
|
||||
```typescript
|
||||
const result = schema.safeParse(data);
|
||||
|
||||
if (!result.success) {
|
||||
// Access formatted errors
|
||||
const formatted = result.error.format();
|
||||
|
||||
// Access flat error list
|
||||
const flat = result.error.flatten();
|
||||
|
||||
// Custom error mapping
|
||||
const errors = result.error.errors.map(err => ({
|
||||
field: err.path.join('.'),
|
||||
message: err.message,
|
||||
code: err.code,
|
||||
}));
|
||||
}
|
||||
```
|
||||
|
||||
## Transform Patterns
|
||||
|
||||
### Pattern: Transform on Parse
|
||||
|
||||
```typescript
|
||||
const userInputSchema = z.object({
|
||||
email: z.string().email().transform(s => s.toLowerCase()),
|
||||
name: z.string().transform(s => s.trim()),
|
||||
tags: z.string().transform(s => s.split(',')),
|
||||
});
|
||||
|
||||
// Input: { email: "USER@EXAMPLE.COM", name: " John ", tags: "a,b,c" }
|
||||
// Output: { email: "user@example.com", name: "John", tags: ["a", "b", "c"] }
|
||||
```
|
||||
|
||||
### Pattern: Default Values
|
||||
|
||||
```typescript
|
||||
const configSchema = z.object({
|
||||
theme: z.enum(['light', 'dark']).default('light'),
|
||||
pageSize: z.number().default(20),
|
||||
features: z.array(z.string()).default([]),
|
||||
});
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Define schemas at module boundaries** - API services, form handlers
|
||||
2. **Use `safeParse` for error handling** - Don't let validation throw unexpectedly
|
||||
3. **Infer types from schemas** - Single source of truth
|
||||
4. **Add meaningful error messages** - Help debugging and user feedback
|
||||
5. **Use transforms for normalization** - Clean data on parse
|
||||
6. **Keep schemas close to usage** - Colocate with services/components
|
||||
15
CLAUDE.md
15
CLAUDE.md
@@ -39,18 +39,17 @@ Task involves external API? → AUTO-INVOKE docs-researcher
|
||||
|
||||
| Trigger | Auto-Invoke Skill |
|
||||
|---------|-------------------|
|
||||
| Writing Angular templates | `angular-template` |
|
||||
| Writing HTML with interactivity | `html-template` |
|
||||
| Writing Angular templates | `template-standards` |
|
||||
| Applying Tailwind classes | `tailwind` |
|
||||
| Writing any Angular code | `logging` |
|
||||
| Creating CSS animations | `css-keyframes-animations` |
|
||||
| Creating new library | `library-scaffolder` |
|
||||
| Regenerating API clients | `swagger-sync-manager` |
|
||||
| Creating CSS animations | `css-animations` |
|
||||
| Creating new library | `library-creator` |
|
||||
| Regenerating API clients | `api-sync` |
|
||||
| Git operations | `git-workflow` |
|
||||
|
||||
**Skill chaining for Angular work:**
|
||||
```
|
||||
angular-template → html-template → logging → tailwind
|
||||
template-standards → logging → tailwind
|
||||
```
|
||||
|
||||
### Implementation Agents (Use for Complex Tasks)
|
||||
@@ -75,7 +74,7 @@ angular-template → html-template → logging → tailwind
|
||||
|
||||
```
|
||||
✅ "Researching [library] API..." → [auto-invokes docs-researcher]
|
||||
✅ "Loading angular-template skill..." → [auto-invokes skill]
|
||||
✅ "Loading template-standards skill..." → [auto-invokes skill]
|
||||
✅ "Implementing based on current docs..."
|
||||
✅ If fails: "Escalating research..." → [auto-invokes docs-researcher-advanced]
|
||||
```
|
||||
@@ -223,7 +222,7 @@ Extract only what's needed, discard the rest:
|
||||
```
|
||||
✓ Created 3 files
|
||||
✓ Tests: 12/12 passing
|
||||
✓ Skills applied: angular-template, logging
|
||||
✓ Skills applied: template-standards, logging
|
||||
```
|
||||
|
||||
<!-- nx configuration start-->
|
||||
|
||||
@@ -1,19 +1,22 @@
|
||||
import { NgModule } from '@angular/core';
|
||||
import { DevScanAdapter } from './dev.scan-adapter';
|
||||
import { NativeScanAdapter } from './native.scan-adapter';
|
||||
import { SCAN_ADAPTER } from './tokens';
|
||||
|
||||
@NgModule({})
|
||||
export class ScanAdapterModule {
|
||||
static forRoot() {
|
||||
return {
|
||||
ngModule: ScanAdapterModule,
|
||||
providers: [
|
||||
{ provide: SCAN_ADAPTER, useClass: NativeScanAdapter, multi: true },
|
||||
{ provide: SCAN_ADAPTER, useClass: DevScanAdapter, multi: true },
|
||||
],
|
||||
// Use for testing:
|
||||
// providers: [{ provide: SCAN_ADAPTER, useClass: dev ? DevScanAdapter : NativeScanAdapter, multi: true }],
|
||||
};
|
||||
}
|
||||
}
|
||||
import { NgModule } from '@angular/core';
|
||||
import { DevScanAdapter } from './dev.scan-adapter';
|
||||
import { NativeScanAdapter } from './native.scan-adapter';
|
||||
import { SCAN_ADAPTER } from './tokens';
|
||||
|
||||
/**
|
||||
* @deprecated Use '@isa/shared/scanner' instead.
|
||||
*/
|
||||
@NgModule({})
|
||||
export class ScanAdapterModule {
|
||||
static forRoot() {
|
||||
return {
|
||||
ngModule: ScanAdapterModule,
|
||||
providers: [
|
||||
{ provide: SCAN_ADAPTER, useClass: NativeScanAdapter, multi: true },
|
||||
{ provide: SCAN_ADAPTER, useClass: DevScanAdapter, multi: true },
|
||||
],
|
||||
// Use for testing:
|
||||
// providers: [{ provide: SCAN_ADAPTER, useClass: dev ? DevScanAdapter : NativeScanAdapter, multi: true }],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,25 @@
|
||||
import { NgModule } from '@angular/core';
|
||||
import { CommonModule } from '@angular/common';
|
||||
|
||||
import { ScanditOverlayComponent } from './scandit-overlay.component';
|
||||
import { ScanditScanAdapter } from './scandit.scan-adapter';
|
||||
import { SCAN_ADAPTER } from '../tokens';
|
||||
|
||||
@NgModule({
|
||||
imports: [CommonModule],
|
||||
exports: [ScanditOverlayComponent],
|
||||
declarations: [ScanditOverlayComponent],
|
||||
})
|
||||
export class ScanditScanAdapterModule {
|
||||
static forRoot() {
|
||||
return {
|
||||
ngModule: ScanditScanAdapterModule,
|
||||
providers: [{ provide: SCAN_ADAPTER, useClass: ScanditScanAdapter, multi: true }],
|
||||
};
|
||||
}
|
||||
}
|
||||
import { NgModule } from '@angular/core';
|
||||
import { CommonModule } from '@angular/common';
|
||||
|
||||
import { ScanditOverlayComponent } from './scandit-overlay.component';
|
||||
import { ScanditScanAdapter } from './scandit.scan-adapter';
|
||||
import { SCAN_ADAPTER } from '../tokens';
|
||||
|
||||
/**
|
||||
* @deprecated Use @isa/shared/scanner instead.
|
||||
*/
|
||||
@NgModule({
|
||||
imports: [CommonModule],
|
||||
exports: [ScanditOverlayComponent],
|
||||
declarations: [ScanditOverlayComponent],
|
||||
})
|
||||
export class ScanditScanAdapterModule {
|
||||
static forRoot() {
|
||||
return {
|
||||
ngModule: ScanditScanAdapterModule,
|
||||
providers: [
|
||||
{ provide: SCAN_ADAPTER, useClass: ScanditScanAdapter, multi: true },
|
||||
],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import { Config } from '@core/config';
|
||||
import { ComponentPortal } from '@angular/cdk/portal';
|
||||
import { ScanditOverlayComponent } from './scandit-overlay.component';
|
||||
import { EnvironmentService } from '@core/environment';
|
||||
import { injectNetworkStatus$ } from 'apps/isa-app/src/app/services/network-status.service';
|
||||
import { injectNetworkStatus$ } from '@isa/core/connectivity';
|
||||
import { toSignal } from '@angular/core/rxjs-interop';
|
||||
|
||||
@Injectable()
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
import { NgModule } from '@angular/core';
|
||||
import { DomainAvailabilityModule } from '@domain/availability';
|
||||
import { DomainCatalogModule } from '@domain/catalog';
|
||||
import { DomainIsaModule } from '@domain/isa';
|
||||
import { DomainCheckoutModule } from '@domain/checkout';
|
||||
import { DomainOmsModule } from '@domain/oms';
|
||||
import { DomainRemissionModule } from '@domain/remission';
|
||||
|
||||
@NgModule({
|
||||
imports: [
|
||||
DomainIsaModule.forRoot(),
|
||||
DomainCatalogModule.forRoot(),
|
||||
DomainAvailabilityModule.forRoot(),
|
||||
DomainCheckoutModule.forRoot(),
|
||||
DomainOmsModule.forRoot(),
|
||||
DomainRemissionModule.forRoot(),
|
||||
],
|
||||
})
|
||||
export class AppDomainModule {}
|
||||
@@ -1,34 +0,0 @@
|
||||
import { NgModule } from '@angular/core';
|
||||
import { EffectsModule } from '@ngrx/effects';
|
||||
import { ActionReducer, MetaReducer, StoreModule } from '@ngrx/store';
|
||||
import { StoreDevtoolsModule } from '@ngrx/store-devtools';
|
||||
import { environment } from '../environments/environment';
|
||||
import { rootReducer } from './store/root.reducer';
|
||||
import { RootState } from './store/root.state';
|
||||
|
||||
export function storeInLocalStorage(
|
||||
reducer: ActionReducer<any>,
|
||||
): ActionReducer<any> {
|
||||
return function (state, action) {
|
||||
if (action.type === 'HYDRATE') {
|
||||
return reducer(action['payload'], action);
|
||||
}
|
||||
return reducer(state, action);
|
||||
};
|
||||
}
|
||||
|
||||
export const metaReducers: MetaReducer<RootState>[] = !environment.production
|
||||
? [storeInLocalStorage]
|
||||
: [storeInLocalStorage];
|
||||
|
||||
@NgModule({
|
||||
imports: [
|
||||
StoreModule.forRoot(rootReducer, { metaReducers }),
|
||||
EffectsModule.forRoot([]),
|
||||
StoreDevtoolsModule.instrument({
|
||||
name: 'ISA Ngrx Application Store',
|
||||
connectInZone: true,
|
||||
}),
|
||||
],
|
||||
})
|
||||
export class AppStoreModule {}
|
||||
@@ -1,40 +0,0 @@
|
||||
import { HttpInterceptorFn, provideHttpClient, withInterceptors } from '@angular/common/http';
|
||||
import { NgModule } from '@angular/core';
|
||||
import { Config } from '@core/config';
|
||||
import { AvConfiguration } from '@generated/swagger/availability-api';
|
||||
import { CatConfiguration } from '@generated/swagger/cat-search-api';
|
||||
import { CheckoutConfiguration } from '@generated/swagger/checkout-api';
|
||||
import { CrmConfiguration } from '@generated/swagger/crm-api';
|
||||
import { EisConfiguration } from '@generated/swagger/eis-api';
|
||||
import { IsaConfiguration } from '@generated/swagger/isa-api';
|
||||
import { OmsConfiguration } from '@generated/swagger/oms-api';
|
||||
import { PrintConfiguration } from '@generated/swagger/print-api';
|
||||
import { RemiConfiguration } from '@generated/swagger/inventory-api';
|
||||
import { WwsConfiguration } from '@generated/swagger/wws-api';
|
||||
|
||||
export function createConfigurationFactory(name: string) {
|
||||
return function (config: Config): { rootUrl: string } {
|
||||
return config.get(`@swagger/${name}`);
|
||||
};
|
||||
}
|
||||
|
||||
const serviceWorkerInterceptor: HttpInterceptorFn = (req, next) => {
|
||||
return next(req.clone({ setHeaders: { 'ngsw-bypass': 'true' } }));
|
||||
};
|
||||
|
||||
@NgModule({
|
||||
providers: [
|
||||
provideHttpClient(withInterceptors([serviceWorkerInterceptor])),
|
||||
{ provide: AvConfiguration, useFactory: createConfigurationFactory('av'), deps: [Config] },
|
||||
{ provide: CatConfiguration, useFactory: createConfigurationFactory('cat'), deps: [Config] },
|
||||
{ provide: CheckoutConfiguration, useFactory: createConfigurationFactory('checkout'), deps: [Config] },
|
||||
{ provide: CrmConfiguration, useFactory: createConfigurationFactory('crm'), deps: [Config] },
|
||||
{ provide: EisConfiguration, useFactory: createConfigurationFactory('eis'), deps: [Config] },
|
||||
{ provide: IsaConfiguration, useFactory: createConfigurationFactory('isa'), deps: [Config] },
|
||||
{ provide: OmsConfiguration, useFactory: createConfigurationFactory('oms'), deps: [Config] },
|
||||
{ provide: PrintConfiguration, useFactory: createConfigurationFactory('print'), deps: [Config] },
|
||||
{ provide: RemiConfiguration, useFactory: createConfigurationFactory('remi'), deps: [Config] },
|
||||
{ provide: WwsConfiguration, useFactory: createConfigurationFactory('wws'), deps: [Config] },
|
||||
],
|
||||
})
|
||||
export class AppSwaggerModule {}
|
||||
@@ -1,28 +1,28 @@
|
||||
@if ($offlineBannerVisible()) {
|
||||
<div [@fadeInOut] class="bg-brand text-white text-center fixed inset-x-0 top-0 z-tooltip p-4">
|
||||
<h3 class="font-bold grid grid-flow-col items-center justify-center text-xl gap-4">
|
||||
<div>
|
||||
<ng-icon name="matWifiOff"></ng-icon>
|
||||
</div>
|
||||
|
||||
<div>Sie sind offline, keine Verbindung zum Netzwerk.</div>
|
||||
</h3>
|
||||
<p>Bereits geladene Ihnalte werden angezeigt, Interaktionen sind aktuell nicht möglich.</p>
|
||||
</div>
|
||||
}
|
||||
@if ($onlineBannerVisible()) {
|
||||
<div [@fadeInOut] class="bg-green-500 text-white text-center fixed inset-x-0 top-0 z-tooltip p-4">
|
||||
<h3 class="font-bold grid grid-flow-col items-center justify-center text-xl gap-4">
|
||||
<div>
|
||||
<ng-icon name="matWifi"></ng-icon>
|
||||
</div>
|
||||
|
||||
<div>Sie sind wieder online.</div>
|
||||
</h3>
|
||||
<button class="fixed top-2 right-4 text-3xl w-12 h-12" type="button" (click)="$onlineBannerVisible.set(false)">
|
||||
<ng-icon name="matClose"></ng-icon>
|
||||
</button>
|
||||
</div>
|
||||
}
|
||||
|
||||
<router-outlet></router-outlet>
|
||||
<!-- @if ($offlineBannerVisible()) {
|
||||
<div [@fadeInOut] class="bg-brand text-white text-center fixed inset-x-0 top-0 z-tooltip p-4">
|
||||
<h3 class="font-bold grid grid-flow-col items-center justify-center text-xl gap-4">
|
||||
<div>
|
||||
<ng-icon name="matWifiOff"></ng-icon>
|
||||
</div>
|
||||
|
||||
<div>Sie sind offline, keine Verbindung zum Netzwerk.</div>
|
||||
</h3>
|
||||
<p>Bereits geladene Ihnalte werden angezeigt, Interaktionen sind aktuell nicht möglich.</p>
|
||||
</div>
|
||||
}
|
||||
@if ($onlineBannerVisible()) {
|
||||
<div [@fadeInOut] class="bg-green-500 text-white text-center fixed inset-x-0 top-0 z-tooltip p-4">
|
||||
<h3 class="font-bold grid grid-flow-col items-center justify-center text-xl gap-4">
|
||||
<div>
|
||||
<ng-icon name="matWifi"></ng-icon>
|
||||
</div>
|
||||
|
||||
<div>Sie sind wieder online.</div>
|
||||
</h3>
|
||||
<button class="fixed top-2 right-4 text-3xl w-12 h-12" type="button" (click)="$onlineBannerVisible.set(false)">
|
||||
<ng-icon name="matClose"></ng-icon>
|
||||
</button>
|
||||
</div>
|
||||
}
|
||||
|
||||
<router-outlet></router-outlet> -->
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
:host {
|
||||
@apply block;
|
||||
}
|
||||
@@ -1,137 +0,0 @@
|
||||
import { Spectator, createComponentFactory, SpyObject, createSpyObject } from '@ngneat/spectator';
|
||||
import { RouterTestingModule } from '@angular/router/testing';
|
||||
import { AppComponent } from './app.component';
|
||||
import { Config } from '@core/config';
|
||||
import { ApplicationService } from '@core/application';
|
||||
import { of } from 'rxjs';
|
||||
import { Renderer2 } from '@angular/core';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { SwUpdate } from '@angular/service-worker';
|
||||
import { NotificationsHub } from '@hub/notifications';
|
||||
import { UserStateService } from '@generated/swagger/isa-api';
|
||||
import { UiModalService } from '@ui/modal';
|
||||
import { AuthService } from '@core/auth';
|
||||
|
||||
describe('AppComponent', () => {
|
||||
let spectator: Spectator<AppComponent>;
|
||||
let config: SpyObject<Config>;
|
||||
let renderer: SpyObject<Renderer2>;
|
||||
let applicationServiceMock: SpyObject<ApplicationService>;
|
||||
let notificationsHubMock: SpyObject<NotificationsHub>;
|
||||
let swUpdateMock: SpyObject<SwUpdate>;
|
||||
const createComponent = createComponentFactory({
|
||||
component: AppComponent,
|
||||
imports: [CommonModule, RouterTestingModule],
|
||||
providers: [],
|
||||
mocks: [Config, SwUpdate, UserStateService, UiModalService, AuthService],
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
applicationServiceMock = createSpyObject(ApplicationService);
|
||||
applicationServiceMock.getSection$.and.returnValue(of('customer'));
|
||||
applicationServiceMock.getActivatedProcessId$.and.returnValue(of(undefined));
|
||||
renderer = jasmine.createSpyObj('Renderer2', ['addClass', 'removeClass']);
|
||||
|
||||
notificationsHubMock = createSpyObject(NotificationsHub);
|
||||
notificationsHubMock.notifications$ = of({});
|
||||
swUpdateMock = createSpyObject(SwUpdate);
|
||||
|
||||
spectator = createComponent({
|
||||
providers: [
|
||||
{ provide: ApplicationService, useValue: applicationServiceMock },
|
||||
{
|
||||
provide: Renderer2,
|
||||
useValue: renderer,
|
||||
},
|
||||
{ provide: NotificationsHub, useValue: notificationsHubMock },
|
||||
{ provide: SwUpdate, useValue: swUpdateMock },
|
||||
],
|
||||
});
|
||||
config = spectator.inject(Config);
|
||||
});
|
||||
|
||||
it('should create the app', () => {
|
||||
expect(spectator.component).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should have a router outlet', () => {
|
||||
expect(spectator.query('router-outlet')).toExist();
|
||||
});
|
||||
|
||||
describe('ngOnInit', () => {
|
||||
it('should call setTitle', () => {
|
||||
const spy = spyOn(spectator.component, 'setTitle');
|
||||
spectator.component.ngOnInit();
|
||||
expect(spy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should call logVersion', () => {
|
||||
const spy = spyOn(spectator.component, 'logVersion');
|
||||
spectator.component.ngOnInit();
|
||||
expect(spy).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('setTitle', () => {
|
||||
it('should call Title.setTitle()', () => {
|
||||
const spyTitleSetTitle = spyOn(spectator.component['_title'], 'setTitle');
|
||||
config.get.and.returnValue('test');
|
||||
spectator.component.setTitle();
|
||||
expect(spyTitleSetTitle).toHaveBeenCalledWith('test');
|
||||
});
|
||||
});
|
||||
|
||||
describe('logVersion', () => {
|
||||
it('should call console.log()', () => {
|
||||
const spyConsoleLog = spyOn(console, 'log');
|
||||
config.get.and.returnValue('test');
|
||||
spectator.component.logVersion();
|
||||
expect(spyConsoleLog).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
// --------------------------------------------------------
|
||||
// Unit Tests Implementation for Angular Version 13.x.x
|
||||
|
||||
// describe('updateClient()', () => {
|
||||
// it('should call checkForUpdate() if SwUpdate.isEnabled is True', () => {
|
||||
// spyOn(spectator.component, 'checkForUpdate');
|
||||
// spyOn(spectator.component, 'initialCheckForUpdate');
|
||||
// (swUpdateMock as any).isEnabled = true;
|
||||
// spectator.component.updateClient();
|
||||
// expect(spectator.component.initialCheckForUpdate).toHaveBeenCalled();
|
||||
// expect(spectator.component.checkForUpdate).toHaveBeenCalled();
|
||||
// });
|
||||
|
||||
// it('should not call checkForUpdate() if SwUpdate.isEnabled is False', () => {
|
||||
// spyOn(spectator.component, 'checkForUpdate');
|
||||
// spyOn(spectator.component, 'initialCheckForUpdate');
|
||||
// (swUpdateMock as any).isEnabled = false;
|
||||
// spectator.component.updateClient();
|
||||
// expect(spectator.component.initialCheckForUpdate).not.toHaveBeenCalled();
|
||||
// expect(spectator.component.checkForUpdate).not.toHaveBeenCalled();
|
||||
// });
|
||||
// });
|
||||
|
||||
// describe('checkForUpdate', () => {
|
||||
// it('should call swUpdate.checkForUpdate() and notifications.updateNotification() every second', fakeAsync(() => {
|
||||
// swUpdateMock.checkForUpdate.and.returnValue(Promise.resolve());
|
||||
// spectator.component.checkForUpdates = 1000;
|
||||
// spectator.component.checkForUpdate();
|
||||
|
||||
// spectator.detectChanges();
|
||||
// tick(1100);
|
||||
|
||||
// expect(notificationsHubMock.updateNotification).toHaveBeenCalled();
|
||||
// discardPeriodicTasks();
|
||||
// }));
|
||||
// });
|
||||
|
||||
// describe('initialCheckForUpdate', () => {
|
||||
// it('should call swUpdate.checkForUpdate()', () => {
|
||||
// swUpdateMock.checkForUpdate.and.returnValue(new Promise(undefined));
|
||||
// spectator.component.initialCheckForUpdate();
|
||||
// expect(swUpdateMock.checkForUpdate).toHaveBeenCalled();
|
||||
// });
|
||||
// });
|
||||
});
|
||||
@@ -1,206 +1,205 @@
|
||||
|
||||
import {
|
||||
Component,
|
||||
effect,
|
||||
HostListener,
|
||||
inject,
|
||||
Inject,
|
||||
Injector,
|
||||
OnInit,
|
||||
Renderer2,
|
||||
signal,
|
||||
untracked,
|
||||
DOCUMENT
|
||||
} from '@angular/core';
|
||||
import { Title } from '@angular/platform-browser';
|
||||
import { SwUpdate } from '@angular/service-worker';
|
||||
import { ApplicationService } from '@core/application';
|
||||
import { Config } from '@core/config';
|
||||
import { NotificationsHub } from '@hub/notifications';
|
||||
import packageInfo from 'packageJson';
|
||||
import { asapScheduler, interval, Subscription } from 'rxjs';
|
||||
import { UserStateService } from '@generated/swagger/isa-api';
|
||||
import { IsaLogProvider } from './providers';
|
||||
import { EnvironmentService } from '@core/environment';
|
||||
import { AuthService, LoginStrategy } from '@core/auth';
|
||||
import { UiMessageModalComponent, UiModalService } from '@ui/modal';
|
||||
import { injectOnline$ } from './services/network-status.service';
|
||||
import { toSignal } from '@angular/core/rxjs-interop';
|
||||
import { animate, style, transition, trigger } from '@angular/animations';
|
||||
|
||||
@Component({
|
||||
selector: 'app-root',
|
||||
templateUrl: './app.component.html',
|
||||
styleUrls: ['./app.component.scss'],
|
||||
animations: [
|
||||
trigger('fadeInOut', [
|
||||
transition(':enter', [
|
||||
// :enter wird ausgelöst, wenn das Element zum DOM hinzugefügt wird
|
||||
style({ opacity: 0, transform: 'translateY(-100%)' }),
|
||||
animate('300ms', style({ opacity: 1, transform: 'translateY(0)' })),
|
||||
]),
|
||||
transition(':leave', [
|
||||
// :leave wird ausgelöst, wenn das Element aus dem DOM entfernt wird
|
||||
animate('300ms', style({ opacity: 0, transform: 'translateY(-100%)' })),
|
||||
]),
|
||||
]),
|
||||
],
|
||||
standalone: false,
|
||||
})
|
||||
export class AppComponent implements OnInit {
|
||||
readonly injector = inject(Injector);
|
||||
|
||||
$online = toSignal(injectOnline$());
|
||||
|
||||
$offlineBannerVisible = signal(false);
|
||||
|
||||
$onlineBannerVisible = signal(false);
|
||||
|
||||
private onlineBannerDismissTimeout: any;
|
||||
|
||||
onlineEffects = effect(() => {
|
||||
const online = this.$online();
|
||||
const offlineBannerVisible = this.$offlineBannerVisible();
|
||||
|
||||
untracked(() => {
|
||||
this.$offlineBannerVisible.set(!online);
|
||||
|
||||
if (!online) {
|
||||
this.$onlineBannerVisible.set(false);
|
||||
clearTimeout(this.onlineBannerDismissTimeout);
|
||||
}
|
||||
|
||||
if (offlineBannerVisible && online) {
|
||||
this.$onlineBannerVisible.set(true);
|
||||
this.onlineBannerDismissTimeout = setTimeout(() => this.$onlineBannerVisible.set(false), 5000);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
private _checkForUpdates: number = this._config.get('checkForUpdates');
|
||||
|
||||
get checkForUpdates(): number {
|
||||
return this._checkForUpdates ?? 60 * 60 * 1000; // default 1 hour
|
||||
}
|
||||
|
||||
// For Unit Testing
|
||||
set checkForUpdates(time: number) {
|
||||
this._checkForUpdates = time;
|
||||
}
|
||||
|
||||
subscriptions = new Subscription();
|
||||
|
||||
constructor(
|
||||
private readonly _config: Config,
|
||||
private readonly _title: Title,
|
||||
private readonly _appService: ApplicationService,
|
||||
@Inject(DOCUMENT) private readonly _document: Document,
|
||||
private readonly _renderer: Renderer2,
|
||||
private readonly _swUpdate: SwUpdate,
|
||||
private readonly _notifications: NotificationsHub,
|
||||
private infoService: UserStateService,
|
||||
private readonly _environment: EnvironmentService,
|
||||
private readonly _authService: AuthService,
|
||||
private readonly _modal: UiModalService,
|
||||
) {
|
||||
this.updateClient();
|
||||
IsaLogProvider.InfoService = this.infoService;
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
this.setTitle();
|
||||
this.logVersion();
|
||||
asapScheduler.schedule(() => this.determinePlatform(), 250);
|
||||
this._appService.getSection$().subscribe(this.sectionChangeHandler.bind(this));
|
||||
|
||||
this.setupSilentRefresh();
|
||||
}
|
||||
|
||||
// Setup interval for silent refresh
|
||||
setupSilentRefresh() {
|
||||
const silentRefreshInterval = this._config.get('silentRefresh.interval');
|
||||
if (silentRefreshInterval > 0) {
|
||||
interval(silentRefreshInterval).subscribe(() => {
|
||||
if (this._authService.isAuthenticated()) {
|
||||
this._authService.refresh();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
setTitle() {
|
||||
this._title.setTitle(this._config.get('title'));
|
||||
}
|
||||
|
||||
logVersion() {
|
||||
console.log(
|
||||
`%c${this._config.get('title')}\r\nVersion: ${packageInfo.version}`,
|
||||
'font-weight: bold; font-size: 20px;',
|
||||
);
|
||||
}
|
||||
|
||||
determinePlatform() {
|
||||
if (this._environment.isNative()) {
|
||||
this._renderer.addClass(this._document.body, 'tablet-native');
|
||||
} else if (this._environment.isTablet()) {
|
||||
this._renderer.addClass(this._document.body, 'tablet-browser');
|
||||
}
|
||||
if (this._environment.isTablet()) {
|
||||
this._renderer.addClass(this._document.body, 'tablet');
|
||||
}
|
||||
if (this._environment.isDesktop()) {
|
||||
this._renderer.addClass(this._document.body, 'desktop');
|
||||
}
|
||||
}
|
||||
|
||||
sectionChangeHandler(section: string) {
|
||||
if (section === 'customer') {
|
||||
this._renderer.removeClass(this._document.body, 'branch');
|
||||
this._renderer.addClass(this._document.body, 'customer');
|
||||
} else if (section === 'branch') {
|
||||
this._renderer.removeClass(this._document.body, 'customer');
|
||||
this._renderer.addClass(this._document.body, 'branch');
|
||||
}
|
||||
}
|
||||
|
||||
updateClient() {
|
||||
if (!this._swUpdate.isEnabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.initialCheckForUpdate();
|
||||
this.checkForUpdate();
|
||||
}
|
||||
|
||||
checkForUpdate() {
|
||||
interval(this._checkForUpdates).subscribe(() => {
|
||||
this._swUpdate.checkForUpdate().then((value) => {
|
||||
console.log('check for update', value);
|
||||
if (value) {
|
||||
this._notifications.updateNotification();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
initialCheckForUpdate() {
|
||||
this._swUpdate.checkForUpdate().then((value) => {
|
||||
console.log('initial check for update', value);
|
||||
if (value) {
|
||||
location.reload();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@HostListener('window:visibilitychange', ['$event'])
|
||||
onVisibilityChange(event: Event) {
|
||||
// refresh token when app is in background
|
||||
if (this._document.hidden && this._authService.isAuthenticated()) {
|
||||
this._authService.refresh();
|
||||
} else if (!this._authService.isAuthenticated()) {
|
||||
const strategy = this.injector.get(LoginStrategy);
|
||||
|
||||
return strategy.login('Sie sind nicht mehr angemeldet');
|
||||
}
|
||||
}
|
||||
}
|
||||
// import {
|
||||
// Component,
|
||||
// effect,
|
||||
// HostListener,
|
||||
// inject,
|
||||
// Inject,
|
||||
// Injector,
|
||||
// OnInit,
|
||||
// Renderer2,
|
||||
// signal,
|
||||
// untracked,
|
||||
// DOCUMENT
|
||||
// } from '@angular/core';
|
||||
// import { Title } from '@angular/platform-browser';
|
||||
// import { SwUpdate } from '@angular/service-worker';
|
||||
// import { ApplicationService } from '@core/application';
|
||||
// import { Config } from '@core/config';
|
||||
// import { NotificationsHub } from '@hub/notifications';
|
||||
// import packageInfo from 'packageJson';
|
||||
// import { asapScheduler, interval, Subscription } from 'rxjs';
|
||||
// import { UserStateService } from '@generated/swagger/isa-api';
|
||||
// import { IsaLogProvider } from './providers';
|
||||
// import { EnvironmentService } from '@core/environment';
|
||||
// import { AuthService, LoginStrategy } from '@core/auth';
|
||||
// import { UiMessageModalComponent, UiModalService } from '@ui/modal';
|
||||
// import { injectNetworkStatus } from '@isa/core/connectivity';
|
||||
// import { animate, style, transition, trigger } from '@angular/animations';
|
||||
|
||||
// @Component({
|
||||
// selector: 'app-root',
|
||||
// templateUrl: './app.component.html',
|
||||
// styleUrls: ['./app.component.scss'],
|
||||
// animations: [
|
||||
// trigger('fadeInOut', [
|
||||
// transition(':enter', [
|
||||
// // :enter wird ausgelöst, wenn das Element zum DOM hinzugefügt wird
|
||||
// style({ opacity: 0, transform: 'translateY(-100%)' }),
|
||||
// animate('300ms', style({ opacity: 1, transform: 'translateY(0)' })),
|
||||
// ]),
|
||||
// transition(':leave', [
|
||||
// // :leave wird ausgelöst, wenn das Element aus dem DOM entfernt wird
|
||||
// animate('300ms', style({ opacity: 0, transform: 'translateY(-100%)' })),
|
||||
// ]),
|
||||
// ]),
|
||||
// ],
|
||||
// standalone: false,
|
||||
// })
|
||||
// export class AppComponent implements OnInit {
|
||||
// readonly injector = inject(Injector);
|
||||
|
||||
// $networkStatus = injectNetworkStatus();
|
||||
|
||||
// $offlineBannerVisible = signal(false);
|
||||
|
||||
// $onlineBannerVisible = signal(false);
|
||||
|
||||
// private onlineBannerDismissTimeout: any;
|
||||
|
||||
// onlineEffects = effect(() => {
|
||||
// const status = this.$networkStatus();
|
||||
// const online = status === 'online';
|
||||
// const offlineBannerVisible = this.$offlineBannerVisible();
|
||||
|
||||
// untracked(() => {
|
||||
// this.$offlineBannerVisible.set(!online);
|
||||
|
||||
// if (!online) {
|
||||
// this.$onlineBannerVisible.set(false);
|
||||
// clearTimeout(this.onlineBannerDismissTimeout);
|
||||
// }
|
||||
|
||||
// if (offlineBannerVisible && online) {
|
||||
// this.$onlineBannerVisible.set(true);
|
||||
// this.onlineBannerDismissTimeout = setTimeout(() => this.$onlineBannerVisible.set(false), 5000);
|
||||
// }
|
||||
// });
|
||||
// });
|
||||
|
||||
// private _checkForUpdates: number = this._config.get('checkForUpdates');
|
||||
|
||||
// get checkForUpdates(): number {
|
||||
// return this._checkForUpdates ?? 60 * 60 * 1000; // default 1 hour
|
||||
// }
|
||||
|
||||
// // For Unit Testing
|
||||
// set checkForUpdates(time: number) {
|
||||
// this._checkForUpdates = time;
|
||||
// }
|
||||
|
||||
// subscriptions = new Subscription();
|
||||
|
||||
// constructor(
|
||||
// private readonly _config: Config,
|
||||
// private readonly _title: Title,
|
||||
// private readonly _appService: ApplicationService,
|
||||
// @Inject(DOCUMENT) private readonly _document: Document,
|
||||
// private readonly _renderer: Renderer2,
|
||||
// private readonly _swUpdate: SwUpdate,
|
||||
// private readonly _notifications: NotificationsHub,
|
||||
// private infoService: UserStateService,
|
||||
// private readonly _environment: EnvironmentService,
|
||||
// private readonly _authService: AuthService,
|
||||
// private readonly _modal: UiModalService,
|
||||
// ) {
|
||||
// this.updateClient();
|
||||
// IsaLogProvider.InfoService = this.infoService;
|
||||
// }
|
||||
|
||||
// ngOnInit() {
|
||||
// this.setTitle();
|
||||
// this.logVersion();
|
||||
// asapScheduler.schedule(() => this.determinePlatform(), 250);
|
||||
// this._appService.getSection$().subscribe(this.sectionChangeHandler.bind(this));
|
||||
|
||||
// this.setupSilentRefresh();
|
||||
// }
|
||||
|
||||
// // Setup interval for silent refresh
|
||||
// setupSilentRefresh() {
|
||||
// const silentRefreshInterval = this._config.get('silentRefresh.interval');
|
||||
// if (silentRefreshInterval > 0) {
|
||||
// interval(silentRefreshInterval).subscribe(() => {
|
||||
// if (this._authService.isAuthenticated()) {
|
||||
// this._authService.refresh();
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
|
||||
// setTitle() {
|
||||
// this._title.setTitle(this._config.get('title'));
|
||||
// }
|
||||
|
||||
// logVersion() {
|
||||
// console.log(
|
||||
// `%c${this._config.get('title')}\r\nVersion: ${packageInfo.version}`,
|
||||
// 'font-weight: bold; font-size: 20px;',
|
||||
// );
|
||||
// }
|
||||
|
||||
// determinePlatform() {
|
||||
// if (this._environment.isNative()) {
|
||||
// this._renderer.addClass(this._document.body, 'tablet-native');
|
||||
// } else if (this._environment.isTablet()) {
|
||||
// this._renderer.addClass(this._document.body, 'tablet-browser');
|
||||
// }
|
||||
// if (this._environment.isTablet()) {
|
||||
// this._renderer.addClass(this._document.body, 'tablet');
|
||||
// }
|
||||
// if (this._environment.isDesktop()) {
|
||||
// this._renderer.addClass(this._document.body, 'desktop');
|
||||
// }
|
||||
// }
|
||||
|
||||
// sectionChangeHandler(section: string) {
|
||||
// if (section === 'customer') {
|
||||
// this._renderer.removeClass(this._document.body, 'branch');
|
||||
// this._renderer.addClass(this._document.body, 'customer');
|
||||
// } else if (section === 'branch') {
|
||||
// this._renderer.removeClass(this._document.body, 'customer');
|
||||
// this._renderer.addClass(this._document.body, 'branch');
|
||||
// }
|
||||
// }
|
||||
|
||||
// updateClient() {
|
||||
// if (!this._swUpdate.isEnabled) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
// this.initialCheckForUpdate();
|
||||
// this.checkForUpdate();
|
||||
// }
|
||||
|
||||
// checkForUpdate() {
|
||||
// interval(this._checkForUpdates).subscribe(() => {
|
||||
// this._swUpdate.checkForUpdate().then((value) => {
|
||||
// console.log('check for update', value);
|
||||
// if (value) {
|
||||
// this._notifications.updateNotification();
|
||||
// }
|
||||
// });
|
||||
// });
|
||||
// }
|
||||
|
||||
// initialCheckForUpdate() {
|
||||
// this._swUpdate.checkForUpdate().then((value) => {
|
||||
// console.log('initial check for update', value);
|
||||
// if (value) {
|
||||
// location.reload();
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
|
||||
// @HostListener('window:visibilitychange', ['$event'])
|
||||
// onVisibilityChange(event: Event) {
|
||||
// // refresh token when app is in background
|
||||
// if (this._document.hidden && this._authService.isAuthenticated()) {
|
||||
// this._authService.refresh();
|
||||
// } else if (!this._authService.isAuthenticated()) {
|
||||
// const strategy = this.injector.get(LoginStrategy);
|
||||
|
||||
// return strategy.login('Sie sind nicht mehr angemeldet');
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -1,52 +1,47 @@
|
||||
import { version } from '../../../../package.json';
|
||||
import { IsaTitleStrategy } from '@isa/common/title-management';
|
||||
import {
|
||||
HTTP_INTERCEPTORS,
|
||||
HttpInterceptorFn,
|
||||
provideHttpClient,
|
||||
withInterceptors,
|
||||
withInterceptorsFromDi,
|
||||
} from '@angular/common/http';
|
||||
import {
|
||||
ApplicationConfig,
|
||||
DEFAULT_CURRENCY_CODE,
|
||||
ErrorHandler,
|
||||
importProvidersFrom,
|
||||
Injector,
|
||||
LOCALE_ID,
|
||||
NgModule,
|
||||
inject,
|
||||
provideAppInitializer,
|
||||
provideZoneChangeDetection,
|
||||
signal,
|
||||
isDevMode,
|
||||
} from '@angular/core';
|
||||
import { BrowserModule } from '@angular/platform-browser';
|
||||
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
|
||||
import { PlatformModule } from '@angular/cdk/platform';
|
||||
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';
|
||||
import { provideRouter, withComponentInputBinding } from '@angular/router';
|
||||
import { ActionReducer, MetaReducer, provideStore } from '@ngrx/store';
|
||||
import { provideStoreDevtools } from '@ngrx/store-devtools';
|
||||
|
||||
import { Config } from '@core/config';
|
||||
import { AuthModule, AuthService, LoginStrategy } from '@core/auth';
|
||||
import { CoreCommandModule } from '@core/command';
|
||||
|
||||
import { AppRoutingModule } from './app-routing.module';
|
||||
import { AppComponent } from './app.component';
|
||||
import {
|
||||
ApplicationService,
|
||||
ApplicationServiceAdapter,
|
||||
CoreApplicationModule,
|
||||
} from '@core/application';
|
||||
import { AppStoreModule } from './app-store.module';
|
||||
import { routes } from './app.routes';
|
||||
|
||||
import { rootReducer } from './store/root.reducer';
|
||||
import { RootState } from './store/root.state';
|
||||
import { ServiceWorkerModule } from '@angular/service-worker';
|
||||
import { environment } from '../environments/environment';
|
||||
import { AppSwaggerModule } from './app-swagger.module';
|
||||
import { AppDomainModule } from './app-domain.module';
|
||||
import { UiModalModule } from '@ui/modal';
|
||||
import {
|
||||
NotificationsHubModule,
|
||||
NOTIFICATIONS_HUB_OPTIONS,
|
||||
} from '@hub/notifications';
|
||||
import { SignalRHubOptions } from '@core/signalr';
|
||||
import { CoreBreadcrumbModule } from '@core/breadcrumb';
|
||||
import { provideCoreBreadcrumb } from '@core/breadcrumb';
|
||||
import { UiCommonModule } from '@ui/common';
|
||||
import { registerLocaleData } from '@angular/common';
|
||||
|
||||
import localeDe from '@angular/common/locales/de';
|
||||
import localeDeExtra from '@angular/common/locales/extra/de';
|
||||
import { HttpErrorInterceptor } from './interceptors';
|
||||
import { CoreLoggerModule, LOG_PROVIDER } from '@core/logger';
|
||||
import { IsaLogProvider } from './providers';
|
||||
@@ -58,19 +53,10 @@ import {
|
||||
} from '@adapter/scan';
|
||||
import * as Commands from './commands';
|
||||
import { NativeContainerService } from '@external/native-container';
|
||||
import { ShellModule } from '@shared/shell';
|
||||
import { MainComponent } from './main.component';
|
||||
import { IconModule } from '@shared/components/icon';
|
||||
import { NgIconsModule } from '@ng-icons/core';
|
||||
import {
|
||||
matClose,
|
||||
matWifi,
|
||||
matWifiOff,
|
||||
} from '@ng-icons/material-icons/baseline';
|
||||
import { NetworkStatusService } from './services/network-status.service';
|
||||
import { NetworkStatusService } from '@isa/core/connectivity';
|
||||
import { debounceTime, filter, firstValueFrom, switchMap } from 'rxjs';
|
||||
import { provideMatomo } from 'ngx-matomo-client';
|
||||
import { withRouter, withRouteData } from 'ngx-matomo-client';
|
||||
import { provideMatomo, withRouter, withRouteData } from 'ngx-matomo-client';
|
||||
import {
|
||||
provideLogging,
|
||||
withLogLevel,
|
||||
@@ -87,18 +73,61 @@ import {
|
||||
import { Store } from '@ngrx/store';
|
||||
import { OAuthService } from 'angular-oauth2-oidc';
|
||||
import z from 'zod';
|
||||
import { TitleStrategy } from '@angular/router';
|
||||
import { TabNavigationService } from '@isa/core/tabs';
|
||||
import { provideScrollPositionRestoration } from '@isa/utils/scroll-position';
|
||||
|
||||
registerLocaleData(localeDe, localeDeExtra);
|
||||
registerLocaleData(localeDe, 'de', localeDeExtra);
|
||||
// Domain modules
|
||||
import { provideDomainCheckout } from '@domain/checkout';
|
||||
|
||||
export function _appInitializerFactory(config: Config, injector: Injector) {
|
||||
// Swagger API configurations
|
||||
import { AvConfiguration } from '@generated/swagger/availability-api';
|
||||
import { CatConfiguration } from '@generated/swagger/cat-search-api';
|
||||
import { CheckoutConfiguration } from '@generated/swagger/checkout-api';
|
||||
import { CrmConfiguration } from '@generated/swagger/crm-api';
|
||||
import { EisConfiguration } from '@generated/swagger/eis-api';
|
||||
import { IsaConfiguration } from '@generated/swagger/isa-api';
|
||||
import { OmsConfiguration } from '@generated/swagger/oms-api';
|
||||
import { PrintConfiguration } from '@generated/swagger/print-api';
|
||||
import { RemiConfiguration } from '@generated/swagger/inventory-api';
|
||||
import { WwsConfiguration } from '@generated/swagger/wws-api';
|
||||
import { UiIconModule } from '@ui/icon';
|
||||
import { provideCoreTabs } from '@isa/core/tabs';
|
||||
|
||||
// --- Store Configuration ---
|
||||
|
||||
function storeHydrateMetaReducer(
|
||||
reducer: ActionReducer<RootState>,
|
||||
): ActionReducer<RootState> {
|
||||
return function (state, action) {
|
||||
if (action.type === 'HYDRATE') {
|
||||
return reducer(action['payload'], action);
|
||||
}
|
||||
return reducer(state, action);
|
||||
};
|
||||
}
|
||||
|
||||
const metaReducers: MetaReducer<RootState>[] = [storeHydrateMetaReducer];
|
||||
|
||||
// --- Swagger Configuration ---
|
||||
|
||||
const swaggerConfigSchema = z.object({ rootUrl: z.string() });
|
||||
|
||||
function createSwaggerConfigFactory(name: string) {
|
||||
return function () {
|
||||
return inject(Config).get(`@swagger/${name}`, swaggerConfigSchema);
|
||||
};
|
||||
}
|
||||
|
||||
const serviceWorkerBypassInterceptor: HttpInterceptorFn = (req, next) => {
|
||||
return next(req.clone({ setHeaders: { 'ngsw-bypass': 'true' } }));
|
||||
};
|
||||
|
||||
// --- App Initializer ---
|
||||
|
||||
function appInitializerFactory(_config: Config, injector: Injector) {
|
||||
return async () => {
|
||||
// Get logging service for initialization logging
|
||||
const logger = loggerFactory(() => ({ service: 'AppInitializer' }));
|
||||
const statusElement = document.querySelector('#init-status');
|
||||
const laoderElement = document.querySelector('#init-loader');
|
||||
const loaderElement = document.querySelector('#init-loader');
|
||||
|
||||
try {
|
||||
logger.info('Starting application initialization');
|
||||
@@ -106,7 +135,8 @@ export function _appInitializerFactory(config: Config, injector: Injector) {
|
||||
let online = false;
|
||||
const networkStatus = injector.get(NetworkStatusService);
|
||||
while (!online) {
|
||||
online = await firstValueFrom(networkStatus.online$);
|
||||
const status = await firstValueFrom(networkStatus.status$);
|
||||
online = status === 'online';
|
||||
|
||||
if (!online) {
|
||||
logger.warn('Waiting for network connection');
|
||||
@@ -161,7 +191,6 @@ export function _appInitializerFactory(config: Config, injector: Injector) {
|
||||
await userStorage.init();
|
||||
|
||||
const store = injector.get(Store);
|
||||
// Hydrate Ngrx Store
|
||||
const state = userStorage.get('store');
|
||||
if (state && state['version'] === version) {
|
||||
store.dispatch({ type: 'HYDRATE', payload: userStorage.get('store') });
|
||||
@@ -171,7 +200,7 @@ export function _appInitializerFactory(config: Config, injector: Injector) {
|
||||
reason: state ? 'version mismatch' : 'no stored state',
|
||||
}));
|
||||
}
|
||||
// Subscribe on Store changes and save to user storage
|
||||
|
||||
auth.initialized$
|
||||
.pipe(
|
||||
filter((initialized) => initialized),
|
||||
@@ -180,15 +209,11 @@ export function _appInitializerFactory(config: Config, injector: Injector) {
|
||||
.subscribe((state) => {
|
||||
userStorage.set('store', state);
|
||||
});
|
||||
|
||||
logger.info('Application initialization completed');
|
||||
// Inject tab navigation service to initialize it
|
||||
injector.get(TabNavigationService).init();
|
||||
} catch (error) {
|
||||
logger.error('Application initialization failed', error as Error, () => ({
|
||||
message: (error as Error).message,
|
||||
}));
|
||||
laoderElement.remove();
|
||||
loaderElement?.remove();
|
||||
statusElement.classList.add('text-xl');
|
||||
statusElement.innerHTML +=
|
||||
'⚡<br><br><b>Fehler bei der Initialisierung</b><br><br>Bitte prüfen Sie die Netzwerkverbindung (WLAN).<br><br>';
|
||||
@@ -223,7 +248,7 @@ export function _appInitializerFactory(config: Config, injector: Injector) {
|
||||
};
|
||||
}
|
||||
|
||||
export function _notificationsHubOptionsFactory(
|
||||
function notificationsHubOptionsFactory(
|
||||
config: Config,
|
||||
auth: AuthService,
|
||||
): SignalRHubOptions {
|
||||
@@ -257,80 +282,151 @@ const USER_SUB_FACTORY = () => {
|
||||
return signal(validation.data);
|
||||
};
|
||||
|
||||
@NgModule({
|
||||
declarations: [AppComponent, MainComponent],
|
||||
bootstrap: [AppComponent],
|
||||
imports: [
|
||||
BrowserModule,
|
||||
BrowserAnimationsModule,
|
||||
ShellModule.forRoot(),
|
||||
AppRoutingModule,
|
||||
AppSwaggerModule,
|
||||
AppDomainModule,
|
||||
CoreBreadcrumbModule.forRoot(),
|
||||
CoreCommandModule.forRoot(Object.values(Commands)),
|
||||
CoreLoggerModule.forRoot(),
|
||||
AppStoreModule,
|
||||
AuthModule.forRoot(),
|
||||
CoreApplicationModule.forRoot(),
|
||||
UiModalModule.forRoot(),
|
||||
UiCommonModule.forRoot(),
|
||||
NotificationsHubModule.forRoot(),
|
||||
ServiceWorkerModule.register('ngsw-worker.js', {
|
||||
enabled: environment.production,
|
||||
registrationStrategy: 'registerWhenStable:30000',
|
||||
}),
|
||||
ScanAdapterModule.forRoot(),
|
||||
ScanditScanAdapterModule.forRoot(),
|
||||
PlatformModule,
|
||||
IconModule.forRoot(),
|
||||
NgIconsModule.withIcons({ matWifiOff, matClose, matWifi }),
|
||||
],
|
||||
export const appConfig: ApplicationConfig = {
|
||||
providers: [
|
||||
provideZoneChangeDetection({ eventCoalescing: true }),
|
||||
provideAnimationsAsync('animations'),
|
||||
provideRouter(routes, withComponentInputBinding()),
|
||||
provideHttpClient(
|
||||
withInterceptorsFromDi(),
|
||||
withInterceptors([serviceWorkerBypassInterceptor]),
|
||||
),
|
||||
provideScrollPositionRestoration(),
|
||||
|
||||
// NgRx Store
|
||||
provideStore(rootReducer, { metaReducers }),
|
||||
provideCoreBreadcrumb(),
|
||||
provideDomainCheckout(),
|
||||
provideStoreDevtools({
|
||||
name: 'ISA Ngrx Application Store',
|
||||
connectInZone: true,
|
||||
}),
|
||||
|
||||
// Swagger API configurations
|
||||
{
|
||||
provide: AvConfiguration,
|
||||
useFactory: createSwaggerConfigFactory('av'),
|
||||
},
|
||||
{
|
||||
provide: CatConfiguration,
|
||||
useFactory: createSwaggerConfigFactory('cat'),
|
||||
},
|
||||
{
|
||||
provide: CheckoutConfiguration,
|
||||
useFactory: createSwaggerConfigFactory('checkout'),
|
||||
},
|
||||
{
|
||||
provide: CrmConfiguration,
|
||||
useFactory: createSwaggerConfigFactory('crm'),
|
||||
},
|
||||
{
|
||||
provide: EisConfiguration,
|
||||
useFactory: createSwaggerConfigFactory('eis'),
|
||||
},
|
||||
{
|
||||
provide: IsaConfiguration,
|
||||
useFactory: createSwaggerConfigFactory('isa'),
|
||||
},
|
||||
{
|
||||
provide: OmsConfiguration,
|
||||
useFactory: createSwaggerConfigFactory('oms'),
|
||||
},
|
||||
{
|
||||
provide: PrintConfiguration,
|
||||
useFactory: createSwaggerConfigFactory('print'),
|
||||
},
|
||||
{
|
||||
provide: RemiConfiguration,
|
||||
useFactory: createSwaggerConfigFactory('remi'),
|
||||
},
|
||||
{
|
||||
provide: WwsConfiguration,
|
||||
useFactory: createSwaggerConfigFactory('wws'),
|
||||
},
|
||||
|
||||
// App initializer
|
||||
provideAppInitializer(() => {
|
||||
const initializerFn = _appInitializerFactory(
|
||||
const initializerFn = appInitializerFactory(
|
||||
inject(Config),
|
||||
inject(Injector),
|
||||
);
|
||||
return initializerFn();
|
||||
}),
|
||||
|
||||
// Notifications hub
|
||||
{
|
||||
provide: NOTIFICATIONS_HUB_OPTIONS,
|
||||
useFactory: _notificationsHubOptionsFactory,
|
||||
useFactory: notificationsHubOptionsFactory,
|
||||
deps: [Config, AuthService],
|
||||
},
|
||||
|
||||
// HTTP interceptors
|
||||
{
|
||||
provide: HTTP_INTERCEPTORS,
|
||||
useClass: HttpErrorInterceptor,
|
||||
multi: true,
|
||||
},
|
||||
|
||||
// Logging
|
||||
{
|
||||
provide: LOG_PROVIDER,
|
||||
useClass: IsaLogProvider,
|
||||
multi: true,
|
||||
},
|
||||
provideLogging(
|
||||
withLogLevel(isDevMode() ? LogLevel.Debug : LogLevel.Info),
|
||||
withSink(ConsoleLogSink),
|
||||
),
|
||||
|
||||
// Error handling
|
||||
{
|
||||
provide: ErrorHandler,
|
||||
useClass: IsaErrorHandler,
|
||||
},
|
||||
{
|
||||
provide: ApplicationService,
|
||||
useClass: ApplicationServiceAdapter,
|
||||
},
|
||||
|
||||
// Locale settings
|
||||
{ provide: LOCALE_ID, useValue: 'de-DE' },
|
||||
provideHttpClient(withInterceptorsFromDi()),
|
||||
{ provide: DEFAULT_CURRENCY_CODE, useValue: 'EUR' },
|
||||
|
||||
// Analytics
|
||||
provideMatomo(
|
||||
{ trackerUrl: 'https://matomo.paragon-data.net', siteId: '1' },
|
||||
withRouter(),
|
||||
withRouteData(),
|
||||
),
|
||||
provideLogging(withLogLevel(LogLevel.Debug), withSink(ConsoleLogSink)),
|
||||
{
|
||||
provide: DEFAULT_CURRENCY_CODE,
|
||||
useValue: 'EUR',
|
||||
},
|
||||
|
||||
// User storage
|
||||
provideUserSubFactory(USER_SUB_FACTORY),
|
||||
{ provide: TitleStrategy, useClass: IsaTitleStrategy },
|
||||
|
||||
// Title strategy
|
||||
provideCoreTabs(),
|
||||
|
||||
// Import providers from NgModules
|
||||
importProvidersFrom(
|
||||
// Core modules
|
||||
CoreCommandModule.forRoot(Object.values(Commands)),
|
||||
CoreLoggerModule.forRoot(),
|
||||
AuthModule.forRoot(),
|
||||
|
||||
// UI modules
|
||||
UiModalModule.forRoot(),
|
||||
UiCommonModule.forRoot(),
|
||||
|
||||
// Hub modules
|
||||
NotificationsHubModule.forRoot(),
|
||||
|
||||
// Service Worker
|
||||
ServiceWorkerModule.register('ngsw-worker.js', {
|
||||
enabled: environment.production,
|
||||
registrationStrategy: 'registerWhenStable:30000',
|
||||
}),
|
||||
|
||||
// Scan adapter
|
||||
ScanAdapterModule.forRoot(),
|
||||
ScanditScanAdapterModule.forRoot(),
|
||||
|
||||
UiIconModule.forRoot(),
|
||||
IconModule.forRoot(),
|
||||
),
|
||||
],
|
||||
})
|
||||
export class AppModule {}
|
||||
};
|
||||
0
apps/isa-app/src/app/app.css
Normal file
0
apps/isa-app/src/app/app.css
Normal file
3
apps/isa-app/src/app/app.html
Normal file
3
apps/isa-app/src/app/app.html
Normal file
@@ -0,0 +1,3 @@
|
||||
<shell-layout>
|
||||
<router-outlet />
|
||||
</shell-layout>
|
||||
@@ -1,5 +1,4 @@
|
||||
import { NgModule } from '@angular/core';
|
||||
import { RouterModule, Routes } from '@angular/router';
|
||||
import { Routes } from '@angular/router';
|
||||
import {
|
||||
CanActivateCartGuard,
|
||||
CanActivateCartWithProcessIdGuard,
|
||||
@@ -11,13 +10,12 @@ import {
|
||||
CanActivateProductWithProcessIdGuard,
|
||||
IsAuthenticatedGuard,
|
||||
} from './guards';
|
||||
import { MainComponent } from './main.component';
|
||||
import {
|
||||
BranchSectionResolver,
|
||||
CustomerSectionResolver,
|
||||
ProcessIdResolver,
|
||||
} from './resolvers';
|
||||
import { TokenLoginComponent, TokenLoginModule } from './token-login';
|
||||
import { TokenLoginComponent } from './token-login';
|
||||
import {
|
||||
ActivateProcessIdGuard,
|
||||
ActivateProcessIdWithConfigKeyGuard,
|
||||
@@ -27,10 +25,10 @@ import {
|
||||
tabResolverFn,
|
||||
processResolverFn,
|
||||
hasTabIdGuard,
|
||||
deactivateTabGuard,
|
||||
} from '@isa/core/tabs';
|
||||
import { provideScrollPositionRestoration } from '@isa/utils/scroll-position';
|
||||
|
||||
const routes: Routes = [
|
||||
export const routes: Routes = [
|
||||
{ path: '', redirectTo: 'kunde/dashboard', pathMatch: 'full' },
|
||||
{
|
||||
path: 'login',
|
||||
@@ -45,12 +43,12 @@ const routes: Routes = [
|
||||
children: [
|
||||
{
|
||||
path: 'kunde',
|
||||
component: MainComponent,
|
||||
children: [
|
||||
{
|
||||
path: 'dashboard',
|
||||
loadChildren: () =>
|
||||
import('@page/dashboard').then((m) => m.DashboardModule),
|
||||
canActivate: [deactivateTabGuard],
|
||||
data: {
|
||||
matomo: {
|
||||
title: 'Dashboard',
|
||||
@@ -72,8 +70,6 @@ const routes: Routes = [
|
||||
processId: ProcessIdResolver,
|
||||
},
|
||||
},
|
||||
// TODO: Check if order and :processId/order is still being used
|
||||
// If not, remove these routes and the related guards and resolvers
|
||||
{
|
||||
path: 'order',
|
||||
loadChildren: () =>
|
||||
@@ -122,7 +118,6 @@ const routes: Routes = [
|
||||
{
|
||||
path: 'pickup-shelf',
|
||||
canActivate: [ActivateProcessIdGuard],
|
||||
// NOTE: This is a workaround for the canActivate guard not being called
|
||||
loadChildren: () =>
|
||||
import('@page/pickup-shelf').then((m) => m.PickupShelfOutModule),
|
||||
},
|
||||
@@ -141,7 +136,6 @@ const routes: Routes = [
|
||||
},
|
||||
{
|
||||
path: 'filiale',
|
||||
component: MainComponent,
|
||||
children: [
|
||||
{
|
||||
path: 'task-calendar',
|
||||
@@ -154,7 +148,6 @@ const routes: Routes = [
|
||||
{
|
||||
path: 'pickup-shelf',
|
||||
canActivate: [ActivateProcessIdWithConfigKeyGuard('pickupShelf')],
|
||||
// NOTE: This is a workaround for the canActivate guard not being called
|
||||
loadChildren: () =>
|
||||
import('@page/pickup-shelf').then((m) => m.PickupShelfInModule),
|
||||
},
|
||||
@@ -188,7 +181,6 @@ const routes: Routes = [
|
||||
},
|
||||
{
|
||||
path: ':tabId',
|
||||
component: MainComponent,
|
||||
resolve: { process: processResolverFn, tab: tabResolverFn },
|
||||
canActivate: [IsAuthenticatedGuard, hasTabIdGuard],
|
||||
children: [
|
||||
@@ -218,7 +210,6 @@ const routes: Routes = [
|
||||
},
|
||||
],
|
||||
},
|
||||
|
||||
{
|
||||
path: 'return',
|
||||
loadChildren: () =>
|
||||
@@ -246,16 +237,3 @@ const routes: Routes = [
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
@NgModule({
|
||||
imports: [
|
||||
RouterModule.forRoot(routes, {
|
||||
bindToComponentInputs: true,
|
||||
enableTracing: false,
|
||||
}),
|
||||
TokenLoginModule,
|
||||
],
|
||||
exports: [RouterModule],
|
||||
providers: [provideScrollPositionRestoration()],
|
||||
})
|
||||
export class AppRoutingModule {}
|
||||
11
apps/isa-app/src/app/app.ts
Normal file
11
apps/isa-app/src/app/app.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { Component } from '@angular/core';
|
||||
import { RouterOutlet } from '@angular/router';
|
||||
import { ShellLayoutComponent } from '@isa/shell/layout';
|
||||
|
||||
@Component({
|
||||
selector: 'app-root',
|
||||
templateUrl: './app.html',
|
||||
styleUrls: ['./app.css'],
|
||||
imports: [RouterOutlet, ShellLayoutComponent],
|
||||
})
|
||||
export class App {}
|
||||
@@ -5,7 +5,7 @@ import { ScanAdapterService } from '@adapter/scan';
|
||||
import { AuthService as IsaAuthService } from '@generated/swagger/isa-api';
|
||||
import { UiConfirmModalComponent, UiErrorModalComponent, UiModalResult, UiModalService } from '@ui/modal';
|
||||
import { EnvironmentService } from '@core/environment';
|
||||
import { injectNetworkStatus$ } from '../services/network-status.service';
|
||||
import { injectNetworkStatus$ } from '@isa/core/connectivity';
|
||||
import { toSignal } from '@angular/core/rxjs-interop';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
|
||||
@@ -9,7 +9,7 @@ import {
|
||||
import { from, NEVER, Observable, throwError } from 'rxjs';
|
||||
import { catchError, filter, mergeMap, takeUntil } from 'rxjs/operators';
|
||||
import { AuthService, LoginStrategy } from '@core/auth';
|
||||
import { injectOnline$ } from '../services/network-status.service';
|
||||
import { injectNetworkStatus$ } from '@isa/core/connectivity';
|
||||
import { logger } from '@isa/core/logging';
|
||||
|
||||
@Injectable()
|
||||
@@ -17,7 +17,7 @@ export class HttpErrorInterceptor implements HttpInterceptor {
|
||||
#logger = logger(() => ({
|
||||
'http-interceptor': 'HttpErrorInterceptor',
|
||||
}));
|
||||
#offline$ = injectOnline$().pipe(filter((online) => !online));
|
||||
#offline$ = injectNetworkStatus$().pipe(filter((status) => status === 'offline'));
|
||||
#injector = inject(Injector);
|
||||
#auth = inject(AuthService);
|
||||
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
<shell-root>
|
||||
<router-outlet></router-outlet>
|
||||
</shell-root>
|
||||
@@ -1,11 +0,0 @@
|
||||
import { Component, ChangeDetectionStrategy } from '@angular/core';
|
||||
|
||||
@Component({
|
||||
selector: 'app-main',
|
||||
templateUrl: 'main.component.html',
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
standalone: false,
|
||||
})
|
||||
export class MainComponent {
|
||||
constructor() {}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
export * from './network-status.service';
|
||||
@@ -1,25 +0,0 @@
|
||||
import { inject, Injectable } from '@angular/core';
|
||||
import { map, Observable } from 'rxjs';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class NetworkStatusService {
|
||||
online$ = new Observable<boolean>((subscriber) => {
|
||||
const handler = () => subscriber.next(navigator.onLine);
|
||||
|
||||
window.addEventListener('online', handler);
|
||||
window.addEventListener('offline', handler);
|
||||
|
||||
handler();
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('online', handler);
|
||||
window.removeEventListener('offline', handler);
|
||||
};
|
||||
});
|
||||
|
||||
status$ = this.online$.pipe(map((online) => (online ? 'online' : 'offline')));
|
||||
}
|
||||
|
||||
export const injectNetworkStatus$ = () => inject(NetworkStatusService).status$;
|
||||
|
||||
export const injectOnline$ = () => inject(NetworkStatusService).online$;
|
||||
@@ -1,2 +1 @@
|
||||
export * from './token-login.component';
|
||||
export * from './token-login.module';
|
||||
|
||||
@@ -1,29 +1,35 @@
|
||||
import { Component, ChangeDetectionStrategy, OnInit } from '@angular/core';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
import { AuthService } from '@core/auth';
|
||||
|
||||
@Component({
|
||||
selector: 'app-token-login',
|
||||
templateUrl: 'token-login.component.html',
|
||||
styleUrls: ['token-login.component.scss'],
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
standalone: false,
|
||||
})
|
||||
export class TokenLoginComponent implements OnInit {
|
||||
constructor(
|
||||
private _route: ActivatedRoute,
|
||||
private _authService: AuthService,
|
||||
private _router: Router,
|
||||
) {}
|
||||
|
||||
ngOnInit() {
|
||||
if (this._route.snapshot.params.token && !this._authService.isAuthenticated()) {
|
||||
this._authService.setKeyCardToken(this._route.snapshot.params.token);
|
||||
this._authService.login();
|
||||
} else if (!this._authService.isAuthenticated()) {
|
||||
this._authService.login();
|
||||
} else if (this._authService.isAuthenticated()) {
|
||||
this._router.navigate(['/']);
|
||||
}
|
||||
}
|
||||
}
|
||||
import {
|
||||
Component,
|
||||
ChangeDetectionStrategy,
|
||||
OnInit,
|
||||
inject,
|
||||
} from '@angular/core';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
import { AuthService } from '@core/auth';
|
||||
|
||||
@Component({
|
||||
selector: 'app-token-login',
|
||||
templateUrl: 'token-login.component.html',
|
||||
styleUrls: ['token-login.component.scss'],
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
standalone: true,
|
||||
})
|
||||
export class TokenLoginComponent implements OnInit {
|
||||
readonly #route = inject(ActivatedRoute);
|
||||
readonly #authService = inject(AuthService);
|
||||
readonly #router = inject(Router);
|
||||
|
||||
ngOnInit() {
|
||||
if (
|
||||
this.#route.snapshot.params.token &&
|
||||
!this.#authService.isAuthenticated()
|
||||
) {
|
||||
this.#authService.setKeyCardToken(this.#route.snapshot.params.token);
|
||||
this.#authService.login();
|
||||
} else if (!this.#authService.isAuthenticated()) {
|
||||
this.#authService.login();
|
||||
} else if (this.#authService.isAuthenticated()) {
|
||||
this.#router.navigate(['/']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
import { NgModule } from '@angular/core';
|
||||
import { CommonModule } from '@angular/common';
|
||||
|
||||
import { TokenLoginComponent } from './token-login.component';
|
||||
|
||||
@NgModule({
|
||||
imports: [CommonModule],
|
||||
exports: [TokenLoginComponent],
|
||||
declarations: [TokenLoginComponent],
|
||||
})
|
||||
export class TokenLoginModule {}
|
||||
@@ -1,23 +0,0 @@
|
||||
import { NgModule, ModuleWithProviders } from '@angular/core';
|
||||
import { StoreModule } from '@ngrx/store';
|
||||
import { applicationReducer } from './store';
|
||||
import { ApplicationService } from './application.service';
|
||||
|
||||
@NgModule({
|
||||
declarations: [],
|
||||
imports: [],
|
||||
exports: [],
|
||||
})
|
||||
export class CoreApplicationModule {
|
||||
static forRoot(): ModuleWithProviders<CoreApplicationModule> {
|
||||
return {
|
||||
ngModule: RootCoreApplicationModule,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@NgModule({
|
||||
imports: [StoreModule.forFeature('core-application', applicationReducer)],
|
||||
providers: [ApplicationService],
|
||||
})
|
||||
export class RootCoreApplicationModule {}
|
||||
@@ -1,337 +0,0 @@
|
||||
import { inject, Injectable } from '@angular/core';
|
||||
import { BehaviorSubject, Observable, of, firstValueFrom } from 'rxjs';
|
||||
import { map, filter, withLatestFrom } from 'rxjs/operators';
|
||||
import { BranchDTO } from '@generated/swagger/checkout-api';
|
||||
import { isBoolean, isNumber } from '@utils/common';
|
||||
import { ApplicationService } from './application.service';
|
||||
import { TabService } from '@isa/core/tabs';
|
||||
import { ApplicationProcess } from './defs/application-process';
|
||||
import { Tab, TabMetadata } from '@isa/core/tabs';
|
||||
import { toObservable } from '@angular/core/rxjs-interop';
|
||||
import { Store } from '@ngrx/store';
|
||||
import { removeProcess } from './store/application.actions';
|
||||
|
||||
/**
|
||||
* Adapter service that bridges the old ApplicationService interface with the new TabService.
|
||||
*
|
||||
* This adapter allows existing code that depends on ApplicationService to work with the new
|
||||
* TabService without requiring immediate code changes. It maps ApplicationProcess concepts
|
||||
* to Tab entities, storing process-specific data in tab metadata.
|
||||
*
|
||||
* Key mappings:
|
||||
* - ApplicationProcess.id <-> Tab.id
|
||||
* - ApplicationProcess.name <-> Tab.name
|
||||
* - ApplicationProcess metadata (section, type, etc.) <-> Tab.metadata with 'process_' prefix
|
||||
* - ApplicationProcess.data <-> Tab.metadata with 'data_' prefix
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // Inject the adapter instead of the original service
|
||||
* constructor(private applicationService: ApplicationServiceAdapter) {}
|
||||
*
|
||||
* // Use the same API as before
|
||||
* const process = await this.applicationService.createCustomerProcess();
|
||||
* this.applicationService.activateProcess(process.id);
|
||||
* ```
|
||||
*/
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class ApplicationServiceAdapter extends ApplicationService {
|
||||
#store = inject(Store);
|
||||
|
||||
#tabService = inject(TabService);
|
||||
|
||||
#activatedProcessId$ = toObservable(this.#tabService.activatedTabId);
|
||||
|
||||
#tabs$ = toObservable(this.#tabService.entities);
|
||||
|
||||
#processes$ = this.#tabs$.pipe(
|
||||
map((tabs) => tabs.map((tab) => this.mapTabToProcess(tab))),
|
||||
);
|
||||
|
||||
#section = new BehaviorSubject<'customer' | 'branch'>('customer');
|
||||
|
||||
readonly REGEX_PROCESS_NAME = /^Vorgang \d+$/;
|
||||
|
||||
get activatedProcessId() {
|
||||
return this.#tabService.activatedTabId();
|
||||
}
|
||||
|
||||
get activatedProcessId$() {
|
||||
return this.#activatedProcessId$;
|
||||
}
|
||||
|
||||
getProcesses$(
|
||||
section?: 'customer' | 'branch',
|
||||
): Observable<ApplicationProcess[]> {
|
||||
return this.#processes$.pipe(
|
||||
map((processes) =>
|
||||
processes.filter((process) =>
|
||||
section ? process.section === section : true,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
getProcessById$(processId: number): Observable<ApplicationProcess> {
|
||||
return this.#processes$.pipe(
|
||||
map((processes) => processes.find((process) => process.id === processId)),
|
||||
);
|
||||
}
|
||||
|
||||
getSection$(): Observable<'customer' | 'branch'> {
|
||||
return this.#section.asObservable();
|
||||
}
|
||||
|
||||
getTitle$(): Observable<'Kundenbereich' | 'Filialbereich'> {
|
||||
return this.getSection$().pipe(
|
||||
map((section) =>
|
||||
section === 'customer' ? 'Kundenbereich' : 'Filialbereich',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/** @deprecated */
|
||||
getActivatedProcessId$(): Observable<number> {
|
||||
return this.activatedProcessId$;
|
||||
}
|
||||
|
||||
activateProcess(activatedProcessId: number): void {
|
||||
this.#tabService.activateTab(activatedProcessId);
|
||||
}
|
||||
|
||||
removeProcess(processId: number): void {
|
||||
this.#tabService.removeTab(processId);
|
||||
this.#store.dispatch(removeProcess({ processId }));
|
||||
}
|
||||
|
||||
patchProcess(processId: number, changes: Partial<ApplicationProcess>): void {
|
||||
const tabChanges: {
|
||||
name?: string;
|
||||
tags?: string[];
|
||||
metadata?: Record<string, unknown>;
|
||||
} = {};
|
||||
|
||||
if (changes.name) {
|
||||
tabChanges.name = changes.name;
|
||||
}
|
||||
|
||||
// Store other ApplicationProcess properties in metadata
|
||||
const metadataKeys = [
|
||||
'section',
|
||||
'type',
|
||||
'closeable',
|
||||
'confirmClosing',
|
||||
'created',
|
||||
'activated',
|
||||
'data',
|
||||
];
|
||||
metadataKeys.forEach((key) => {
|
||||
if (tabChanges.metadata === undefined) {
|
||||
tabChanges.metadata = {};
|
||||
}
|
||||
|
||||
if (changes[key as keyof ApplicationProcess] !== undefined) {
|
||||
tabChanges.metadata[`process_${key}`] =
|
||||
changes[key as keyof ApplicationProcess];
|
||||
}
|
||||
});
|
||||
|
||||
// Apply the changes to the tab
|
||||
this.#tabService.patchTab(processId, tabChanges);
|
||||
}
|
||||
|
||||
patchProcessData(processId: number, data: Record<string, unknown>): void {
|
||||
const currentProcess = this.#tabService.entityMap()[processId];
|
||||
const currentData: TabMetadata =
|
||||
(currentProcess?.metadata?.['process_data'] as TabMetadata) ?? {};
|
||||
|
||||
this.#tabService.patchTab(processId, {
|
||||
metadata: { [`process_data`]: { ...currentData, ...data } },
|
||||
});
|
||||
}
|
||||
|
||||
getSelectedBranch$(): Observable<BranchDTO> {
|
||||
return this.#processes$.pipe(
|
||||
withLatestFrom(this.#activatedProcessId$),
|
||||
map(([processes, activatedProcessId]) =>
|
||||
processes.find((process) => process.id === activatedProcessId),
|
||||
),
|
||||
filter((process): process is ApplicationProcess => !!process),
|
||||
map((process) => process.data?.selectedBranch as BranchDTO),
|
||||
);
|
||||
}
|
||||
|
||||
async createCustomerProcess(processId?: number): Promise<ApplicationProcess> {
|
||||
const processes = await firstValueFrom(this.getProcesses$('customer'));
|
||||
|
||||
const processIds = processes
|
||||
.filter((x) => this.REGEX_PROCESS_NAME.test(x.name))
|
||||
.map((x) => +x.name.split(' ')[1]);
|
||||
|
||||
const maxId = processIds.length > 0 ? Math.max(...processIds) : 0;
|
||||
|
||||
const process: ApplicationProcess = {
|
||||
id: processId ?? Date.now(),
|
||||
type: 'cart',
|
||||
name: `Vorgang ${maxId + 1}`,
|
||||
section: 'customer',
|
||||
closeable: true,
|
||||
};
|
||||
|
||||
await this.createProcess(process);
|
||||
return process;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new ApplicationProcess by first creating a Tab and then storing
|
||||
* process-specific properties in the tab's metadata.
|
||||
*
|
||||
* @param process - The ApplicationProcess to create
|
||||
* @throws {Error} If process ID already exists or is invalid
|
||||
*/
|
||||
async createProcess(process: ApplicationProcess): Promise<void> {
|
||||
const existingProcess = this.#tabService.entityMap()[process.id];
|
||||
if (existingProcess?.id === process?.id) {
|
||||
throw new Error('Process Id existiert bereits');
|
||||
}
|
||||
|
||||
if (!isNumber(process.id)) {
|
||||
throw new Error('Process Id nicht gesetzt');
|
||||
}
|
||||
|
||||
if (!isBoolean(process.closeable)) {
|
||||
process.closeable = true;
|
||||
}
|
||||
|
||||
if (!isBoolean(process.confirmClosing)) {
|
||||
process.confirmClosing = true;
|
||||
}
|
||||
|
||||
process.created = this.createTimestamp();
|
||||
process.activated = 0;
|
||||
|
||||
// Create tab with process data and preserve the process ID
|
||||
this.#tabService.addTab({
|
||||
id: process.id,
|
||||
name: process.name,
|
||||
tags: [process.section, process.type].filter(Boolean),
|
||||
metadata: {
|
||||
process_section: process.section,
|
||||
process_type: process.type,
|
||||
process_closeable: process.closeable,
|
||||
process_confirmClosing: process.confirmClosing,
|
||||
process_created: process.created,
|
||||
process_activated: process.activated,
|
||||
process_data: process.data,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
setSection(section: 'customer' | 'branch'): void {
|
||||
this.#section.next(section);
|
||||
}
|
||||
|
||||
getLastActivatedProcessWithSectionAndType$(
|
||||
section: 'customer' | 'branch',
|
||||
type: string,
|
||||
): Observable<ApplicationProcess> {
|
||||
return this.getProcesses$(section).pipe(
|
||||
map((processes) =>
|
||||
processes
|
||||
?.filter((process) => process.type === type)
|
||||
?.reduce((latest, current) => {
|
||||
if (!latest) {
|
||||
return current;
|
||||
}
|
||||
return latest?.activated > current?.activated ? latest : current;
|
||||
}, undefined),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
getLastActivatedProcessWithSection$(
|
||||
section: 'customer' | 'branch',
|
||||
): Observable<ApplicationProcess> {
|
||||
return this.getProcesses$(section).pipe(
|
||||
map((processes) =>
|
||||
processes?.reduce((latest, current) => {
|
||||
if (!latest) {
|
||||
return current;
|
||||
}
|
||||
return latest?.activated > current?.activated ? latest : current;
|
||||
}, undefined),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps Tab entities to ApplicationProcess objects by extracting process-specific
|
||||
* metadata and combining it with tab properties.
|
||||
*
|
||||
* @param tab - The tab entity to convert
|
||||
* @returns The corresponding ApplicationProcess object
|
||||
*/
|
||||
private mapTabToProcess(tab: Tab): ApplicationProcess {
|
||||
return {
|
||||
id: tab.id,
|
||||
name: tab.name,
|
||||
created:
|
||||
this.getMetadataValue<number>(tab.metadata, 'process_created') ??
|
||||
tab.createdAt,
|
||||
activated:
|
||||
this.getMetadataValue<number>(tab.metadata, 'process_activated') ??
|
||||
tab.activatedAt ??
|
||||
0,
|
||||
section:
|
||||
this.getMetadataValue<'customer' | 'branch'>(
|
||||
tab.metadata,
|
||||
'process_section',
|
||||
) ?? 'customer',
|
||||
type: this.getMetadataValue<string>(tab.metadata, 'process_type'),
|
||||
closeable:
|
||||
this.getMetadataValue<boolean>(tab.metadata, 'process_closeable') ??
|
||||
true,
|
||||
confirmClosing:
|
||||
this.getMetadataValue<boolean>(
|
||||
tab.metadata,
|
||||
'process_confirmClosing',
|
||||
) ?? true,
|
||||
data: this.extractDataFromMetadata(tab.metadata),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts ApplicationProcess data properties from tab metadata.
|
||||
* Data properties are stored with a 'data_' prefix in tab metadata.
|
||||
*
|
||||
* @param metadata - The tab metadata object
|
||||
* @returns The extracted data object or undefined if no data properties exist
|
||||
*/
|
||||
private extractDataFromMetadata(
|
||||
metadata: TabMetadata,
|
||||
): Record<string, unknown> | undefined {
|
||||
// Return the complete data object stored under 'process_data'
|
||||
const processData = metadata?.['process_data'];
|
||||
|
||||
if (
|
||||
processData &&
|
||||
typeof processData === 'object' &&
|
||||
processData !== null
|
||||
) {
|
||||
return processData as Record<string, unknown>;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
private getMetadataValue<T>(
|
||||
metadata: TabMetadata,
|
||||
key: string,
|
||||
): T | undefined {
|
||||
return metadata?.[key] as T | undefined;
|
||||
}
|
||||
|
||||
private createTimestamp(): number {
|
||||
return Date.now();
|
||||
}
|
||||
}
|
||||
@@ -1,233 +0,0 @@
|
||||
// import { createServiceFactory, SpectatorService, SpyObject } from '@ngneat/spectator';
|
||||
// import { Store } from '@ngrx/store';
|
||||
// import { Observable, of } from 'rxjs';
|
||||
// import { first } from 'rxjs/operators';
|
||||
// import { ApplicationProcess } from './defs';
|
||||
|
||||
// import { ApplicationService } from './application.service';
|
||||
// import * as actions from './store/application.actions';
|
||||
|
||||
// describe('ApplicationService', () => {
|
||||
// let spectator: SpectatorService<ApplicationService>;
|
||||
// let store: SpyObject<Store>;
|
||||
// const createService = createServiceFactory({
|
||||
// service: ApplicationService,
|
||||
// mocks: [Store],
|
||||
// });
|
||||
|
||||
// beforeEach(() => {
|
||||
// spectator = createService({});
|
||||
// store = spectator.inject(Store);
|
||||
// });
|
||||
|
||||
// it('should be created', () => {
|
||||
// expect(spectator.service).toBeTruthy();
|
||||
// });
|
||||
|
||||
// describe('activatedProcessId$', () => {
|
||||
// it('should return an observable', () => {
|
||||
// expect(spectator.service.activatedProcessId$).toBeInstanceOf(Observable);
|
||||
// });
|
||||
// });
|
||||
|
||||
// describe('activatedProcessId', () => {
|
||||
// it('should return the process id as a number', () => {
|
||||
// spyOnProperty(spectator.service['activatedProcessIdSubject'] as any, 'value').and.returnValue(2);
|
||||
// expect(spectator.service.activatedProcessId).toBe(2);
|
||||
// });
|
||||
// });
|
||||
|
||||
// describe('getProcesses$()', () => {
|
||||
// it('should call select on store and return all selected processes', async () => {
|
||||
// const processes: ApplicationProcess[] = [
|
||||
// { id: 1, name: 'Vorgang', type: 'cart', section: 'customer', data: { count: 1 } },
|
||||
// { id: 2, name: 'Vorgang', type: 'task-calendar', section: 'branch' },
|
||||
// ];
|
||||
// store.select.and.returnValue(of(processes));
|
||||
// const result = await spectator.service.getProcesses$().pipe(first()).toPromise();
|
||||
// expect(result).toEqual(processes);
|
||||
// expect(store.select).toHaveBeenCalled();
|
||||
// });
|
||||
|
||||
// it('should call select on store and return all section customer processes', async () => {
|
||||
// const processes: ApplicationProcess[] = [
|
||||
// { id: 1, name: 'Vorgang', type: 'cart', section: 'customer', data: { count: 1 } },
|
||||
// { id: 2, name: 'Vorgang', type: 'task-calendar', section: 'branch' },
|
||||
// ];
|
||||
// store.select.and.returnValue(of(processes));
|
||||
// const result = await spectator.service.getProcesses$('customer').pipe(first()).toPromise();
|
||||
// expect(result).toEqual([processes[0]]);
|
||||
// expect(store.select).toHaveBeenCalled();
|
||||
// });
|
||||
|
||||
// it('should call select on store and return all section branch processes', async () => {
|
||||
// const processes: ApplicationProcess[] = [
|
||||
// { id: 1, name: 'Vorgang', type: 'cart', section: 'customer', data: { count: 1 } },
|
||||
// { id: 2, name: 'Vorgang', type: 'task-calendar', section: 'branch' },
|
||||
// ];
|
||||
// store.select.and.returnValue(of(processes));
|
||||
// const result = await spectator.service.getProcesses$('branch').pipe(first()).toPromise();
|
||||
// expect(result).toEqual([processes[1]]);
|
||||
// expect(store.select).toHaveBeenCalled();
|
||||
// });
|
||||
// });
|
||||
|
||||
// describe('getProcessById$()', () => {
|
||||
// it('should return the process by id', async () => {
|
||||
// const processes: ApplicationProcess[] = [
|
||||
// { id: 1, name: 'Vorgang 1', section: 'customer' },
|
||||
// { id: 2, name: 'Vorgang 2', section: 'customer' },
|
||||
// ];
|
||||
// spyOn(spectator.service, 'getProcesses$').and.returnValue(of(processes));
|
||||
|
||||
// const process = await spectator.service.getProcessById$(1).toPromise();
|
||||
// expect(process.id).toBe(1);
|
||||
// });
|
||||
// });
|
||||
|
||||
// describe('getSection$()', () => {
|
||||
// it('should return the selected section branch', async () => {
|
||||
// const section = 'branch';
|
||||
// store.select.and.returnValue(of(section));
|
||||
// const result = await spectator.service.getSection$().pipe(first()).toPromise();
|
||||
// expect(result).toEqual(section);
|
||||
// expect(store.select).toHaveBeenCalled();
|
||||
// });
|
||||
// });
|
||||
|
||||
// describe('getActivatedProcessId$', () => {
|
||||
// it('should return the current selected activated process id', async () => {
|
||||
// const activatedProcessId = 2;
|
||||
// store.select.and.returnValue(of({ id: activatedProcessId }));
|
||||
// const result = await spectator.service.getActivatedProcessId$().pipe(first()).toPromise();
|
||||
// expect(result).toEqual(activatedProcessId);
|
||||
// expect(store.select).toHaveBeenCalled();
|
||||
// });
|
||||
// });
|
||||
|
||||
// describe('activateProcess()', () => {
|
||||
// it('should dispatch action setActivatedProcess with argument activatedProcessId and action type', () => {
|
||||
// const activatedProcessId = 2;
|
||||
// spectator.service.activateProcess(activatedProcessId);
|
||||
// expect(store.dispatch).toHaveBeenCalledWith({ activatedProcessId, type: actions.setActivatedProcess.type });
|
||||
// });
|
||||
// });
|
||||
|
||||
// describe('removeProcess()', () => {
|
||||
// it('should dispatch action removeProcess with argument processId and action type', () => {
|
||||
// const processId = 2;
|
||||
// spectator.service.removeProcess(processId);
|
||||
// expect(store.dispatch).toHaveBeenCalledWith({ processId, type: actions.removeProcess.type });
|
||||
// });
|
||||
// });
|
||||
|
||||
// describe('createProcess()', () => {
|
||||
// it('should dispatch action addProcess with process', async () => {
|
||||
// const process: ApplicationProcess = {
|
||||
// id: 1,
|
||||
// name: 'Vorgang 1',
|
||||
// section: 'customer',
|
||||
// type: 'cart',
|
||||
// };
|
||||
|
||||
// const timestamp = 100;
|
||||
// spyOn(spectator.service as any, '_createTimestamp').and.returnValue(timestamp);
|
||||
// spyOn(spectator.service, 'getProcessById$').and.returnValue(of(undefined));
|
||||
// await spectator.service.createProcess(process);
|
||||
|
||||
// expect(store.dispatch).toHaveBeenCalledWith({
|
||||
// type: actions.addProcess.type,
|
||||
// process: {
|
||||
// ...process,
|
||||
// activated: 0,
|
||||
// created: timestamp,
|
||||
// },
|
||||
// });
|
||||
// });
|
||||
|
||||
// it('should throw an error if the process id is already existing', async () => {
|
||||
// const process: ApplicationProcess = {
|
||||
// id: 1,
|
||||
// name: 'Vorgang 1',
|
||||
// section: 'customer',
|
||||
// type: 'cart',
|
||||
// };
|
||||
// spyOn(spectator.service, 'getProcessById$').and.returnValue(of(process));
|
||||
// await expectAsync(spectator.service.createProcess(process)).toBeRejectedWithError('Process Id existiert bereits');
|
||||
// });
|
||||
|
||||
// it('should throw an error if the process id is not a number', async () => {
|
||||
// const process: ApplicationProcess = {
|
||||
// id: undefined,
|
||||
// name: 'Vorgang 1',
|
||||
// section: 'customer',
|
||||
// type: 'cart',
|
||||
// };
|
||||
// spyOn(spectator.service, 'getProcessById$').and.returnValue(of({ id: 5, name: 'Vorgang 2', section: 'customer' }));
|
||||
// await expectAsync(spectator.service.createProcess(process)).toBeRejectedWithError('Process Id nicht gesetzt');
|
||||
// });
|
||||
// });
|
||||
|
||||
// describe('patchProcess', () => {
|
||||
// it('should dispatch action patchProcess with changes', async () => {
|
||||
// const process: ApplicationProcess = {
|
||||
// id: 1,
|
||||
// name: 'Vorgang 1',
|
||||
// section: 'customer',
|
||||
// type: 'cart',
|
||||
// };
|
||||
|
||||
// await spectator.service.patchProcess(process.id, process);
|
||||
|
||||
// expect(store.dispatch).toHaveBeenCalledWith({
|
||||
// type: actions.patchProcess.type,
|
||||
// processId: process.id,
|
||||
// changes: {
|
||||
// ...process,
|
||||
// },
|
||||
// });
|
||||
// });
|
||||
// });
|
||||
|
||||
// describe('setSection()', () => {
|
||||
// it('should dispatch action setSection with argument section and action type', () => {
|
||||
// const section = 'customer';
|
||||
// spectator.service.setSection(section);
|
||||
// expect(store.dispatch).toHaveBeenCalledWith({ section, type: actions.setSection.type });
|
||||
// });
|
||||
// });
|
||||
|
||||
// describe('getLastActivatedProcessWithSectionAndType()', () => {
|
||||
// it('should return the last activated process by section and type', async () => {
|
||||
// const processes: ApplicationProcess[] = [
|
||||
// { id: 1, name: 'Vorgang 1', section: 'customer', type: 'cart', activated: 100 },
|
||||
// { id: 2, name: 'Vorgang 2', section: 'customer', type: 'cart', activated: 200 },
|
||||
// { id: 3, name: 'Vorgang 3', section: 'customer', type: 'goodsOut', activated: 300 },
|
||||
// ];
|
||||
// spyOn(spectator.service, 'getProcesses$').and.returnValue(of(processes));
|
||||
|
||||
// expect(await spectator.service.getLastActivatedProcessWithSectionAndType$('customer', 'cart').pipe(first()).toPromise()).toBe(
|
||||
// processes[1]
|
||||
// );
|
||||
// });
|
||||
// });
|
||||
|
||||
// describe('getLastActivatedProcessWithSection()', () => {
|
||||
// it('should return the last activated process by section', async () => {
|
||||
// const processes: ApplicationProcess[] = [
|
||||
// { id: 1, name: 'Vorgang 1', section: 'customer', activated: 100 },
|
||||
// { id: 2, name: 'Vorgang 2', section: 'customer', activated: 200 },
|
||||
// { id: 3, name: 'Vorgang 3', section: 'customer', activated: 300 },
|
||||
// ];
|
||||
// spyOn(spectator.service, 'getProcesses$').and.returnValue(of(processes));
|
||||
|
||||
// expect(await spectator.service.getLastActivatedProcessWithSection$('customer').pipe(first()).toPromise()).toBe(processes[2]);
|
||||
// });
|
||||
// });
|
||||
|
||||
// describe('_createTimestamp', () => {
|
||||
// it('should return the current timestamp in ms', () => {
|
||||
// expect(spectator.service['_createTimestamp']()).toBeCloseTo(Date.now());
|
||||
// });
|
||||
// });
|
||||
// });
|
||||
@@ -1,41 +1,68 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Store } from '@ngrx/store';
|
||||
import { inject, Injectable } from '@angular/core';
|
||||
import { BehaviorSubject, Observable, of, firstValueFrom } from 'rxjs';
|
||||
import { map, filter, withLatestFrom } from 'rxjs/operators';
|
||||
import { BranchDTO } from '@generated/swagger/checkout-api';
|
||||
import { isBoolean, isNumber } from '@utils/common';
|
||||
import { BehaviorSubject, Observable } from 'rxjs';
|
||||
import { first, map, switchMap } from 'rxjs/operators';
|
||||
import { ApplicationProcess } from './defs';
|
||||
import {
|
||||
removeProcess,
|
||||
selectSection,
|
||||
selectProcesses,
|
||||
setSection,
|
||||
addProcess,
|
||||
setActivatedProcess,
|
||||
selectActivatedProcess,
|
||||
patchProcess,
|
||||
patchProcessData,
|
||||
selectTitle,
|
||||
setTitle,
|
||||
} from './store';
|
||||
import { TabService } from '@isa/core/tabs';
|
||||
import { ApplicationProcess } from './defs/application-process';
|
||||
import { Tab, TabMetadata } from '@isa/core/tabs';
|
||||
import { toObservable } from '@angular/core/rxjs-interop';
|
||||
import { Store } from '@ngrx/store';
|
||||
import { removeProcess } from './store/application.actions';
|
||||
|
||||
@Injectable()
|
||||
/**
|
||||
* Adapter service that bridges the old ApplicationService interface with the new TabService.
|
||||
*
|
||||
* This adapter allows existing code that depends on ApplicationService to work with the new
|
||||
* TabService without requiring immediate code changes. It maps ApplicationProcess concepts
|
||||
* to Tab entities, storing process-specific data in tab metadata.
|
||||
*
|
||||
* Key mappings:
|
||||
* - ApplicationProcess.id <-> Tab.id
|
||||
* - ApplicationProcess.name <-> Tab.name
|
||||
* - ApplicationProcess metadata (section, type, etc.) <-> Tab.metadata with 'process_' prefix
|
||||
* - ApplicationProcess.data <-> Tab.metadata with 'data_' prefix
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* // Inject the adapter instead of the original service
|
||||
* constructor(private applicationService: ApplicationServiceAdapter) {}
|
||||
*
|
||||
* // Use the same API as before
|
||||
* const process = await this.applicationService.createCustomerProcess();
|
||||
* this.applicationService.activateProcess(process.id);
|
||||
* ```
|
||||
*/
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class ApplicationService {
|
||||
private activatedProcessIdSubject = new BehaviorSubject<number>(undefined);
|
||||
#store = inject(Store);
|
||||
|
||||
#tabService = inject(TabService);
|
||||
|
||||
#activatedProcessId$ = toObservable(this.#tabService.activatedTabId);
|
||||
|
||||
#tabs$ = toObservable(this.#tabService.entities);
|
||||
|
||||
#processes$ = this.#tabs$.pipe(
|
||||
map((tabs) => tabs.map((tab) => this.mapTabToProcess(tab))),
|
||||
);
|
||||
|
||||
#section = new BehaviorSubject<'customer' | 'branch'>('customer');
|
||||
|
||||
readonly REGEX_PROCESS_NAME = /^Vorgang \d+$/;
|
||||
|
||||
get activatedProcessId() {
|
||||
return this.activatedProcessIdSubject.value;
|
||||
return this.#tabService.activatedTabId();
|
||||
}
|
||||
|
||||
get activatedProcessId$() {
|
||||
return this.activatedProcessIdSubject.asObservable();
|
||||
return this.#activatedProcessId$;
|
||||
}
|
||||
|
||||
constructor(private store: Store) {}
|
||||
|
||||
getProcesses$(section?: 'customer' | 'branch') {
|
||||
const processes$ = this.store.select(selectProcesses);
|
||||
return processes$.pipe(
|
||||
getProcesses$(
|
||||
section?: 'customer' | 'branch',
|
||||
): Observable<ApplicationProcess[]> {
|
||||
return this.#processes$.pipe(
|
||||
map((processes) =>
|
||||
processes.filter((process) =>
|
||||
section ? process.section === section : true,
|
||||
@@ -45,69 +72,96 @@ export class ApplicationService {
|
||||
}
|
||||
|
||||
getProcessById$(processId: number): Observable<ApplicationProcess> {
|
||||
return this.getProcesses$().pipe(
|
||||
return this.#processes$.pipe(
|
||||
map((processes) => processes.find((process) => process.id === processId)),
|
||||
);
|
||||
}
|
||||
|
||||
getSection$() {
|
||||
return this.store.select(selectSection);
|
||||
getSection$(): Observable<'customer' | 'branch'> {
|
||||
return this.#section.asObservable();
|
||||
}
|
||||
|
||||
getTitle$() {
|
||||
getTitle$(): Observable<'Kundenbereich' | 'Filialbereich'> {
|
||||
return this.getSection$().pipe(
|
||||
map((section) => {
|
||||
return section === 'customer' ? 'Kundenbereich' : 'Filialbereich';
|
||||
}),
|
||||
map((section) =>
|
||||
section === 'customer' ? 'Kundenbereich' : 'Filialbereich',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/** @deprecated */
|
||||
getActivatedProcessId$() {
|
||||
return this.store
|
||||
.select(selectActivatedProcess)
|
||||
.pipe(map((process) => process?.id));
|
||||
getActivatedProcessId$(): Observable<number> {
|
||||
return this.activatedProcessId$;
|
||||
}
|
||||
|
||||
activateProcess(activatedProcessId: number) {
|
||||
this.store.dispatch(setActivatedProcess({ activatedProcessId }));
|
||||
this.activatedProcessIdSubject.next(activatedProcessId);
|
||||
activateProcess(activatedProcessId: number): void {
|
||||
this.#tabService.activateTab(activatedProcessId);
|
||||
}
|
||||
|
||||
removeProcess(processId: number) {
|
||||
this.store.dispatch(removeProcess({ processId }));
|
||||
removeProcess(processId: number): void {
|
||||
this.#tabService.removeTab(processId);
|
||||
this.#store.dispatch(removeProcess({ processId }));
|
||||
}
|
||||
|
||||
patchProcess(processId: number, changes: Partial<ApplicationProcess>) {
|
||||
this.store.dispatch(patchProcess({ processId, changes }));
|
||||
}
|
||||
patchProcess(processId: number, changes: Partial<ApplicationProcess>): void {
|
||||
const tabChanges: {
|
||||
name?: string;
|
||||
tags?: string[];
|
||||
metadata?: Record<string, unknown>;
|
||||
} = {};
|
||||
|
||||
patchProcessData(processId: number, data: Record<string, any>) {
|
||||
this.store.dispatch(patchProcessData({ processId, data }));
|
||||
}
|
||||
|
||||
getSelectedBranch$(processId?: number): Observable<BranchDTO> {
|
||||
if (!processId) {
|
||||
return this.activatedProcessId$.pipe(
|
||||
switchMap((processId) =>
|
||||
this.getProcessById$(processId).pipe(
|
||||
map((process) => process?.data?.selectedBranch),
|
||||
),
|
||||
),
|
||||
);
|
||||
if (changes.name) {
|
||||
tabChanges.name = changes.name;
|
||||
}
|
||||
|
||||
return this.getProcessById$(processId).pipe(
|
||||
map((process) => process?.data?.selectedBranch),
|
||||
// Store other ApplicationProcess properties in metadata
|
||||
const metadataKeys = [
|
||||
'section',
|
||||
'type',
|
||||
'closeable',
|
||||
'confirmClosing',
|
||||
'created',
|
||||
'activated',
|
||||
'data',
|
||||
];
|
||||
metadataKeys.forEach((key) => {
|
||||
if (tabChanges.metadata === undefined) {
|
||||
tabChanges.metadata = {};
|
||||
}
|
||||
|
||||
if (changes[key as keyof ApplicationProcess] !== undefined) {
|
||||
tabChanges.metadata[`process_${key}`] =
|
||||
changes[key as keyof ApplicationProcess];
|
||||
}
|
||||
});
|
||||
|
||||
// Apply the changes to the tab
|
||||
this.#tabService.patchTab(processId, tabChanges);
|
||||
}
|
||||
|
||||
patchProcessData(processId: number, data: Record<string, unknown>): void {
|
||||
const currentProcess = this.#tabService.entityMap()[processId];
|
||||
const currentData: TabMetadata =
|
||||
(currentProcess?.metadata?.['process_data'] as TabMetadata) ?? {};
|
||||
|
||||
this.#tabService.patchTab(processId, {
|
||||
metadata: { [`process_data`]: { ...currentData, ...data } },
|
||||
});
|
||||
}
|
||||
|
||||
getSelectedBranch$(): Observable<BranchDTO> {
|
||||
return this.#processes$.pipe(
|
||||
withLatestFrom(this.#activatedProcessId$),
|
||||
map(([processes, activatedProcessId]) =>
|
||||
processes.find((process) => process.id === activatedProcessId),
|
||||
),
|
||||
filter((process): process is ApplicationProcess => !!process),
|
||||
map((process) => process.data?.selectedBranch as BranchDTO),
|
||||
);
|
||||
}
|
||||
|
||||
readonly REGEX_PROCESS_NAME = /^Vorgang \d+$/;
|
||||
|
||||
async createCustomerProcess(processId?: number): Promise<ApplicationProcess> {
|
||||
const processes = await this.getProcesses$('customer')
|
||||
.pipe(first())
|
||||
.toPromise();
|
||||
const processes = await firstValueFrom(this.getProcesses$('customer'));
|
||||
|
||||
const processIds = processes
|
||||
.filter((x) => this.REGEX_PROCESS_NAME.test(x.name))
|
||||
@@ -124,14 +178,18 @@ export class ApplicationService {
|
||||
};
|
||||
|
||||
await this.createProcess(process);
|
||||
|
||||
return process;
|
||||
}
|
||||
|
||||
async createProcess(process: ApplicationProcess) {
|
||||
const existingProcess = await this.getProcessById$(process?.id)
|
||||
.pipe(first())
|
||||
.toPromise();
|
||||
/**
|
||||
* Creates a new ApplicationProcess by first creating a Tab and then storing
|
||||
* process-specific properties in the tab's metadata.
|
||||
*
|
||||
* @param process - The ApplicationProcess to create
|
||||
* @throws {Error} If process ID already exists or is invalid
|
||||
*/
|
||||
async createProcess(process: ApplicationProcess): Promise<void> {
|
||||
const existingProcess = this.#tabService.entityMap()[process.id];
|
||||
if (existingProcess?.id === process?.id) {
|
||||
throw new Error('Process Id existiert bereits');
|
||||
}
|
||||
@@ -148,13 +206,28 @@ export class ApplicationService {
|
||||
process.confirmClosing = true;
|
||||
}
|
||||
|
||||
process.created = this._createTimestamp();
|
||||
process.created = this.createTimestamp();
|
||||
process.activated = 0;
|
||||
this.store.dispatch(addProcess({ process }));
|
||||
|
||||
// Create tab with process data and preserve the process ID
|
||||
this.#tabService.addTab({
|
||||
id: process.id,
|
||||
name: process.name,
|
||||
tags: [process.section, process.type].filter(Boolean),
|
||||
metadata: {
|
||||
process_section: process.section,
|
||||
process_type: process.type,
|
||||
process_closeable: process.closeable,
|
||||
process_confirmClosing: process.confirmClosing,
|
||||
process_created: process.created,
|
||||
process_activated: process.activated,
|
||||
process_data: process.data,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
setSection(section: 'customer' | 'branch') {
|
||||
this.store.dispatch(setSection({ section }));
|
||||
setSection(section: 'customer' | 'branch'): void {
|
||||
this.#section.next(section);
|
||||
}
|
||||
|
||||
getLastActivatedProcessWithSectionAndType$(
|
||||
@@ -190,7 +263,74 @@ export class ApplicationService {
|
||||
);
|
||||
}
|
||||
|
||||
private _createTimestamp() {
|
||||
/**
|
||||
* Maps Tab entities to ApplicationProcess objects by extracting process-specific
|
||||
* metadata and combining it with tab properties.
|
||||
*
|
||||
* @param tab - The tab entity to convert
|
||||
* @returns The corresponding ApplicationProcess object
|
||||
*/
|
||||
private mapTabToProcess(tab: Tab): ApplicationProcess {
|
||||
return {
|
||||
id: tab.id,
|
||||
name: tab.name,
|
||||
created:
|
||||
this.getMetadataValue<number>(tab.metadata, 'process_created') ??
|
||||
tab.createdAt,
|
||||
activated:
|
||||
this.getMetadataValue<number>(tab.metadata, 'process_activated') ??
|
||||
tab.activatedAt ??
|
||||
0,
|
||||
section:
|
||||
this.getMetadataValue<'customer' | 'branch'>(
|
||||
tab.metadata,
|
||||
'process_section',
|
||||
) ?? 'customer',
|
||||
type: this.getMetadataValue<string>(tab.metadata, 'process_type'),
|
||||
closeable:
|
||||
this.getMetadataValue<boolean>(tab.metadata, 'process_closeable') ??
|
||||
true,
|
||||
confirmClosing:
|
||||
this.getMetadataValue<boolean>(
|
||||
tab.metadata,
|
||||
'process_confirmClosing',
|
||||
) ?? true,
|
||||
data: this.extractDataFromMetadata(tab.metadata),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts ApplicationProcess data properties from tab metadata.
|
||||
* Data properties are stored with a 'data_' prefix in tab metadata.
|
||||
*
|
||||
* @param metadata - The tab metadata object
|
||||
* @returns The extracted data object or undefined if no data properties exist
|
||||
*/
|
||||
private extractDataFromMetadata(
|
||||
metadata: TabMetadata,
|
||||
): Record<string, unknown> | undefined {
|
||||
// Return the complete data object stored under 'process_data'
|
||||
const processData = metadata?.['process_data'];
|
||||
|
||||
if (
|
||||
processData &&
|
||||
typeof processData === 'object' &&
|
||||
processData !== null
|
||||
) {
|
||||
return processData as Record<string, unknown>;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
private getMetadataValue<T>(
|
||||
metadata: TabMetadata,
|
||||
key: string,
|
||||
): T | undefined {
|
||||
return metadata?.[key] as T | undefined;
|
||||
}
|
||||
|
||||
private createTimestamp(): number {
|
||||
return Date.now();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
export * from './application.module';
|
||||
export * from './application.service';
|
||||
export * from './application.service-adapter';
|
||||
export * from './defs';
|
||||
export * from './store';
|
||||
export * from './application.service';
|
||||
export * from './defs';
|
||||
export * from './store/application.actions';
|
||||
|
||||
@@ -1,27 +1,8 @@
|
||||
import { createAction, props } from '@ngrx/store';
|
||||
import { ApplicationProcess } from '..';
|
||||
|
||||
const prefix = '[CORE-APPLICATION]';
|
||||
|
||||
export const setTitle = createAction(`${prefix} Set Title`, props<{ title: string }>());
|
||||
|
||||
export const setSection = createAction(`${prefix} Set Section`, props<{ section: 'customer' | 'branch' }>());
|
||||
|
||||
export const addProcess = createAction(`${prefix} Add Process`, props<{ process: ApplicationProcess }>());
|
||||
|
||||
export const removeProcess = createAction(`${prefix} Remove Process`, props<{ processId: number }>());
|
||||
|
||||
export const setActivatedProcess = createAction(
|
||||
`${prefix} Set Activated Process`,
|
||||
props<{ activatedProcessId: number }>(),
|
||||
);
|
||||
|
||||
export const patchProcess = createAction(
|
||||
`${prefix} Patch Process`,
|
||||
props<{ processId: number; changes: Partial<ApplicationProcess> }>(),
|
||||
);
|
||||
|
||||
export const patchProcessData = createAction(
|
||||
`${prefix} Patch Process Data`,
|
||||
props<{ processId: number; data: Record<string, any> }>(),
|
||||
);
|
||||
import { createAction, props } from '@ngrx/store';
|
||||
|
||||
const prefix = '[CORE-APPLICATION]';
|
||||
|
||||
export const removeProcess = createAction(
|
||||
`${prefix} Remove Process`,
|
||||
props<{ processId: number }>(),
|
||||
);
|
||||
|
||||
@@ -1,200 +0,0 @@
|
||||
import { INITIAL_APPLICATION_STATE } from './application.state';
|
||||
import * as actions from './application.actions';
|
||||
import { applicationReducer } from './application.reducer';
|
||||
import { ApplicationProcess } from '../defs';
|
||||
import { ApplicationState } from './application.state';
|
||||
|
||||
describe('applicationReducer', () => {
|
||||
describe('setSection()', () => {
|
||||
it('should return modified state with section customer', () => {
|
||||
const initialState = INITIAL_APPLICATION_STATE;
|
||||
|
||||
const action = actions.setSection({ section: 'customer' });
|
||||
const state = applicationReducer(initialState, action);
|
||||
|
||||
expect(state).toEqual({
|
||||
...initialState,
|
||||
section: 'customer',
|
||||
});
|
||||
});
|
||||
|
||||
it('should return modified state with section branch', () => {
|
||||
const initialState = INITIAL_APPLICATION_STATE;
|
||||
|
||||
const action = actions.setSection({ section: 'branch' });
|
||||
const state = applicationReducer(initialState, action);
|
||||
|
||||
expect(state).toEqual({
|
||||
...initialState,
|
||||
section: 'branch',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('addProcess()', () => {
|
||||
it('should return modified state with new process if no processes are registered in the state', () => {
|
||||
const initialState = INITIAL_APPLICATION_STATE;
|
||||
|
||||
const process: ApplicationProcess = {
|
||||
id: 1,
|
||||
name: 'Vorgang',
|
||||
section: 'customer',
|
||||
type: 'cart',
|
||||
data: {},
|
||||
};
|
||||
|
||||
const action = actions.addProcess({ process });
|
||||
const state = applicationReducer(initialState, action);
|
||||
expect(state.processes[0]).toEqual(process);
|
||||
});
|
||||
});
|
||||
|
||||
describe('patchProcess()', () => {
|
||||
it('should return modified state with updated process when id is found', () => {
|
||||
const initialState = INITIAL_APPLICATION_STATE;
|
||||
|
||||
const process: ApplicationProcess = {
|
||||
id: 1,
|
||||
name: 'Vorgang',
|
||||
section: 'customer',
|
||||
type: 'cart',
|
||||
};
|
||||
|
||||
const action = actions.patchProcess({ processId: process.id, changes: { ...process, name: 'Test' } });
|
||||
const state = applicationReducer(
|
||||
{
|
||||
...initialState,
|
||||
processes: [process],
|
||||
},
|
||||
action,
|
||||
);
|
||||
expect(state.processes[0].name).toEqual('Test');
|
||||
});
|
||||
|
||||
it('should return unmodified state when id is not existing', () => {
|
||||
const initialState = INITIAL_APPLICATION_STATE;
|
||||
|
||||
const process: ApplicationProcess = {
|
||||
id: 1,
|
||||
name: 'Vorgang',
|
||||
section: 'customer',
|
||||
type: 'cart',
|
||||
};
|
||||
|
||||
const action = actions.patchProcess({ processId: process.id, changes: { ...process, id: 2 } });
|
||||
const state = applicationReducer(
|
||||
{
|
||||
...initialState,
|
||||
processes: [process],
|
||||
},
|
||||
action,
|
||||
);
|
||||
expect(state.processes).toEqual([process]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('removeProcess()', () => {
|
||||
it('should return initial state if no processes are registered in the state', () => {
|
||||
const initialState = INITIAL_APPLICATION_STATE;
|
||||
|
||||
const action = actions.removeProcess({ processId: 2 });
|
||||
const state = applicationReducer(initialState, action);
|
||||
expect(state).toEqual(initialState);
|
||||
});
|
||||
|
||||
it('should return the unmodified state if processId not found', () => {
|
||||
const initialState = INITIAL_APPLICATION_STATE;
|
||||
const modifiedState: ApplicationState = {
|
||||
...initialState,
|
||||
section: 'customer',
|
||||
processes: [
|
||||
{
|
||||
id: 1,
|
||||
name: 'Vorgang',
|
||||
section: 'customer',
|
||||
type: 'cart',
|
||||
},
|
||||
{
|
||||
id: 4,
|
||||
name: 'Vorgang',
|
||||
section: 'customer',
|
||||
type: 'goods-out',
|
||||
},
|
||||
] as ApplicationProcess[],
|
||||
};
|
||||
|
||||
const action = actions.removeProcess({ processId: 2 });
|
||||
const state = applicationReducer(modifiedState, action);
|
||||
expect(state).toEqual(modifiedState);
|
||||
});
|
||||
|
||||
it('should return modified state, after process gets removed', () => {
|
||||
const initialState = INITIAL_APPLICATION_STATE;
|
||||
const modifiedState: ApplicationState = {
|
||||
...initialState,
|
||||
section: 'customer',
|
||||
processes: [
|
||||
{
|
||||
id: 1,
|
||||
name: 'Vorgang',
|
||||
section: 'customer',
|
||||
type: 'cart',
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
name: 'Vorgang',
|
||||
section: 'customer',
|
||||
type: 'goods-out',
|
||||
},
|
||||
] as ApplicationProcess[],
|
||||
};
|
||||
|
||||
const action = actions.removeProcess({ processId: 2 });
|
||||
const state = applicationReducer(modifiedState, action);
|
||||
expect(state.processes).toEqual([
|
||||
{
|
||||
id: 1,
|
||||
name: 'Vorgang',
|
||||
section: 'customer',
|
||||
type: 'cart',
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('setActivatedProcess()', () => {
|
||||
it('should return modified state with process.activated value', () => {
|
||||
const process: ApplicationProcess = {
|
||||
id: 3,
|
||||
name: 'Vorgang 3',
|
||||
section: 'customer',
|
||||
};
|
||||
const initialState: ApplicationState = {
|
||||
...INITIAL_APPLICATION_STATE,
|
||||
processes: [process],
|
||||
};
|
||||
|
||||
const action = actions.setActivatedProcess({ activatedProcessId: 3 });
|
||||
const state = applicationReducer(initialState, action);
|
||||
|
||||
expect(state.processes[0].activated).toBeDefined();
|
||||
});
|
||||
|
||||
it('should return modified state without process.activated value when activatedProcessId doesnt exist', () => {
|
||||
const process: ApplicationProcess = {
|
||||
id: 1,
|
||||
name: 'Vorgang 3',
|
||||
section: 'customer',
|
||||
};
|
||||
const initialState: ApplicationState = {
|
||||
...INITIAL_APPLICATION_STATE,
|
||||
processes: [process],
|
||||
};
|
||||
|
||||
const action = actions.setActivatedProcess({ activatedProcessId: 3 });
|
||||
const state = applicationReducer(initialState, action);
|
||||
|
||||
expect(state.processes[0].activated).toBeUndefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,56 +0,0 @@
|
||||
import { Action, createReducer, on } from '@ngrx/store';
|
||||
import {
|
||||
setSection,
|
||||
addProcess,
|
||||
removeProcess,
|
||||
setActivatedProcess,
|
||||
patchProcess,
|
||||
patchProcessData,
|
||||
setTitle,
|
||||
} from './application.actions';
|
||||
import { ApplicationState, INITIAL_APPLICATION_STATE } from './application.state';
|
||||
|
||||
const _applicationReducer = createReducer(
|
||||
INITIAL_APPLICATION_STATE,
|
||||
on(setTitle, (state, { title }) => ({ ...state, title })),
|
||||
on(setSection, (state, { section }) => ({ ...state, section })),
|
||||
on(addProcess, (state, { process }) => ({ ...state, processes: [...state.processes, { data: {}, ...process }] })),
|
||||
on(removeProcess, (state, { processId }) => {
|
||||
const processes = state?.processes?.filter((process) => process.id !== processId) || [];
|
||||
return { ...state, processes };
|
||||
}),
|
||||
on(setActivatedProcess, (state, { activatedProcessId }) => {
|
||||
const processes = state.processes.map((process) => {
|
||||
if (process.id === activatedProcessId) {
|
||||
return { ...process, activated: Date.now() };
|
||||
}
|
||||
return process;
|
||||
});
|
||||
|
||||
return { ...state, processes };
|
||||
}),
|
||||
on(patchProcess, (state, { processId, changes }) => {
|
||||
const processes = state.processes.map((process) => {
|
||||
if (process.id === processId) {
|
||||
return { ...process, ...changes, id: processId };
|
||||
}
|
||||
return process;
|
||||
});
|
||||
|
||||
return { ...state, processes };
|
||||
}),
|
||||
on(patchProcessData, (state, { processId, data }) => {
|
||||
const processes = state.processes.map((process) => {
|
||||
if (process.id === processId) {
|
||||
return { ...process, data: { ...(process.data || {}), ...data } };
|
||||
}
|
||||
return process;
|
||||
});
|
||||
|
||||
return { ...state, processes };
|
||||
}),
|
||||
);
|
||||
|
||||
export function applicationReducer(state: ApplicationState, action: Action) {
|
||||
return _applicationReducer(state, action);
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
// import { ApplicationState } from './application.state';
|
||||
// import { ApplicationProcess } from '../defs';
|
||||
// import * as selectors from './application.selectors';
|
||||
|
||||
// describe('applicationSelectors', () => {
|
||||
// it('should select the processes', () => {
|
||||
// const processes: ApplicationProcess[] = [{ id: 1, name: 'Vorgang 1', section: 'customer' }];
|
||||
// const state: ApplicationState = {
|
||||
// processes,
|
||||
// section: 'customer',
|
||||
// };
|
||||
// expect(selectors.selectProcesses.projector(state)).toEqual(processes);
|
||||
// });
|
||||
|
||||
// it('should select the section', () => {
|
||||
// const state: ApplicationState = {
|
||||
// processes: [],
|
||||
// section: 'customer',
|
||||
// };
|
||||
// expect(selectors.selectSection.projector(state)).toEqual('customer');
|
||||
// });
|
||||
|
||||
// it('should select the activatedProcess', () => {
|
||||
// const processes: ApplicationProcess[] = [
|
||||
// { id: 1, name: 'Vorgang 1', section: 'customer', activated: 100 },
|
||||
// { id: 2, name: 'Vorgang 2', section: 'customer', activated: 300 },
|
||||
// { id: 3, name: 'Vorgang 3', section: 'customer', activated: 200 },
|
||||
// ];
|
||||
// const state: ApplicationState = {
|
||||
// processes,
|
||||
// section: 'customer',
|
||||
// };
|
||||
// expect(selectors.selectActivatedProcess.projector(state)).toEqual(processes[1]);
|
||||
// });
|
||||
// });
|
||||
@@ -1,18 +0,0 @@
|
||||
import { createFeatureSelector, createSelector } from '@ngrx/store';
|
||||
import { ApplicationState } from './application.state';
|
||||
export const selectApplicationState = createFeatureSelector<ApplicationState>('core-application');
|
||||
|
||||
export const selectTitle = createSelector(selectApplicationState, (s) => s.title);
|
||||
|
||||
export const selectSection = createSelector(selectApplicationState, (s) => s.section);
|
||||
|
||||
export const selectProcesses = createSelector(selectApplicationState, (s) => s.processes);
|
||||
|
||||
export const selectActivatedProcess = createSelector(selectApplicationState, (s) =>
|
||||
s?.processes?.reduce((process, current) => {
|
||||
if (!process) {
|
||||
return current;
|
||||
}
|
||||
return process.activated > current.activated ? process : current;
|
||||
}, undefined),
|
||||
);
|
||||
@@ -1,13 +0,0 @@
|
||||
import { ApplicationProcess } from '../defs';
|
||||
|
||||
export interface ApplicationState {
|
||||
title: string;
|
||||
processes: ApplicationProcess[];
|
||||
section: 'customer' | 'branch';
|
||||
}
|
||||
|
||||
export const INITIAL_APPLICATION_STATE: ApplicationState = {
|
||||
title: '',
|
||||
processes: [],
|
||||
section: 'customer',
|
||||
};
|
||||
@@ -1,6 +0,0 @@
|
||||
// start:ng42.barrel
|
||||
export * from './application.actions';
|
||||
export * from './application.reducer';
|
||||
export * from './application.selectors';
|
||||
export * from './application.state';
|
||||
// end:ng42.barrel
|
||||
@@ -3,7 +3,7 @@ import { inject, Injectable } from '@angular/core';
|
||||
import { toSignal } from '@angular/core/rxjs-interop';
|
||||
import { EnvironmentService } from '@core/environment';
|
||||
import { UiConfirmModalComponent, UiModalResult, UiModalService } from '@ui/modal';
|
||||
import { injectNetworkStatus$ } from '../../app/services';
|
||||
import { injectNetworkStatus$ } from '@isa/core/connectivity';
|
||||
import { AuthService } from './auth.service';
|
||||
import { AuthService as IsaAuthService } from '@generated/swagger/isa-api';
|
||||
import { firstValueFrom, lastValueFrom } from 'rxjs';
|
||||
|
||||
@@ -1,22 +1,15 @@
|
||||
import { ModuleWithProviders, NgModule } from '@angular/core';
|
||||
import { EffectsModule } from '@ngrx/effects';
|
||||
import { StoreModule } from '@ngrx/store';
|
||||
import { BreadcrumbService } from './breadcrumb.service';
|
||||
import { BreadcrumbEffects } from './store/breadcrumb.effect';
|
||||
import { breadcrumbReducer } from './store/breadcrumb.reducer';
|
||||
import { featureName } from './store/breadcrumb.state';
|
||||
|
||||
@NgModule()
|
||||
export class CoreBreadcrumbModule {
|
||||
static forRoot(): ModuleWithProviders<CoreBreadcrumbModule> {
|
||||
return {
|
||||
ngModule: CoreBreadcrumbForRootModule,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@NgModule({
|
||||
imports: [StoreModule.forFeature(featureName, breadcrumbReducer), EffectsModule.forFeature([BreadcrumbEffects])],
|
||||
providers: [BreadcrumbService],
|
||||
})
|
||||
export class CoreBreadcrumbForRootModule {}
|
||||
import { EnvironmentProviders, makeEnvironmentProviders } from '@angular/core';
|
||||
import { provideEffects } from '@ngrx/effects';
|
||||
import { provideState } from '@ngrx/store';
|
||||
import { BreadcrumbService } from './breadcrumb.service';
|
||||
import { BreadcrumbEffects } from './store/breadcrumb.effect';
|
||||
import { breadcrumbReducer } from './store/breadcrumb.reducer';
|
||||
import { featureName } from './store/breadcrumb.state';
|
||||
|
||||
export function provideCoreBreadcrumb(): EnvironmentProviders {
|
||||
return makeEnvironmentProviders([
|
||||
provideState({ name: featureName, reducer: breadcrumbReducer }),
|
||||
provideEffects(BreadcrumbEffects),
|
||||
BreadcrumbService,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
import { ModuleWithProviders, NgModule } from '@angular/core';
|
||||
import { DomainAvailabilityService } from './availability.service';
|
||||
|
||||
@NgModule()
|
||||
export class DomainAvailabilityModule {
|
||||
static forRoot(): ModuleWithProviders<DomainAvailabilityModule> {
|
||||
return {
|
||||
ngModule: DomainAvailabilityModule,
|
||||
providers: [DomainAvailabilityService],
|
||||
};
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,4 +1,3 @@
|
||||
export * from './availability.module';
|
||||
export * from './availability.service';
|
||||
export * from './defs';
|
||||
export * from './in-stock.service';
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
import { ModuleWithProviders, NgModule } from '@angular/core';
|
||||
import { DomainCatalogService } from './catalog.service';
|
||||
import { ThumbnailUrlPipe } from './thumbnail-url.pipe';
|
||||
import { DomainCatalogThumbnailService } from './thumbnail.service';
|
||||
|
||||
@NgModule({
|
||||
declarations: [ThumbnailUrlPipe],
|
||||
imports: [],
|
||||
exports: [ThumbnailUrlPipe],
|
||||
})
|
||||
export class DomainCatalogModule {
|
||||
static forRoot(): ModuleWithProviders<DomainCatalogModule> {
|
||||
return {
|
||||
ngModule: DomainCatalogModule,
|
||||
providers: [DomainCatalogService, DomainCatalogThumbnailService],
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,105 +1,111 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { ApplicationService } from '@core/application';
|
||||
import {
|
||||
AutocompleteTokenDTO,
|
||||
PromotionService,
|
||||
QueryTokenDTO,
|
||||
SearchService,
|
||||
} from '@generated/swagger/cat-search-api';
|
||||
import { memorize } from '@utils/common';
|
||||
import { map, share, shareReplay } from 'rxjs/operators';
|
||||
|
||||
@Injectable()
|
||||
export class DomainCatalogService {
|
||||
constructor(
|
||||
private searchService: SearchService,
|
||||
private promotionService: PromotionService,
|
||||
private applicationService: ApplicationService,
|
||||
) {}
|
||||
|
||||
@memorize()
|
||||
getFilters() {
|
||||
return this.searchService.SearchSearchFilter().pipe(
|
||||
map((res) => res.result),
|
||||
shareReplay(),
|
||||
);
|
||||
}
|
||||
|
||||
@memorize()
|
||||
getOrderBy() {
|
||||
return this.searchService.SearchSearchSort().pipe(
|
||||
map((res) => res.result),
|
||||
shareReplay(),
|
||||
);
|
||||
}
|
||||
|
||||
getSearchHistory({ take }: { take: number }) {
|
||||
return this.searchService.SearchHistory(take ?? 5).pipe(map((res) => res.result));
|
||||
}
|
||||
|
||||
@memorize({ ttl: 120000 })
|
||||
search({ queryToken }: { queryToken: QueryTokenDTO }) {
|
||||
return this.searchService
|
||||
.SearchSearch({
|
||||
...queryToken,
|
||||
stockId: null,
|
||||
})
|
||||
.pipe(share());
|
||||
}
|
||||
|
||||
@memorize({ ttl: 120000 })
|
||||
searchWithStockId({ queryToken }: { queryToken: QueryTokenDTO }) {
|
||||
return this.searchService
|
||||
.SearchSearch2({
|
||||
queryToken,
|
||||
stockId: queryToken?.stockId ?? null,
|
||||
})
|
||||
.pipe(share());
|
||||
}
|
||||
|
||||
getDetailsById({ id }: { id: number }) {
|
||||
return this.searchService.SearchDetail({
|
||||
id,
|
||||
});
|
||||
}
|
||||
|
||||
getDetailsByEan({ ean }: { ean: string }) {
|
||||
return this.searchService.SearchDetailByEAN(ean);
|
||||
}
|
||||
|
||||
searchByIds({ ids }: { ids: number[] }) {
|
||||
return this.searchService.SearchById(ids);
|
||||
}
|
||||
|
||||
searchByEans({ eans }: { eans: string[] }) {
|
||||
return this.searchService.SearchByEAN(eans);
|
||||
}
|
||||
|
||||
searchTop({ queryToken }: { queryToken: QueryTokenDTO }) {
|
||||
return this.searchService.SearchTop(queryToken);
|
||||
}
|
||||
|
||||
searchComplete({ queryToken }: { queryToken: AutocompleteTokenDTO }) {
|
||||
return this.searchService.SearchAutocomplete(queryToken);
|
||||
}
|
||||
|
||||
@memorize()
|
||||
getPromotionPoints({ items }: { items: { id: number; quantity: number; price?: number }[] }) {
|
||||
return this.promotionService.PromotionLesepunkte(items).pipe(shareReplay());
|
||||
}
|
||||
|
||||
@memorize()
|
||||
getSettings() {
|
||||
return this.searchService.SearchSettings().pipe(
|
||||
map((res) => res.result),
|
||||
shareReplay(),
|
||||
);
|
||||
}
|
||||
|
||||
getRecommendations({ digId }: { digId: number }) {
|
||||
return this.searchService.SearchGetRecommendations({
|
||||
digId: digId + '',
|
||||
sessionId: this.applicationService.activatedProcessId + '',
|
||||
});
|
||||
}
|
||||
}
|
||||
import { Injectable } from '@angular/core';
|
||||
import { ApplicationService } from '@core/application';
|
||||
import {
|
||||
AutocompleteTokenDTO,
|
||||
PromotionService,
|
||||
QueryTokenDTO,
|
||||
SearchService,
|
||||
} from '@generated/swagger/cat-search-api';
|
||||
import { memorize } from '@utils/common';
|
||||
import { map, share, shareReplay } from 'rxjs/operators';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class DomainCatalogService {
|
||||
constructor(
|
||||
private searchService: SearchService,
|
||||
private promotionService: PromotionService,
|
||||
private applicationService: ApplicationService,
|
||||
) {}
|
||||
|
||||
@memorize()
|
||||
getFilters() {
|
||||
return this.searchService.SearchSearchFilter().pipe(
|
||||
map((res) => res.result),
|
||||
shareReplay(),
|
||||
);
|
||||
}
|
||||
|
||||
@memorize()
|
||||
getOrderBy() {
|
||||
return this.searchService.SearchSearchSort().pipe(
|
||||
map((res) => res.result),
|
||||
shareReplay(),
|
||||
);
|
||||
}
|
||||
|
||||
getSearchHistory({ take }: { take: number }) {
|
||||
return this.searchService
|
||||
.SearchHistory(take ?? 5)
|
||||
.pipe(map((res) => res.result));
|
||||
}
|
||||
|
||||
@memorize({ ttl: 120000 })
|
||||
search({ queryToken }: { queryToken: QueryTokenDTO }) {
|
||||
return this.searchService
|
||||
.SearchSearch({
|
||||
...queryToken,
|
||||
stockId: null,
|
||||
})
|
||||
.pipe(share());
|
||||
}
|
||||
|
||||
@memorize({ ttl: 120000 })
|
||||
searchWithStockId({ queryToken }: { queryToken: QueryTokenDTO }) {
|
||||
return this.searchService
|
||||
.SearchSearch2({
|
||||
queryToken,
|
||||
stockId: queryToken?.stockId ?? null,
|
||||
})
|
||||
.pipe(share());
|
||||
}
|
||||
|
||||
getDetailsById({ id }: { id: number }) {
|
||||
return this.searchService.SearchDetail({
|
||||
id,
|
||||
});
|
||||
}
|
||||
|
||||
getDetailsByEan({ ean }: { ean: string }) {
|
||||
return this.searchService.SearchDetailByEAN(ean);
|
||||
}
|
||||
|
||||
searchByIds({ ids }: { ids: number[] }) {
|
||||
return this.searchService.SearchById(ids);
|
||||
}
|
||||
|
||||
searchByEans({ eans }: { eans: string[] }) {
|
||||
return this.searchService.SearchByEAN(eans);
|
||||
}
|
||||
|
||||
searchTop({ queryToken }: { queryToken: QueryTokenDTO }) {
|
||||
return this.searchService.SearchTop(queryToken);
|
||||
}
|
||||
|
||||
searchComplete({ queryToken }: { queryToken: AutocompleteTokenDTO }) {
|
||||
return this.searchService.SearchAutocomplete(queryToken);
|
||||
}
|
||||
|
||||
@memorize()
|
||||
getPromotionPoints({
|
||||
items,
|
||||
}: {
|
||||
items: { id: number; quantity: number; price?: number }[];
|
||||
}) {
|
||||
return this.promotionService.PromotionLesepunkte(items).pipe(shareReplay());
|
||||
}
|
||||
|
||||
@memorize()
|
||||
getSettings() {
|
||||
return this.searchService.SearchSettings().pipe(
|
||||
map((res) => res.result),
|
||||
shareReplay(),
|
||||
);
|
||||
}
|
||||
|
||||
getRecommendations({ digId }: { digId: number }) {
|
||||
return this.searchService.SearchGetRecommendations({
|
||||
digId: digId + '',
|
||||
sessionId: this.applicationService.activatedProcessId + '',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
export * from './catalog.module';
|
||||
export * from './catalog.service';
|
||||
export * from './thumbnail-url.pipe';
|
||||
export * from './thumbnail.service';
|
||||
|
||||
@@ -6,7 +6,7 @@ import { DomainCatalogThumbnailService } from './thumbnail.service';
|
||||
@Pipe({
|
||||
name: 'thumbnailUrl',
|
||||
pure: false,
|
||||
standalone: false,
|
||||
standalone: true,
|
||||
})
|
||||
export class ThumbnailUrlPipe implements PipeTransform, OnDestroy {
|
||||
private input$ = new BehaviorSubject<{ width?: number; height?: number; ean?: string }>(undefined);
|
||||
|
||||
@@ -1,20 +1,28 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { memorize } from '@utils/common';
|
||||
import { map, shareReplay } from 'rxjs/operators';
|
||||
import { DomainCatalogService } from './catalog.service';
|
||||
|
||||
@Injectable()
|
||||
export class DomainCatalogThumbnailService {
|
||||
constructor(private domainCatalogService: DomainCatalogService) {}
|
||||
|
||||
@memorize()
|
||||
getThumnaulUrl({ ean, height, width }: { width?: number; height?: number; ean?: string }) {
|
||||
return this.domainCatalogService.getSettings().pipe(
|
||||
map((settings) => {
|
||||
let thumbnailUrl = settings.imageUrl.replace(/{ean}/, ean);
|
||||
return thumbnailUrl;
|
||||
}),
|
||||
shareReplay(),
|
||||
);
|
||||
}
|
||||
}
|
||||
import { Injectable } from '@angular/core';
|
||||
import { memorize } from '@utils/common';
|
||||
import { map, shareReplay } from 'rxjs/operators';
|
||||
import { DomainCatalogService } from './catalog.service';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class DomainCatalogThumbnailService {
|
||||
constructor(private domainCatalogService: DomainCatalogService) {}
|
||||
|
||||
@memorize()
|
||||
getThumnaulUrl({
|
||||
ean,
|
||||
height,
|
||||
width,
|
||||
}: {
|
||||
width?: number;
|
||||
height?: number;
|
||||
ean?: string;
|
||||
}) {
|
||||
return this.domainCatalogService.getSettings().pipe(
|
||||
map((settings) => {
|
||||
const thumbnailUrl = settings.imageUrl.replace(/{ean}/, ean);
|
||||
return thumbnailUrl;
|
||||
}),
|
||||
shareReplay(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,29 +1,15 @@
|
||||
import { ModuleWithProviders, NgModule } from '@angular/core';
|
||||
import { StoreModule } from '@ngrx/store';
|
||||
import { DomainCheckoutService } from './checkout.service';
|
||||
import { domainCheckoutReducer } from './store/domain-checkout.reducer';
|
||||
import { storeFeatureName } from './store/domain-checkout.state';
|
||||
import { EffectsModule } from '@ngrx/effects';
|
||||
import { DomainCheckoutEffects } from './store/domain-checkout.effects';
|
||||
|
||||
@NgModule({
|
||||
declarations: [],
|
||||
imports: [StoreModule.forFeature(storeFeatureName, domainCheckoutReducer)],
|
||||
providers: [DomainCheckoutService],
|
||||
})
|
||||
export class DomainCheckoutModule {
|
||||
static forRoot(): ModuleWithProviders<DomainCheckoutModule> {
|
||||
return {
|
||||
ngModule: RootDomainCheckoutModule,
|
||||
providers: [DomainCheckoutService],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@NgModule({
|
||||
imports: [
|
||||
StoreModule.forFeature(storeFeatureName, domainCheckoutReducer),
|
||||
EffectsModule.forFeature([DomainCheckoutEffects]),
|
||||
],
|
||||
})
|
||||
export class RootDomainCheckoutModule {}
|
||||
import { EnvironmentProviders, makeEnvironmentProviders } from '@angular/core';
|
||||
import { provideEffects } from '@ngrx/effects';
|
||||
import { provideState } from '@ngrx/store';
|
||||
import { DomainCheckoutService } from './checkout.service';
|
||||
import { DomainCheckoutEffects } from './store/domain-checkout.effects';
|
||||
import { domainCheckoutReducer } from './store/domain-checkout.reducer';
|
||||
import { storeFeatureName } from './store/domain-checkout.state';
|
||||
|
||||
export function provideDomainCheckout(): EnvironmentProviders {
|
||||
return makeEnvironmentProviders([
|
||||
provideState({ name: storeFeatureName, reducer: domainCheckoutReducer }),
|
||||
provideEffects(DomainCheckoutEffects),
|
||||
DomainCheckoutService,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -1071,7 +1071,7 @@ export class DomainCheckoutService {
|
||||
});
|
||||
} else if (orderType === 'B2B-Versand') {
|
||||
const branch = await this.applicationService
|
||||
.getSelectedBranch$(processId)
|
||||
.getSelectedBranch$()
|
||||
.pipe(first())
|
||||
.toPromise();
|
||||
availability$ =
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { InfoService } from '@generated/swagger/isa-api';
|
||||
|
||||
@Injectable()
|
||||
export class DomainDashboardService {
|
||||
constructor(private readonly _infoService: InfoService) {}
|
||||
|
||||
feed() {
|
||||
return this._infoService.InfoInfo({});
|
||||
}
|
||||
}
|
||||
import { Injectable } from '@angular/core';
|
||||
import { InfoService } from '@generated/swagger/isa-api';
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class DomainDashboardService {
|
||||
constructor(private readonly _infoService: InfoService) {}
|
||||
|
||||
feed() {
|
||||
return this._infoService.InfoInfo({});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
import { ModuleWithProviders, NgModule } from '@angular/core';
|
||||
import { DomainDashboardService } from './dashboard.service';
|
||||
|
||||
@NgModule({})
|
||||
export class DomainIsaModule {
|
||||
static forRoot(): ModuleWithProviders<DomainIsaModule> {
|
||||
return {
|
||||
ngModule: DomainIsaModule,
|
||||
providers: [DomainDashboardService],
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,2 @@
|
||||
export * from './dashboard.service';
|
||||
export * from './defs';
|
||||
export * from './domain-isa.module';
|
||||
|
||||
@@ -1,116 +1,130 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { AbholfachService, AutocompleteTokenDTO, QueryTokenDTO } from '@generated/swagger/oms-api';
|
||||
import { DateAdapter } from '@ui/common';
|
||||
import { memorize } from '@utils/common';
|
||||
import { shareReplay } from 'rxjs/operators';
|
||||
@Injectable()
|
||||
export class DomainGoodsService {
|
||||
constructor(
|
||||
private abholfachService: AbholfachService,
|
||||
private dateAdapter: DateAdapter,
|
||||
) {}
|
||||
|
||||
searchWareneingang(queryToken: QueryTokenDTO) {
|
||||
return this.abholfachService.AbholfachWareneingang(queryToken);
|
||||
}
|
||||
|
||||
searchWarenausgabe(queryToken: QueryTokenDTO) {
|
||||
return this.abholfachService.AbholfachWarenausgabe(queryToken);
|
||||
}
|
||||
|
||||
wareneingangComplete(autocompleteToken: AutocompleteTokenDTO) {
|
||||
return this.abholfachService.AbholfachWareneingangAutocomplete(autocompleteToken);
|
||||
}
|
||||
|
||||
warenausgabeComplete(autocompleteToken: AutocompleteTokenDTO) {
|
||||
return this.abholfachService.AbholfachWarenausgabeAutocomplete(autocompleteToken);
|
||||
}
|
||||
|
||||
getWareneingangItemByOrderNumber(orderNumber: string) {
|
||||
return this.abholfachService.AbholfachWareneingang({
|
||||
filter: { all_branches: 'true', archive: 'true' },
|
||||
input: {
|
||||
qs: orderNumber,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
getWarenausgabeItemByOrderNumber(orderNumber: string, archive: boolean) {
|
||||
return this.abholfachService.AbholfachWarenausgabe({
|
||||
filter: { all_branches: 'true', archive: `${archive}` },
|
||||
input: {
|
||||
qs: orderNumber,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
getWarenausgabeItemByCompartment(compartmentCode: string, archive: boolean) {
|
||||
return this.abholfachService.AbholfachWarenausgabe({
|
||||
filter: { all_branches: 'true', archive: `${archive}` },
|
||||
input: {
|
||||
qs: compartmentCode,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
getWareneingangItemByCustomerNumber(customerNumber: string) {
|
||||
// Suche anhand der Kundennummer mit Status Bestellt, nachbestellt, eingetroffen, weitergeleitet intern
|
||||
return this.abholfachService.AbholfachWareneingang({
|
||||
filter: { orderitemprocessingstatus: '16;128;8192;1048576' },
|
||||
input: {
|
||||
customer_no: customerNumber,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
list() {
|
||||
const base = this.dateAdapter.today();
|
||||
const startDate = this.dateAdapter.addCalendarDays(base, -5);
|
||||
const endDate = this.dateAdapter.addCalendarDays(base, 1);
|
||||
const queryToken: QueryTokenDTO = {
|
||||
filter: {
|
||||
orderitemprocessingstatus: '16;8192;1024;512;2048',
|
||||
estimatedshippingdate: `"${startDate.toJSON()}"-"${endDate.toJSON()}"`,
|
||||
},
|
||||
orderBy: [{ by: 'estimatedshippingdate' }],
|
||||
skip: 0,
|
||||
take: 20,
|
||||
};
|
||||
return this.searchWareneingang(queryToken);
|
||||
}
|
||||
|
||||
@memorize()
|
||||
goodsInQuerySettings() {
|
||||
return this.abholfachService.AbholfachWareneingangQuerySettings().pipe(shareReplay());
|
||||
}
|
||||
|
||||
@memorize()
|
||||
goodsOutQuerySettings() {
|
||||
return this.abholfachService.AbholfachWarenausgabeQuerySettings().pipe(shareReplay());
|
||||
}
|
||||
|
||||
goodsInList(queryToken: QueryTokenDTO) {
|
||||
return this.abholfachService.AbholfachWareneingangsliste(queryToken);
|
||||
}
|
||||
|
||||
@memorize()
|
||||
goodsInListQuerySettings() {
|
||||
return this.abholfachService.AbholfachWareneingangslisteQuerySettings().pipe(shareReplay());
|
||||
}
|
||||
|
||||
goodsInCleanupList() {
|
||||
return this.abholfachService.AbholfachAbholfachbereinigungsliste();
|
||||
}
|
||||
|
||||
goodsInReservationList(queryToken: QueryTokenDTO) {
|
||||
return this.abholfachService.AbholfachReservierungen(queryToken);
|
||||
}
|
||||
|
||||
goodsInRemissionPreviewList() {
|
||||
return this.abholfachService.AbholfachAbholfachremissionsvorschau();
|
||||
}
|
||||
|
||||
createGoodsInRemissionFromPreviewList() {
|
||||
return this.abholfachService.AbholfachCreateAbholfachremission();
|
||||
}
|
||||
}
|
||||
import { Injectable } from '@angular/core';
|
||||
import {
|
||||
AbholfachService,
|
||||
AutocompleteTokenDTO,
|
||||
QueryTokenDTO,
|
||||
} from '@generated/swagger/oms-api';
|
||||
import { DateAdapter } from '@ui/common';
|
||||
import { memorize } from '@utils/common';
|
||||
import { shareReplay } from 'rxjs/operators';
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class DomainGoodsService {
|
||||
constructor(
|
||||
private abholfachService: AbholfachService,
|
||||
private dateAdapter: DateAdapter,
|
||||
) {}
|
||||
|
||||
searchWareneingang(queryToken: QueryTokenDTO) {
|
||||
return this.abholfachService.AbholfachWareneingang(queryToken);
|
||||
}
|
||||
|
||||
searchWarenausgabe(queryToken: QueryTokenDTO) {
|
||||
return this.abholfachService.AbholfachWarenausgabe(queryToken);
|
||||
}
|
||||
|
||||
wareneingangComplete(autocompleteToken: AutocompleteTokenDTO) {
|
||||
return this.abholfachService.AbholfachWareneingangAutocomplete(
|
||||
autocompleteToken,
|
||||
);
|
||||
}
|
||||
|
||||
warenausgabeComplete(autocompleteToken: AutocompleteTokenDTO) {
|
||||
return this.abholfachService.AbholfachWarenausgabeAutocomplete(
|
||||
autocompleteToken,
|
||||
);
|
||||
}
|
||||
|
||||
getWareneingangItemByOrderNumber(orderNumber: string) {
|
||||
return this.abholfachService.AbholfachWareneingang({
|
||||
filter: { all_branches: 'true', archive: 'true' },
|
||||
input: {
|
||||
qs: orderNumber,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
getWarenausgabeItemByOrderNumber(orderNumber: string, archive: boolean) {
|
||||
return this.abholfachService.AbholfachWarenausgabe({
|
||||
filter: { all_branches: 'true', archive: `${archive}` },
|
||||
input: {
|
||||
qs: orderNumber,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
getWarenausgabeItemByCompartment(compartmentCode: string, archive: boolean) {
|
||||
return this.abholfachService.AbholfachWarenausgabe({
|
||||
filter: { all_branches: 'true', archive: `${archive}` },
|
||||
input: {
|
||||
qs: compartmentCode,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
getWareneingangItemByCustomerNumber(customerNumber: string) {
|
||||
// Suche anhand der Kundennummer mit Status Bestellt, nachbestellt, eingetroffen, weitergeleitet intern
|
||||
return this.abholfachService.AbholfachWareneingang({
|
||||
filter: { orderitemprocessingstatus: '16;128;8192;1048576' },
|
||||
input: {
|
||||
customer_no: customerNumber,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
list() {
|
||||
const base = this.dateAdapter.today();
|
||||
const startDate = this.dateAdapter.addCalendarDays(base, -5);
|
||||
const endDate = this.dateAdapter.addCalendarDays(base, 1);
|
||||
const queryToken: QueryTokenDTO = {
|
||||
filter: {
|
||||
orderitemprocessingstatus: '16;8192;1024;512;2048',
|
||||
estimatedshippingdate: `"${startDate.toJSON()}"-"${endDate.toJSON()}"`,
|
||||
},
|
||||
orderBy: [{ by: 'estimatedshippingdate' }],
|
||||
skip: 0,
|
||||
take: 20,
|
||||
};
|
||||
return this.searchWareneingang(queryToken);
|
||||
}
|
||||
|
||||
@memorize()
|
||||
goodsInQuerySettings() {
|
||||
return this.abholfachService
|
||||
.AbholfachWareneingangQuerySettings()
|
||||
.pipe(shareReplay());
|
||||
}
|
||||
|
||||
@memorize()
|
||||
goodsOutQuerySettings() {
|
||||
return this.abholfachService
|
||||
.AbholfachWarenausgabeQuerySettings()
|
||||
.pipe(shareReplay());
|
||||
}
|
||||
|
||||
goodsInList(queryToken: QueryTokenDTO) {
|
||||
return this.abholfachService.AbholfachWareneingangsliste(queryToken);
|
||||
}
|
||||
|
||||
@memorize()
|
||||
goodsInListQuerySettings() {
|
||||
return this.abholfachService
|
||||
.AbholfachWareneingangslisteQuerySettings()
|
||||
.pipe(shareReplay());
|
||||
}
|
||||
|
||||
goodsInCleanupList() {
|
||||
return this.abholfachService.AbholfachAbholfachbereinigungsliste();
|
||||
}
|
||||
|
||||
goodsInReservationList(queryToken: QueryTokenDTO) {
|
||||
return this.abholfachService.AbholfachReservierungen(queryToken);
|
||||
}
|
||||
|
||||
goodsInRemissionPreviewList() {
|
||||
return this.abholfachService.AbholfachAbholfachremissionsvorschau();
|
||||
}
|
||||
|
||||
createGoodsInRemissionFromPreviewList() {
|
||||
return this.abholfachService.AbholfachCreateAbholfachremission();
|
||||
}
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user