mailpit-api 1.1.1 → 1.2.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/dist/cjs/index.js CHANGED
@@ -44,7 +44,34 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
44
44
  Object.defineProperty(exports, "__esModule", { value: true });
45
45
  exports.MailpitClient = void 0;
46
46
  const axios_1 = __importStar(require("axios"));
47
+ /**
48
+ * Client for interacting with the {@link https://mailpit.axllent.org/docs/api-v1/ | Mailpit API}.
49
+ * @example
50
+ * ```typescript
51
+ * import { MailpitClient } from "mailpit-api";
52
+ * const mailpit = new MailpitClient("http://localhost:8025");
53
+ * console.log(await mailpit.getInfo());
54
+ * ```
55
+ */
47
56
  class MailpitClient {
57
+ /**
58
+ * Creates an instance of {@link MailpitClient}.
59
+ * @param baseURL - The base URL of the Mailpit API.
60
+ * @param auth - Optional authentication credentials.
61
+ * @param auth.username - The username for basic authentication.
62
+ * @param auth.password - The password for basic authentication.
63
+ * @example No Auth
64
+ * ```typescript
65
+ * const mailpit = new MailpitClient("http://localhost:8025");
66
+ * ```
67
+ * @example Basic Auth
68
+ * ```typescript
69
+ * const mailpit = new MailpitClient("http://localhost:8025", {
70
+ * username: "admin",
71
+ * password: "supersecret",
72
+ * });
73
+ * ```
74
+ */
48
75
  constructor(baseURL, auth) {
49
76
  this.axiosInstance = axios_1.default.create({
50
77
  baseURL,
@@ -84,28 +111,77 @@ class MailpitClient {
84
111
  }
85
112
  });
86
113
  }
87
- // Application
114
+ /**
115
+ * Retrieves information about the Mailpit instance.
116
+ *
117
+ * @returns Basic runtime information, message totals and latest release version.
118
+ * @example
119
+ * ```typescript
120
+ * const info = await mailpit.getInfo();
121
+ * ```
122
+ */
88
123
  getInfo() {
89
124
  return __awaiter(this, void 0, void 0, function* () {
90
125
  return yield this.handleRequest(() => this.axiosInstance.get("/api/v1/info"));
91
126
  });
92
127
  }
128
+ /**
129
+ * Retrieves the configuration of the Mailpit web UI.
130
+ * @remarks Intended for web UI only!
131
+ * @returns Configuration settings
132
+ * @example
133
+ * ```typescript
134
+ * const config = await mailpit.getConfiguration();
135
+ * ```
136
+ */
93
137
  getConfiguration() {
94
138
  return __awaiter(this, void 0, void 0, function* () {
95
139
  return yield this.handleRequest(() => this.axiosInstance.get("/api/v1/webui"));
96
140
  });
97
141
  }
98
- // Message
142
+ /**
143
+ * Retrieves a summary of a specific message and marks it as read.
144
+ * @param id - The message database ID. Defaults to `latest` to return the latest message.
145
+ * @returns Message summary
146
+ * @example
147
+ * ```typescript
148
+ * const message = await mailpit.getMessageSummary();
149
+ * ```
150
+ */
99
151
  getMessageSummary() {
100
152
  return __awaiter(this, arguments, void 0, function* (id = "latest") {
101
153
  return yield this.handleRequest(() => this.axiosInstance.get(`/api/v1/message/${id}`));
102
154
  });
103
155
  }
156
+ /**
157
+ * Retrieves the headers of a specific message.
158
+ * @remarks Header keys are returned alphabetically.
159
+ * @param id - The message database ID. Defaults to `latest` to return the latest message.
160
+ * @returns Message headers
161
+ * @example
162
+ * ```typescript
163
+ * const headers = await mailpit.getMessageHeaders();
164
+ * ```
165
+ */
104
166
  getMessageHeaders() {
105
167
  return __awaiter(this, arguments, void 0, function* (id = "latest") {
106
168
  return yield this.handleRequest(() => this.axiosInstance.get(`/api/v1/message/${id}/headers`));
107
169
  });
108
170
  }
171
+ /**
172
+ * Retrieves a specific attachment from a message.
173
+ * @param id - Message database ID or "latest"
174
+ * @param partID - The attachment part ID
175
+ * @returns Attachment as binary data and the content type
176
+ * @example
177
+ * ```typescript
178
+ * const message = await mailpit.getMessageSummary();
179
+ * if (message.Attachments.length) {
180
+ * const attachment = await mailpit.getMessageAttachment(message.ID, message.Attachments[0].PartID);
181
+ * // Do something with the attachment data
182
+ * }
183
+ * ```
184
+ */
109
185
  getMessageAttachment(id, partID) {
110
186
  return __awaiter(this, void 0, void 0, function* () {
111
187
  const response = yield this.handleRequest(() => this.axiosInstance.get(`/api/v1/message/${id}/part/${partID}`, { responseType: "arraybuffer" }), { fullResponse: true });
@@ -115,11 +191,24 @@ class MailpitClient {
115
191
  };
116
192
  });
117
193
  }
118
- getMessageSource() {
119
- return __awaiter(this, arguments, void 0, function* (id = "latest") {
120
- return yield this.handleRequest(() => this.axiosInstance.get(`/api/v1/message/${id}/raw`));
121
- });
122
- }
194
+ /**
195
+ * Generates a cropped 180x120 JPEG thumbnail of an image attachment from a message.
196
+ * Only image attachments are supported.
197
+ * @remarks
198
+ * If the image is smaller than 180x120 then the image is padded.
199
+ * If the attachment is not an image then a blank image is returned.
200
+ * @param id - Message database ID or "latest"
201
+ * @param partID - The attachment part ID
202
+ * @returns Image attachment thumbnail as binary data and the content type
203
+ * @example
204
+ * ```typescript
205
+ * const message = await mailpit.getMessageSummary();
206
+ * if (message.Attachments.length) {
207
+ * const thumbnail = await mailpit.getAttachmentThumbnail(message.ID, message.Attachments[0].PartID);
208
+ * // Do something with the thumbnail data
209
+ * }
210
+ * ```
211
+ */
123
212
  getAttachmentThumbnail(id, partID) {
124
213
  return __awaiter(this, void 0, void 0, function* () {
125
214
  const response = yield this.handleRequest(() => this.axiosInstance.get(`/api/v1/message/${id}/part/${partID}/thumb`, {
@@ -131,43 +220,108 @@ class MailpitClient {
131
220
  };
132
221
  });
133
222
  }
134
- releaseMessage(id, releaseRequest) {
223
+ /**
224
+ * Retrieves the full email message source as plain text.
225
+ * @param id - The message database ID. Defaults to `latest` to return the latest message.
226
+ * @returns Plain text message source
227
+ * @example
228
+ * ```typescript
229
+ * const messageSource = await mailpit.getMessageSource();
230
+ * ```
231
+ */
232
+ getMessageSource() {
233
+ return __awaiter(this, arguments, void 0, function* (id = "latest") {
234
+ return yield this.handleRequest(() => this.axiosInstance.get(`/api/v1/message/${id}/raw`));
235
+ });
236
+ }
237
+ /**
238
+ * Release a message via a pre-configured external SMTP server.
239
+ * @remarks This is only enabled if message relaying has been configured.
240
+ * @param id - The message database ID. Use `latest` to return the latest message.
241
+ * @param relayTo - Array of email addresses to relay the message to
242
+ * @returns Plain text "ok" response
243
+ * @example
244
+ * ```typescript
245
+ * const message = await mailpit.releaseMessage("latest", ["user1@example.test", "user2@example.test"]);
246
+ * ```
247
+ */
248
+ releaseMessage(id, relayTo) {
135
249
  return __awaiter(this, void 0, void 0, function* () {
136
- return yield this.handleRequest(() => this.axiosInstance.post(`/api/v1/message/${id}/release`, releaseRequest));
250
+ return yield this.handleRequest(() => this.axiosInstance.post(`/api/v1/message/${id}/release`, relayTo));
137
251
  });
138
252
  }
253
+ /**
254
+ * Sends a message
255
+ * @param sendReqest - The request containing the message details.
256
+ * @returns Response containing database messsage ID
257
+ * @example
258
+ * ```typescript
259
+ * await mailpit.sendMessage(
260
+ * From: { Email: "user@example.test", Name: "First LastName" },
261
+ * To: [{ Email: "rec@example.test", Name: "Recipient Name"}, {Email: "another@example.test"}],
262
+ * Subject: "Test Email",
263
+ * );
264
+ * ```
265
+ */
139
266
  sendMessage(sendReqest) {
140
267
  return __awaiter(this, void 0, void 0, function* () {
141
268
  return yield this.handleRequest(() => this.axiosInstance.post(`/api/v1/send`, sendReqest));
142
269
  });
143
270
  }
144
- // Other
145
- htmlCheck() {
146
- return __awaiter(this, arguments, void 0, function* (id = "latest") {
147
- return yield this.handleRequest(() => this.axiosInstance.get(`/api/v1/message/${id}/html-check`));
148
- });
149
- }
150
- linkCheck() {
151
- return __awaiter(this, arguments, void 0, function* (id = "latest", follow = "false") {
152
- return yield this.handleRequest(() => this.axiosInstance.get(`/api/v1/message/${id}/link-check`, { params: { follow } }));
153
- });
154
- }
155
- spamAssassinCheck() {
156
- return __awaiter(this, arguments, void 0, function* (id = "latest") {
157
- return yield this.handleRequest(() => this.axiosInstance.get(`/api/v1/message/${id}/sa-check`));
158
- });
159
- }
160
- // Messages
271
+ /**
272
+ * Retrieves a list of message summaries ordered from newest to oldest.
273
+ * @remarks Only contains the number of attachments and a snippet of the message body.
274
+ * @see {@link MailpitClient.getMessageSummary | getMessageSummary()} for more attachment and body details for a specific message.
275
+ * @param start - The pagination offset. Defaults to `0`.
276
+ * @param limit - The number of messages to retrieve. Defaults to `50`.
277
+ * @returns A list of message summaries
278
+ * @example
279
+ * ```typescript
280
+ * const messages = await.listMessages();
281
+ * ```
282
+ */
161
283
  listMessages() {
162
284
  return __awaiter(this, arguments, void 0, function* (start = 0, limit = 50) {
163
285
  return yield this.handleRequest(() => this.axiosInstance.get(`/api/v1/messages`, { params: { start, limit } }));
164
286
  });
165
287
  }
288
+ /**
289
+ * Set the read status of messages.
290
+ * @param readStatus - The request containing the message database IDs and read status.
291
+ * @param readStatus.Read - The read status to set. Defaults to `false`.
292
+ * @param readStatus.IDs - The IDs of the messages to update. If not set then all messages are updated.
293
+ * @returns Plain text "ok" response
294
+ * @example
295
+ * ```typescript
296
+ * // Set all messages as unread
297
+ * await mailpit.setReadStatus();
298
+ *
299
+ * // Set all messages as read
300
+ * await mailpit.setReadStatus({ Read: true });
301
+ *
302
+ * // Set specific messages as read
303
+ * await mailpit.setReadStatus({ IDs: ["1", "2", "3"], Read: true });
304
+ * ```
305
+ */
166
306
  setReadStatus(readStatus) {
167
307
  return __awaiter(this, void 0, void 0, function* () {
168
308
  return yield this.handleRequest(() => this.axiosInstance.put(`/api/v1/messages`, readStatus));
169
309
  });
170
310
  }
311
+ /**
312
+ * Delete individual or all messages.
313
+ * @remarks If no `IDs` are provided then all messages are deleted.
314
+ * @param deleteRequest - The request containing the message database IDs to delete.
315
+ * @returns Plain text "ok" response
316
+ * @example
317
+ * ```typescript
318
+ * // Delete all messages
319
+ * await mailpit.deleteMessages();
320
+ *
321
+ * // Delete specific messages
322
+ * await mailpit.deleteMessages({ IDs: ["1", "2", "3"] });
323
+ * ```
324
+ */
171
325
  deleteMessages(deleteRequest) {
172
326
  return __awaiter(this, void 0, void 0, function* () {
173
327
  return yield this.handleRequest(() => this.axiosInstance.delete(`/api/v1/messages`, {
@@ -175,7 +329,19 @@ class MailpitClient {
175
329
  }));
176
330
  });
177
331
  }
178
- // See https://mailpit.axllent.org/docs/usage/search-filters/
332
+ /**
333
+ * Retrieve messages matching a search, sorted by received date (descending).
334
+ * @see {@link https://mailpit.axllent.org/docs/usage/search-filters/ | Search filters}
335
+ * @remarks Only contains the number of attachments and a snippet of the message body.
336
+ * @see {@link MailpitClient.getMessageSummary | getMessageSummary()} for more attachment and body details for a specific message.
337
+ * @param search - The search request containing the query and optional parameters.
338
+ * @returns A list of message summaries matching the search criteria.
339
+ * @example
340
+ * ```typescript
341
+ * // Search for messages from a the domain example.test
342
+ * const messages = await mailpit.searchMessages({query: "from:example.test"});
343
+ * ```
344
+ */
179
345
  searchMessages(search) {
180
346
  return __awaiter(this, void 0, void 0, function* () {
181
347
  return yield this.handleRequest(() => this.axiosInstance.get(`/api/v1/search`, {
@@ -183,57 +349,202 @@ class MailpitClient {
183
349
  }));
184
350
  });
185
351
  }
186
- // See https://mailpit.axllent.org/docs/usage/search-filters/
352
+ /**
353
+ * Delete all messages matching a search.
354
+ * @see {@link https://mailpit.axllent.org/docs/usage/search-filters/ | Search filters}
355
+ * @param search - The search request containing the query.
356
+ * @returns Plain text "ok" response
357
+ * @example
358
+ * ```typescript
359
+ * // Delete all messages from the domain example.test
360
+ * await mailpit.deleteMessagesBySearch({query: "from:example.test"});
361
+ * ```
362
+ */
187
363
  deleteMessagesBySearch(search) {
188
364
  return __awaiter(this, void 0, void 0, function* () {
189
365
  return yield this.handleRequest(() => this.axiosInstance.delete(`/api/v1/search`, { params: search }));
190
366
  });
191
367
  }
192
- // Tags
368
+ /**
369
+ * Performs an HTML check on a specific message.
370
+ * @param id - The message database ID. Defaults to `latest` to return the latest message.
371
+ * @returns The summary of the message HTML checker
372
+ * @example
373
+ * ```typescript
374
+ * const htmlCheck = await mailpit.htmlCheck();
375
+ * ```
376
+ */
377
+ htmlCheck() {
378
+ return __awaiter(this, arguments, void 0, function* (id = "latest") {
379
+ return yield this.handleRequest(() => this.axiosInstance.get(`/api/v1/message/${id}/html-check`));
380
+ });
381
+ }
382
+ /**
383
+ * Performs a link check on a specific message.
384
+ * @param id - The message database ID. Defaults to `latest` to return the latest message.
385
+ * @param follow - Whether to follow links. Defaults to `false`.
386
+ * @returns The summary of the message Link checker.
387
+ * @example
388
+ * ```typescript
389
+ * const linkCheck = await mailpit.linkCheck();
390
+ * ```
391
+ */
392
+ linkCheck() {
393
+ return __awaiter(this, arguments, void 0, function* (id = "latest", follow = "false") {
394
+ return yield this.handleRequest(() => this.axiosInstance.get(`/api/v1/message/${id}/link-check`, { params: { follow } }));
395
+ });
396
+ }
397
+ /**
398
+ * Performs a SpamAssassin check (if enabled) on a specific message.
399
+ * @param id - The message database ID. Defaults to `latest` to return the latest message.
400
+ * @returns The SpamAssassin summary (if enabled)
401
+ * @example
402
+ * ```typescript
403
+ * const spamAssassinCheck = await mailpit.spamAssassinCheck();
404
+ * ```
405
+ */
406
+ spamAssassinCheck() {
407
+ return __awaiter(this, arguments, void 0, function* (id = "latest") {
408
+ return yield this.handleRequest(() => this.axiosInstance.get(`/api/v1/message/${id}/sa-check`));
409
+ });
410
+ }
411
+ /**
412
+ * Retrieves a list of all the unique tags.
413
+ * @returns All unique message tags
414
+ * @example
415
+ * ```typescript
416
+ * const tags = await mailpit.getTags();
417
+ * ```
418
+ */
193
419
  getTags() {
194
420
  return __awaiter(this, void 0, void 0, function* () {
195
421
  return yield this.handleRequest(() => this.axiosInstance.get(`/api/v1/tags`));
196
422
  });
197
423
  }
424
+ /**
425
+ * Sets and removes tag(s) on message(s). This will overwrite any existing tags for selected message database IDs.
426
+ * @param request - The request containing the message IDs and tags. To remove all tags from a message, pass an empty `Tags` array or exclude `Tags` entirely.
427
+ * @remarks
428
+ * Tags are limited to the following characters: `a-z`, `A-Z`, `0-9`, `-`, `.`, `spaces`, and `_`, and must be a minimum of 1 character.
429
+ * Other characters are silently stripped from the tag.
430
+ * @returns Plain text "ok" response
431
+ * @example
432
+ * ```typescript
433
+ * // Set tags on message(s)
434
+ * await mailpit.setTags({ IDs: ["1", "2", "3"], Tags: ["tag1", "tag2"] });
435
+ * // Remove tags from message(s)
436
+ * await mailpit.setTags({ IDs: ["1", "2", "3"]});
437
+ * ```
438
+ */
198
439
  setTags(request) {
199
440
  return __awaiter(this, void 0, void 0, function* () {
200
441
  return yield this.handleRequest(() => this.axiosInstance.put(`/api/v1/tags`, request));
201
442
  });
202
443
  }
444
+ /**
445
+ * Renames an existing tag.
446
+ * @param tag - The current name of the tag.
447
+ * @param newTagName - A new name for the tag.
448
+ * @remarks
449
+ * Tags are limited to the following characters: `a-z`, `A-Z`, `0-9`, `-`, `.`, `spaces`, and `_`, and must be a minimum of 1 character.
450
+ * Other characters are silently stripped from the tag.
451
+ * @returns Plain text "ok" response
452
+ * @example
453
+ * ```typescript
454
+ * await mailpit.renameTag("Old Tag Name", "New Tag Name");
455
+ * ```
456
+ */
203
457
  renameTag(tag, newTagName) {
204
458
  return __awaiter(this, void 0, void 0, function* () {
205
- const encodedTag = encodeURI(tag);
459
+ const encodedTag = encodeURIComponent(tag);
206
460
  return yield this.handleRequest(() => this.axiosInstance.put(`/api/v1/tags/${encodedTag}`, {
207
461
  Name: newTagName,
208
462
  }));
209
463
  });
210
464
  }
465
+ /**
466
+ * Deletes a tag from all messages.
467
+ * @param tag - The name of the tag to delete.
468
+ * @remarks This does NOT delete any messages
469
+ * @returns Plain text "ok" response
470
+ * ```typescript
471
+ * await mailpit.deleteTag("Tag 1");
472
+ * ```
473
+ */
211
474
  deleteTag(tag) {
212
475
  return __awaiter(this, void 0, void 0, function* () {
213
- const encodedTag = encodeURI(tag);
476
+ const encodedTag = encodeURIComponent(tag);
214
477
  return yield this.handleRequest(() => this.axiosInstance.delete(`/api/v1/tags/${encodedTag}`));
215
478
  });
216
479
  }
217
- // Testing
218
- renderMessageHTML() {
219
- return __awaiter(this, arguments, void 0, function* (id = "latest", embed) {
220
- return yield this.handleRequest(() => this.axiosInstance.get(`/view/${id}.html`, { params: { embed } }));
221
- });
222
- }
223
- renderMessageText() {
224
- return __awaiter(this, arguments, void 0, function* (id = "latest") {
225
- return yield this.handleRequest(() => this.axiosInstance.get(`/view/${id}.txt`));
226
- });
227
- }
480
+ /**
481
+ * Retrieves the current Chaos triggers configuration (if enabled).
482
+ * @remarks This will return an error if Chaos is not enabled at runtime.
483
+ * @returns The Chaos triggers configuration
484
+ * @example
485
+ * ```typescript
486
+ * const triggers = await mailpit.getChaosTriggers();
487
+ * ```
488
+ */
228
489
  getChaosTriggers() {
229
490
  return __awaiter(this, void 0, void 0, function* () {
230
491
  return yield this.handleRequest(() => this.axiosInstance.get("/api/v1/chaos"));
231
492
  });
232
493
  }
494
+ /**
495
+ * Sets and/or resets the Chaos triggers configuration (if enabled).
496
+ * @param triggers - The request containing the chaos triggers. Omitted triggers will reset to the default `0%` probabibility.
497
+ * @remarks This will return an error if Chaos is not enabled at runtime.
498
+ * @returns The updated Chaos triggers configuration
499
+ * @example
500
+ * ```typescript
501
+ * // Reset all triggers to `0%` probability
502
+ * const triggers = await mailpit.setChaosTriggers();
503
+ * // Set `Sender` and reset `Authentication` and `Recipient` triggers
504
+ * const triggers = await mailpit.setChaosTriggers({ Sender: { ErrorCode: 451, Probability: 5 } });
505
+ * ```
506
+ */
233
507
  setChaosTriggers() {
234
508
  return __awaiter(this, arguments, void 0, function* (triggers = {}) {
235
509
  return yield this.handleRequest(() => this.axiosInstance.put("/api/v1/chaos", triggers));
236
510
  });
237
511
  }
512
+ /**
513
+ * Renders the HTML part of a specific message which can be used for UI integration testing.
514
+ * @remarks
515
+ * Attached inline images are modified to link to the API provided they exist.
516
+ * If the message does not contain an HTML part then a 404 error is returned.
517
+ *
518
+ *
519
+ * @param id - The message database ID. Defaults to `latest` to return the latest message.
520
+ * @param embed - Whether this route is to be embedded in an iframe. Defaults to `undefined`. Set to `1` to embed.
521
+ * The `embed` parameter will add `target="_blank"` and `rel="noreferrer noopener"` to all links.
522
+ * In addition, a small script will be added to the end of the document to post (postMessage()) the height of the document back to the parent window for optional iframe height resizing.
523
+ * Note that this will also transform the message into a full HTML document (if it isn't already), so this option is useful for viewing but not programmatic testing.
524
+ * @returns Rendered HTML
525
+ * @example
526
+ * ```typescript
527
+ * const html = await mailpit.renderMessageHTML();
528
+ * ```
529
+ */
530
+ renderMessageHTML() {
531
+ return __awaiter(this, arguments, void 0, function* (id = "latest", embed) {
532
+ return yield this.handleRequest(() => this.axiosInstance.get(`/view/${id}.html`, { params: { embed } }));
533
+ });
534
+ }
535
+ /**
536
+ * Renders just the message's text part which can be used for UI integration testing.
537
+ * @param id - The message database ID. Defaults to `latest` to return the latest message.
538
+ * @returns Plain text
539
+ * @example
540
+ * ```typescript
541
+ * const html = await mailpit.renderMessageText();
542
+ * ```
543
+ */
544
+ renderMessageText() {
545
+ return __awaiter(this, arguments, void 0, function* (id = "latest") {
546
+ return yield this.handleRequest(() => this.axiosInstance.get(`/view/${id}.txt`));
547
+ });
548
+ }
238
549
  }
239
550
  exports.MailpitClient = MailpitClient;