@igniter-js/mail 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/AGENTS.md ADDED
@@ -0,0 +1,394 @@
1
+ # @igniter-js/mail - AI Agent Instructions
2
+
3
+ > **Package Version:** 0.1.0
4
+ > **Last Updated:** 2025-01-13
5
+ > **Status:** Ready for Publication
6
+
7
+ ---
8
+
9
+ ## Package Overview
10
+
11
+ **Name:** `@igniter-js/mail`
12
+ **Purpose:** Type-safe email library for Igniter.js with React Email templates and multiple provider adapters
13
+ **Type:** Standalone Library (can be used independently or with Igniter.js)
14
+
15
+ ### Core Features
16
+ - Type-safe email templates with React Email components
17
+ - Runtime validation with StandardSchema support
18
+ - Multiple provider adapters (Resend, Postmark, SendGrid, SMTP, Webhook)
19
+ - Queue integration for async email delivery
20
+ - Lifecycle hooks for monitoring and logging
21
+ - Builder pattern for fluent configuration
22
+ - Server-first design (Node.js, Bun, Deno)
23
+
24
+ ---
25
+
26
+ ## Architecture
27
+
28
+ ### Design Principles
29
+
30
+ 1. **Type Safety First**
31
+ - End-to-end TypeScript inference from templates to send params
32
+ - Template payload validation with StandardSchema
33
+ - No `any` types in public APIs
34
+
35
+ 2. **React Email Integration**
36
+ - Templates are React components
37
+ - Automatic HTML and plain-text rendering
38
+ - Leverage React Email's component library
39
+
40
+ 3. **Adapter-Based Architecture**
41
+ - Core defines interfaces, adapters provide implementations
42
+ - Easy to add new email providers
43
+ - Adapters are peer dependencies (optional)
44
+
45
+ 4. **Builder Pattern**
46
+ - Fluent API for configuration
47
+ - Type-safe template registration
48
+ - Compile-time validation of configuration
49
+
50
+ ---
51
+
52
+ ## File Structure
53
+
54
+ ```
55
+ packages/mail/
56
+ ├── src/
57
+ │ ├── index.ts # Public exports
58
+ │ │
59
+ │ ├── core/
60
+ │ │ └── igniter-mail.tsx # Core runtime logic
61
+ │ │
62
+ │ ├── builders/
63
+ │ │ ├── igniter-mail.builder.ts # Main builder
64
+ │ │ ├── mail-template.builder.ts # Template builder
65
+ │ │ └── mail-provider.builder.ts # Provider builder
66
+ │ │
67
+ │ ├── adapters/
68
+ │ │ ├── resend.adapter.ts # Resend provider
69
+ │ │ ├── postmark.adapter.ts # Postmark provider
70
+ │ │ ├── sendgrid.adapter.ts # SendGrid provider
71
+ │ │ ├── smtp.adapter.ts # SMTP provider
72
+ │ │ ├── webhook.adapter.ts # Webhook testing adapter
73
+ │ │ └── test.adapter.ts # In-memory test adapter
74
+ │ │
75
+ │ ├── errors/
76
+ │ │ ├── igniter-mail.error.ts # Main error class
77
+ │ │ └── mail-provider.error.ts # Provider-specific errors
78
+ │ │
79
+ │ ├── interfaces/
80
+ │ │ ├── adapter.interface.ts # Adapter contract (deprecated)
81
+ │ │ └── provider.interface.ts # Provider contract (deprecated)
82
+ │ │
83
+ │ ├── types/
84
+ │ │ ├── adapter.ts # Adapter types
85
+ │ │ ├── provider.ts # Provider types
86
+ │ │ └── templates.ts # Template types
87
+ │ │
88
+ │ ├── utils/
89
+ │ │ ├── get-adapter.ts # Adapter resolution utility
90
+ │ │ └── validate-standard-schema-input.ts # Schema validation
91
+ │ │
92
+ │ ├── mail.provider.tsx # Legacy provider (deprecated)
93
+ │ ├── igniter-mail.spec.ts # Unit tests
94
+ │ └── adapters.spec.ts # Adapter tests
95
+
96
+ ├── package.json
97
+ ├── tsconfig.json
98
+ ├── tsup.config.ts
99
+ ├── vitest.config.ts
100
+ ├── README.md
101
+ ├── AGENTS.md # This file
102
+ └── CHANGELOG.md
103
+ ```
104
+
105
+ ---
106
+
107
+ ## Key Responsibilities
108
+
109
+ ### `IgniterMail` (Core Runtime)
110
+
111
+ The core class handles:
112
+ - Template registration and management
113
+ - Template data validation using StandardSchema
114
+ - React Email rendering (HTML + plain text)
115
+ - Adapter orchestration
116
+ - Queue integration for scheduled sends
117
+ - Lifecycle hooks (onSendStarted, onSendSuccess, onSendError)
118
+ - Error handling and normalization
119
+
120
+ **Key Invariants:**
121
+ - All templates must have a `subject`, `schema`, and `render` function
122
+ - Template data is validated before rendering
123
+ - All predictable failures throw `IgniterMailError` with stable `code`
124
+ - Hooks are always invoked (started → success/error)
125
+
126
+ ### `MailAdapter` (Infrastructure-only)
127
+
128
+ Adapters **must**:
129
+ - Implement the `send` method with `MailAdapterSendParams`
130
+ - Handle provider-specific API calls and authentication
131
+ - NOT implement business logic (no validation, no hooks, no queuing)
132
+ - Return cleanly or throw errors (which core will normalize)
133
+
134
+ **Mental Model:**
135
+ - **Core** decides *what* to send and *when*
136
+ - **Adapter** executes *how* to talk to the provider
137
+
138
+ ---
139
+
140
+ ## Development Guidelines
141
+
142
+ ### Adding New Features
143
+
144
+ 1. **Business Logic → Core**
145
+ - If it's a rule or decision, it belongs in `IgniterMail`
146
+ - Examples: validation, hooks, queuing
147
+
148
+ 2. **Infrastructure Logic → Adapter**
149
+ - If it's provider-specific, it belongs in the adapter
150
+ - Examples: API calls, authentication, retry logic
151
+
152
+ 3. **Public API → Builder or Core**
153
+ - If users need to configure it, add to `IgniterMailBuilder`
154
+ - If users need to call it at runtime, add to `IgniterMail`
155
+
156
+ ### Testing Strategy
157
+
158
+ **Unit Tests** (`igniter-mail.spec.ts`):
159
+ - Use `TestMailAdapter` for isolated core testing
160
+ - Test template validation (valid and invalid schemas)
161
+ - Test hooks lifecycle (started, success, error)
162
+ - Test queue integration (enqueue vs immediate send)
163
+
164
+ **Adapter Tests** (`adapters.spec.ts`):
165
+ - Test each adapter's builder and configuration
166
+ - Mock provider APIs
167
+ - Test error handling
168
+
169
+ **Type Tests** (future):
170
+ - Verify template key type inference
171
+ - Verify payload type inference
172
+ - Verify builder fluent API type safety
173
+
174
+ ### Code Style
175
+
176
+ - Follow ESLint rules (`npm run lint`)
177
+ - Use JSDoc comments for public APIs (in English)
178
+ - Prefer explicit types over inference in public APIs
179
+ - Use `readonly` for immutable properties
180
+ - Use `async/await` over raw Promises
181
+
182
+ ### Error Handling
183
+
184
+ - All predictable errors must throw `IgniterMailError`
185
+ - Use stable error codes (e.g., `MAIL_PROVIDER_TEMPLATE_NOT_FOUND`)
186
+ - Include relevant context in `error.metadata`
187
+
188
+ ### Commit Messages
189
+
190
+ Follow Conventional Commits:
191
+ ```
192
+ feat(mail): add SendGrid adapter
193
+ fix(mail): handle schema validation errors correctly
194
+ docs(mail): update README with queue examples
195
+ test(mail): add tests for template validation
196
+ ```
197
+
198
+ ---
199
+
200
+ ## Adding a New Adapter
201
+
202
+ 1. Create new file in `src/adapters/` (e.g., `aws-ses.adapter.ts`)
203
+ 2. Create builder class extending pattern (e.g., `AwsSesMailAdapterBuilder`)
204
+ 3. Implement `send` method with provider API calls
205
+ 4. Export adapter and builder from `src/index.ts`
206
+ 5. Add provider to `IgniterMailBuilder.withAdapter()` shorthand (optional)
207
+ 6. Add tests for adapter-specific behavior
208
+ 7. Update README with adapter documentation
209
+ 8. Add peer dependency to `package.json`
210
+
211
+ **Adapter Checklist:**
212
+ - ✅ Implement `MailAdapter` interface
213
+ - ✅ Handle provider authentication
214
+ - ✅ Support `to`, `subject`, `html`, `text` fields
215
+ - ✅ Optionally support `scheduledAt` if provider supports it
216
+ - ✅ Throw descriptive errors
217
+ - ✅ Export builder with `create()` factory
218
+
219
+ ---
220
+
221
+ ## Common Tasks
222
+
223
+ ### Running Tests
224
+
225
+ ```bash
226
+ # Run all tests
227
+ npm run test
228
+
229
+ # Watch mode
230
+ npm run test:watch
231
+
232
+ # Run specific test file
233
+ npm run test -- igniter-mail.spec.ts
234
+ ```
235
+
236
+ ### Building
237
+
238
+ ```bash
239
+ # Build once
240
+ npm run build
241
+
242
+ # Build and watch
243
+ npm run dev
244
+ ```
245
+
246
+ ### Type Checking
247
+
248
+ ```bash
249
+ npm run typecheck
250
+ ```
251
+
252
+ ### Linting
253
+
254
+ ```bash
255
+ npm run lint
256
+ ```
257
+
258
+ ---
259
+
260
+ ## Environment Variables
261
+
262
+ While the package doesn't directly read environment variables, adapters typically require:
263
+
264
+ **Resend:**
265
+ - `RESEND_API_KEY` - Resend API key
266
+
267
+ **Postmark:**
268
+ - `POSTMARK_SERVER_TOKEN` - Postmark server token
269
+
270
+ **SendGrid:**
271
+ - `SENDGRID_API_KEY` - SendGrid API key
272
+
273
+ **SMTP:**
274
+ - `SMTP_HOST` - SMTP server host
275
+ - `SMTP_PORT` - SMTP server port
276
+ - `SMTP_USER` - SMTP username
277
+ - `SMTP_PASS` - SMTP password
278
+
279
+ ---
280
+
281
+ ## Publishing Workflow
282
+
283
+ ### Pre-Publish Checklist
284
+
285
+ - [ ] All tests passing (`npm run test`)
286
+ - [ ] Build succeeds (`npm run build`)
287
+ - [ ] TypeScript compiles (`npm run typecheck`)
288
+ - [ ] Linting passes (`npm run lint`)
289
+ - [ ] README.md is up-to-date
290
+ - [ ] CHANGELOG.md is updated with version changes
291
+ - [ ] Version in `package.json` is correct
292
+ - [ ] All exports in `src/index.ts` are correct
293
+
294
+ ### Version Update Process
295
+
296
+ **NEVER update version without user approval.**
297
+
298
+ When changes are ready:
299
+ 1. Review changes made
300
+ 2. Suggest version bump options (patch/minor/major)
301
+ 3. Wait for user approval
302
+ 4. Update `package.json` version
303
+ 5. Update `CHANGELOG.md`
304
+ 6. Run quality checks
305
+ 7. Ask about publishing
306
+
307
+ ### Publishing
308
+
309
+ ```bash
310
+ # Login to npm (if not already)
311
+ npm login
312
+
313
+ # Publish (from package directory)
314
+ cd packages/mail
315
+ npm publish --access public
316
+ ```
317
+
318
+ ---
319
+
320
+ ## Error Codes Reference
321
+
322
+ | Code | Reason |
323
+ |------|--------|
324
+ | `MAIL_PROVIDER_ADAPTER_REQUIRED` | No adapter configured |
325
+ | `MAIL_PROVIDER_TEMPLATES_REQUIRED` | No templates registered |
326
+ | `MAIL_PROVIDER_TEMPLATE_NOT_FOUND` | Template key doesn't exist |
327
+ | `MAIL_PROVIDER_TEMPLATE_DATA_INVALID` | Schema validation failed |
328
+ | `MAIL_PROVIDER_SEND_FAILED` | Failed to send email |
329
+ | `MAIL_PROVIDER_SCHEDULE_DATE_INVALID` | Schedule date is in the past |
330
+ | `MAIL_PROVIDER_SCHEDULE_FAILED` | Failed to schedule email |
331
+ | `MAIL_PROVIDER_FROM_REQUIRED` | FROM address not configured |
332
+ | `MAIL_PROVIDER_ADAPTER_SECRET_REQUIRED` | Adapter secret not provided |
333
+ | `MAIL_PROVIDER_ADAPTER_NOT_FOUND` | Unknown adapter provider |
334
+ | `MAIL_ADAPTER_CONFIGURATION_INVALID` | Adapter configuration error |
335
+
336
+ ---
337
+
338
+ ## Non-Goals
339
+
340
+ By design, this package does NOT:
341
+ - Implement business rules in adapters (that's the core's job)
342
+ - Provide browser-side email sending (server-first)
343
+ - Bundle all adapter dependencies (they're peer dependencies)
344
+ - Support synchronous email sending (always async)
345
+
346
+ ---
347
+
348
+ ## Quick Reference
349
+
350
+ ### Key Files
351
+
352
+ | File | Purpose |
353
+ |------|---------|
354
+ | `src/index.ts` | Public exports |
355
+ | `src/core/igniter-mail.tsx` | Core runtime logic |
356
+ | `src/builders/igniter-mail.builder.ts` | Builder API |
357
+ | `src/adapters/*.adapter.ts` | Provider adapters |
358
+ | `src/types/*.ts` | Public types |
359
+ | `src/errors/*.ts` | Error classes |
360
+
361
+ ### Key Commands
362
+
363
+ | Command | Purpose |
364
+ |---------|---------|
365
+ | `npm run dev` | Build and watch |
366
+ | `npm run build` | Build once |
367
+ | `npm run test` | Run tests |
368
+ | `npm run typecheck` | Check types |
369
+ | `npm run lint` | Lint code |
370
+
371
+ ---
372
+
373
+ ## Support & Communication
374
+
375
+ When working on this package:
376
+ - **Read this file first** before making changes
377
+ - **Follow existing patterns** - consistency is key
378
+ - **Update documentation** after every code change
379
+ - **Write tests** for new features and bug fixes
380
+ - **Ask for clarification** if requirements are unclear
381
+ - **Suggest improvements** to this AGENTS.md if needed
382
+
383
+ ---
384
+
385
+ ## Changelog History
386
+
387
+ ### v0.1.0 (Initial Release)
388
+ - Core email functionality with React Email
389
+ - Type-safe templates with StandardSchema validation
390
+ - Multiple adapters (Resend, Postmark, SendGrid, SMTP, Webhook, Test)
391
+ - Queue integration for scheduled sends
392
+ - Lifecycle hooks
393
+ - Builder pattern API
394
+ - Comprehensive documentation
package/CHANGELOG.md ADDED
@@ -0,0 +1,95 @@
1
+ # Changelog
2
+
3
+ All notable changes to `@igniter-js/mail` will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [0.1.0] - 2025-01-13
9
+
10
+ ### 🎉 Initial Release
11
+
12
+ First public release of `@igniter-js/mail` - a type-safe email library for Igniter.js with React Email templates.
13
+
14
+ ### ✨ Features
15
+
16
+ #### Core Functionality
17
+ - **Type-Safe Templates** - Full TypeScript support with compile-time inference
18
+ - **React Email Integration** - Build beautiful emails with React components
19
+ - **StandardSchema Validation** - Runtime validation of template payloads
20
+ - **Builder Pattern** - Fluent API for configuration
21
+ - **Queue Integration** - Schedule emails with BullMQ or custom queues
22
+ - **Lifecycle Hooks** - React to send events (started, success, error)
23
+
24
+ #### Email Operations
25
+ - **`send()`** - Send emails immediately
26
+ - **`schedule()`** - Schedule emails for future delivery
27
+ - **Template Management** - Type-safe template registration
28
+ - **Subject Override** - Per-send subject customization
29
+ - **HTML + Plain Text** - Automatic generation of both formats
30
+
31
+ #### Adapters
32
+ - **Resend Adapter** - Official Resend integration
33
+ - **Postmark Adapter** - Postmark email service
34
+ - **SendGrid Adapter** - SendGrid email service
35
+ - **SMTP Adapter** - Standard SMTP protocol support (Nodemailer)
36
+ - **Webhook Adapter** - HTTP webhook for testing
37
+ - **Test Adapter** - In-memory adapter for unit tests
38
+
39
+ #### Developer Experience
40
+ - **Comprehensive Error Handling** - Typed `IgniterMailError` with stable error codes
41
+ - **Rich Type Definitions** - Complete TypeScript types for all APIs
42
+ - **React Email Components** - Full access to @react-email/components
43
+ - **Type Inference** - Template keys and payloads inferred at compile time
44
+ - **Builder Validation** - Configuration errors caught at build time
45
+
46
+ #### Integration Features
47
+ - **Queue Integration** - Optional BullMQ integration for async delivery
48
+ - **Logger Support** - Attach Igniter.js logger for debugging
49
+ - **Hook System** - Extensible hooks for monitoring and analytics
50
+ - **Legacy API Support** - Backwards compatibility with older initialization
51
+
52
+ ### 📦 Package Configuration
53
+ - ESM and CJS exports
54
+ - TypeScript declaration files
55
+ - Tree-shakeable exports
56
+ - Peer dependencies for adapters (optional)
57
+ - Source maps included
58
+
59
+ ### 📚 Documentation
60
+ - Comprehensive README with examples
61
+ - API reference documentation
62
+ - Adapter configuration guides
63
+ - Queue integration examples
64
+ - Error codes reference
65
+ - Type inference guide
66
+ - AGENTS.md for AI agent development
67
+
68
+ ### 🧪 Testing
69
+ - Unit tests for core functionality
70
+ - Adapter-specific tests
71
+ - Template validation tests
72
+ - Hook lifecycle tests
73
+ - Queue integration tests
74
+
75
+ ---
76
+
77
+ ## Future Releases
78
+
79
+ Planned features for upcoming versions:
80
+ - AWS SES adapter
81
+ - Mailgun adapter
82
+ - SparkPost adapter
83
+ - Email preview server
84
+ - Template hot-reloading
85
+ - Batch sending
86
+ - Attachment support
87
+ - Inline images
88
+ - Email tracking/analytics
89
+ - Template versioning
90
+ - A/B testing support
91
+ - Unsubscribe link management
92
+
93
+ ---
94
+
95
+ [0.1.0]: https://github.com/felipebarcelospro/igniter-js/releases/tag/@igniter-js/mail@0.1.0