mailisk
Version:
Mailisk library for NodeJS
144 lines (142 loc) • 4.87 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
MailiskClient: () => MailiskClient
});
module.exports = __toCommonJS(src_exports);
// src/mailisk.ts
var import_axios = __toESM(require("axios"));
var import_nodemailer = __toESM(require("nodemailer"));
var MailiskClient = class {
constructor({ apiKey, baseUrl, auth }) {
this.axiosInstance = import_axios.default.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 = import_nodemailer.default.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 import_axios.default.get(result.data.download_url, { responseType: "arraybuffer" });
return Buffer.from(response.data);
}
};
__name(MailiskClient, "MailiskClient");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
MailiskClient
});
//# sourceMappingURL=index.js.map