UNPKG

mnotify-ts-sdk

Version:

Modern, zero-dependency TypeScript SDK for mNotify BMS API - Type-safe SMS, contacts, and account management with Railway-Oriented Programming

298 lines 11.2 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TemplateService = void 0; const validation_1 = require("../utils/validation"); const Result_1 = require("../types/Result"); const MNotifyError_1 = require("../errors/MNotifyError"); const errorContext_1 = require("../errors/errorContext"); /** * Validates template response */ const validateTemplate = (data) => { if (!(0, validation_1.isObject)(data)) return false; const id = ((0, validation_1.isString)(data.id) && data.id) || ((0, validation_1.isString)(data._id) && data._id) || null; const name = ((0, validation_1.isString)(data.name) && data.name) || ((0, validation_1.isString)(data.title) && data.title) || null; const content = ((0, validation_1.isString)(data.content) && data.content) || ((0, validation_1.isString)(data.body) && data.body) || null; return Boolean(id && name && content); }; const normalizeTemplate = (data) => { if (!(0, validation_1.isObject)(data)) return null; const id = ((0, validation_1.isString)(data.id) && data.id) || ((0, validation_1.isString)(data._id) && data._id) || null; const name = ((0, validation_1.isString)(data.name) && data.name) || ((0, validation_1.isString)(data.title) && data.title) || null; const content = ((0, validation_1.isString)(data.content) && data.content) || ((0, validation_1.isString)(data.body) && data.body) || null; if (!id || !name || !content) { return null; } return { id, name, content, status: (0, validation_1.isString)(data.status) ? data.status : "pending", created_at: (0, validation_1.isString)(data.created_at) ? data.created_at : "", updated_at: (0, validation_1.isString)(data.updated_at) ? data.updated_at : "", }; }; /** * Service for managing SMS templates with mNotify API */ class TemplateService { constructor(client) { this.client = client; } annotate(result, operation) { return (0, errorContext_1.annotateResultError)(result, { service: "TemplateService", operation, }); } /** * Creates a new SMS template (railway-oriented programming) * @param input - Template data * @returns Result containing created template or error * * @example * ```typescript * const result = await templateService.createTemplateSafe({ * name: 'Welcome Message', * content: 'Welcome {{name}} to our service!' * }); * result.match({ * ok: (template) => console.log('Template created:', template), * err: (error) => console.error('Failed to create template:', error) * }); * ``` */ createTemplateSafe(input) { return __awaiter(this, void 0, void 0, function* () { const result = this.annotate(yield this.client.requestSafe({ method: "POST", url: "/template", data: { title: input.name, body: input.content, }, }), "createTemplateSafe"); if (result.isErr()) { return result; } if (!validateTemplate(result.value)) { return (0, Result_1.err)(new MNotifyError_1.MNotifyError("Invalid template response format", 0, result.value, { service: "TemplateService", operation: "createTemplateSafe", stage: "validation", })); } const normalized = normalizeTemplate(result.value); if (!normalized) { return (0, Result_1.err)(new MNotifyError_1.MNotifyError("Invalid template response format", 0, result.value, { service: "TemplateService", operation: "createTemplateSafe", stage: "validation", })); } return (0, Result_1.ok)(normalized); }); } /** * Creates a new SMS template (throws on error - legacy API) * @param input - Template data * @returns Created template * @throws {MNotifyError} On API failure * * @example * ```typescript * const template = await templateService.createTemplate({ * name: 'Welcome Message', * content: 'Welcome {{name}} to our service!' * }); * ``` */ createTemplate(input) { return __awaiter(this, void 0, void 0, function* () { const result = yield this.createTemplateSafe(input); return result.unwrap(); }); } /** * Retrieves all templates (railway-oriented programming) * @returns Result containing array of templates or error * * @example * ```typescript * const result = await templateService.getTemplatesSafe(); * if (result.isOk()) { * console.log('Templates:', result.value); * } * ``` */ getTemplatesSafe() { return __awaiter(this, void 0, void 0, function* () { const result = this.annotate(yield this.client.requestSafe({ method: "GET", url: "/template", }), "getTemplatesSafe"); if (result.isErr()) { return result; } if (!(0, validation_1.isArray)(result.value)) { return (0, Result_1.err)(new MNotifyError_1.MNotifyError("Invalid templates response format", 0, result.value, { service: "TemplateService", operation: "getTemplatesSafe", stage: "validation", })); } const normalized = result.value .map((item) => normalizeTemplate(item)) .filter((item) => item !== null); return (0, Result_1.ok)(normalized); }); } /** * Retrieves all templates (throws on error - legacy API) * @returns Array of templates * @throws {MNotifyError} On API failure * * @example * ```typescript * const templates = await templateService.getTemplates(); * console.log('Templates:', templates); * ``` */ getTemplates() { return __awaiter(this, void 0, void 0, function* () { const result = yield this.getTemplatesSafe(); return result.unwrap(); }); } /** * Retrieves a specific template by ID (railway-oriented programming) * @param id - Template ID * @returns Result containing template details or error * * @example * ```typescript * const result = await templateService.getTemplateSafe('template_123'); * result.match({ * ok: (template) => console.log('Template:', template), * err: (error) => console.error('Failed to get template:', error) * }); * ``` */ getTemplateSafe(id) { return __awaiter(this, void 0, void 0, function* () { const result = this.annotate(yield this.client.requestSafe({ method: "GET", url: `/template/${id}`, }), "getTemplateSafe"); if (result.isErr()) { return result; } if (!validateTemplate(result.value)) { return (0, Result_1.err)(new MNotifyError_1.MNotifyError("Invalid template response format", 0, result.value, { service: "TemplateService", operation: "getTemplateSafe", stage: "validation", })); } const normalized = normalizeTemplate(result.value); if (!normalized) { return (0, Result_1.err)(new MNotifyError_1.MNotifyError("Invalid template response format", 0, result.value, { service: "TemplateService", operation: "getTemplateSafe", stage: "validation", })); } return (0, Result_1.ok)(normalized); }); } /** * Retrieves a specific template by ID (throws on error - legacy API) * @param id - Template ID * @returns Template details * @throws {MNotifyError} On API failure * * @example * ```typescript * const template = await templateService.getTemplate('template_123'); * console.log('Template:', template); * ``` */ getTemplate(id) { return __awaiter(this, void 0, void 0, function* () { const result = yield this.getTemplateSafe(id); return result.unwrap(); }); } /** * Deletes a template (railway-oriented programming) * @param id - Template ID * @returns Result containing deletion confirmation or error * * @example * ```typescript * const result = await templateService.deleteTemplateSafe('template_123'); * if (result.isOk()) { * console.log('Template deleted successfully'); * } * ``` */ deleteTemplateSafe(id) { return __awaiter(this, void 0, void 0, function* () { const result = this.annotate(yield this.client.requestSafe({ method: "DELETE", url: `/template/${id}`, }), "deleteTemplateSafe"); if (result.isErr()) { return result; } if (!(0, validation_1.isObject)(result.value)) { return (0, Result_1.err)(new MNotifyError_1.MNotifyError("Invalid delete response format", 0, result.value, { service: "TemplateService", operation: "deleteTemplateSafe", stage: "validation", })); } return (0, Result_1.ok)(result.value); }); } /** * Deletes a template (throws on error - legacy API) * @param id - Template ID * @returns Deletion confirmation * @throws {MNotifyError} On API failure * * @example * ```typescript * await templateService.deleteTemplate('template_123'); * ``` */ deleteTemplate(id) { return __awaiter(this, void 0, void 0, function* () { const result = yield this.deleteTemplateSafe(id); return result.unwrap(); }); } } exports.TemplateService = TemplateService; //# sourceMappingURL=TemplateService.js.map