| 1 | # Enricher Source Template |
| 2 | |
| 3 | Template for adding a new enricher to `packages/evlog/src/enrichers/index.ts`. |
| 4 | |
| 5 | Replace `{Name}`, `{name}`, and ` |
| 6 | |
| 7 | ## Info Interface |
| 8 | |
| 9 | Define the output shape of the enricher: |
| 10 | |
| 11 | ```typescript |
| 12 | export interface {Name} |
| 13 | /** Description of field */ |
| 14 | field1?: string |
| 15 | /** Description of field */ |
| 16 | field2?: number |
| 17 | } |
| 18 | ``` |
| 19 | |
| 20 | ## Factory Function |
| 21 | |
| 22 | ```typescript |
| 23 | /** |
| 24 | * Enrich events with {DISPLAY} data. |
| 25 | * Sets `event.{name}` with `{Name}Info` shape: `{ field1?, field2? }`. |
| 26 | */ |
| 27 | export function create{Name |
| 28 | return (ctx) |
| 29 | // 1. Extract data from headers (case-insensitive) |
| 30 | const value = |
| 31 | if (!value) |
| 32 | |
| 33 | // 2. Compute the enriched data |
| 34 | const info: |
| 35 | field1: value, |
| 36 | field2: Number(value |
| 37 | } |
| 38 | |
| 39 | // 3. Merge with existing event field (respects overwrite option) |
| 40 | ctx.event.{name |
| 41 | } |
| 42 | } |
| 43 | ``` |
| 44 | |
| 45 | ## Architecture Rules |
| 46 | |
| 47 | 1. **Use existing helpers** -- |
| 48 | 2. **Single event field** -- each enricher sets one top-level field on |
| 49 | 3. **Factory pattern** -- always return a function, never execute directly |
| 50 | 4. **EnricherOptions** -- accept |
| 51 | 5. **Early return** -- skip if required data is missing |
| 52 | 6. **No side effects** -- never throw, never log, only mutate |
| 53 | 7. **Clean undefined values** -- skip the enricher entirely if all computed values are |
| 54 | |
| 55 | ## Available Helpers |
| 56 | |
| 57 | These helpers are already defined in the enrichers file: |
| 58 | |
| 59 | ```typescript |
| 60 | // Case-insensitive header lookup |
| 61 | function getHeader(headers: |
| 62 | |
| 63 | // Merge computed data with existing event fields, respecting overwrite |
| 64 | function mergeEventField<T |
| 65 | |
| 66 | // Parse string to number, returning undefined for non-finite values |
| 67 | function normalizeNumber(value: |
| 68 | ``` |
| 69 | |
| 70 | ## Data Sources |
| 71 | |
| 72 | Enrichers typically read from: |
| 73 | |
| 74 | - **`ctx.headers` |
| 75 | - **`ctx.response?.headers` |
| 76 | - **`ctx.response?.status` |
| 77 | - **`ctx.request` |
| 78 | - **`process.env` |
| 79 | - **`ctx.event` |