@bernierllc/email-webhook-events 1.4.1 → 1.6.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.
Files changed (52) hide show
  1. package/dist/EmailWebhookEventsService.js +4 -0
  2. package/dist/EmailWebhookEventsService.js.map +1 -1
  3. package/dist/analytics/AnalyticsEngine.js +2 -1
  4. package/dist/analytics/AnalyticsEngine.js.map +1 -1
  5. package/dist/errors.d.ts +12 -0
  6. package/dist/errors.d.ts.map +1 -0
  7. package/dist/errors.js +25 -0
  8. package/dist/errors.js.map +1 -0
  9. package/dist/index.d.ts +1 -0
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +3 -1
  12. package/dist/index.js.map +1 -1
  13. package/dist/persistence/InMemoryPersistenceAdapter.js +2 -4
  14. package/dist/persistence/InMemoryPersistenceAdapter.js.map +1 -1
  15. package/package.json +6 -2
  16. package/.eslintrc.cjs +0 -30
  17. package/CHANGELOG.md +0 -139
  18. package/__tests__/EmailWebhookEventsService.test.ts +0 -251
  19. package/__tests__/WebhookEventCallbacks.test.ts +0 -343
  20. package/__tests__/analytics/AnalyticsEngine.test.ts +0 -250
  21. package/__tests__/fixtures/sendgrid-webhooks/.gitkeep +0 -0
  22. package/__tests__/fixtures/sendgrid-webhooks/README.md +0 -63
  23. package/__tests__/fixtures/sendgrid-webhooks/deferred.json +0 -31
  24. package/__tests__/fixtures/sendgrid-webhooks/delivered.json +0 -83
  25. package/__tests__/fixtures/sendgrid-webhooks/open.json +0 -83
  26. package/__tests__/integration/ngrok-tunnel-manager.ts +0 -88
  27. package/__tests__/integration/sendgrid-webhook-integration.test.ts +0 -272
  28. package/__tests__/integration/webhook-server.ts +0 -188
  29. package/__tests__/mocks/README.md +0 -172
  30. package/__tests__/mocks/index.ts +0 -94
  31. package/__tests__/mocks/sendgrid-api-responses.ts +0 -391
  32. package/__tests__/mocks/sendgrid-webhook-payloads.ts +0 -463
  33. package/__tests__/normalizers/MailgunNormalizer.test.ts +0 -149
  34. package/__tests__/normalizers/PostmarkNormalizer.test.ts +0 -112
  35. package/__tests__/normalizers/SESNormalizer.test.ts +0 -168
  36. package/__tests__/normalizers/SMTP2GONormalizer.test.ts +0 -83
  37. package/__tests__/normalizers/SendGridNormalizer.test.ts +0 -181
  38. package/__tests__/persistence/PersistenceAdapter.test.ts +0 -103
  39. package/jest.config.cjs +0 -38
  40. package/src/EmailWebhookEventsService.ts +0 -290
  41. package/src/analytics/AnalyticsEngine.ts +0 -230
  42. package/src/index.ts +0 -40
  43. package/src/normalizers/EventNormalizer.ts +0 -13
  44. package/src/normalizers/MailgunNormalizer.ts +0 -72
  45. package/src/normalizers/PostmarkNormalizer.ts +0 -72
  46. package/src/normalizers/SESNormalizer.ts +0 -117
  47. package/src/normalizers/SMTP2GONormalizer.ts +0 -54
  48. package/src/normalizers/SendGridNormalizer.ts +0 -63
  49. package/src/persistence/InMemoryPersistenceAdapter.ts +0 -68
  50. package/src/persistence/PersistenceAdapter.ts +0 -15
  51. package/src/types.ts +0 -261
  52. package/tsconfig.json +0 -31
