mailpit-api 1.0.6 → 1.1.1

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.
@@ -1,3 +1,22 @@
1
+ interface Address {
2
+ Address: string;
3
+ Name: string;
4
+ }
5
+ interface Email {
6
+ Email: string;
7
+ Name: string;
8
+ }
9
+ interface Attachment {
10
+ ContentID: string;
11
+ ContentType: string;
12
+ FileName: string;
13
+ PartID: string;
14
+ Size: number;
15
+ }
16
+ interface ChaosTrigger {
17
+ ErrorCode: number;
18
+ Probability: number;
19
+ }
1
20
  export interface MailpitInfoResponse {
2
21
  Database: string;
3
22
  DatabaseSize: number;
@@ -30,49 +49,22 @@ export interface MailpitConfigurationResponse {
30
49
  };
31
50
  }
32
51
  export interface MailpitMessageSummaryResponse {
33
- Attachments: {
34
- ContentID: string;
35
- ContentType: string;
36
- FileName: string;
37
- PartID: string;
38
- Size: number;
39
- }[];
40
- Bcc: {
41
- Address: string;
42
- Name: string;
43
- }[];
44
- Cc: {
45
- Address: string;
46
- Name: string;
47
- }[];
52
+ Attachments: Attachment[];
53
+ Bcc: Address[];
54
+ Cc: Address[];
48
55
  Date: string;
49
- From: {
50
- Address: string;
51
- Name: string;
52
- };
56
+ From: Address;
53
57
  HTML: string;
54
58
  ID: string;
55
- Inline: {
56
- ContentID: string;
57
- ContentType: string;
58
- FileName: string;
59
- PartID: string;
60
- Size: number;
61
- }[];
59
+ Inline: Attachment[];
62
60
  MessageID: string;
63
- ReplyTo: {
64
- Address: string;
65
- Name: string;
66
- }[];
61
+ ReplyTo: Address[];
67
62
  ReturnPath: string;
68
63
  Size: number;
69
64
  Subject: string;
70
65
  Tags: string[];
71
66
  Text: string;
72
- To: {
73
- Address: string;
74
- Name: string;
75
- }[];
67
+ To: Address[];
76
68
  }
