UNPKG

tyntec-sdk

Version:

TypeScript SDK for Tyntec Conversations API V3

44 lines (43 loc) 1.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.whatsAppMessageSchemaAsBase = exports.messageSchema = exports.whatsAppMessageStructure = exports.messageBaseFields = void 0; exports.validateMessage = validateMessage; const zod_1 = require("zod"); const whatsapp_1 = require("./whatsapp"); // Define supported channels const SUPPORTED_CHANNELS = ['whatsapp', 'telegram', 'facebook']; // Base fields that are common to all message types exports.messageBaseFields = zod_1.z.object({ to: zod_1.z.string(), idSender: zod_1.z.string().optional(), channel: zod_1.z.enum(SUPPORTED_CHANNELS), sender: zod_1.z.boolean().optional(), }); // WhatsApp specific message structure exports.whatsAppMessageStructure = zod_1.z.object({ channel: zod_1.z.literal(SUPPORTED_CHANNELS.find((c) => c === 'whatsapp')), content: whatsapp_1.whatsAppMessageSchema, from: zod_1.z.string(), senderName: zod_1.z.string().optional(), }); // Combined message schema that supports multiple channels exports.messageSchema = exports.messageBaseFields.and(zod_1.z.discriminatedUnion('channel', [ exports.whatsAppMessageStructure, // Add other channel message structures here as needed ])); // Validation function function validateMessage(message) { const result = exports.messageSchema.safeParse(message); if (!result.success) { return { success: false, error: result.error, }; } return { success: true, data: result.data, }; } // Export the WhatsApp message schema as the base message schema exports.whatsAppMessageSchemaAsBase = whatsapp_1.whatsAppMessageSchema;