UNPKG

mailora-ai

Version:

SDK for integrating with Mailora AI API to send bulk email data

151 lines (149 loc) 5.66 kB
"use strict"; 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 __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( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var __async = (__this, __arguments, generator) => { return new Promise((resolve, reject) => { var fulfilled = (value) => { try { step(generator.next(value)); } catch (e) { reject(e); } }; var rejected = (value) => { try { step(generator.throw(value)); } catch (e) { reject(e); } }; var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); step((generator = generator.apply(__this, __arguments)).next()); }); }; // src/index.ts var index_exports = {}; __export(index_exports, { MailoraAI: () => MailoraAI, MailoraUserSchema: () => MailoraUserSchema }); module.exports = __toCommonJS(index_exports); var import_axios = __toESM(require("axios")); var import_zod = require("zod"); var import_axios_retry = __toESM(require("axios-retry")); var MailoraUserSchema = import_zod.z.object({ name: import_zod.z.string().min(2, "Name must be at least 2 characters."), email: import_zod.z.string().email("Invalid email format."), mobile: import_zod.z.string().optional().refine((val) => !val || /^\+[1-9]\d{1,14}$/.test(val), { message: "Mobile number must be in E.164 format (e.g. +14155552671)" }), location: import_zod.z.string().max(200, "Location must be under 200 characters.").optional() }); var MailoraAI = class { constructor({ apiKey, baseUrl }) { if (!apiKey || typeof apiKey !== "string") { throw new Error("API key is required and must be a string."); } const fallbackProdUrl = "https://mailora-ai.vercel.app"; this.baseUrl = baseUrl || fallbackProdUrl; if (!this.baseUrl || this.baseUrl === "/") { throw new Error("[MailoraAI SDK] \u274C Invalid base URL. Provide `baseUrl` or use a valid fallback."); } console.log("[MailoraAI SDK] Using base URL:", this.baseUrl); this.apiKey = apiKey; this.axiosInstance = import_axios.default.create({ baseURL: this.baseUrl, headers: { Authorization: `Bearer ${this.apiKey}`, "Content-Type": "application/json" }, timeout: 1e4 }); const retries = 3; if (process.env.NODE_ENV !== "test") { (0, import_axios_retry.default)(this.axiosInstance, { retries, // Now it's defined retryDelay: import_axios_retry.default.exponentialDelay, retryCondition: (err) => { var _a; return import_axios_retry.default.isNetworkOrIdempotentRequestError(err) || ((_a = err.response) == null ? void 0 : _a.status) && err.response.status >= 500 && err.response.status < 600; } }); } } getVersion() { return "1.1.6"; } sendUser(user) { return __async(this, null, function* () { try { MailoraUserSchema.parse(user); } catch (error) { if (error instanceof import_zod.ZodError) { const message = error.issues.map((e) => e.message).join(", "); throw new Error(message); } throw error; } return this.sendBulk([user]); }); } sendBulk(users) { return __async(this, null, function* () { var _a, _b, _c; if (!Array.isArray(users) || users.length === 0) { throw new Error('The "emails" array must not be empty.'); } users.forEach((user) => { MailoraUserSchema.parse(user); }); try { const response = yield this.axiosInstance.post("/api/users/auth/email/bluk", { emails: users }); return response.data; } catch (error) { const status = (_a = error == null ? void 0 : error.response) == null ? void 0 : _a.status; const apiError = ((_c = (_b = error == null ? void 0 : error.response) == null ? void 0 : _b.data) == null ? void 0 : _c.error) || (error == null ? void 0 : error.message); if (status === 401 || status === 403) { throw new Error(`Unauthorized: ${apiError}`); } if (status >= 400 && status < 500) { throw new Error(`Client Error: ${apiError}`); } throw new Error(`Mailora API Error: ${apiError}`); } }); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { MailoraAI, MailoraUserSchema });