@@ -1,391 +0,0 @@
1
- /*
2
- Copyright (c) 2025 Bernier LLC
3
-
4
- This file is licensed to the client under a limited-use license.
5
- The client may use and modify this code *only within the scope of the project it was delivered for*.
6
- Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.
7
- */
8
-
9
- /**
10
- * SendGrid API Mock Responses
11
- *
12
- * These mocks simulate SendGrid's Mail Send API responses for testing
13
- * the email sending side without hitting the real SendGrid API.
14
- *
15
- * Philosophy: "Only mock services that are not owned by us"
16
- * - SendGrid is external, so we mock it
17
- * - Our internal packages use real implementations
18
- *
19
- * Based on SendGrid API v3 documentation and real API responses.
20
- */
21
-
22
- // ============================================================================
23
- // TYPE DEFINITIONS
24
- // ============================================================================
25
-
26
- export interface SendGridSuccessResponse {
27
- statusCode: 202;
28
- headers: {
29
- 'x-message-id': string;
30
- 'content-type': string;
31
- 'date': string;
32
- };
33
- body: string; // Empty string on success
34
- }
35
-
36
- export interface SendGridErrorResponse {
37
- statusCode: number;
38
- headers: {
39
- 'content-type': string;
40
- 'date': string;
41
- };
42
- body: {
43
- errors: Array<{
44
- message: string;
45
- field: string | null;
46
- help: string | null;
47
- }>;
48
- };
49
- }
50
-
51
- export interface SendGridRateLimitResponse {
52
- statusCode: 429;
53
- headers: {
54
- 'content-type': string;
55
- 'x-ratelimit-limit': string;
56
- 'x-ratelimit-remaining': string;
57
- 'x-ratelimit-reset': string;
58
- 'retry-after': string;
59
- };
60
- body: {
61
- errors: Array<{
62
- message: string;
63
- field: string | null;
64
- help: string | null;
65
- }>;
66
- };
67
- }
68
-
69
- // ============================================================================
70
- // MOCK GENERATORS
71
- // ============================================================================
72
-
73
- /**
74
- * Generate a unique SendGrid message ID (format: xxx.yyy)
75
- */
76
- function generateMessageId(): string {
77
- const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
78
- let prefix = '';
79
- for (let i = 0; i < 22; i++) {
80
- prefix += chars.charAt(Math.floor(Math.random() * chars.length));
81
- }
82
- return prefix;
83
- }
84
-
85
- /**
86
- * Create a successful send response (202 Accepted)
87
- */
88
- export function createSuccessResponse(messageId?: string): SendGridSuccessResponse {
89
- return {
90
- statusCode: 202,
91
- headers: {
92
- 'x-message-id': messageId || generateMessageId(),
93
- 'content-type': 'application/json',
94
- 'date': new Date().toUTCString()
95
- },
96
- body: '' // SendGrid returns empty body on success
97
- };
98
- }
99
-
100
- /**
101
- * Create a validation error response (400 Bad Request)
102
- */
103
- export function createValidationErrorResponse(
104
- errors: Array<{ message: string; field?: string }>
105
- ): SendGridErrorResponse {
106
- return {
107
- statusCode: 400,
108
- headers: {
109
- 'content-type': 'application/json',
110
- 'date': new Date().toUTCString()
111
- },
112
- body: {
113
- errors: errors.map(e => ({
114
- message: e.message,
115
- field: e.field || null,
116
- help: null
117
- }))
118
- }
119
- };
120
- }
121
-
122
- /**
123
- * Create an authentication error response (401 Unauthorized)
124
- */
125
- export function createAuthErrorResponse(): SendGridErrorResponse {
126
- return {
127
- statusCode: 401,
128
- headers: {
129
- 'content-type': 'application/json',
130
- 'date': new Date().toUTCString()
131
- },
132
- body: {
133
- errors: [{
134
- message: 'The provided authorization grant is invalid, expired, or revoked',
135
- field: null,
136
- help: null
137
- }]
138
- }
139
- };
140
- }
141
-
142
- /**
143
- * Create a forbidden error response (403 Forbidden)
144
- */
145
- export function createForbiddenResponse(reason: string = 'Access to this resource is denied'): SendGridErrorResponse {
146
- return {
147
- statusCode: 403,
148
- headers: {
149
- 'content-type': 'application/json',
150
- 'date': new Date().toUTCString()
151
- },
152
- body: {
153
- errors: [{
154
- message: reason,
155
- field: null,
156
- help: null
157
- }]
158
- }
159
- };
160
- }
161
-
162
- /**
163
- * Create a sender identity error response (403)
164
- */
165
- export function createSenderIdentityErrorResponse(): SendGridErrorResponse {
166
- return createForbiddenResponse(
167
- 'The from address does not match a verified Sender Identity. Mail cannot be sent until this error is resolved. Visit https://sendgrid.com/docs/for-developers/sending-email/sender-identity/ to see the Sender Identity requirements'
168
- );
169
- }
170
-
171
- /**
172
- * Create a rate limit response (429 Too Many Requests)
173
- */
174
- export function createRateLimitResponse(
175
- retryAfterSeconds: number = 60,
176
- limit: number = 500,
177
- remaining: number = 0
178
- ): SendGridRateLimitResponse {
179
- const resetTime = Math.floor(Date.now() / 1000) + retryAfterSeconds;
180
-
181
- return {
182
- statusCode: 429,
183
- headers: {
184
- 'content-type': 'application/json',
185
- 'x-ratelimit-limit': limit.toString(),
186
- 'x-ratelimit-remaining': remaining.toString(),
187
- 'x-ratelimit-reset': resetTime.toString(),
188
- 'retry-after': retryAfterSeconds.toString()
189
- },
190
- body: {
191
- errors: [{
192
- message: 'Too many requests',
193
- field: null,
194
- help: null
195
- }]
196
- }
197
- };
198
- }
199
-
200
- /**
201
- * Create a server error response (500 Internal Server Error)
202
- */
203
- export function createServerErrorResponse(): SendGridErrorResponse {
204
- return {
205
- statusCode: 500,
206
- headers: {
207
- 'content-type': 'application/json',
208
- 'date': new Date().toUTCString()
209
- },
210
- body: {
211
- errors: [{
212
- message: 'An error occurred on the server. Please try again later.',
213
- field: null,
214
- help: null
215
- }]
216
- }
217
- };
218
- }
219
-
220
- /**
221
- * Create a service unavailable response (503)
222
- */
223
- export function createServiceUnavailableResponse(): SendGridErrorResponse {
224
- return {
225
- statusCode: 503,
226
- headers: {
227
- 'content-type': 'application/json',
228
- 'date': new Date().toUTCString()
229
- },
230
- body: {
231
- errors: [{
232
- message: 'The service is temporarily unavailable. Please try again later.',
233
- field: null,
234
- help: null
235
- }]
236
- }
237
- };
238
- }
239
-
240
- // ============================================================================
241
- // FETCH MOCK HELPERS
242
- // ============================================================================
243
-
244
- /**
245
- * Create a mock fetch response for testing
246
- */
247
- export function createMockFetchResponse(
248
- response: SendGridSuccessResponse | SendGridErrorResponse | SendGridRateLimitResponse
249
- ): Response {
250
- const headers = new Headers();
251
- Object.entries(response.headers).forEach(([key, value]) => {
252
- headers.set(key, value);
253
- });
254
-
255
- return {
256
- ok: response.statusCode >= 200 && response.statusCode < 300,
257
- status: response.statusCode,
258
- statusText: getStatusText(response.statusCode),
259
- headers,
260
- json: async () => response.body || {},
261
- text: async () => typeof response.body === 'string' ? response.body : JSON.stringify(response.body),
262
- clone: function() { return this; },
263
- body: null,
264
- bodyUsed: false,
265
- arrayBuffer: async () => new ArrayBuffer(0),
266
- blob: async () => new Blob(),
267
- formData: async () => new FormData(),
268
- redirected: false,
269
- type: 'basic' as ResponseType,
270
- url: 'https://api.sendgrid.com/v3/mail/send'
271
- } as Response;
272
- }
273
-
274
- function getStatusText(code: number): string {
275
- const statusTexts: Record<number, string> = {
276
- 200: 'OK',
277
- 202: 'Accepted',
278
- 400: 'Bad Request',
279
- 401: 'Unauthorized',
280
- 403: 'Forbidden',
281
- 404: 'Not Found',
282
- 429: 'Too Many Requests',
283
- 500: 'Internal Server Error',
284
- 503: 'Service Unavailable'
285
- };
286
- return statusTexts[code] || 'Unknown';
287
- }
288
-
289
- // ============================================================================
290
- // TEST SCENARIO HELPERS
291
- // ============================================================================
292
-
293
- /**
294
- * Mock fetch that succeeds
295
- */
296
- export function mockSuccessfulSend(): jest.Mock | (() => Promise<Response>) {
297
- const response = createSuccessResponse();
298
- return async () => createMockFetchResponse(response);
299
- }
300
-
301
- /**
302
- * Mock fetch that fails with rate limit then succeeds
303
- */
304
- export function mockRateLimitThenSuccess(): () => Promise<Response> {
305
- let callCount = 0;
306
- return async () => {
307
- callCount++;
308
- if (callCount === 1) {
309
- return createMockFetchResponse(createRateLimitResponse(1));
310
- }
311
- return createMockFetchResponse(createSuccessResponse());
312
- };
313
- }
314
-
315
- /**
316
- * Mock fetch that fails with server error then succeeds
317
- */
318
- export function mockServerErrorThenSuccess(): () => Promise<Response> {
319
- let callCount = 0;
320
- return async () => {
321
- callCount++;
322
- if (callCount === 1) {
323
- return createMockFetchResponse(createServerErrorResponse());
324
- }
325
- return createMockFetchResponse(createSuccessResponse());
326
- };
327
- }
328
-
329
- /**
330
- * Mock fetch that always fails
331
- */
332
- export function mockPermanentFailure(errorType: 'auth' | 'forbidden' | 'validation' | 'server' = 'server'): () => Promise<Response> {
333
- return async () => {
334
- switch (errorType) {
335
- case 'auth':
336
- return createMockFetchResponse(createAuthErrorResponse());
337
- case 'forbidden':
338
- return createMockFetchResponse(createSenderIdentityErrorResponse());
339
- case 'validation':
340
- return createMockFetchResponse(createValidationErrorResponse([
341
- { message: 'Invalid email address', field: 'to' }
342
- ]));
343
- case 'server':
344
- default:
345
- return createMockFetchResponse(createServerErrorResponse());
346
- }
347
- };
348
- }
349
-
350
- // ============================================================================
351
- // USER PROFILE API MOCKS (for connection testing)
352
- // ============================================================================
353
-
354
- export interface SendGridUserProfile {
355
- address: string;
356
- address2: string;
357
- city: string;
358
- company: string;
359
- country: string;
360
- email: string;
361
- first_name: string;
362
- last_name: string;
363
- phone: string;
364
- state: string;
365
- website: string;
366
- zip: string;
367
- }
368
-
369
- /**
370
- * Create a successful user profile response (for testConnection)
371
- */
372
- export function createUserProfileResponse(): { statusCode: 200; body: SendGridUserProfile } {
373
- return {
374
- statusCode: 200,
375
- body: {
376
- address: '123 Test St',
377
- address2: '',
378
- city: 'Test City',
379
- company: 'Test Company',
380
- country: 'US',
381
- email: 'test@example.com',
382
- first_name: 'Test',
383
- last_name: 'User',
384
- phone: '+1234567890',
385
- state: 'CA',
386
- website: 'https://example.com',
387
- zip: '12345'
388
- }
389
- };
390
- }
391
-