UNPKG

@convex-dev/twilio

Version:

Convex component for sending/receiving SMS messages with Twilio.

40 lines 1.21 kB
import { parse } from "convex-helpers/validators"; export const twilioRequest = async function (path, account_sid, auth_token, body, method = "POST") { const url = `https://api.twilio.com/2010-04-01/Accounts/${account_sid}/${path}`; const auth = btoa(`${account_sid}:${auth_token}`); const request = { method, headers: { Authorization: `Basic ${auth}`, "Content-Type": "application/x-www-form-urlencoded", }, }; if (method === "POST") { request["body"] = new URLSearchParams(body); } const response = await fetch(url, request); if (!response.ok) { console.log(response.status); console.log(await response.text()); throw new Error("Failed to send request to Twilio"); } return await response.json(); }; /** * Generic function to attempt parsing with proper TypeScript type narrowing */ export function attemptToParse(validator, value) { try { return { kind: "success", data: parse(validator, value), }; } catch (error) { return { kind: "error", error, }; } } //# sourceMappingURL=utils.js.map