diff --git a/src/index.ts b/src/index.ts index 41e8a8f75..a5f95dbc4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -38,6 +38,25 @@ type AuthMode = | { accountSid: string; authToken: string } | { accountSid: string; apiKey: string; apiSecret: string }; +type TwilioRequestOptions = { + method: 'get' | 'post'; + uri: string; + params?: Record; + form?: Record; +}; + +type TwilioSender = { sid: string; sender_id: string }; + +type TwilioRequestResponse = { + data?: { + senders?: TwilioSender[]; + }; +}; + +type TwilioRequester = { + request: (options: TwilioRequestOptions) => Promise; +}; + type GlobalOptions = { verbose: boolean; yes?: boolean; @@ -449,26 +468,18 @@ async function ensureFunnel(port: number) { async function findWhatsappSenderSid(client: ReturnType, from: string) { // Fetch sender SID that matches configured WhatsApp from number. try { - const resp = await (client as unknown as { - request: (options: Record) => Promise<{ data?: unknown }> - }).request({ + const resp = await (client as unknown as TwilioRequester).request({ method: 'get', uri: 'https://messaging.twilio.com/v2/Channels/Senders', params: { Channel: 'whatsapp', PageSize: 50 } }); - const data = resp?.data as Record | undefined; - const senders = Array.isArray((data as Record | undefined)?.senders) - ? (data as { senders: unknown[] }).senders - : undefined; + const senders = resp.data?.senders; if (!senders) { throw new Error('List senders response missing "senders" array'); } const match = senders.find( - (s) => - typeof s === 'object' && - s !== null && - (s as Record).sender_id === withWhatsAppPrefix(from) - ) as { sid?: string } | undefined; + (s) => typeof s?.sender_id === 'string' && s.sender_id === withWhatsAppPrefix(from) + ); if (!match || typeof match.sid !== 'string') { throw new Error(`Could not find sender ${withWhatsAppPrefix(from)} in Twilio account`); } @@ -492,7 +503,7 @@ async function updateWebhook( method: 'POST' | 'GET' = 'POST' ) { // Point Twilio sender webhook at the provided URL. - await (client as unknown as { request: (options: Record) => Promise }) + await (client as unknown as TwilioRequester) .request({ method: 'post', uri: `https://messaging.twilio.com/v2/Channels/Senders/${senderSid}`,