mailora-ai
Version:
SDK for integrating with Mailora AI API to send bulk email data
116 lines (114 loc) • 3.84 kB
JavaScript
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
import axios from "axios";
import { z, ZodError } from "zod";
import axiosRetry from "axios-retry";
var MailoraUserSchema = z.object({
name: z.string().min(2, "Name must be at least 2 characters."),
email: z.string().email("Invalid email format."),
mobile: 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: 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 = axios.create({
baseURL: this.baseUrl,
headers: {
Authorization: `Bearer ${this.apiKey}`,
"Content-Type": "application/json"
},
timeout: 1e4
});
const retries = 3;
if (process.env.NODE_ENV !== "test") {
axiosRetry(this.axiosInstance, {
retries,
// Now it's defined
retryDelay: axiosRetry.exponentialDelay,
retryCondition: (err) => {
var _a;
return axiosRetry.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 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}`);
}
});
}
};
export {
MailoraAI,
MailoraUserSchema
};