77
69
  export interface MailpitMessagesSummaryResponse {
78
70
  messages: {
@@ -84,26 +76,11 @@ export interface MailpitMessagesSummaryResponse {
84
76
  ID: string;
85
77
  MessageID: string;
86
78
  Read: boolean;
87
- Bcc: {
88
- Address: string;
89
- Name: string;
90
- }[];
91
- Cc: {
92
- Address: string;
93
- Name: string;
94
- }[];
95
- From: {
96
- Address: string;
97
- Name: string;
98
- };
99
- ReplyTo: {
100
- Address: string;
101
- Name: string;
102
- }[];
103
- To: {
104
- Address: string;
105
- Name: string;
106
- }[];
79
+ Bcc: Address[];
80
+ Cc: Address[];
81
+ From: Address;
82
+ ReplyTo: Address[];
83
+ To: Address[];
107
84
  }[];
108
85
  messages_count: number;
109
86
  start: number;
@@ -120,29 +97,17 @@ export interface MailpitSendRequest {
120
97
  Filename: string;
121
98
  }[];
122
99
  Bcc: string[];
123
- Cc: {
124
- Email: string;
125
- Name: string;
126
- }[];
127
- From: {
128
- Email: string;
129
- Name: string;
130
- };
100
+ Cc: Email[];
101
+ From: Email;
131
102
  HTML: string;
132
103
  Headers: {
133
104
  [key: string]: string;
134
105
  };
135
- ReplyTo: {
136
- Email: string;
137
- Name: string;
138
- }[];
106
+ ReplyTo: Email[];
139
107
  Subject: string;
140
108
  Tags: string[];
141
109
  Text: string;
142
- To: {
143
- Email: string;
144
- Name: string;
145
- }[];
110
+ To: Email[];
146
111
  }
147
112
  export interface MailpitSendMessageConfirmationResponse {
148
113
  ID: string;
@@ -225,44 +190,33 @@ export interface MailpitSetTagsRequest {
225
190
  Tags: string[];
226
191
  }
227
192
  export interface ChaosTriggersRequest {
228
- Authentication?: {
229
- ErrorCode: number;
230
- Probability: number;
231
- };
232
- Recipient?: {
233
- ErrorCode: number;
234
- Probability: number;
235
- };
236
- Sender?: {
237
- ErrorCode: number;
238
- Probability: number;
239
- };
193
+ Authentication?: ChaosTrigger;
194
+ Recipient?: ChaosTrigger;
195
+ Sender?: ChaosTrigger;
240
196
  }
241
197
  export interface ChaosTriggersResponse {
242
- Authentication: {
243
- ErrorCode: number;
244
- Probability: number;
245
- };
246
- Recipient?: {
247
- ErrorCode: number;
248
- Probability: number;
249
- };
250
- Sender?: {
251
- ErrorCode: number;
252
- Probability: number;
253
- };
198
+ Authentication: ChaosTrigger;
199
+ Recipient?: ChaosTrigger;
200
+ Sender?: ChaosTrigger;
201
+ }
202
+ interface AttachmentResponse {
203
+ data: ArrayBuffer;
204
+ contentType: string;
254
205
  }
255
206
  export declare class MailpitClient {
256
207
  private axiosInstance;
257
- constructor(baseURL: string);
208
+ constructor(baseURL: string, auth?: {
209
+ username: string;
210
+ password: string;
211
+ });
258
212
  private handleRequest;
259
213
  getInfo(): Promise<MailpitInfoResponse>;
260
214
  getConfiguration(): Promise<MailpitConfigurationResponse>;
261
215
  getMessageSummary(id?: string): Promise<MailpitMessageSummaryResponse>;
262
216
  getMessageHeaders(id?: string): Promise<MailpitMessageHeadersResponse>;
263
- getMessageAttachment(id: string, partID: string): Promise<string>;
217
+ getMessageAttachment(id: string, partID: string): Promise<AttachmentResponse>;
264
218
  getMessageSource(id?: string): Promise<string>;
265
- getAttachmentThumbnail(id: string, partID: string): Promise<string>;
219
+ getAttachmentThumbnail(id: string, partID: string): Promise<AttachmentResponse>;
266
220
  releaseMessage(id: string, releaseRequest: {
267
221
  To: string[];
268
222
  }): Promise<string>;
@@ -279,8 +233,9 @@ export declare class MailpitClient {
279
233
  setTags(request: MailpitSetTagsRequest): Promise<string>;
280
234
  renameTag(tag: string, newTagName: string): Promise<string>;
281
235
  deleteTag(tag: string): Promise<string>;
282
- renderMessageHTML(id?: string): Promise<string>;
236
+ renderMessageHTML(id?: string, embed?: 1): Promise<string>;
283
237
  renderMessageText(id?: string): Promise<string>;
284
238
  getChaosTriggers(): Promise<ChaosTriggersResponse>;
285
239
  setChaosTriggers(triggers?: ChaosTriggersRequest): Promise<ChaosTriggersResponse>;
286
240
  }
241
+ export {};
package/dist/cjs/index.js CHANGED
@@ -1,4 +1,37 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
2
35
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
36
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
37
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -8,44 +41,45 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
41
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
42
  });
10
43
  };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
44
  Object.defineProperty(exports, "__esModule", { value: true });
15
45
  exports.MailpitClient = void 0;
