mnotify-ts-sdk
Version:
Modern, zero-dependency TypeScript SDK for mNotify BMS API - Type-safe SMS, contacts, and account management with Railway-Oriented Programming
149 lines • 6.02 kB
JavaScript
;
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.ContactService = 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 contact response
*/
const validateContact = (data) => {
if (!(0, validation_1.isObject)(data))
return false;
const hasId = (0, validation_1.isString)(data._id) || (0, validation_1.isString)(data.id);
return hasId && (0, validation_1.isString)(data.phone);
};
const normalizeContact = (data) => {
if (!(0, validation_1.isObject)(data))
return null;
const id = typeof data._id === "string"
? data._id
: typeof data.id === "string"
? data.id
: null;
if (!id || !(0, validation_1.isString)(data.phone)) {
return null;
}
return {
_id: id,
phone: data.phone,
firstname: (0, validation_1.isString)(data.firstname) ? data.firstname : "",
lastname: (0, validation_1.isString)(data.lastname) ? data.lastname : "",
title: (0, validation_1.isString)(data.title) ? data.title : undefined,
email: (0, validation_1.isArray)(data.email) && data.email.every((item) => (0, validation_1.isString)(item))
? data.email
: undefined,
dbo: (0, validation_1.isString)(data.dbo) ? data.dbo : undefined,
};
};
/**
* Service for managing contacts with mNotify API
*/
class ContactService {
constructor(client) {
this.client = client;
}
annotate(result, operation) {
return (0, errorContext_1.annotateResultError)(result, {
service: "ContactService",
operation,
});
}
/**
* Creates a new contact (railway-oriented programming)
* @param contact - Contact data
* @returns Result containing created contact with ID or error
*/
createContactSafe(contact, groupId) {
return __awaiter(this, void 0, void 0, function* () {
if (!groupId) {
return (0, Result_1.err)(new MNotifyError_1.MNotifyError("mNotify v2 requires groupId to create a contact. Pass createContactSafe(contact, groupId).", 400, undefined, {
service: "ContactService",
operation: "createContactSafe",
stage: "validation",
path: "/contact/{group_id}",
}));
}
const result = this.annotate(yield this.client.requestSafe({
method: "POST",
url: `/contact/${groupId}`,
data: contact,
}), "createContactSafe");
if (result.isErr()) {
return result;
}
if (!validateContact(result.value)) {
return (0, Result_1.err)(new MNotifyError_1.MNotifyError("Invalid contact response format", 0, result.value, {
service: "ContactService",
operation: "createContactSafe",
stage: "validation",
}));
}
const normalized = normalizeContact(result.value);
if (!normalized) {
return (0, Result_1.err)(new MNotifyError_1.MNotifyError("Invalid contact response format", 0, result.value, {
service: "ContactService",
operation: "createContactSafe",
stage: "validation",
}));
}
return (0, Result_1.ok)(normalized);
});
}
/**
* Creates a new contact (throws on error - legacy API)
* @param contact - Contact data
* @returns Created contact with ID
*/
createContact(contact, groupId) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this.createContactSafe(contact, groupId);
return result.unwrap();
});
}
/**
* Retrieves all contacts (railway-oriented programming)
* @returns Result containing array of contacts or error
*/
getContactsSafe() {
return __awaiter(this, void 0, void 0, function* () {
const result = this.annotate(yield this.client.requestSafe({
method: "GET",
url: "/contact",
}), "getContactsSafe");
if (result.isErr()) {
return result;
}
if (!(0, validation_1.isArray)(result.value)) {
return (0, Result_1.err)(new MNotifyError_1.MNotifyError("Invalid contacts response format", 0, result.value, {
service: "ContactService",
operation: "getContactsSafe",
stage: "validation",
}));
}
return (0, Result_1.ok)(result.value);
});
}
/**
* Retrieves all contacts (throws on error - legacy API)
* @returns Array of contacts
*/
getContacts() {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this.getContactsSafe();
return result.unwrap();
});
}
}
exports.ContactService = ContactService;
//# sourceMappingURL=ContactService.js.map