UNPKG

mailisk

Version:

Mailisk library for NodeJS

113 lines (112 loc) 3.49 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); // src/mailisk.ts import axios from "axios"; import nodemailer from "nodemailer"; var MailiskClient = class { constructor({ apiKey, baseUrl, auth }) { this.axiosInstance = axios.create({ headers: { "X-Api-Key": apiKey }, baseURL: baseUrl || "https://api.mailisk.com/", auth }); } axiosInstance; async searchSmsMessages(phoneNumber, params, config) { let _params = { ...params }; if (params?.from_date === void 0 || params?.from_date === null) { _params.from_date = new Date(Date.now() - 15 * 60 * 1e3).toISOString(); } if (params?.wait !== false) { _params.wait = true; } let _config = { ...config }; if (config?.maxRedirects === void 0) { _config.maxRedirects = 99999; } if (_params.wait && config?.timeout === void 0) { _config.timeout = 1e3 * 60 * 5; } const requestParams = { ..._params, from_date: _params.from_date ?? void 0, to_date: _params.to_date ?? void 0 }; return (await this.axiosInstance.get(`api/sms/${phoneNumber}/messages`, { ..._config, params: requestParams })).data; } async listSmsNumbers() { return (await this.axiosInstance.get("api/sms/numbers")).data; } async sendVirtualSms(params) { return (await this.axiosInstance.post("api/sms/virtual", params)).data; } async listNamespaces() { return (await this.axiosInstance.get("api/namespaces")).data; } async sendVirtualEmail(namespace, params) { const smtpSettings = await this.getSmtpSettings(namespace); const transport = nodemailer.createTransport({ host: smtpSettings.data.host, port: smtpSettings.data.port, secure: false, auth: { user: smtpSettings.data.username, pass: smtpSettings.data.password } }); const { from, to, subject, text, html, headers, attachments } = params; await transport.sendMail({ from, to, subject, text, html, headers, attachments }); transport.close(); } async searchInbox(namespace, params, config) { let _params = { ...params }; if (params?.from_timestamp === void 0 || params?.from_timestamp === null) { _params.from_timestamp = Math.floor(new Date().getTime() / 1e3) - 15 * 60; } if (params?.wait !== false) { _params.wait = true; } let _config = { ...config }; if (config?.maxRedirects === void 0) { _config.maxRedirects = 99999; } if (_params.wait && config?.timeout === void 0) { _config.timeout = 1e3 * 60 * 5; } return (await this.axiosInstance.get(`api/emails/${namespace}/inbox`, { ..._config, params: _params })).data; } async getSmtpSettings(namespace) { const result = await this.axiosInstance.get(`api/smtp/${namespace}`); return result.data; } async getAttachment(attachmentId) { const result = await this.axiosInstance.get(`api/attachments/${attachmentId}`); return result.data; } async downloadAttachment(attachmentId) { const result = await this.getAttachment(attachmentId); const response = await axios.get(result.data.download_url, { responseType: "arraybuffer" }); return Buffer.from(response.data); } }; __name(MailiskClient, "MailiskClient"); export { MailiskClient }; //# sourceMappingURL=index.mjs.map