UNPKG

cloudmailin

Version:

Official Node.js for the CloudMailin Email API - https://www.cloudmailin.com

146 lines 6.21 kB
"use strict"; 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 (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var errors = __importStar(require("./models/errors")); var axios_1 = __importDefault(require("axios")); // Allow us to easily fetch the current version from the package without hacks // eslint-disable-next-line @typescript-eslint/no-var-requires var version = require("../package.json").version; /** * Parses a CloudMailin SMTP URL to extract username and API key * @param smtpUrl - URL in format: smtp://username:apikey@host:port?params * @returns Parsed username and API key */ function parseSMTPUrl(smtpUrl) { try { var url = new URL(smtpUrl); var username = url.username; var apiKey = url.password; if (!username || !apiKey) { throw new Error('Username and API key are required in the SMTP URL'); } return { username: username, apiKey: apiKey }; } catch (error) { throw new Error("Failed to parse CLOUDMAILIN_SMTP_URL: ".concat(error instanceof Error ? error.message : 'Unknown error')); } } /** * Creates MessageClientOptions from environment variables or provided options * @param options - Optional MessageClientOptions * @returns MessageClientOptions with values from options or environment */ function createOptions(options) { // If options are provided and have both required fields, use them if ((options === null || options === void 0 ? void 0 : options.username) && (options === null || options === void 0 ? void 0 : options.apiKey)) { return { username: options.username, apiKey: options.apiKey, host: options.host, baseURL: options.baseURL }; } // If smtpUrl is provided in options, use it if (options === null || options === void 0 ? void 0 : options.smtpUrl) { var parsed_1 = parseSMTPUrl(options.smtpUrl); return { username: parsed_1.username, apiKey: parsed_1.apiKey, host: options.host, baseURL: options.baseURL }; } // If partial options are provided but missing required fields, throw error if (options && (options.username || options.apiKey)) { throw new Error('Either provide MessageClientOptions with username and apiKey, or provide smtpUrl option, or set CLOUDMAILIN_SMTP_URL environment variable'); } // Otherwise, try to parse from environment variable var smtpUrl = process.env.CLOUDMAILIN_SMTP_URL; if (!smtpUrl) { throw new Error('Either provide MessageClientOptions with username and apiKey, or provide smtpUrl option, or set CLOUDMAILIN_SMTP_URL environment variable'); } var parsed = parseSMTPUrl(smtpUrl); return { username: parsed.username, apiKey: parsed.apiKey, host: options === null || options === void 0 ? void 0 : options.host, baseURL: options === null || options === void 0 ? void 0 : options.baseURL }; } var MessageClient = /** @class */ (function () { function MessageClient(options) { this.version = version; this.options = createOptions(options); this.options.host = this.options.host || "api.cloudmailin.com"; this.options.baseURL = this.options.baseURL || "https://".concat(this.options.host, "/api/v0.1"); } MessageClient.prototype.sendMessage = function (message) { return this.makeRequest("POST", "/messages", message); }; MessageClient.prototype.sendRawMessage = function (message) { return this.makeRequest("POST", "/messages", message); }; // Allow body to be anything // eslint-disable-next-line @typescript-eslint/ban-types MessageClient.prototype.makeRequest = function (method, path, body) { var _this = this; var client = this.makeClient(); return client.request({ method: method, url: path, data: body }) .then(function (response) { return response.data; }) .catch(function (error) { var newError = _this.handleError(error); throw newError; }); }; // Wrap the error in our own error class, in future we may have // more specific errors for different types of error. MessageClient.prototype.handleError = function (error) { return new errors.CloudMailinError(error.message, error); }; MessageClient.prototype.makeClient = function () { var baseURL = "".concat(this.options.baseURL, "/").concat(this.options.username); var headers = { Authorization: "Bearer ".concat(this.options.apiKey), "User-Agent": "CloudMailin-js ".concat(this.version) }; return axios_1.default.create({ baseURL: baseURL, responseType: "json", maxContentLength: Infinity, maxBodyLength: Infinity, headers: headers }); }; return MessageClient; }()); exports.default = MessageClient; //# sourceMappingURL=messageClient.js.map