mailbreeze 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/README.md ADDED
@@ -0,0 +1,360 @@
1
+ # MailBreeze JavaScript SDK
2
+
3
+ The official JavaScript/TypeScript SDK for the MailBreeze email platform.
4
+
5
+ ## Features
6
+
7
+ - **Full TypeScript support** - Complete type definitions for all API methods
8
+ - **Zero dependencies** - Uses native `fetch` API
9
+ - **Universal runtime** - Works with Node.js (v20+), Bun, and Deno
10
+ - **Automatic retries** - Built-in retry logic with exponential backoff
11
+ - **ESM & CJS** - Dual module support for all environments
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ # npm
17
+ npm install mailbreeze
18
+
19
+ # pnpm
20
+ pnpm add mailbreeze
21
+
22
+ # bun
23
+ bun add mailbreeze
24
+ ```
25
+
26
+ ## Quick Start
27
+
28
+ ```typescript
29
+ import { MailBreeze } from "mailbreeze";
30
+
31
+ const mailbreeze = new MailBreeze({
32
+ apiKey: "sk_live_xxx",
33
+ });
34
+
35
+ // Send an email
36
+ const result = await mailbreeze.emails.send({
37
+ from: "hello@yourdomain.com",
38
+ to: "user@example.com",
39
+ subject: "Welcome!",
40
+ html: "<h1>Welcome to our platform!</h1>",
41
+ });
42
+
43
+ console.log(result.id); // email_xxx
44
+ ```
45
+
46
+ ## Configuration
47
+
48
+ ```typescript
49
+ const mailbreeze = new MailBreeze({
50
+ // Required
51
+ apiKey: "sk_live_xxx",
52
+
53
+ // Optional
54
+ baseUrl: "https://api.mailbreeze.com", // API endpoint
55
+ timeout: 30000, // Request timeout in ms (default: 30s)
56
+ maxRetries: 3, // Max retry attempts (default: 3)
57
+ authStyle: "header", // "header" (X-API-Key) or "bearer"
58
+ });
59
+ ```
60
+
61
+ ## Resources
62
+
63
+ ### Emails
64
+
65
+ Send and manage emails.
66
+
67
+ ```typescript
68
+ // Send an email
69
+ const result = await mailbreeze.emails.send({
70
+ from: "hello@yourdomain.com",
71
+ to: "user@example.com",
72
+ subject: "Hello",
73
+ html: "<p>Hello World!</p>",
74
+ });
75
+
76
+ // Send with a template
77
+ const result = await mailbreeze.emails.send({
78
+ from: "hello@yourdomain.com",
79
+ to: ["user1@example.com", "user2@example.com"],
80
+ templateId: "welcome-template",
81
+ variables: { name: "John", plan: "Pro" },
82
+ });
83
+
84
+ // Send with attachments
85
+ const result = await mailbreeze.emails.send({
86
+ from: "hello@yourdomain.com",
87
+ to: "user@example.com",
88
+ subject: "Your Report",
89
+ html: "<p>Please find your report attached.</p>",
90
+ attachmentIds: ["att_xxx"],
91
+ });
92
+
93
+ // List emails
94
+ const { data, pagination } = await mailbreeze.emails.list({
95
+ status: "delivered",
96
+ page: 1,
97
+ limit: 20,
98
+ });
99
+
100
+ // Get email details
101
+ const email = await mailbreeze.emails.get("email_xxx");
102
+
103
+ // Get email statistics
104
+ const stats = await mailbreeze.emails.stats();
105
+ console.log(stats.deliveryRate); // 0.98
106
+ ```
107
+
108
+ ### Attachments
109
+
110
+ Handle email attachments using a two-step upload process.
111
+
112
+ ```typescript
113
+ // Step 1: Create upload URL
114
+ const { attachmentId, uploadUrl, uploadToken } = await mailbreeze.attachments.createUpload({
115
+ fileName: "report.pdf",
116
+ contentType: "application/pdf",
117
+ fileSize: 1024000,
118
+ });
119
+
120
+ // Step 2: Upload file directly to URL
121
+ await fetch(uploadUrl, {
122
+ method: "PUT",
123
+ body: fileBuffer,
124
+ headers: { "Content-Type": "application/pdf" },
125
+ });
126
+
127
+ // Step 3: Confirm upload
128
+ const attachment = await mailbreeze.attachments.confirm({ uploadToken });
129
+
130
+ // Step 4: Use in email
131
+ await mailbreeze.emails.send({
132
+ from: "hello@yourdomain.com",
133
+ to: "user@example.com",
134
+ subject: "Your report",
135
+ html: "<p>Please find the report attached.</p>",
136
+ attachmentIds: [attachmentId],
137
+ });
138
+ ```
139
+
140
+ ### Contact Lists
141
+
142
+ Create and manage contact lists.
143
+
144
+ ```typescript
145
+ // Create a list
146
+ const list = await mailbreeze.lists.create({
147
+ name: "Newsletter Subscribers",
148
+ description: "Weekly newsletter recipients",
149
+ customFields: [
150
+ { key: "company", label: "Company", type: "text" },
151
+ { key: "plan", label: "Plan", type: "select", options: ["free", "pro", "enterprise"] },
152
+ ],
153
+ });
154
+
155
+ // List all lists
156
+ const { data } = await mailbreeze.lists.list();
157
+
158
+ // Get a list
159
+ const list = await mailbreeze.lists.get("list_xxx");
160
+
161
+ // Update a list
162
+ const updated = await mailbreeze.lists.update("list_xxx", {
163
+ name: "Updated Name",
164
+ });
165
+
166
+ // Delete a list
167
+ await mailbreeze.lists.delete("list_xxx");
168
+
169
+ // Get list statistics
170
+ const stats = await mailbreeze.lists.stats("list_xxx");
171
+ console.log(stats.activeContacts); // 1000
172
+ ```
173
+
174
+ ### Contacts
175
+
176
+ Manage contacts within a specific list.
177
+
178
+ ```typescript
179
+ // Get contacts for a list
180
+ const contacts = mailbreeze.contacts("list_xxx");
181
+
182
+ // Create a contact
183
+ const contact = await contacts.create({
184
+ email: "user@example.com",
185
+ firstName: "John",
186
+ lastName: "Doe",
187
+ customFields: { company: "Acme Inc" },
188
+ });
189
+
190
+ // List contacts
191
+ const { data, pagination } = await contacts.list({
192
+ status: "active",
193
+ page: 1,
194
+ limit: 50,
195
+ });
196
+
197
+ // Get a contact
198
+ const contact = await contacts.get("contact_xxx");
199
+
200
+ // Update a contact
201
+ const updated = await contacts.update("contact_xxx", {
202
+ firstName: "Jane",
203
+ customFields: { plan: "enterprise" },
204
+ });
205
+
206
+ // Delete a contact
207
+ await contacts.delete("contact_xxx");
208
+
209
+ // Suppress a contact
210
+ await contacts.suppress("contact_xxx", "manual");
211
+ ```
212
+
213
+ ### Email Verification
214
+
215
+ Verify email addresses before sending.
216
+
217
+ ```typescript
218
+ // Verify a single email
219
+ const result = await mailbreeze.verification.verify("user@example.com");
220
+ console.log(result.isValid); // true
221
+ console.log(result.result); // "valid"
222
+ console.log(result.details?.isDisposable); // false
223
+
224
+ // Batch verification
225
+ const batch = await mailbreeze.verification.batch({
226
+ emails: ["user1@example.com", "user2@example.com", "user3@example.com"],
227
+ });
228
+
229
+ // Check batch status
230
+ const status = await mailbreeze.verification.get(batch.verificationId);
231
+ if (status.status === "completed") {
232
+ console.log(status.results);
233
+ console.log(status.analytics);
234
+ }
235
+
236
+ // List verification batches
237
+ const { data } = await mailbreeze.verification.list({
238
+ status: "completed",
239
+ page: 1,
240
+ });
241
+
242
+ // Get verification statistics
243
+ const stats = await mailbreeze.verification.stats();
244
+ console.log(`Verified: ${stats.totalVerified}, Valid: ${stats.valid}`);
245
+ ```
246
+
247
+ ### Automations
248
+
249
+ Enroll contacts in automation workflows.
250
+
251
+ ```typescript
252
+ // Enroll a contact in an automation
253
+ const enrollment = await mailbreeze.automations.enroll({
254
+ automationId: "auto_welcome",
255
+ contactId: "contact_xxx",
256
+ variables: { couponCode: "WELCOME10" },
257
+ });
258
+
259
+ // List enrollments
260
+ const { data } = await mailbreeze.automations.enrollments.list({
261
+ automationId: "auto_xxx",
262
+ status: "active",
263
+ });
264
+
265
+ // Cancel an enrollment
266
+ const result = await mailbreeze.automations.enrollments.cancel("enroll_xxx");
267
+ console.log(result.cancelled); // true
268
+ ```
269
+
270
+ ## Error Handling
271
+
272
+ The SDK provides typed error classes for different failure scenarios:
273
+
274
+ ```typescript
275
+ import {
276
+ MailBreezeError,
277
+ AuthenticationError,
278
+ ValidationError,
279
+ NotFoundError,
280
+ RateLimitError,
281
+ ServerError,
282
+ } from "mailbreeze";
283
+
284
+ try {
285
+ await mailbreeze.emails.send({
286
+ from: "invalid",
287
+ to: "user@example.com",
288
+ subject: "Test",
289
+ });
290
+ } catch (error) {
291
+ if (error instanceof ValidationError) {
292
+ console.log("Validation failed:", error.message);
293
+ console.log("Details:", error.details);
294
+ } else if (error instanceof AuthenticationError) {
295
+ console.log("Invalid API key");
296
+ } else if (error instanceof RateLimitError) {
297
+ console.log(`Rate limited. Retry after ${error.retryAfter} seconds`);
298
+ } else if (error instanceof NotFoundError) {
299
+ console.log("Resource not found");
300
+ } else if (error instanceof ServerError) {
301
+ console.log("Server error:", error.statusCode);
302
+ } else if (error instanceof MailBreezeError) {
303
+ console.log("API error:", error.code, error.message);
304
+ }
305
+ }
306
+ ```
307
+
308
+ ### Error Properties
309
+
310
+ All errors extend `MailBreezeError` and include:
311
+
312
+ - `message` - Human-readable error message
313
+ - `code` - Machine-readable error code
314
+ - `statusCode` - HTTP status code (when applicable)
315
+ - `requestId` - Unique request ID for debugging
316
+ - `details` - Additional error details (for validation errors)
317
+
318
+ ## Idempotency
319
+
320
+ Prevent duplicate operations by passing an idempotency key:
321
+
322
+ ```typescript
323
+ const result = await mailbreeze.emails.send({
324
+ from: "hello@yourdomain.com",
325
+ to: "user@example.com",
326
+ subject: "Order Confirmation",
327
+ html: "<p>Thank you for your order!</p>",
328
+ idempotencyKey: `order-confirmation-${orderId}`,
329
+ });
330
+ ```
331
+
332
+ ## TypeScript
333
+
334
+ All types are exported for use in your application:
335
+
336
+ ```typescript
337
+ import type {
338
+ SendEmailParams,
339
+ SendEmailResult,
340
+ Email,
341
+ Contact,
342
+ ContactList,
343
+ VerifyEmailResult,
344
+ Enrollment,
345
+ PaginatedList,
346
+ } from "mailbreeze";
347
+
348
+ function sendWelcomeEmail(params: SendEmailParams): Promise<SendEmailResult> {
349
+ return mailbreeze.emails.send(params);
350
+ }
351
+ ```
352
+
353
+ ## Requirements
354
+
355
+ - Node.js v20 or higher (or Bun/Deno)
356
+ - A MailBreeze API key ([get one here](https://console.mailbreeze.com))
357
+
358
+ ## License
359
+
360
+ MIT