( response: Response )
| 50 | * operation (template, media, interactive, reaction) on `/messages`. |
| 51 | */ |
| 52 | export async function transformWhatsAppSendResponse( |
| 53 | response: Response |
| 54 | ): Promise<WhatsAppSendResponse> { |
| 55 | const data = await parseWhatsAppResponse(response) |
| 56 | |
| 57 | if (!response.ok) { |
| 58 | throw new Error(extractErrorMessage(data, response.status)) |
| 59 | } |
| 60 | |
| 61 | const contacts = Array.isArray(data.contacts) |
| 62 | ? data.contacts.filter(isRecord).map((contact) => ({ |
| 63 | input: typeof contact.input === 'string' ? contact.input : '', |
| 64 | wa_id: typeof contact.wa_id === 'string' ? contact.wa_id : null, |
| 65 | })) |
| 66 | : [] |
| 67 | const firstMessage = |
| 68 | Array.isArray(data.messages) && isRecord(data.messages[0]) ? data.messages[0] : undefined |
| 69 | const messageId = typeof firstMessage?.id === 'string' ? firstMessage.id : undefined |
| 70 | const messageStatus = |
| 71 | typeof firstMessage?.message_status === 'string' ? firstMessage.message_status : undefined |
| 72 | |
| 73 | if (!messageId) { |
| 74 | throw new Error('WhatsApp API response did not include a message ID') |
| 75 | } |
| 76 | |
| 77 | return { |
| 78 | success: true, |
| 79 | output: { |
| 80 | success: true, |
| 81 | messageId, |
| 82 | messageStatus, |
| 83 | messagingProduct: |
| 84 | typeof data.messaging_product === 'string' ? data.messaging_product : undefined, |
| 85 | inputPhoneNumber: contacts[0]?.input ?? null, |
| 86 | whatsappUserId: contacts[0]?.wa_id ?? null, |
| 87 | contacts, |
| 88 | }, |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Shared output schema for every outbound send operation. Mirrors the |
nothing calls this directly
no test coverage detected