UNPKG

@solufy/evolution-sdk

Version:

Unofficial SDK for the Evolution Whatsapp API v2

140 lines (133 loc) 4.67 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/modules/messages/schemas/poll.ts var poll_exports = {}; __export(poll_exports, { BodySchema: () => PollMessageBodySchema, OptionsSchema: () => PollMessageOptionsSchema, PollMessageBodySchema: () => PollMessageBodySchema, PollMessageOptionsSchema: () => PollMessageOptionsSchema, PollMessageResponseSchema: () => PollMessageResponseSchema, ResponseSchema: () => PollMessageResponseSchema }); module.exports = __toCommonJS(poll_exports); var import_zod3 = require("zod"); // src/types/tags.ts var Jid = (jid) => jid; var MessageId = (id) => id; // src/utils/phone-numer-from-jid.ts var import_libphonenumber_js = require("libphonenumber-js"); function phoneNumberFromJid(jid) { return (0, import_libphonenumber_js.parsePhoneNumber)(`+${jid.split("@")[0]}`).number; } // src/modules/messages/schemas/base.ts var import_zod2 = require("zod"); // src/schemas/common.ts var import_libphonenumber_js2 = require("libphonenumber-js"); var import_zod = require("zod"); var PhoneNumberSchema = import_zod.z.custom((value) => (0, import_libphonenumber_js2.isValidPhoneNumber)(value), "Invalid phone number").transform((phoneNumber) => (0, import_libphonenumber_js2.parsePhoneNumber)(phoneNumber).number); var JidSchema = import_zod.z.string().endsWith( "@s.whatsapp.net", "Invalid remote JID, should end with @s.whatsapp.net" ); var GroupJidSchema = import_zod.z.string().endsWith( "@g.us", "Invalid group JID, should end with @g.us" ); var GroupInviteCodeSchema = import_zod.z.string().length(22).regex( /^[a-zA-Z0-9]{22}$/, "Invalid group invite code" ); var ApiNumberSchema = import_zod.z.union([ PhoneNumberSchema, JidSchema, GroupJidSchema ]); var mediaSchema = import_zod.z.union([import_zod.z.string().url(), import_zod.z.string().base64()]); // src/modules/messages/schemas/base.ts var BaseMessageOptionsSchema = import_zod2.z.object({ /** * Number (with country code) or JID to receive the message */ number: ApiNumberSchema, /** * Time in milliseconds before sending message */ delay: import_zod2.z.number().optional() }); // src/modules/messages/schemas/poll.ts var PollMessageOptionsSchema = BaseMessageOptionsSchema.extend({ /** * Name of the poll */ name: import_zod3.z.string(), /** * Whether multiple options can be selected * @default false */ multiple: import_zod3.z.boolean().optional().default(false), /** * Poll options */ options: import_zod3.z.array(import_zod3.z.string()) }); var PollMessageBodySchema = PollMessageOptionsSchema.transform( ({ multiple, options, ...data }) => ({ ...data, selectableCount: multiple ? options.length : 1, values: options }) ); var PollMessageResponseSchema = import_zod3.z.object({ key: import_zod3.z.object({ remoteJid: import_zod3.z.string(), id: import_zod3.z.string() }), message: import_zod3.z.object({ pollCreationMessageV3: import_zod3.z.object({ name: import_zod3.z.string(), options: import_zod3.z.array(import_zod3.z.object({ optionName: import_zod3.z.string() })), selectableOptionsCount: import_zod3.z.number() }) }), messageTimestamp: import_zod3.z.coerce.date() }).transform((data) => ({ receiver: { phoneNumber: phoneNumberFromJid(data.key.remoteJid), jid: Jid(data.key.remoteJid) }, poll: { name: data.message.pollCreationMessageV3.name, options: data.message.pollCreationMessageV3.options.map( (option) => option.optionName ), multiple: data.message.pollCreationMessageV3.selectableOptionsCount > 1 }, id: MessageId(data.key.id), timestamp: data.messageTimestamp })); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { BodySchema, OptionsSchema, PollMessageBodySchema, PollMessageOptionsSchema, PollMessageResponseSchema, ResponseSchema }); //# sourceMappingURL=poll.js.map