@orello/mailer
Version:
SDK for Orello Email Service — A developer-friendly toolkit to integrate email delivery, events, and interactions.
171 lines (170 loc) • 6.26 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.Orello = void 0;
// src/orello.ts
const dotenv_1 = require("dotenv");
const axios_1 = __importStar(require("axios"));
const interface_1 = require("./core/interface");
(0, dotenv_1.configDotenv)(); // safe to call early
class Orello {
constructor(options) {
// dotenv already configured above
const envApi = orelloConfig.ORELLO_API_URL;
if (!options.apiKey) {
throw new Error("Missing API Key in OrelloConfig.");
}
this.apiUrl = envApi;
this.apiKey = options.apiKey;
this.timeoutMs = options.timeoutMs ?? 15000; // default 15s
}
authHeaders() {
return {
"X-ORELLO-API-KEY": this.apiKey,
};
}
ensureFetchAndFormDataAvailable() {
const hasFetch = typeof globalThis.fetch === "function";
const hasFormData = typeof globalThis.FormData !== "undefined";
return { hasFetch, hasFormData };
}
/**
* sendMail - sends immediately.
* If attachments exist we send as multipart/form-data, otherwise JSON.
*/
async sendMail(template, mail, onSuccess, onError) {
try {
if (template) {
const response = await axios_1.default.post(`${this.apiUrl}/send`, {
to: mail.to,
cc: mail.cc,
bcc: mail.bcc,
subject: mail.subject,
text: mail.text,
html: mail.html,
headers: mail.headers,
metadata: mail.metadata,
}, {
headers: this.authHeaders(),
timeout: this.timeoutMs,
});
if (response.data) {
onSuccess(response.data);
}
}
}
catch (error) {
if ((0, axios_1.isAxiosError)(error)) {
const axiosError = error;
onError(new interface_1.OrelloError(axiosError.message, axiosError.response?.status, axiosError.response?.data));
}
else {
onError(new interface_1.OrelloError("Unknown error occurred", 500, error));
}
}
}
async sendMailTemplate(template, mail, onSuccess, onError) {
try {
if (template) {
const response = await axios_1.default.post(`${this.apiUrl}/send`, {
to: mail.to,
cc: mail.cc,
bcc: mail.bcc,
}, {
headers: this.authHeaders(),
timeout: this.timeoutMs,
});
if (response.data) {
onSuccess(response.data);
return new interface_1.OrelloResponse("Email sent successfully");
}
}
return new interface_1.OrelloError("Template ID is not provided", 400);
}
catch (error) {
if ((0, axios_1.isAxiosError)(error)) {
const axiosError = error;
onError(new interface_1.OrelloError(axiosError.message, axiosError.response?.status, axiosError.response?.data));
}
else {
onError(new interface_1.OrelloError("Unknown error occurred", 500, error));
}
}
}
async scheduleSend(mail) { }
/**
* sendMail - Instanciate the email template and data
* If attachments exist we send as multipart/form-data, otherwise JSON.
*/
useTemplate(template, options) {
if (!template)
throw new interface_1.OrelloError("Template ID is not provided");
let email;
const events = {};
const triggerEvent = (event) => {
if (events[event]) {
events[event].forEach(cb => cb());
}
};
return {
send: async () => {
return await this.sendMailTemplate(template, options, () => triggerEvent("SENT"), (err) => triggerEvent("ERROR"));
},
scheduleSend: async (time) => {
// NOT IMPLEMENTED
},
queue: async () => {
// implement queuing (e.g., push to DB or message queue)
// triggerEvent("QUEUED");
},
on: (event, callback) => {
if (!events[event])
events[event] = [];
events[event].push(callback);
// return this; // allow chaining
}
};
}
/**
* Subscribe to updates
* @param subscription
* @returns void
*/
async subscribe(subscription) {
if (!subscription)
throw new interface_1.OrelloError("Missing subscription payload");
}
}
exports.Orello = Orello;