mailisk 2.2.4 → 2.4.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/index.mjs CHANGED
@@ -5,6 +5,9 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable:
5
5
  import axios from "axios";
6
6
  import nodemailer from "nodemailer";
7
7
  var MailiskClient = class {
8
+ static {
9
+ __name(this, "MailiskClient");
10
+ }
8
11
  constructor({ apiKey, baseUrl, auth }) {
9
12
  this.axiosInstance = axios.create({
10
13
  headers: {
@@ -15,6 +18,15 @@ var MailiskClient = class {
15
18
  });
16
19
  }
17
20
  axiosInstance;
21
+ /**
22
+ * Search SMS messages sent to a phone number.
23
+ *
24
+ * @example
25
+ * Search for SMS messages sent to a phone number
26
+ * ```typescript
27
+ * const { data: smsMessages } = await client.searchSmsMessages("1234567890");
28
+ * ```
29
+ */
18
30
  async searchSmsMessages(phoneNumber, params, config) {
19
31
  let _params = { ...params };
20
32
  if (params?.from_date === void 0 || params?.from_date === null) {
@@ -40,15 +52,171 @@ var MailiskClient = class {
40
52
  params: requestParams
41
53
  })).data;
42
54
  }
55
+ /**
56
+ * List all SMS phone numbers associated with the current account.
57
+ *
58
+ * @example
59
+ * List all SMS phone numbers
60
+ * ```typescript
61
+ * const { data: smsNumbers } = await client.listSmsNumbers();
62
+ * ```
63
+ */
43
64
  async listSmsNumbers() {
44
65
  return (await this.axiosInstance.get("api/sms/numbers")).data;
45
66
  }
46
67
  async sendVirtualSms(params) {
47
68
  return (await this.axiosInstance.post("api/sms/virtual", params)).data;
48
69
  }
70
+ /**
71
+ * List saved TOTP devices.
72
+ *
73
+ * @example
74
+ * List saved TOTP devices for an issuer and username
75
+ * ```typescript
76
+ * const { items: devices } = await client.listTotpDevices({
77
+ * issuer: "GitHub",
78
+ * username: "qa@example.com",
79
+ * });
80
+ * ```
81
+ */
82
+ async listTotpDevices(params) {
83
+ const requestParams = {
84
+ ...params,
85
+ username: params?.username?.trim(),
86
+ issuer: params?.issuer?.trim()
87
+ };
88
+ return (await this.axiosInstance.get("api/devices", {
89
+ params: requestParams
90
+ })).data;
91
+ }
92
+ /**
93
+ * Create a saved TOTP device from a Base32 shared secret using default TOTP settings.
94
+ *
95
+ * @example
96
+ * Create a saved TOTP device from a shared secret
97
+ * ```typescript
98
+ * const device = await client.createTotpDevice({
99
+ * name: "GitHub staging",
100
+ * shared_secret: "JBSWY3DPEHPK3PXP",
101
+ * });
102
+ * ```
103
+ */
104
+ async createTotpDevice(params) {
105
+ return (await this.axiosInstance.post("api/devices", params)).data;
106
+ }
107
+ /**
108
+ * Create a saved TOTP device with custom settings.
109
+ *
110
+ * @example
111
+ * Create a saved TOTP device with custom settings
112
+ * ```typescript
113
+ * const device = await client.createCustomTotpDevice({
114
+ * name: "GitHub staging",
115
+ * secret: "JBSWY3DPEHPK3PXP",
116
+ * username: "qa@example.com",
117
+ * issuer: "GitHub",
118
+ * digits: 6,
119
+ * period: 30,
120
+ * algorithm: "SHA1",
121
+ * });
122
+ * ```
123
+ */
124
+ async createCustomTotpDevice(params) {
125
+ return (await this.axiosInstance.post("api/devices/custom", params)).data;
126
+ }
127
+ /**
128
+ * Create a saved TOTP device from a Base32 secret key.
129
+ *
130
+ * @example
131
+ * Create a saved TOTP device from a Base32 secret key
132
+ * ```typescript
133
+ * const device = await client.createTotpDeviceFromBase32SecretKey({
134
+ * base32_secret_key: "JBSWY3DPEHPK3PXP",
135
+ * username: "qa@example.com",
136
+ * issuer: "GitHub",
137
+ * });
138
+ * ```
139
+ */
140
+ async createTotpDeviceFromBase32SecretKey(params) {
141
+ return (await this.axiosInstance.post("api/devices/base32-secret-key", params)).data;
142
+ }
143
+ /**
144
+ * Create a saved TOTP device from an otpauth://totp URL.
145
+ *
146
+ * @example
147
+ * Create a saved TOTP device from an otpauth URL
148
+ * ```typescript
149
+ * const device = await client.createTotpDeviceFromOtpAuthUrl({
150
+ * otp_auth_url: "otpauth://totp/GitHub:qa@example.com?secret=JBSWY3DPEHPK3PXP&issuer=GitHub",
151
+ * });
152
+ * ```
153
+ */
154
+ async createTotpDeviceFromOtpAuthUrl(params) {
155
+ return (await this.axiosInstance.post("api/devices/otpauth-url", params)).data;
156
+ }
157
+ /**
158
+ * Generate a TOTP code from a shared secret without saving a device.
159
+ *
160
+ * @example
161
+ * Generate a TOTP code from a shared secret
162
+ * ```typescript
163
+ * const { code } = await client.getTotpOtpBySharedSecret("JBSWY3DPEHPK3PXP", {
164
+ * min_seconds_until_expire: 10,
165
+ * });
166
+ * ```
167
+ */
168
+ async getTotpOtpBySharedSecret(sharedSecret, params) {
169
+ return (await this.axiosInstance.post("api/devices/otp", { shared_secret: sharedSecret, ...params })).data;
170
+ }
171
+ /**
172
+ * Generate a TOTP code for a saved device.
173
+ *
174
+ * @example
175
+ * Generate a TOTP code for a saved device
176
+ * ```typescript
177
+ * const { code } = await client.getTotpOtpByDeviceId(device.id, {
178
+ * min_seconds_until_expire: 10,
179
+ * });
180
+ * ```
181
+ */
182
+ async getTotpOtpByDeviceId(deviceId, params) {
183
+ const url = `api/devices/${deviceId}/otp`;
184
+ return params ? (await this.axiosInstance.get(url, { params })).data : (await this.axiosInstance.get(url)).data;
185
+ }
186
+ /**
187
+ * Delete a saved TOTP device.
188
+ *
189
+ * @example
190
+ * Delete a saved TOTP device
191
+ * ```typescript
192
+ * await client.deleteTotpDevice(device.id);
193
+ * ```
194
+ */
195
+ async deleteTotpDevice(deviceId) {
196
+ await this.axiosInstance.delete(`api/devices/${deviceId}`);
197
+ }
198
+ /**
199
+ * List all namespaces that belong to the current account (API key).
200
+ */
49
201
  async listNamespaces() {
50
202
  return (await this.axiosInstance.get("api/namespaces")).data;
51
203
  }
204
+ /**
205
+ * Send an email using the Virtual SMTP.
206
+ *
207
+ * These emails can only be sent to valid Mailisk namespaces, i.e. emails that end in @mynamespace.mailisk.net
208
+ *
209
+ * @example
210
+ * For example, sending a test email:
211
+ * ```typescript
212
+ * client.sendVirtualEmail(namespace, {
213
+ * from: "test@example.com",
214
+ * to: `john@${namespace}.mailisk.net`,
215
+ * subject: "This is a test",
216
+ * text: "Testing",
217
+ * });
218
+ * ```
219
+ */
52
220
  async sendVirtualEmail(namespace, params) {
53
221
  const smtpSettings = await this.getSmtpSettings(namespace);
54
222
  const transport = nodemailer.createTransport({
@@ -72,10 +240,42 @@ var MailiskClient = class {
72
240
  });
73
241
  transport.close();
74
242
  }
243
+ /**
244
+ * Search inbox of a namespace.
245
+ *
246
+ * By default, this calls the api using the `wait` flag. This means the call won't timeout until at least one email is received or 5 minutes pass.
247
+ * It also uses a default `from_timestamp` of **current timestamp - 15 minutes**. This means that older emails will be ignored.
248
+ *
249
+ * Both of these settings can be overriden by passing them in the `params` object.
250
+ *
251
+ * @example
252
+ * Get the latest emails
253
+ * ```typescript
254
+ * const { data: emails } = await client.searchInbox(namespace);
255
+ * ```
256
+ *
257
+ * @example
258
+ * Get the latest emails for a specific email address
259
+ * ```typescript
260
+ * const { data: emails } = await client.searchInbox(namespace, {
261
+ * to_addr_prefix: 'john@mynamespace.mailisk.net'
262
+ * });
263
+ * ```
264
+ *
265
+ * @example
266
+ * Get the last 20 emails in the namespace
267
+ * ```typescript
268
+ * const { data: emails } = await mailisk.searchInbox(namespace, {
269
+ * wait: false,
270
+ * from_timestamp: 0,
271
+ * limit: 20
272
+ * });
273
+ * ```
274
+ */
75
275
  async searchInbox(namespace, params, config) {
76
276
  let _params = { ...params };
77
277
  if (params?.from_timestamp === void 0 || params?.from_timestamp === null) {
78
- _params.from_timestamp = Math.floor(new Date().getTime() / 1e3) - 15 * 60;
278
+ _params.from_timestamp = Math.floor((/* @__PURE__ */ new Date()).getTime() / 1e3) - 15 * 60;
79
279
  }
80
280
  if (params?.wait !== false) {
81
281
  _params.wait = true;
@@ -92,6 +292,9 @@ var MailiskClient = class {
92
292
  params: _params
93
293
  })).data;
94
294
  }
295
+ /**
296
+ * Get the SMTP settings for a namespace.
297
+ */
95
298
  async getSmtpSettings(namespace) {
96
299
  const result = await this.axiosInstance.get(`api/smtp/${namespace}`);
97
300
  return result.data;
@@ -100,13 +303,25 @@ var MailiskClient = class {
100
303
  const result = await this.axiosInstance.get(`api/attachments/${attachmentId}`);
101
304
  return result.data;
102
305
  }
306
+ /**
307
+ * Download an attachment from an attachment ID.
308
+ *
309
+ * @example
310
+ * Download an attachment from an email
311
+ * ```typescript
312
+ * const attachment = email.attachments[0];
313
+ * const attachmentBuffer = await client.downloadAttachment(attachment.id);
314
+ *
315
+ * // save to file
316
+ * fs.writeFileSync(attachment.filename, attachmentBuffer);
317
+ * ```
318
+ */
103
319
  async downloadAttachment(attachmentId) {
104
320
  const result = await this.getAttachment(attachmentId);
105
321
  const response = await axios.get(result.data.download_url, { responseType: "arraybuffer" });
106
322
  return Buffer.from(response.data);
107
323
  }
108
324
  };
109
- __name(MailiskClient, "MailiskClient");
110
325
  export {
111
326
  MailiskClient
112
327
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/mailisk.ts"],"sourcesContent":["import axios, { AxiosBasicCredentials, AxiosInstance, AxiosRequestConfig } from \"axios\";\nimport {\n GetAttachmentResponse,\n ListNamespacesResponse,\n ListSmsNumbersResponse,\n SearchInboxParams,\n SearchInboxResponse,\n SearchSmsMessagesParams,\n SearchSmsMessagesResponse,\n SendVirtualEmailParams,\n SendVirtualSmsParams,\n SmtpSettings,\n} from \"./mailisk.interfaces\";\nimport nodemailer from \"nodemailer\";\n\nexport class MailiskClient {\n constructor({ apiKey, baseUrl, auth }: { apiKey: string; baseUrl?: string; auth?: AxiosBasicCredentials }) {\n this.axiosInstance = axios.create({\n headers: {\n \"X-Api-Key\": apiKey,\n },\n baseURL: baseUrl || \"https://api.mailisk.com/\",\n auth,\n });\n }\n\n private readonly axiosInstance: AxiosInstance;\n\n /**\n * Search SMS messages sent to a phone number.\n *\n * @example\n * Search for SMS messages sent to a phone number\n * ```typescript\n * const { data: smsMessages } = await client.searchSmsMessages(\"1234567890\");\n * ```\n */\n async searchSmsMessages(\n phoneNumber: string,\n params?: SearchSmsMessagesParams,\n config?: AxiosRequestConfig\n ): Promise<SearchSmsMessagesResponse> {\n let _params: SearchSmsMessagesParams = { ...params };\n\n // default from timestamp, 15 minutes before starting this request\n if (params?.from_date === undefined || params?.from_date === null) {\n _params.from_date = new Date(Date.now() - 15 * 60 * 1000).toISOString();\n }\n\n // by default wait for sms\n if (params?.wait !== false) {\n _params.wait = true;\n }\n\n let _config = { ...config };\n\n if (config?.maxRedirects === undefined) {\n _config.maxRedirects = 99999;\n }\n\n // by default, wait 5 minutes for emails before timing out\n if (_params.wait && config?.timeout === undefined) {\n _config.timeout = 1000 * 60 * 5;\n }\n\n const requestParams = {\n ..._params,\n from_date: _params.from_date ?? undefined,\n to_date: _params.to_date ?? undefined,\n };\n\n return (\n await this.axiosInstance.get(`api/sms/${phoneNumber}/messages`, {\n ..._config,\n params: requestParams,\n })\n ).data;\n }\n\n /**\n * List all SMS phone numbers associated with the current account.\n *\n * @example\n * List all SMS phone numbers\n * ```typescript\n * const { data: smsNumbers } = await client.listSmsNumbers();\n * ```\n */\n async listSmsNumbers(): Promise<ListSmsNumbersResponse> {\n return (await this.axiosInstance.get(\"api/sms/numbers\")).data;\n }\n\n async sendVirtualSms(params: SendVirtualSmsParams): Promise<void> {\n return (await this.axiosInstance.post(\"api/sms/virtual\", params)).data;\n }\n\n /**\n * List all namespaces that belong to the current account (API key).\n */\n async listNamespaces(): Promise<ListNamespacesResponse> {\n return (await this.axiosInstance.get(\"api/namespaces\")).data;\n }\n\n /**\n * Send an email using the Virtual SMTP.\n *\n * These emails can only be sent to valid Mailisk namespaces, i.e. emails that end in @mynamespace.mailisk.net\n *\n * @example\n * For example, sending a test email:\n * ```typescript\n * client.sendVirtualEmail(namespace, {\n * from: \"test@example.com\",\n * to: `john@${namespace}.mailisk.net`,\n * subject: \"This is a test\",\n * text: \"Testing\",\n * });\n * ```\n */\n async sendVirtualEmail(namespace: string, params: SendVirtualEmailParams): Promise<void> {\n const smtpSettings = await this.getSmtpSettings(namespace);\n\n const transport = nodemailer.createTransport({\n host: smtpSettings.data.host,\n port: smtpSettings.data.port,\n secure: false,\n auth: {\n user: smtpSettings.data.username,\n pass: smtpSettings.data.password,\n },\n });\n\n const { from, to, subject, text, html, headers, attachments } = params;\n\n await transport.sendMail({\n from,\n to,\n subject,\n text,\n html,\n headers,\n attachments,\n });\n\n transport.close();\n }\n\n /**\n * Search inbox of a namespace.\n *\n * By default, this calls the api using the `wait` flag. This means the call won't timeout until at least one email is received or 5 minutes pass.\n * It also uses a default `from_timestamp` of **current timestamp - 15 minutes**. This means that older emails will be ignored.\n *\n * Both of these settings can be overriden by passing them in the `params` object.\n *\n * @example\n * Get the latest emails\n * ```typescript\n * const { data: emails } = await client.searchInbox(namespace);\n * ```\n *\n * @example\n * Get the latest emails for a specific email address\n * ```typescript\n * const { data: emails } = await client.searchInbox(namespace, {\n * to_addr_prefix: 'john@mynamespace.mailisk.net'\n * });\n * ```\n *\n * @example\n * Get the last 20 emails in the namespace\n * ```typescript\n * const { data: emails } = await mailisk.searchInbox(namespace, {\n * wait: false,\n * from_timestamp: 0,\n * limit: 20\n * });\n * ```\n */\n async searchInbox(\n namespace: string,\n params?: SearchInboxParams,\n config?: AxiosRequestConfig\n ): Promise<SearchInboxResponse> {\n let _params = { ...params };\n\n // default from timestamp, 15 minutes before starting this request\n if (params?.from_timestamp === undefined || params?.from_timestamp === null) {\n _params.from_timestamp = Math.floor(new Date().getTime() / 1000) - 15 * 60;\n }\n\n // by default wait for email\n if (params?.wait !== false) {\n _params.wait = true;\n }\n\n let _config = { ...config };\n\n if (config?.maxRedirects === undefined) {\n _config.maxRedirects = 99999;\n }\n\n // by default, wait 5 minutes for emails before timing out\n if (_params.wait && config?.timeout === undefined) {\n _config.timeout = 1000 * 60 * 5;\n }\n\n return (\n await this.axiosInstance.get(`api/emails/${namespace}/inbox`, {\n ..._config,\n params: _params,\n })\n ).data;\n }\n\n /**\n * Get the SMTP settings for a namespace.\n */\n async getSmtpSettings(namespace: string): Promise<SmtpSettings> {\n const result = await this.axiosInstance.get(`api/smtp/${namespace}`);\n return result.data;\n }\n\n async getAttachment(attachmentId: string): Promise<GetAttachmentResponse> {\n const result = await this.axiosInstance.get(`api/attachments/${attachmentId}`);\n return result.data;\n }\n\n /**\n * Download an attachment from an attachment ID.\n *\n * @example\n * Download an attachment from an email\n * ```typescript\n * const attachment = email.attachments[0];\n * const attachmentBuffer = await client.downloadAttachment(attachment.id);\n *\n * // save to file\n * fs.writeFileSync(attachment.filename, attachmentBuffer);\n * ```\n */\n async downloadAttachment(attachmentId: string): Promise<Buffer> {\n const result = await this.getAttachment(attachmentId);\n\n const response = await axios.get(result.data.download_url, { responseType: \"arraybuffer\" });\n return Buffer.from(response.data);\n }\n}\n"],"mappings":";;;;AAAA,OAAO,WAAyE;AAahF,OAAO,gBAAgB;AAEhB,IAAM,gBAAN,MAAoB;AAAA,EACzB,YAAY,EAAE,QAAQ,SAAS,KAAK,GAAuE;AACzG,SAAK,gBAAgB,MAAM,OAAO;AAAA,MAChC,SAAS;AAAA,QACP,aAAa;AAAA,MACf;AAAA,MACA,SAAS,WAAW;AAAA,MACpB;AAAA,IACF,CAAC;AAAA,EACH;AAAA,EAEiB;AAAA,EAWjB,MAAM,kBACJ,aACA,QACA,QACoC;AACpC,QAAI,UAAmC,EAAE,GAAG,OAAO;AAGnD,QAAI,QAAQ,cAAc,UAAa,QAAQ,cAAc,MAAM;AACjE,cAAQ,YAAY,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,GAAI,EAAE,YAAY;AAAA,IACxE;AAGA,QAAI,QAAQ,SAAS,OAAO;AAC1B,cAAQ,OAAO;AAAA,IACjB;AAEA,QAAI,UAAU,EAAE,GAAG,OAAO;AAE1B,QAAI,QAAQ,iBAAiB,QAAW;AACtC,cAAQ,eAAe;AAAA,IACzB;AAGA,QAAI,QAAQ,QAAQ,QAAQ,YAAY,QAAW;AACjD,cAAQ,UAAU,MAAO,KAAK;AAAA,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,QAAQ,IAAI,GAAI,IAAI,KAAK;AAAA,IAC1E;AAGA,QAAI,QAAQ,SAAS,OAAO;AAC1B,cAAQ,OAAO;AAAA,IACjB;AAEA,QAAI,UAAU,EAAE,GAAG,OAAO;AAE1B,QAAI,QAAQ,iBAAiB,QAAW;AACtC,cAAQ,eAAe;AAAA,IACzB;AAGA,QAAI,QAAQ,QAAQ,QAAQ,YAAY,QAAW;AACjD,cAAQ,UAAU,MAAO,KAAK;AAAA,IAChC;AAEA,YACE,MAAM,KAAK,cAAc,IAAI,cAAc,mBAAmB;AAAA,MAC5D,GAAG;AAAA,MACH,QAAQ;AAAA,IACV,CAAC,GACD;AAAA,EACJ;AAAA,EAKA,MAAM,gBAAgB,WAA0C;AAC9D,UAAM,SAAS,MAAM,KAAK,cAAc,IAAI,YAAY,WAAW;AACnE,WAAO,OAAO;AAAA,EAChB;AAAA,EAEA,MAAM,cAAc,cAAsD;AACxE,UAAM,SAAS,MAAM,KAAK,cAAc,IAAI,mBAAmB,cAAc;AAC7E,WAAO,OAAO;AAAA,EAChB;AAAA,EAeA,MAAM,mBAAmB,cAAuC;AAC9D,UAAM,SAAS,MAAM,KAAK,cAAc,YAAY;AAEpD,UAAM,WAAW,MAAM,MAAM,IAAI,OAAO,KAAK,cAAc,EAAE,cAAc,cAAc,CAAC;AAC1F,WAAO,OAAO,KAAK,SAAS,IAAI;AAAA,EAClC;AACF;AAxOa;","names":[]}
1
+ {"version":3,"sources":["../src/mailisk.ts"],"sourcesContent":["import axios, { AxiosBasicCredentials, AxiosInstance, AxiosRequestConfig } from \"axios\";\nimport {\n CreateBase32SecretKeyTotpDeviceParams,\n CreateCustomTotpDeviceParams,\n CreateOtpAuthUrlTotpDeviceParams,\n CreateTotpDeviceParams,\n GetAttachmentResponse,\n GetTotpOtpParams,\n ListNamespacesResponse,\n ListSmsNumbersResponse,\n ListTotpDevicesParams,\n ListTotpDevicesResponse,\n SearchInboxParams,\n SearchInboxResponse,\n SearchSmsMessagesParams,\n SearchSmsMessagesResponse,\n SendVirtualEmailParams,\n SendVirtualSmsParams,\n SmtpSettings,\n TotpDevice,\n TotpOtpResponse,\n} from \"./mailisk.interfaces\";\nimport nodemailer from \"nodemailer\";\n\nexport class MailiskClient {\n constructor({ apiKey, baseUrl, auth }: { apiKey: string; baseUrl?: string; auth?: AxiosBasicCredentials }) {\n this.axiosInstance = axios.create({\n headers: {\n \"X-Api-Key\": apiKey,\n },\n baseURL: baseUrl || \"https://api.mailisk.com/\",\n auth,\n });\n }\n\n private readonly axiosInstance: AxiosInstance;\n\n /**\n * Search SMS messages sent to a phone number.\n *\n * @example\n * Search for SMS messages sent to a phone number\n * ```typescript\n * const { data: smsMessages } = await client.searchSmsMessages(\"1234567890\");\n * ```\n */\n async searchSmsMessages(\n phoneNumber: string,\n params?: SearchSmsMessagesParams,\n config?: AxiosRequestConfig,\n ): Promise<SearchSmsMessagesResponse> {\n let _params: SearchSmsMessagesParams = { ...params };\n\n // default from timestamp, 15 minutes before starting this request\n if (params?.from_date === undefined || params?.from_date === null) {\n _params.from_date = new Date(Date.now() - 15 * 60 * 1000).toISOString();\n }\n\n // by default wait for sms\n if (params?.wait !== false) {\n _params.wait = true;\n }\n\n let _config = { ...config };\n\n if (config?.maxRedirects === undefined) {\n _config.maxRedirects = 99999;\n }\n\n // by default, wait 5 minutes for emails before timing out\n if (_params.wait && config?.timeout === undefined) {\n _config.timeout = 1000 * 60 * 5;\n }\n\n const requestParams = {\n ..._params,\n from_date: _params.from_date ?? undefined,\n to_date: _params.to_date ?? undefined,\n };\n\n return (\n await this.axiosInstance.get(`api/sms/${phoneNumber}/messages`, {\n ..._config,\n params: requestParams,\n })\n ).data;\n }\n\n /**\n * List all SMS phone numbers associated with the current account.\n *\n * @example\n * List all SMS phone numbers\n * ```typescript\n * const { data: smsNumbers } = await client.listSmsNumbers();\n * ```\n */\n async listSmsNumbers(): Promise<ListSmsNumbersResponse> {\n return (await this.axiosInstance.get(\"api/sms/numbers\")).data;\n }\n\n async sendVirtualSms(params: SendVirtualSmsParams): Promise<void> {\n return (await this.axiosInstance.post(\"api/sms/virtual\", params)).data;\n }\n\n /**\n * List saved TOTP devices.\n *\n * @example\n * List saved TOTP devices for an issuer and username\n * ```typescript\n * const { items: devices } = await client.listTotpDevices({\n * issuer: \"GitHub\",\n * username: \"qa@example.com\",\n * });\n * ```\n */\n async listTotpDevices(params?: ListTotpDevicesParams): Promise<ListTotpDevicesResponse> {\n const requestParams: ListTotpDevicesParams = {\n ...params,\n username: params?.username?.trim(),\n issuer: params?.issuer?.trim(),\n };\n\n return (\n await this.axiosInstance.get(\"api/devices\", {\n params: requestParams,\n })\n ).data;\n }\n\n /**\n * Create a saved TOTP device from a Base32 shared secret using default TOTP settings.\n *\n * @example\n * Create a saved TOTP device from a shared secret\n * ```typescript\n * const device = await client.createTotpDevice({\n * name: \"GitHub staging\",\n * shared_secret: \"JBSWY3DPEHPK3PXP\",\n * });\n * ```\n */\n async createTotpDevice(params: CreateTotpDeviceParams): Promise<TotpDevice> {\n return (await this.axiosInstance.post(\"api/devices\", params)).data;\n }\n\n /**\n * Create a saved TOTP device with custom settings.\n *\n * @example\n * Create a saved TOTP device with custom settings\n * ```typescript\n * const device = await client.createCustomTotpDevice({\n * name: \"GitHub staging\",\n * secret: \"JBSWY3DPEHPK3PXP\",\n * username: \"qa@example.com\",\n * issuer: \"GitHub\",\n * digits: 6,\n * period: 30,\n * algorithm: \"SHA1\",\n * });\n * ```\n */\n async createCustomTotpDevice(params: CreateCustomTotpDeviceParams): Promise<TotpDevice> {\n return (await this.axiosInstance.post(\"api/devices/custom\", params)).data;\n }\n\n /**\n * Create a saved TOTP device from a Base32 secret key.\n *\n * @example\n * Create a saved TOTP device from a Base32 secret key\n * ```typescript\n * const device = await client.createTotpDeviceFromBase32SecretKey({\n * base32_secret_key: \"JBSWY3DPEHPK3PXP\",\n * username: \"qa@example.com\",\n * issuer: \"GitHub\",\n * });\n * ```\n */\n async createTotpDeviceFromBase32SecretKey(params: CreateBase32SecretKeyTotpDeviceParams): Promise<TotpDevice> {\n return (await this.axiosInstance.post(\"api/devices/base32-secret-key\", params)).data;\n }\n\n /**\n * Create a saved TOTP device from an otpauth://totp URL.\n *\n * @example\n * Create a saved TOTP device from an otpauth URL\n * ```typescript\n * const device = await client.createTotpDeviceFromOtpAuthUrl({\n * otp_auth_url: \"otpauth://totp/GitHub:qa@example.com?secret=JBSWY3DPEHPK3PXP&issuer=GitHub\",\n * });\n * ```\n */\n async createTotpDeviceFromOtpAuthUrl(params: CreateOtpAuthUrlTotpDeviceParams): Promise<TotpDevice> {\n return (await this.axiosInstance.post(\"api/devices/otpauth-url\", params)).data;\n }\n\n /**\n * Generate a TOTP code from a shared secret without saving a device.\n *\n * @example\n * Generate a TOTP code from a shared secret\n * ```typescript\n * const { code } = await client.getTotpOtpBySharedSecret(\"JBSWY3DPEHPK3PXP\", {\n * min_seconds_until_expire: 10,\n * });\n * ```\n */\n async getTotpOtpBySharedSecret(sharedSecret: string, params?: GetTotpOtpParams): Promise<TotpOtpResponse> {\n return (await this.axiosInstance.post(\"api/devices/otp\", { shared_secret: sharedSecret, ...params })).data;\n }\n\n /**\n * Generate a TOTP code for a saved device.\n *\n * @example\n * Generate a TOTP code for a saved device\n * ```typescript\n * const { code } = await client.getTotpOtpByDeviceId(device.id, {\n * min_seconds_until_expire: 10,\n * });\n * ```\n */\n async getTotpOtpByDeviceId(deviceId: string, params?: GetTotpOtpParams): Promise<TotpOtpResponse> {\n const url = `api/devices/${deviceId}/otp`;\n\n return params ? (await this.axiosInstance.get(url, { params })).data : (await this.axiosInstance.get(url)).data;\n }\n\n /**\n * Delete a saved TOTP device.\n *\n * @example\n * Delete a saved TOTP device\n * ```typescript\n * await client.deleteTotpDevice(device.id);\n * ```\n */\n async deleteTotpDevice(deviceId: string): Promise<void> {\n await this.axiosInstance.delete(`api/devices/${deviceId}`);\n }\n\n /**\n * List all namespaces that belong to the current account (API key).\n */\n async listNamespaces(): Promise<ListNamespacesResponse> {\n return (await this.axiosInstance.get(\"api/namespaces\")).data;\n }\n\n /**\n * Send an email using the Virtual SMTP.\n *\n * These emails can only be sent to valid Mailisk namespaces, i.e. emails that end in @mynamespace.mailisk.net\n *\n * @example\n * For example, sending a test email:\n * ```typescript\n * client.sendVirtualEmail(namespace, {\n * from: \"test@example.com\",\n * to: `john@${namespace}.mailisk.net`,\n * subject: \"This is a test\",\n * text: \"Testing\",\n * });\n * ```\n */\n async sendVirtualEmail(namespace: string, params: SendVirtualEmailParams): Promise<void> {\n const smtpSettings = await this.getSmtpSettings(namespace);\n\n const transport = nodemailer.createTransport({\n host: smtpSettings.data.host,\n port: smtpSettings.data.port,\n secure: false,\n auth: {\n user: smtpSettings.data.username,\n pass: smtpSettings.data.password,\n },\n });\n\n const { from, to, subject, text, html, headers, attachments } = params;\n\n await transport.sendMail({\n from,\n to,\n subject,\n text,\n html,\n headers,\n attachments,\n });\n\n transport.close();\n }\n\n /**\n * Search inbox of a namespace.\n *\n * By default, this calls the api using the `wait` flag. This means the call won't timeout until at least one email is received or 5 minutes pass.\n * It also uses a default `from_timestamp` of **current timestamp - 15 minutes**. This means that older emails will be ignored.\n *\n * Both of these settings can be overriden by passing them in the `params` object.\n *\n * @example\n * Get the latest emails\n * ```typescript\n * const { data: emails } = await client.searchInbox(namespace);\n * ```\n *\n * @example\n * Get the latest emails for a specific email address\n * ```typescript\n * const { data: emails } = await client.searchInbox(namespace, {\n * to_addr_prefix: 'john@mynamespace.mailisk.net'\n * });\n * ```\n *\n * @example\n * Get the last 20 emails in the namespace\n * ```typescript\n * const { data: emails } = await mailisk.searchInbox(namespace, {\n * wait: false,\n * from_timestamp: 0,\n * limit: 20\n * });\n * ```\n */\n async searchInbox(\n namespace: string,\n params?: SearchInboxParams,\n config?: AxiosRequestConfig,\n ): Promise<SearchInboxResponse> {\n let _params = { ...params };\n\n // default from timestamp, 15 minutes before starting this request\n if (params?.from_timestamp === undefined || params?.from_timestamp === null) {\n _params.from_timestamp = Math.floor(new Date().getTime() / 1000) - 15 * 60;\n }\n\n // by default wait for email\n if (params?.wait !== false) {\n _params.wait = true;\n }\n\n let _config = { ...config };\n\n if (config?.maxRedirects === undefined) {\n _config.maxRedirects = 99999;\n }\n\n // by default, wait 5 minutes for emails before timing out\n if (_params.wait && config?.timeout === undefined) {\n _config.timeout = 1000 * 60 * 5;\n }\n\n return (\n await this.axiosInstance.get(`api/emails/${namespace}/inbox`, {\n ..._config,\n params: _params,\n })\n ).data;\n }\n\n /**\n * Get the SMTP settings for a namespace.\n */\n async getSmtpSettings(namespace: string): Promise<SmtpSettings> {\n const result = await this.axiosInstance.get(`api/smtp/${namespace}`);\n return result.data;\n }\n\n async getAttachment(attachmentId: string): Promise<GetAttachmentResponse> {\n const result = await this.axiosInstance.get(`api/attachments/${attachmentId}`);\n return result.data;\n }\n\n /**\n * Download an attachment from an attachment ID.\n *\n * @example\n * Download an attachment from an email\n * ```typescript\n * const attachment = email.attachments[0];\n * const attachmentBuffer = await client.downloadAttachment(attachment.id);\n *\n * // save to file\n * fs.writeFileSync(attachment.filename, attachmentBuffer);\n * ```\n */\n async downloadAttachment(attachmentId: string): Promise<Buffer> {\n const result = await this.getAttachment(attachmentId);\n\n const response = await axios.get(result.data.download_url, { responseType: \"arraybuffer\" });\n return Buffer.from(response.data);\n }\n}\n"],"mappings":";;;;AAAA,OAAO,WAAyE;AAsBhF,OAAO,gBAAgB;AAEhB,IAAM,gBAAN,MAAoB;AAAA,EAxB3B,OAwB2B;AAAA;AAAA;AAAA,EACzB,YAAY,EAAE,QAAQ,SAAS,KAAK,GAAuE;AACzG,SAAK,gBAAgB,MAAM,OAAO;AAAA,MAChC,SAAS;AAAA,QACP,aAAa;AAAA,MACf;AAAA,MACA,SAAS,WAAW;AAAA,MACpB;AAAA,IACF,CAAC;AAAA,EACH;AAAA,EAEiB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWjB,MAAM,kBACJ,aACA,QACA,QACoC;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AM,oBAAI,KAAK,GAAE,QAAQ,IAAI,GAAI,IAAI,KAAK;AAAA,IAC1E;AAGA,QAAI,QAAQ,SAAS,OAAO;AAC1B,cAAQ,OAAO;AAAA,IACjB;AAEA,QAAI,UAAU,EAAE,GAAG,OAAO;AAE1B,QAAI,QAAQ,iBAAiB,QAAW;AACtC,cAAQ,eAAe;AAAA,IACzB;AAGA,QAAI,QAAQ,QAAQ,QAAQ,YAAY,QAAW;AACjD,cAAQ,UAAU,MAAO,KAAK;AAAA,IAChC;AAEA,YACE,MAAM,KAAK,cAAc,IAAI,cAAc,SAAS,UAAU;AAAA,MAC5D,GAAG;AAAA,MACH,QAAQ;AAAA,IACV,CAAC,GACD;AAAA,EACJ;AAAA;AAAA;AAAA;AAAA,EAKA,MAAM,gBAAgB,WAA0C;AAC9D,UAAM,SAAS,MAAM,KAAK,cAAc,IAAI,YAAY,SAAS,EAAE;AACnE,WAAO,OAAO;AAAA,EAChB;AAAA,EAEA,MAAM,cAAc,cAAsD;AACxE,UAAM,SAAS,MAAM,KAAK,cAAc,IAAI,mBAAmB,YAAY,EAAE;AAC7E,WAAO,OAAO;AAAA,EAChB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAeA,MAAM,mBAAmB,cAAuC;AAC9D,UAAM,SAAS,MAAM,KAAK,cAAc,YAAY;AAEpD,UAAM,WAAW,MAAM,MAAM,IAAI,OAAO,KAAK,cAAc,EAAE,cAAc,cAAc,CAAC;AAC1F,WAAO,OAAO,KAAK,SAAS,IAAI;AAAA,EAClC;AACF;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mailisk",
3
- "version": "2.2.4",
3
+ "version": "2.4.0",
4
4
  "description": "Mailisk library for NodeJS",
5
5
  "keywords": [
6
6
  "mailisk",
package/src/index.ts CHANGED
@@ -1 +1,2 @@
1
1
  export * from "./mailisk";
2
+ export * from "./mailisk.interfaces";
@@ -241,3 +241,117 @@ export interface SendVirtualSmsParams {
241
241
  /** The body of the SMS message */
242
242
  body: string;
243
243
  }
244
+
245
+ export type TotpAlgorithm = "SHA1" | "SHA256" | "SHA512";
246
+
247
+ export type KnownTotpDeviceSource = "shared_secret" | "custom" | "base32_secret_key" | "otpauth_url";
248
+
249
+ export type TotpDeviceSource = KnownTotpDeviceSource | (string & {});
250
+
251
+ export interface TotpDevice {
252
+ id: string;
253
+ organisation_id: string;
254
+ name: string;
255
+ username?: string | null;
256
+ issuer?: string | null;
257
+ digits: number;
258
+ period: number;
259
+ algorithm: TotpAlgorithm;
260
+ source: TotpDeviceSource;
261
+ expires_at?: string | null;
262
+ created_at: string;
263
+ updated_at: string;
264
+ }
265
+
266
+ export interface ListTotpDevicesParams {
267
+ /** The maximum number of saved TOTP devices returned (1-100), used alongside `offset` for pagination. */
268
+ limit?: number;
269
+ /** The number of saved TOTP devices to skip/ignore, used alongside `limit` for pagination. Must be >= 0. */
270
+ offset?: number;
271
+ /** Case-insensitive partial username match. Trimmed before sending. */
272
+ username?: string;
273
+ /** Case-insensitive partial issuer match. Trimmed before sending. */
274
+ issuer?: string;
275
+ }
276
+
277
+ export interface ListTotpDevicesResponse {
278
+ total_count: number;
279
+ options: ListTotpDevicesParams;
280
+ items: TotpDevice[];
281
+ }
282
+
283
+ export interface CreateTotpDeviceParams {
284
+ /** Base32 shared secret. Uses default TOTP settings. */
285
+ shared_secret: string;
286
+ /** Optional saved-device display name. Max 120 characters. */
287
+ name?: string;
288
+ /** Future ISO timestamp after which the saved device expires. */
289
+ expires_at?: string;
290
+ }
291
+
292
+ export interface CreateCustomTotpDeviceParams {
293
+ /** Base32 shared secret. */
294
+ secret: string;
295
+ /** Optional saved-device display name. Max 120 characters. */
296
+ name?: string;
297
+ /** Account label. Max 240 characters. */
298
+ username?: string;
299
+ /** Issuer/app label. Max 240 characters. */
300
+ issuer?: string;
301
+ /** Number of OTP digits. */
302
+ digits?: 6 | 8;
303
+ /** OTP period in seconds. Must be an integer from 10 to 300. */
304
+ period?: number;
305
+ /** Hashing algorithm. */
306
+ algorithm?: TotpAlgorithm;
307
+ /** Future ISO timestamp after which the saved device expires. */
308
+ expires_at?: string;
309
+ }
310
+
311
+ export interface CreateBase32SecretKeyTotpDeviceParams {
312
+ /** Base32 shared secret key. */
313
+ base32_secret_key: string;
314
+ /** Optional saved-device display name. Max 120 characters. */
315
+ name?: string;
316
+ /** Account label. Max 240 characters. */
317
+ username?: string;
318
+ /** Issuer/app label. Max 240 characters. */
319
+ issuer?: string;
320
+ /** Number of OTP digits. */
321
+ digits?: 6 | 8;
322
+ /** OTP period in seconds. Must be an integer from 10 to 300. */
323
+ period?: number;
324
+ /** Hashing algorithm. */
325
+ algorithm?: TotpAlgorithm;
326
+ /** Future ISO timestamp after which the saved device expires. */
327
+ expires_at?: string;
328
+ }
329
+
330
+ export interface CreateOtpAuthUrlTotpDeviceParams {
331
+ /** otpauth://totp URL with a secret query parameter. */
332
+ otp_auth_url: string;
333
+ /** Optional saved-device display name. Max 120 characters. */
334
+ name?: string;
335
+ /** Account label, used when missing from the URL label. Max 240 characters. */
336
+ username?: string;
337
+ /** Issuer/app label, used when missing from the URL. Max 240 characters. */
338
+ issuer?: string;
339
+ /** Number of OTP digits, used when missing from the URL. */
340
+ digits?: 6 | 8;
341
+ /** OTP period in seconds, used when missing from the URL. Must be an integer from 10 to 300. */
342
+ period?: number;
343
+ /** Hashing algorithm, used when missing from the URL. */
344
+ algorithm?: TotpAlgorithm;
345
+ /** Future ISO timestamp after which the saved device expires. */
346
+ expires_at?: string;
347
+ }
348
+
349
+ export interface GetTotpOtpParams {
350
+ /** Minimum number of seconds the returned OTP should remain valid. Must be >= 0 and less than the TOTP period. */
351
+ min_seconds_until_expire?: number;
352
+ }
353
+
354
+ export interface TotpOtpResponse {
355
+ code: string;
356
+ expires: string;
357
+ }
package/src/mailisk.ts CHANGED
@@ -1,8 +1,15 @@
1
1
  import axios, { AxiosBasicCredentials, AxiosInstance, AxiosRequestConfig } from "axios";
2
2
  import {
3
+ CreateBase32SecretKeyTotpDeviceParams,
4
+ CreateCustomTotpDeviceParams,
5
+ CreateOtpAuthUrlTotpDeviceParams,
6
+ CreateTotpDeviceParams,
3
7
  GetAttachmentResponse,
8
+ GetTotpOtpParams,
4
9
  ListNamespacesResponse,
5
10
  ListSmsNumbersResponse,
11
+ ListTotpDevicesParams,
12
+ ListTotpDevicesResponse,
6
13
  SearchInboxParams,
7
14
  SearchInboxResponse,
8
15
  SearchSmsMessagesParams,
@@ -10,6 +17,8 @@ import {
10
17
  SendVirtualEmailParams,
11
18
  SendVirtualSmsParams,
12
19
  SmtpSettings,
20
+ TotpDevice,
21
+ TotpOtpResponse,
13
22
  } from "./mailisk.interfaces";
14
23
  import nodemailer from "nodemailer";
15
24
 
@@ -38,7 +47,7 @@ export class MailiskClient {
38
47
  async searchSmsMessages(
39
48
  phoneNumber: string,
40
49
  params?: SearchSmsMessagesParams,
41
- config?: AxiosRequestConfig
50
+ config?: AxiosRequestConfig,
42
51
  ): Promise<SearchSmsMessagesResponse> {
43
52
  let _params: SearchSmsMessagesParams = { ...params };
44
53
 
@@ -94,6 +103,146 @@ export class MailiskClient {
94
103
  return (await this.axiosInstance.post("api/sms/virtual", params)).data;
95
104
  }
96
105
 
106
+ /**
107
+ * List saved TOTP devices.
108
+ *
109
+ * @example
110
+ * List saved TOTP devices for an issuer and username
111
+ * ```typescript
112
+ * const { items: devices } = await client.listTotpDevices({
113
+ * issuer: "GitHub",
114
+ * username: "qa@example.com",
115
+ * });
116
+ * ```
117
+ */
118
+ async listTotpDevices(params?: ListTotpDevicesParams): Promise<ListTotpDevicesResponse> {
119
+ const requestParams: ListTotpDevicesParams = {
120
+ ...params,
121
+ username: params?.username?.trim(),
122
+ issuer: params?.issuer?.trim(),
123
+ };
124
+
125
+ return (
126
+ await this.axiosInstance.get("api/devices", {
127
+ params: requestParams,
128
+ })
129
+ ).data;
130
+ }
131
+
132
+ /**
133
+ * Create a saved TOTP device from a Base32 shared secret using default TOTP settings.
134
+ *
135
+ * @example
136
+ * Create a saved TOTP device from a shared secret
137
+ * ```typescript
138
+ * const device = await client.createTotpDevice({
139
+ * name: "GitHub staging",
140
+ * shared_secret: "JBSWY3DPEHPK3PXP",
141
+ * });
142
+ * ```
143
+ */
144
+ async createTotpDevice(params: CreateTotpDeviceParams): Promise<TotpDevice> {
145
+ return (await this.axiosInstance.post("api/devices", params)).data;
146
+ }
147
+
148
+ /**
149
+ * Create a saved TOTP device with custom settings.
150
+ *
151
+ * @example
152
+ * Create a saved TOTP device with custom settings
153
+ * ```typescript
154
+ * const device = await client.createCustomTotpDevice({
155
+ * name: "GitHub staging",
156
+ * secret: "JBSWY3DPEHPK3PXP",
157
+ * username: "qa@example.com",
158
+ * issuer: "GitHub",
159
+ * digits: 6,
160
+ * period: 30,
161
+ * algorithm: "SHA1",
162
+ * });
163
+ * ```
164
+ */
165
+ async createCustomTotpDevice(params: CreateCustomTotpDeviceParams): Promise<TotpDevice> {
166
+ return (await this.axiosInstance.post("api/devices/custom", params)).data;
167
+ }
168
+
169
+ /**
170
+ * Create a saved TOTP device from a Base32 secret key.
171
+ *
172
+ * @example
173
+ * Create a saved TOTP device from a Base32 secret key
174
+ * ```typescript
175
+ * const device = await client.createTotpDeviceFromBase32SecretKey({
176
+ * base32_secret_key: "JBSWY3DPEHPK3PXP",
177
+ * username: "qa@example.com",
178
+ * issuer: "GitHub",
179
+ * });
180
+ * ```
181
+ */
182
+ async createTotpDeviceFromBase32SecretKey(params: CreateBase32SecretKeyTotpDeviceParams): Promise<TotpDevice> {
183
+ return (await this.axiosInstance.post("api/devices/base32-secret-key", params)).data;
184
+ }
185
+
186
+ /**
187
+ * Create a saved TOTP device from an otpauth://totp URL.
188
+ *
189
+ * @example
190
+ * Create a saved TOTP device from an otpauth URL
191
+ * ```typescript
192
+ * const device = await client.createTotpDeviceFromOtpAuthUrl({
193
+ * otp_auth_url: "otpauth://totp/GitHub:qa@example.com?secret=JBSWY3DPEHPK3PXP&issuer=GitHub",
194
+ * });
195
+ * ```
196
+ */
197
+ async createTotpDeviceFromOtpAuthUrl(params: CreateOtpAuthUrlTotpDeviceParams): Promise<TotpDevice> {
198
+ return (await this.axiosInstance.post("api/devices/otpauth-url", params)).data;
199
+ }
200
+
201
+ /**
202
+ * Generate a TOTP code from a shared secret without saving a device.
203
+ *
204
+ * @example
205
+ * Generate a TOTP code from a shared secret
206
+ * ```typescript
207
+ * const { code } = await client.getTotpOtpBySharedSecret("JBSWY3DPEHPK3PXP", {
208
+ * min_seconds_until_expire: 10,
209
+ * });
210
+ * ```
211
+ */
212
+ async getTotpOtpBySharedSecret(sharedSecret: string, params?: GetTotpOtpParams): Promise<TotpOtpResponse> {
213
+ return (await this.axiosInstance.post("api/devices/otp", { shared_secret: sharedSecret, ...params })).data;
214
+ }
215
+
216
+ /**
217
+ * Generate a TOTP code for a saved device.
218
+ *
219
+ * @example
220
+ * Generate a TOTP code for a saved device
221
+ * ```typescript
222
+ * const { code } = await client.getTotpOtpByDeviceId(device.id, {
223
+ * min_seconds_until_expire: 10,
224
+ * });
225
+ * ```
226
+ */
227
+ async getTotpOtpByDeviceId(deviceId: string, params?: GetTotpOtpParams): Promise<TotpOtpResponse> {
228
+ const url = `api/devices/${deviceId}/otp`;
229
+
230
+ return params ? (await this.axiosInstance.get(url, { params })).data : (await this.axiosInstance.get(url)).data;
231
+ }
232
+
233
+ /**
234
+ * Delete a saved TOTP device.
235
+ *
236
+ * @example
237
+ * Delete a saved TOTP device
238
+ * ```typescript
239
+ * await client.deleteTotpDevice(device.id);
240
+ * ```
241
+ */
242
+ async deleteTotpDevice(deviceId: string): Promise<void> {
243
+ await this.axiosInstance.delete(`api/devices/${deviceId}`);
244
+ }
245
+
97
246
  /**
98
247
  * List all namespaces that belong to the current account (API key).
99
248
  */
@@ -180,7 +329,7 @@ export class MailiskClient {
180
329
  async searchInbox(
181
330
  namespace: string,
182
331
  params?: SearchInboxParams,
183
- config?: AxiosRequestConfig
332
+ config?: AxiosRequestConfig,
184
333
  ): Promise<SearchInboxResponse> {
185
334
  let _params = { ...params };
186
335
 
@@ -106,3 +106,34 @@ export const mockSmsNumbersResponse = {
106
106
  },
107
107
  ],
108
108
  };
109
+
110
+ export const mockTotpDevice = {
111
+ id: "9b1f6ec0-b90d-4bd8-8dd0-f6b2d5138273",
112
+ organisation_id: "7f0a9c32-66b2-4e25-a4cf-1f77db8f7f3b",
113
+ name: "GitHub staging",
114
+ username: "qa@example.com",
115
+ issuer: "GitHub",
116
+ digits: 6,
117
+ period: 30,
118
+ algorithm: "SHA1",
119
+ source: "custom",
120
+ expires_at: null,
121
+ created_at: "2026-05-18T12:00:00.000Z",
122
+ updated_at: "2026-05-18T12:00:00.000Z",
123
+ };
124
+
125
+ export const mockTotpDevicesResponse = {
126
+ total_count: 1,
127
+ options: {
128
+ limit: 20,
129
+ offset: 0,
130
+ issuer: "GitHub",
131
+ username: "qa@example.com",
132
+ },
133
+ items: [mockTotpDevice],
134
+ };
135
+
136
+ export const mockTotpOtpResponse = {
137
+ code: "123456",
138
+ expires: "2026-05-18T12:00:30.000Z",
139
+ };