UNPKG

@solufy/evolution-sdk

Version:

Unofficial SDK for the Evolution Whatsapp API v2

1 lines 13 kB
{"version":3,"sources":["../../../src/modules/groups/index.ts","../../../src/api/routes.ts","../../../src/schemas/common.ts","../../../src/modules/groups/schemas/find-all.ts","../../../src/modules/groups/schemas/common.ts","../../../src/types/tags.ts","../../../src/utils/phone-numer-from-jid.ts","../../../src/modules/groups/schemas/find-by-invite-code.ts","../../../src/modules/groups/schemas/find-by-jid.ts"],"sourcesContent":["import { z } from \"zod\";\n\nimport { Routes } from \"@/api/routes\";\nimport type { ApiService } from \"@/api/service\";\nimport { GroupInviteCodeSchema, GroupJidSchema } from \"@/schemas/common\";\nimport type { GroupInviteCode, GroupJid } from \"@/types/tags\";\n\nimport * as FindAll from \"./schemas/find-all\";\nimport * as FindByInviteCode from \"./schemas/find-by-invite-code\";\nimport * as FindByJid from \"./schemas/find-by-jid\";\n\nexport class GroupsModule {\n\tconstructor(private readonly api: ApiService) {}\n\n\t/**\n\t * Gets all groups\n\t * @param getParticipants - Whether to get participants\n\t */\n\tasync findAll(getParticipants: false): Promise<FindAll.FindAllGroupsResponse>;\n\tasync findAll(\n\t\tgetParticipants: true,\n\t): Promise<FindAll.FindAllGroupsWithParticipantsResponse>;\n\tasync findAll(\n\t\tgetParticipants = false,\n\t): Promise<\n\t\t| FindAll.FindAllGroupsResponse\n\t\t| FindAll.FindAllGroupsWithParticipantsResponse\n\t> {\n\t\tconst response = await this.api.get(Routes.Groups.FindAll, {\n\t\t\tparams: { getParticipants: z.boolean().parse(getParticipants) },\n\t\t});\n\n\t\tif (getParticipants) {\n\t\t\treturn FindAll.ResponseWithParticipantsSchema.parse(response);\n\t\t}\n\t\treturn FindAll.ResponseSchema.parse(response);\n\t}\n\n\t/**\n\t * Gets a group by invite code\n\t * @param inviteCode - The group invite code (not the URL)\n\t */\n\tasync findByInviteCode(\n\t\tinviteCode: string | GroupInviteCode,\n\t): Promise<FindByInviteCode.FindGroupByInviteCodeResponse> {\n\t\tconst response = await this.api.get(Routes.Groups.FindByInviteCode, {\n\t\t\tparams: { inviteCode: GroupInviteCodeSchema.parse(inviteCode) },\n\t\t});\n\n\t\treturn FindByInviteCode.ResponseSchema.parse(response);\n\t}\n\n\t/**\n\t * Gets a group by JID\n\t * @param groupJid - The group JID terminated with \\@g.us\n\t */\n\tasync findByJid(\n\t\tgroupJid: string | GroupJid,\n\t): Promise<FindByJid.FindGroupByJidResponse> {\n\t\tconst response = await this.api.get(Routes.Groups.FindByJid, {\n\t\t\tparams: { groupJid: GroupJidSchema.parse(groupJid) },\n\t\t});\n\n\t\treturn FindByJid.ResponseSchema.parse(response);\n\t}\n}\n","export const Routes = {\n\tMessage: {\n\t\tSendText: \"message/sendText\",\n\t\tSendMedia: \"message/sendMedia\",\n\t\tSendVoice: \"message/sendWhatsAppAudio\",\n\t\tSendSticker: \"message/sendSticker\",\n\t\tSendLocation: \"message/sendLocation\",\n\t\tSendContact: \"message/sendContact\",\n\t\tSendPoll: \"message/sendPoll\",\n\t},\n\tChats: {\n\t\tCheck: \"chat/whatsappNumbers\",\n\t\tFindAll: \"chat/findChats\",\n\t\tSendPresence: \"chat/sendPresence\",\n\t},\n\tGroups: {\n\t\tFindAll: \"group/fetchAllGroups\",\n\t\tFindByJid: \"group/findGroupInfos\",\n\t\tFindByInviteCode: \"group/inviteInfo\",\n\t},\n};\n","import { isValidPhoneNumber, parsePhoneNumber } from \"libphonenumber-js\";\nimport { z } from \"zod\";\n\nimport type { GroupInviteCode, GroupJid, Jid } from \"@/types/tags\";\n\nexport const PhoneNumberSchema = z\n\t.custom<string>((value) => isValidPhoneNumber(value), \"Invalid phone number\")\n\t.transform<string>((phoneNumber) => parsePhoneNumber(phoneNumber).number);\n\nexport const JidSchema = z\n\t.string()\n\t.endsWith(\n\t\t\"@s.whatsapp.net\",\n\t\t\"Invalid remote JID, should end with @s.whatsapp.net\",\n\t) as z.ZodType<Jid>;\n\nexport const GroupJidSchema = z\n\t.string()\n\t.endsWith(\n\t\t\"@g.us\",\n\t\t\"Invalid group JID, should end with @g.us\",\n\t) as z.ZodType<GroupJid>;\n\nexport const GroupInviteCodeSchema = z\n\t.string()\n\t.length(22)\n\t.regex(\n\t\t/^[a-zA-Z0-9]{22}$/,\n\t\t\"Invalid group invite code\",\n\t) as unknown as z.ZodType<GroupInviteCode>;\n\nexport const ApiNumberSchema = z.union([\n\tPhoneNumberSchema,\n\tJidSchema,\n\tGroupJidSchema,\n]);\n\nexport const mediaSchema = z.union([z.string().url(), z.string().base64()]);\n","import { z } from \"zod\";\n\nimport {\n\tGroupResponseSchema,\n\tGroupResponseSchemaTransform,\n\tGroupWithParticipantsResponseSchema,\n\tGroupWithParticipantsResponseSchemaTransform,\n} from \"./common\";\n\nexport const FindAllGroupsResponseSchema = z\n\t.array(GroupResponseSchema)\n\t.transform((groups) =>\n\t\tgroups.map((group) => GroupResponseSchemaTransform(group)),\n\t);\n\nexport const FindAllGroupsWithParticipantsResponseSchema = z\n\t.array(GroupWithParticipantsResponseSchema)\n\t.transform((groups) =>\n\t\tgroups.map((group) => GroupWithParticipantsResponseSchemaTransform(group)),\n\t);\n\nexport type FindAllGroupsResponse = z.infer<typeof FindAllGroupsResponseSchema>;\nexport type FindAllGroupsWithParticipantsResponse = z.infer<\n\ttypeof FindAllGroupsWithParticipantsResponseSchema\n>;\n\nexport {\n\tFindAllGroupsResponseSchema as ResponseSchema,\n\tFindAllGroupsWithParticipantsResponseSchema as ResponseWithParticipantsSchema,\n};\n","import { z } from \"zod\";\n\nimport { GroupJid, Jid } from \"@/types/tags\";\nimport { phoneNumberFromJid } from \"@/utils/phone-numer-from-jid\";\n\nexport const GroupResponseSchema = z.object({\n\tid: z.string(),\n\tsubject: z.string(),\n\tsubjectOwner: z.string(),\n\tsubjectTime: z.coerce.date(),\n\tpictureUrl: z.string().url().nullish(),\n\tsize: z.number(),\n\tcreation: z.coerce.date(),\n\towner: z.string(),\n\trestrict: z.boolean(),\n\tannounce: z.boolean(),\n});\n\nexport const ParticipantResponseSchema = z.object({\n\tid: z.string(),\n\tadmin: z.enum([\"admin\", \"superadmin\"]).nullish(),\n});\n\nexport const GroupWithParticipantsResponseSchema = GroupResponseSchema.extend({\n\tparticipants: z.array(ParticipantResponseSchema),\n});\n\nexport const GroupResponseSchemaTransform = (\n\tgroup: z.infer<typeof GroupResponseSchema>,\n) => ({\n\tjid: GroupJid(group.id),\n\tname: group.subject,\n\tpictureUrl: group.pictureUrl || undefined,\n\tsize: group.size,\n\tsubject: {\n\t\towner: Jid(group.subjectOwner),\n\t\ttime: group.subjectTime,\n\t},\n\towner: {\n\t\tjid: Jid(group.owner),\n\t\tphoneNumber: phoneNumberFromJid(group.owner),\n\t},\n\tcreatedAt: group.creation,\n\trestrict: group.restrict,\n\tannounce: group.announce,\n});\n\nexport const ParticipantResponseSchemaTransform = (\n\tparticipant: z.infer<typeof ParticipantResponseSchema>,\n) => ({\n\tid: participant.id,\n\trole: participant.admin || (\"member\" as const),\n});\n\nexport const GroupWithParticipantsResponseSchemaTransform = (\n\tgroup: z.infer<typeof GroupWithParticipantsResponseSchema>,\n) => ({\n\t...GroupResponseSchemaTransform(group),\n\tparticipants: group.participants.map(ParticipantResponseSchemaTransform),\n});\n\nexport type GroupResponse = z.infer<typeof GroupResponseSchema>;\nexport type ParticipantResponse = z.infer<typeof ParticipantResponseSchema>;\nexport type GroupWithParticipantsResponse = z.infer<\n\ttypeof GroupWithParticipantsResponseSchema\n>;\n","export type Jid = `${string}@s.whatsapp.net`;\nexport const Jid = (jid: string) => jid as Jid;\n\nexport type GroupJid = `${string}@g.us`;\nexport const GroupJid = (jid: string) => jid as GroupJid;\n\nexport type GroupInviteCode = string & { __tag: \"GroupInviteCode\" };\nexport const GroupInviteCode = (code: string) => code as GroupInviteCode;\n\nexport type MessageId = string & { __tag: \"MessageId\" };\nexport const MessageId = (id: string) => id as MessageId;\n\nexport type ChatId = string & { __tag: \"ChatId\" };\nexport const ChatId = (id: string) => id as ChatId;\n","import { parsePhoneNumber } from \"libphonenumber-js\";\n\n/**\n * Get phone number from JID\n * @param jid - JID (remote JID)\n */\nexport function phoneNumberFromJid(jid: string) {\n\treturn parsePhoneNumber(`+${jid.split(\"@\")[0]}`).number;\n}\n","import { z } from \"zod\";\n\nimport {\n\tGroupWithParticipantsResponseSchema,\n\tGroupWithParticipantsResponseSchemaTransform,\n} from \"./common\";\n\nexport const FindGroupByInviteCodeResponseSchema =\n\tGroupWithParticipantsResponseSchema.extend({\n\t\tisCommunity: z.boolean(),\n\t\tisCommunityAnnounce: z.boolean(),\n\t\tjoinApprovalMode: z.boolean(),\n\t\tmemberAddMode: z.boolean(),\n\t})\n\t\t.omit({ pictureUrl: true })\n\t\t.transform((group) => ({\n\t\t\t...GroupWithParticipantsResponseSchemaTransform({\n\t\t\t\t...group,\n\t\t\t\tpictureUrl: null,\n\t\t\t}),\n\t\t\tisCommunity: group.isCommunity,\n\t\t\tisCommunityAnnounce: group.isCommunityAnnounce,\n\t\t\tjoinApprovalMode: group.joinApprovalMode,\n\t\t\tmemberAddMode: group.memberAddMode,\n\t\t}));\n\nexport type FindGroupByInviteCodeResponse = z.infer<\n\ttypeof FindGroupByInviteCodeResponseSchema\n>;\n\nexport { FindGroupByInviteCodeResponseSchema as ResponseSchema };\n","import type { z } from \"zod\";\n\nimport {\n\tGroupWithParticipantsResponseSchema,\n\tGroupWithParticipantsResponseSchemaTransform,\n} from \"./common\";\n\nexport const FindGroupByJidResponseSchema =\n\tGroupWithParticipantsResponseSchema.transform(\n\t\tGroupWithParticipantsResponseSchemaTransform,\n\t);\n\nexport type FindGroupByJidResponse = z.infer<\n\ttypeof FindGroupByJidResponseSchema\n>;\n\nexport { FindGroupByJidResponseSchema as ResponseSchema };\n"],"mappings":";AAAA,SAAS,KAAAA,UAAS;;;ACAX,IAAM,SAAS;AAAA,EACrB,SAAS;AAAA,IACR,UAAU;AAAA,IACV,WAAW;AAAA,IACX,WAAW;AAAA,IACX,aAAa;AAAA,IACb,cAAc;AAAA,IACd,aAA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