16
- const axios_1 = __importDefault(require("axios"));
46
+ const axios_1 = __importStar(require("axios"));
17
47
  class MailpitClient {
18
- constructor(baseURL) {
48
+ constructor(baseURL, auth) {
19
49
  this.axiosInstance = axios_1.default.create({
20
- baseURL: baseURL,
50
+ baseURL,
51
+ auth,
21
52
  validateStatus: function (status) {
22
53
  return status === 200;
23
54
  },
24
55
  });
25
56
  }
26
- handleRequest(request) {
27
- return __awaiter(this, void 0, void 0, function* () {
57
+ handleRequest(request_1) {
58
+ return __awaiter(this, arguments, void 0, function* (request, options = { fullResponse: false }) {
59
+ var _a, _b, _c;
28
60
  try {
29
61
  const response = yield request();
30
- return response.data;
62
+ return options.fullResponse ? response : response.data;
31
63
  }
32
64
  catch (error) {
33
- if (axios_1.default.isAxiosError(error)) {
65
+ if ((0, axios_1.isAxiosError)(error)) {
66
+ const url = ((_a = error.config) === null || _a === void 0 ? void 0 : _a.url) || "UNKNOWN URL";
67
+ const method = ((_c = (_b = error.config) === null || _b === void 0 ? void 0 : _b.method) === null || _c === void 0 ? void 0 : _c.toUpperCase()) || "UNKNOWN METHOD";
34
68
  if (error.response) {
35
69
  // Server responded with a status other than 2xx
36
- throw new Error(`Mailpit API Error: ${error.response.status} ${error.response.statusText}: ${JSON.stringify(error.response.data)}`);
70
+ throw new Error(`Mailpit API Error: ${error.response.status.toString()} ${error.response.statusText} at ${method} ${url}: ${JSON.stringify(error.response.data)}`);
37
71
  }
38
72
  else if (error.request) {
39
73
  // Request was made but no response was received
40
- throw new Error("Mailpit API Error: No response received from server.");
74
+ throw new Error(`Mailpit API Error: No response received from server at ${method} ${url}`);
41
75
  }
42
76
  else {
43
77
  // Something happened in setting up the request
44
- throw new Error(`Mailpit API Error: ${error.message}`);
78
+ throw new Error(`Mailpit API Error: ${error.toString()} at ${method} ${url}`);
45
79
  }
46
80
  }
47
81
  else {
48
- throw new Error("Unexpected Error: " + error);
82
+ throw new Error(`Unexpected Error: ${error}`);
49
83
  }
50
84
  }
51
85
  });
@@ -53,80 +87,90 @@ class MailpitClient {
53
87
  // Application
54
88
  getInfo() {
55
89
  return __awaiter(this, void 0, void 0, function* () {
56
- return this.handleRequest(() => this.axiosInstance.get("/api/v1/info"));
90
+ return yield this.handleRequest(() => this.axiosInstance.get("/api/v1/info"));
57
91
  });
58
92
  }
59
93
  getConfiguration() {
60
94
  return __awaiter(this, void 0, void 0, function* () {
61
- return this.handleRequest(() => this.axiosInstance.get("/api/v1/webui"));
95
+ return yield this.handleRequest(() => this.axiosInstance.get("/api/v1/webui"));
62
96
  });
63
97
  }
64
98
  // Message
65
99
  getMessageSummary() {
66
100
  return __awaiter(this, arguments, void 0, function* (id = "latest") {
67
- return this.handleRequest(() => this.axiosInstance.get(`/api/v1/message/${id}`));
101
+ return yield this.handleRequest(() => this.axiosInstance.get(`/api/v1/message/${id}`));
68
102
  });
69
103
  }
70
104
  getMessageHeaders() {
71
105
  return __awaiter(this, arguments, void 0, function* (id = "latest") {
72
- return this.handleRequest(() => this.axiosInstance.get(`/api/v1/message/${id}/headers`));
106
+ return yield this.handleRequest(() => this.axiosInstance.get(`/api/v1/message/${id}/headers`));
73
107
  });
74
108
  }
75
109
  getMessageAttachment(id, partID) {
76
110
  return __awaiter(this, void 0, void 0, function* () {
77
- return this.handleRequest(() => this.axiosInstance.get(`/api/v1/message/${id}/part/${partID}`));
111
+ const response = yield this.handleRequest(() => this.axiosInstance.get(`/api/v1/message/${id}/part/${partID}`, { responseType: "arraybuffer" }), { fullResponse: true });
112
+ return {
113
+ data: response.data,
114
+ contentType: response.headers["content-type"],
115
+ };
78
116
  });
79
117
  }
80
118
  getMessageSource() {
81
119
  return __awaiter(this, arguments, void 0, function* (id = "latest") {
82
- return this.handleRequest(() => this.axiosInstance.get(`/api/v1/message/${id}/raw`));
120
+ return yield this.handleRequest(() => this.axiosInstance.get(`/api/v1/message/${id}/raw`));
83
121
  });
84
122
  }
85
123
  getAttachmentThumbnail(id, partID) {
86
124
  return __awaiter(this, void 0, void 0, function* () {
87
- return this.handleRequest(() => this.axiosInstance.get(`/api/v1/message/${id}/part/${partID}/thumb`));
125
+ const response = yield this.handleRequest(() => this.axiosInstance.get(`/api/v1/message/${id}/part/${partID}/thumb`, {
126
+ responseType: "arraybuffer",
127
+ }), { fullResponse: true });
128
+ return {
129
+ data: response.data,
130
+ contentType: response.headers["content-type"],
131
+ };
88
132
  });
89
133
  }
90
134
  releaseMessage(id, releaseRequest) {
91
135
  return __awaiter(this, void 0, void 0, function* () {
92
- return this.handleRequest(() => this.axiosInstance.post(`/api/v1/message/${id}/release`, releaseRequest));
136
+ return yield this.handleRequest(() => this.axiosInstance.post(`/api/v1/message/${id}/release`, releaseRequest));
93
137
  });
94
138
  }
95
139
  sendMessage(sendReqest) {
96
140
  return __awaiter(this, void 0, void 0, function* () {
97
- return this.handleRequest(() => this.axiosInstance.post(`/api/v1/send`, sendReqest));
141
+ return yield this.handleRequest(() => this.axiosInstance.post(`/api/v1/send`, sendReqest));
98
142
  });
99
143
  }
100
144
  // Other
101
145
  htmlCheck() {
102
146
  return __awaiter(this, arguments, void 0, function* (id = "latest") {
103
- return this.handleRequest(() => this.axiosInstance.get(`/api/v1/message/${id}/html-check`));
147
+ return yield this.handleRequest(() => this.axiosInstance.get(`/api/v1/message/${id}/html-check`));
104
148
  });
105
149
  }
106
150
  linkCheck() {
107
151
  return __awaiter(this, arguments, void 0, function* (id = "latest", follow = "false") {
108
- return this.handleRequest(() => this.axiosInstance.get(`/api/v1/message/${id}/link-check`, { params: { follow } }));
152
+ return yield this.handleRequest(() => this.axiosInstance.get(`/api/v1/message/${id}/link-check`, { params: { follow } }));
109
153
  });
110
154
  }
111
155
  spamAssassinCheck() {
112
156
  return __awaiter(this, arguments, void 0, function* (id = "latest") {
113
- return this.handleRequest(() => this.axiosInstance.get(`/api/v1/message/${id}/sa-check`));
157
+ return yield this.handleRequest(() => this.axiosInstance.get(`/api/v1/message/${id}/sa-check`));
114
158
  });
115
159
  }
116
160
  // Messages
117
161
  listMessages() {
118
162
  return __awaiter(this, arguments, void 0, function* (start = 0, limit = 50) {
119
- return this.handleRequest(() => this.axiosInstance.get(`/api/v1/messages`, { params: { start, limit } }));
163
+ return yield this.handleRequest(() => this.axiosInstance.get(`/api/v1/messages`, { params: { start, limit } }));
120
164
  });
121
165
  }
122
166
  setReadStatus(readStatus) {
123
167
  return __awaiter(this, void 0, void 0, function* () {
124
- return this.handleRequest(() => this.axiosInstance.put(`/api/v1/messages`, readStatus));
168
+ return yield this.handleRequest(() => this.axiosInstance.put(`/api/v1/messages`, readStatus));
125
169
  });
126
170
  }
127
171
  deleteMessages(deleteRequest) {
128
172
  return __awaiter(this, void 0, void 0, function* () {
129
- return this.handleRequest(() => this.axiosInstance.delete(`/api/v1/messages`, {
173
+ return yield this.handleRequest(() => this.axiosInstance.delete(`/api/v1/messages`, {
130
174
  data: deleteRequest,
131
175
  }));
132
176
  });
@@ -134,7 +178,7 @@ class MailpitClient {
134
178
  // See https://mailpit.axllent.org/docs/usage/search-filters/
135
179
  searchMessages(search) {
136
180
  return __awaiter(this, void 0, void 0, function* () {
137
- return this.handleRequest(() => this.axiosInstance.get(`/api/v1/search`, {
181
+ return yield this.handleRequest(() => this.axiosInstance.get(`/api/v1/search`, {
138
182
  params: search,
139
183
  }));
140
184
  });
@@ -142,24 +186,24 @@ class MailpitClient {
142
186
  // See https://mailpit.axllent.org/docs/usage/search-filters/
143
187
  deleteMessagesBySearch(search) {
144
188
  return __awaiter(this, void 0, void 0, function* () {
145
- return this.handleRequest(() => this.axiosInstance.delete(`/api/v1/search`, { params: search }));
189
+ return yield this.handleRequest(() => this.axiosInstance.delete(`/api/v1/search`, { params: search }));
146
190
  });
147
191
  }
148
192
  // Tags
149
193
  getTags() {
150
194
  return __awaiter(this, void 0, void 0, function* () {
151
- return this.handleRequest(() => this.axiosInstance.get(`/api/v1/tags`));
195
+ return yield this.handleRequest(() => this.axiosInstance.get(`/api/v1/tags`));
152
196
  });
153
197
  }
154
198
  setTags(request) {
155
199
  return __awaiter(this, void 0, void 0, function* () {
156
- return this.handleRequest(() => this.axiosInstance.put(`/api/v1/tags`, request));
200
+ return yield this.handleRequest(() => this.axiosInstance.put(`/api/v1/tags`, request));
157
201
  });
158
202
  }
159
203
  renameTag(tag, newTagName) {
160
204
  return __awaiter(this, void 0, void 0, function* () {
161
205
  const encodedTag = encodeURI(tag);
162
- return this.handleRequest(() => this.axiosInstance.put(`/api/v1/tags/${encodedTag}`, {
206
+ return yield this.handleRequest(() => this.axiosInstance.put(`/api/v1/tags/${encodedTag}`, {
163
207
  Name: newTagName,
164
208
  }));
165
209
  });
@@ -167,28 +211,28 @@ class MailpitClient {
167
211
  deleteTag(tag) {
168
212
  return __awaiter(this, void 0, void 0, function* () {
169
213
  const encodedTag = encodeURI(tag);
170
- return this.handleRequest(() => this.axiosInstance.delete(`/api/v1/tags/${encodedTag}`));
214
+ return yield this.handleRequest(() => this.axiosInstance.delete(`/api/v1/tags/${encodedTag}`));
171
215
  });
172
216
  }
173
217
  // Testing
174
218
  renderMessageHTML() {
175
- return __awaiter(this, arguments, void 0, function* (id = "latest") {
176
- return this.handleRequest(() => this.axiosInstance.get(`/view/${id}.html`));
219
+ return __awaiter(this, arguments, void 0, function* (id = "latest", embed) {
220
+ return yield this.handleRequest(() => this.axiosInstance.get(`/view/${id}.html`, { params: { embed } }));
177
221
  });
178
222
  }
179
223
  renderMessageText() {
180
224
  return __awaiter(this, arguments, void 0, function* (id = "latest") {
181
- return this.handleRequest(() => this.axiosInstance.get(`/view/${id}.txt`));
225
+ return yield this.handleRequest(() => this.axiosInstance.get(`/view/${id}.txt`));
182
226
  });
183
227
  }
184
228
  getChaosTriggers() {
185
229
  return __awaiter(this, void 0, void 0, function* () {
186
- return this.handleRequest(() => this.axiosInstance.get("/api/v1/chaos"));
230
+ return yield this.handleRequest(() => this.axiosInstance.get("/api/v1/chaos"));
187
231
  });
188
232
  }
189
233
  setChaosTriggers() {
190
234
  return __awaiter(this, arguments, void 0, function* (triggers = {}) {
191
- return this.handleRequest(() => this.axiosInstance.put("/api/v1/chaos", triggers));
235
+ return yield this.handleRequest(() => this.axiosInstance.put("/api/v1/chaos", triggers));
192
236
  });
193
237
  }
194
238
  }