@usepoodle/poodle-js
Version:
Poodle JavaScript and TypeScript SDK
81 lines (80 loc) • 3.11 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 __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PoodleClient = void 0;
const axios_1 = __importDefault(require("axios"));
const types_1 = require("./types");
/**
* Default base URL for the Poodle API
*/
const DEFAULT_BASE_URL = 'https://api.usepoodle.com/v1';
/**
* Main client class for interacting with the Poodle API
*/
class PoodleClient {
/**
* Creates a new instance of the Poodle client
* @param options Configuration options for the client
*/
constructor(options) {
this.options = options;
if (!options.apiKey) {
throw new Error('API key is required');
}
this.client = axios_1.default.create({
baseURL: options.baseUrl || DEFAULT_BASE_URL,
headers: {
Authorization: `Bearer ${options.apiKey}`,
'Content-Type': 'application/json',
'User-Agent': 'poodle-js/1.0.0',
},
});
}
/**
* Sends an email using the Poodle API
* @param options Email sending options
* @returns A promise that resolves with the send email response
* @throws {PoodleError} If the API request fails
*/
async sendEmail(options) {
if (!options.from)
throw new Error('From address is required');
if (!options.to)
throw new Error('To address is required');
if (!options.subject)
throw new Error('Subject is required');
if (!options.html && !options.text) {
throw new Error('Either html or text content is required');
}
try {
const response = await this.client.post('/send-email', options);
return response.data;
}
catch (error) {
if (axios_1.default.isAxiosError(error) && error.response && error.response.data) {
const errorData = error.response.data;
throw new types_1.PoodleError(errorData.message || 'API request failed', error.response.status, errorData.error);
}
throw new types_1.PoodleError(error instanceof Error ? error.message : 'Failed to send email');
}
}
}
exports.PoodleClient = PoodleClient;
// Export types
__exportStar(require("./types"), exports);