@xenterprises/fastify-xtwilio 1.0.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/CHANGELOG.md ADDED
@@ -0,0 +1,189 @@
1
+ # Changelog
2
+
3
+ All notable changes to xTwilio 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
+ ## [Unreleased]
9
+
10
+ ### Planned
11
+
12
+ - Webhook event retry logic with exponential backoff
13
+ - Batch API operations for bulk SMS/email
14
+ - Message queue integration (Bull, RabbitMQ)
15
+ - Advanced analytics and reporting dashboard
16
+ - Multi-tenant support for messaging services
17
+ - Real-time delivery tracking with webhooks
18
+ - SMS campaign management tools
19
+ - Email list segmentation features
20
+ - A/B testing for email campaigns
21
+ - WhatsApp Business API integration
22
+ - Facebook Messenger integration
23
+ - Message template versioning and rollback
24
+
25
+ ---
26
+
27
+ ## [1.0.0] - 2024-12-29
28
+
29
+ ### Added
30
+
31
+ #### SMS Service
32
+ - `send()` - Send SMS messages
33
+ - `sendMMS()` - Send multimedia messages
34
+ - `schedule()` - Schedule messages for future delivery
35
+ - `cancelScheduled()` - Cancel scheduled messages
36
+ - `get()` - Retrieve message details
37
+ - `getStatus()` - Get message delivery status
38
+ - `list()` - List messages with filters
39
+ - `delete()` - Delete messages
40
+ - `getMedia()` - Get MMS media URLs
41
+ - `validatePhoneNumber()` - Validate and lookup phone numbers
42
+ - `sendBulk()` - Send bulk SMS to multiple recipients
43
+
44
+ #### Conversations Service
45
+ - `create()` - Create new conversations
46
+ - `get()` - Retrieve conversation details
47
+ - `update()` - Update conversation properties
48
+ - `list()` - List conversations with filters
49
+ - `delete()` - Delete conversations
50
+ - `addParticipant()` - Add participants to conversations
51
+ - `listParticipants()` - List conversation participants
52
+ - `removeParticipant()` - Remove participants
53
+ - `sendMessage()` - Send messages to conversations
54
+ - `sendMediaMessage()` - Send media in conversations
55
+ - `getMessages()` - Retrieve conversation messages
56
+ - `getMessage()` - Get specific message
57
+ - `deleteMessage()` - Delete conversation messages
58
+ - `getWebhooks()` - Get webhook configuration
59
+ - `updateWebhooks()` - Update webhook settings
60
+
61
+ #### RCS Service
62
+ - `send()` - Send RCS messages
63
+ - `sendMedia()` - Send RCS with media
64
+ - `sendTemplate()` - Send from templates
65
+ - `sendRichCard()` - Send rich card messages
66
+ - `sendCarousel()` - Send carousel of cards
67
+ - `sendQuickReplies()` - Send messages with quick reply options
68
+ - `getStatus()` - Get message status
69
+ - `listTemplates()` - List content templates
70
+ - `getTemplate()` - Get template details
71
+ - `deleteTemplate()` - Delete templates
72
+
73
+ #### Email Service
74
+ - `send()` - Send emails
75
+ - `sendTemplate()` - Send from SendGrid templates
76
+ - `sendWithAttachments()` - Send with file attachments
77
+ - `sendBulk()` - Send bulk emails
78
+ - `sendPersonalizedBulk()` - Send personalized bulk emails
79
+ - `validate()` - Validate email addresses
80
+ - `addContact()` - Add contacts to SendGrid
81
+ - `searchContact()` - Search contacts by email
82
+ - `deleteContact()` - Delete contacts
83
+ - `createList()` - Create contact lists
84
+ - `getLists()` - List contact lists
85
+ - `deleteList()` - Delete contact lists
86
+
87
+ #### Plugin Features
88
+ - Fastify plugin integration with decorator pattern
89
+ - TypeScript definitions for all services
90
+ - Environment-based configuration
91
+ - Service enablement toggles (Twilio, SendGrid)
92
+ - Request/response logging (opt-in)
93
+ - Comprehensive error handling
94
+ - Rate limiting ready (via Fastify plugins)
95
+ - Webhook support for Twilio events
96
+
97
+ ### Documentation
98
+ - Comprehensive README with usage examples
99
+ - Detailed API reference for all services
100
+ - TypeScript type definitions (index.d.ts)
101
+ - Security guidelines and best practices
102
+ - Environment configuration documentation
103
+ - QUICK_START guide for new users
104
+
105
+ ### Testing
106
+ - Unit tests for all services
107
+ - Integration test examples
108
+ - Mock implementations for testing
109
+ - Test fixtures and helpers
110
+
111
+ ### Configuration
112
+ - `.env.example` with comprehensive options
113
+ - Support for multiple environments
114
+ - Flexible credential management
115
+ - Feature flags for services
116
+
117
+ ### Infrastructure
118
+ - Docker support (Dockerfile, docker-compose.yml)
119
+ - CI/CD pipeline ready (.gitlab-ci.yml)
120
+ - Health check endpoints
121
+ - Logging and monitoring hooks
122
+
123
+ ---
124
+
125
+ ## Versioning
126
+
127
+ - **Major**: Breaking changes to API
128
+ - **Minor**: New features (backwards compatible)
129
+ - **Patch**: Bug fixes and improvements
130
+
131
+ ## Support
132
+
133
+ - **LTS**: Versions will be supported for 12 months from release
134
+ - **Current**: Latest version receives active development
135
+ - **Maintenance**: Critical security fixes only
136
+
137
+ ## Migration Guides
138
+
139
+ Future major versions will include migration guides in this changelog.
140
+
141
+ ---
142
+
143
+ ## Notes for Developers
144
+
145
+ ### Using RCS Service
146
+
147
+ The RCS service requires:
148
+ - Twilio Messaging Service SID
149
+ - Messaging Service configured for RCS in Twilio dashboard
150
+ - Supported recipient countries (check Twilio RCS availability)
151
+
152
+ ### Using Conversations Service
153
+
154
+ Conversations provide multi-channel messaging (SMS, Email, WhatsApp, etc.).
155
+
156
+ ### Using SendGrid Templates
157
+
158
+ Create templates in SendGrid dashboard and reference by ID:
159
+ - Get template IDs from SendGrid dashboard
160
+ - Store IDs in environment variables for easy management
161
+
162
+ ### Webhook Validation
163
+
164
+ Always validate webhook signatures in production:
165
+ - Use X-Twilio-Signature header
166
+ - Implement HMAC-SHA1 validation
167
+ - Log failed validations for security monitoring
168
+
169
+ ---
170
+
171
+ ## Related Projects
172
+
173
+ - [xStorage](https://github.com/xenterprises/fastify-xstorage) - File storage integration
174
+ - [xStripe](https://github.com/xenterprises/fastify-xstripe) - Payment processing
175
+ - [xPDF](https://github.com/xenterprises/fastify-xpdf) - PDF generation and manipulation
176
+
177
+ ---
178
+
179
+ ## Security Updates
180
+
181
+ Security vulnerabilities should be reported to security@xenterprises.com
182
+
183
+ ## License
184
+
185
+ ISC
186
+
187
+ ---
188
+
189
+ **Last Updated**: 2024-12-29
package/LICENSE ADDED
@@ -0,0 +1,15 @@
1
+ ISC License
2
+
3
+ Copyright (c) 2024 Tim Mushen
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any
6
+ purpose with or without fee is hereby granted, provided that the above
7
+ copyright notice and this permission notice appear in all copies.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,261 @@
1
+ # xTwilio
2
+
3
+ Fastify v5 plugin for Twilio communications (SMS, Conversations, RCS) and SendGrid email services.
4
+
5
+ ## Requirements
6
+
7
+ - **Fastify v5.0.0+**
8
+ - **Node.js v20+**
9
+
10
+ ## Installation
11
+
12
+ ```bash
13
+ npm install @xenterprises/fastify-xtwilio fastify@5
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ ```javascript
19
+ import Fastify from 'fastify';
20
+ import xTwilio from '@xenterprises/fastify-xtwilio';
21
+
22
+ const fastify = Fastify();
23
+
24
+ await fastify.register(xTwilio, {
25
+ twilio: {
26
+ accountSid: process.env.TWILIO_ACCOUNT_SID,
27
+ authToken: process.env.TWILIO_AUTH_TOKEN,
28
+ phoneNumber: process.env.TWILIO_PHONE_NUMBER,
29
+ messagingServiceSid: process.env.TWILIO_MESSAGING_SERVICE_SID, // For RCS
30
+ },
31
+ sendgrid: {
32
+ apiKey: process.env.SENDGRID_API_KEY,
33
+ fromEmail: process.env.SENDGRID_FROM_EMAIL,
34
+ },
35
+ });
36
+ ```
37
+
38
+ ## Services
39
+
40
+ ### SMS Service (`fastify.sms`)
41
+
42
+ ```javascript
43
+ // Send SMS
44
+ await fastify.sms.send('+1234567890', 'Hello World');
45
+
46
+ // Send MMS
47
+ await fastify.sms.sendMMS('+1234567890', 'Check this out!', 'https://example.com/image.jpg');
48
+
49
+ // Schedule SMS
50
+ await fastify.sms.schedule('+1234567890', 'Reminder', new Date('2024-12-31T10:00:00Z'));
51
+
52
+ // Send bulk SMS
53
+ await fastify.sms.sendBulk([
54
+ { to: '+1234567890', body: 'Message 1' },
55
+ { to: '+0987654321', body: 'Message 2' },
56
+ ]);
57
+
58
+ // Get message status
59
+ await fastify.sms.getStatus('SMXXXXXXXX');
60
+
61
+ // Validate phone number
62
+ await fastify.sms.validatePhoneNumber('+1234567890');
63
+ ```
64
+
65
+ ### Conversations Service (`fastify.conversations`)
66
+
67
+ ```javascript
68
+ // Create conversation
69
+ const conversation = await fastify.conversations.create('Support Chat');
70
+
71
+ // Add participants
72
+ await fastify.conversations.addParticipant(conversation.sid, 'user@example.com');
73
+ await fastify.conversations.addParticipant(conversation.sid, null, '+1234567890');
74
+
75
+ // Send message
76
+ await fastify.conversations.sendMessage(conversation.sid, 'Hello!', 'user@example.com');
77
+
78
+ // Get messages
79
+ const messages = await fastify.conversations.getMessages(conversation.sid);
80
+
81
+ // List participants
82
+ const participants = await fastify.conversations.listParticipants(conversation.sid);
83
+ ```
84
+
85
+ ### RCS Service (`fastify.rcs`)
86
+
87
+ ```javascript
88
+ // Send basic RCS message
89
+ await fastify.rcs.send('+1234567890', 'Hello from RCS!');
90
+
91
+ // Send rich card
92
+ await fastify.rcs.sendRichCard('+1234567890', {
93
+ title: 'Summer Sale',
94
+ description: '50% off all items',
95
+ mediaUrl: 'https://example.com/banner.jpg',
96
+ actions: [
97
+ { type: 'url', text: 'Shop Now', url: 'https://example.com/shop' },
98
+ ],
99
+ });
100
+
101
+ // Send carousel
102
+ await fastify.rcs.sendCarousel('+1234567890', [
103
+ { title: 'Product 1', description: 'Description 1', mediaUrl: 'url1.jpg' },
104
+ { title: 'Product 2', description: 'Description 2', mediaUrl: 'url2.jpg' },
105
+ ]);
106
+
107
+ // Send with quick replies
108
+ await fastify.rcs.sendQuickReplies('+1234567890', 'How can we help?', [
109
+ { text: 'Support', payload: 'support' },
110
+ { text: 'Sales', payload: 'sales' },
111
+ ]);
112
+
113
+ // Use content template
114
+ await fastify.rcs.sendTemplate('+1234567890', 'HXXXXXXXC', { name: 'John' });
115
+ ```
116
+
117
+ ### Email Service (`fastify.email`)
118
+
119
+ ```javascript
120
+ // Send email
121
+ await fastify.email.send(
122
+ 'user@example.com',
123
+ 'Welcome!',
124
+ '<h1>Welcome to our service</h1>',
125
+ 'Welcome to our service'
126
+ );
127
+
128
+ // Send with template
129
+ await fastify.email.sendTemplate(
130
+ 'user@example.com',
131
+ 'Welcome Email',
132
+ 'd-1234567890abcdef',
133
+ { firstName: 'John', code: '123456' }
134
+ );
135
+
136
+ // Send with attachments
137
+ await fastify.email.sendWithAttachments(
138
+ 'user@example.com',
139
+ 'Invoice',
140
+ '<p>Your invoice is attached</p>',
141
+ [
142
+ {
143
+ content: 'base64EncodedContent',
144
+ filename: 'invoice.pdf',
145
+ type: 'application/pdf',
146
+ },
147
+ ]
148
+ );
149
+
150
+ // Send bulk emails
151
+ await fastify.email.sendBulk(
152
+ ['user1@example.com', 'user2@example.com'],
153
+ 'Newsletter',
154
+ '<h1>This months updates</h1>'
155
+ );
156
+
157
+ // Validate email
158
+ const validation = await fastify.email.validate('test@example.com');
159
+
160
+ // Contact management
161
+ await fastify.email.addContact('user@example.com', {
162
+ firstName: 'John',
163
+ lastName: 'Doe',
164
+ });
165
+
166
+ await fastify.email.searchContact('user@example.com');
167
+
168
+ // List management
169
+ const list = await fastify.email.createList('Newsletter Subscribers');
170
+ const lists = await fastify.email.getLists();
171
+ ```
172
+
173
+ ## Configuration Options
174
+
175
+ ### Twilio Options
176
+
177
+ ```javascript
178
+ {
179
+ twilio: {
180
+ accountSid: 'ACXXXXXXXX', // Required
181
+ authToken: 'your_auth_token', // Required
182
+ phoneNumber: '+1234567890', // Required for SMS (or use messagingServiceSid)
183
+ messagingServiceSid: 'MGXXXXXXXX', // Required for RCS, optional for SMS
184
+ active: true, // Optional, default true
185
+ }
186
+ }
187
+ ```
188
+
189
+ ### SendGrid Options
190
+
191
+ ```javascript
192
+ {
193
+ sendgrid: {
194
+ apiKey: 'SG.XXXXXXXX', // Required
195
+ fromEmail: 'noreply@example.com', // Required
196
+ active: true, // Optional, default true
197
+ }
198
+ }
199
+ ```
200
+
201
+ ## API Reference
202
+
203
+ ### SMS Methods
204
+ - `send(to, body, options)` - Send SMS
205
+ - `sendMMS(to, body, mediaUrl, options)` - Send MMS
206
+ - `schedule(to, body, sendAt)` - Schedule message
207
+ - `cancelScheduled(messageSid)` - Cancel scheduled message
208
+ - `get(messageSid)` - Get message details
209
+ - `getStatus(messageSid)` - Get message status
210
+ - `list(filters)` - List messages
211
+ - `delete(messageSid)` - Delete message
212
+ - `getMedia(messageSid)` - Get media from MMS
213
+ - `validatePhoneNumber(phoneNumber, options)` - Validate phone
214
+ - `sendBulk(messages)` - Send bulk SMS
215
+
216
+ ### Conversations Methods
217
+ - `create(friendlyName, attributes)` - Create conversation
218
+ - `get(conversationSid)` - Get conversation
219
+ - `update(conversationSid, updates)` - Update conversation
220
+ - `list(filters)` - List conversations
221
+ - `delete(conversationSid)` - Delete conversation
222
+ - `addParticipant(conversationSid, identity, messagingBindingAddress)` - Add participant
223
+ - `listParticipants(conversationSid)` - List participants
224
+ - `removeParticipant(conversationSid, participantSid)` - Remove participant
225
+ - `sendMessage(conversationSid, body, author, attributes)` - Send message
226
+ - `sendMediaMessage(conversationSid, mediaUrl, body, author)` - Send media
227
+ - `getMessages(conversationSid, options)` - Get messages
228
+ - `getMessage(conversationSid, messageSid)` - Get specific message
229
+ - `deleteMessage(conversationSid, messageSid)` - Delete message
230
+ - `getWebhooks(conversationSid)` - Get webhooks
231
+ - `updateWebhooks(conversationSid, webhookConfig)` - Update webhooks
232
+
233
+ ### RCS Methods
234
+ - `send(to, body, options)` - Send basic RCS
235
+ - `sendMedia(to, body, mediaUrl)` - Send with media
236
+ - `sendTemplate(to, contentSid, contentVariables)` - Send template
237
+ - `sendRichCard(to, card)` - Send rich card
238
+ - `sendCarousel(to, cards)` - Send carousel
239
+ - `sendQuickReplies(to, body, replies)` - Send quick replies
240
+ - `getStatus(messageSid)` - Get status
241
+ - `listTemplates(options)` - List templates
242
+ - `getTemplate(contentSid)` - Get template
243
+ - `deleteTemplate(contentSid)` - Delete template
244
+
245
+ ### Email Methods
246
+ - `send(to, subject, html, text, options)` - Send email
247
+ - `sendTemplate(to, subject, templateId, dynamicData, options)` - Send template
248
+ - `sendWithAttachments(to, subject, html, attachments)` - Send with attachments
249
+ - `sendBulk(to, subject, html)` - Send bulk
250
+ - `sendPersonalizedBulk(messages)` - Send personalized bulk
251
+ - `validate(email)` - Validate email
252
+ - `addContact(email, data, listIds)` - Add contact
253
+ - `searchContact(email)` - Search contact
254
+ - `deleteContact(contactId)` - Delete contact
255
+ - `createList(name)` - Create list
256
+ - `getLists()` - Get lists
257
+ - `deleteList(listId)` - Delete list
258
+
259
+ ## License
260
+
261
+ ISC