mnotify-ts-sdk
Version:
Third-Party TypeScript SDK for mNotify BMS API
143 lines • 5.95 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.SMSService = exports.SendSMSResponseSchema = void 0;
const zod_1 = require("zod");
/**
* Schema for validating SMS send responses
* @property {string} status - 'success' or 'error'
* @property {string} code - Response status code
* @property {string} message - Human-readable response message
* @property {object} summary - Detailed send summary
* @property {string} summary._id - Internal campaign ID
* @property {string} summary.message_id - Public message identifier
* @property {string} summary.type - Message type
* @property {number} summary.total_sent - Total messages successfully sent
* @property {number} summary.contacts - Number of contacts targeted
* @property {number} summary.total_rejected - Number of failed deliveries
* @property {string[]} summary.numbers_sent - Array of numbers that received the message
* @property {number} summary.credit_used - Credits consumed
* @property {number} summary.credit_left - Remaining account balance
*/
exports.SendSMSResponseSchema = zod_1.z.object({
status: zod_1.z.enum(["success", "error"]),
code: zod_1.z.string(),
message: zod_1.z.string(),
summary: zod_1.z.object({
_id: zod_1.z.string(),
message_id: zod_1.z.string(),
type: zod_1.z.string(),
total_sent: zod_1.z.number(),
contacts: zod_1.z.number(),
total_rejected: zod_1.z.number(),
numbers_sent: zod_1.z.array(zod_1.z.string()),
credit_used: zod_1.z.number(),
credit_left: zod_1.z.number(),
}),
});
/**
* Schema for validating SMS delivery reports
* @property {string} status - 'success' or 'error'
* @property {object[]} report - Array of delivery status objects
* @property {number} report._id - Internal report ID
* @property {string} report.recipient - Recipient phone number
* @property {string} report.message - Message content
* @property {string} report.sender - Sender ID
* @property {string} report.status - Delivery status
* @property {string} report.date_sent - ISO timestamp of send attempt
* @property {string} [report.campaign_id] - Optional campaign identifier
* @property {number} report.retries - Number of delivery attempts
*/
const SmsDeliveryReportSchema = zod_1.z.object({
status: zod_1.z.enum(["success", "error"]),
report: zod_1.z.array(zod_1.z.object({
_id: zod_1.z.number(),
recipient: zod_1.z.string(),
message: zod_1.z.string(),
sender: zod_1.z.string(),
status: zod_1.z.string(),
date_sent: zod_1.z.string(),
campaign_id: zod_1.z.string().optional(),
retries: zod_1.z.number(),
})),
});
/**
* Service for managing SMS operations with mNotify BMS API
*/
class SMSService {
/**
* Creates an instance of SMSService
* @param {MNotifyClient} client - Configured API client instance
*/
constructor(client) {
this.client = client;
}
/**
* Sends bulk SMS messages to one or more recipients
* @param {SendSMSOptions} options - SMS configuration
* @returns {Promise<SendSMSResponse>} Detailed send report
* @throws {MNotifyError} On API failure or validation errors
*
* @example
* ```typescript
* await smsService.sendQuickBulkSMS({
* recipient: ['233200000000', '233244444444'],
* sender: 'MyApp',
* message: 'Hello from mNotify!'
* });
* ```
*/
sendQuickBulkSMS(options) {
return __awaiter(this, void 0, void 0, function* () {
const recipients = Array.isArray(options.recipient)
? options.recipient
: [options.recipient];
const payload = {
recipient: recipients,
sender: options.sender,
message: options.message,
is_schedule: options.is_schedule || false,
schedule_date: options.schedule_date || "",
};
const response = yield this.client.request({
method: "POST",
url: "/sms/quick",
data: payload,
});
console.log("SMS sent response:", response);
return exports.SendSMSResponseSchema.parse(response);
});
}
/**
* Retrieves delivery status for a sent SMS campaign
* @param {string} campaignId - ID from send response
* @param {string} [status='null'] - Optional status filter
* @returns {Promise<SmsDeliveryReport>} Detailed delivery report
* @throws {MNotifyError} On API failure or invalid campaign ID
*
* @example
* ```typescript
* const report = await smsService.getSMSStatus('campaign_123');
* console.log(report.status); // 'delivered', 'failed', etc.
* ```
*/
getSMSStatus(campaignId_1) {
return __awaiter(this, arguments, void 0, function* (campaignId, status = "null") {
const response = yield this.client.request({
method: "GET",
url: `/campaign/${campaignId}/${status}`,
});
return SmsDeliveryReportSchema.parse(response);
});
}
}
exports.SMSService = SMSService;
//# sourceMappingURL=SMSService.js.map