export type InputRecord = Record export const DEFAULT_ACCOUNT_SID_SECRET = 'twilioAccountSid' export const DEFAULT_AUTH_TOKEN_SECRET = 'twilioAuthToken' export const API_HOST = 'api.twilio.com' export const API_ORIGIN = 'https://api.twilio.com' export const API_ACCOUNT_PREFIX = '/2010-04-01/Accounts/' const ACCOUNT_LABEL_PATTERN = /^[A-Za-z0-9][A-Za-z0-9_-]{0,47}$/ const ACCOUNT_SID_SECRET_PATTERN = /^twilioAccountSid(?:-[A-Za-z0-9][A-Za-z0-9_-]{0,47})?$/ const AUTH_TOKEN_SECRET_PATTERN = /^twilioAuthToken(?:-[A-Za-z0-9][A-Za-z0-9_-]{0,47})?$/ const E164_PATTERN = /^\+[1-9]\d{1,14}$/ const MESSAGING_SERVICE_SID_PATTERN = /^MG[0-9a-fA-F]{32}$/ export function inputRecord(value: unknown): InputRecord { if (value === null || typeof value !== 'object' || Array.isArray(value)) { throw new Error('Input must be an object.') } return value as InputRecord } export function optionalString( input: InputRecord, key: string, ): string | undefined { const value = input[key] if (value === undefined || value === null || value === '') return undefined if (typeof value !== 'string') throw new Error(key + ' must be a string.') return value.trim() } export function requiredString(input: InputRecord, key: string): string { const value = optionalString(input, key) if (!value) throw new Error(key + ' is required.') return value } export function optionalBoolean( input: InputRecord, key: string, ): boolean | undefined { const value = input[key] if (value === undefined || value === null) return undefined if (typeof value !== 'boolean') throw new Error(key + ' must be a boolean.') return value } export function boundedPageSize( value: unknown, fallback = 20, max = 1000, ): number { if (value === undefined || value === null || value === '') return fallback if (!Number.isInteger(value) || Number(value) < 1 || Number(value) > max) { throw new Error( 'pageSize must be an integer from 1 through ' + max + '.', ) } return Number(value) } function accountLabel(value: string | undefined): string | null { const trimmed = (value ?? '').trim() if (!trimmed || trimmed === 'default') return null if (!ACCOUNT_LABEL_PATTERN.test(trimmed)) { throw new Error( 'account must be a short label such as "work" or "live" (letters, numbers, _ or -).', ) } return trimmed } export type TwilioAuthOptions = { /** Extra Twilio account label. `work` reads `twilioAccountSid-work` / `twilioAuthToken-work`. */ account?: string /** Override the Account SID secret name. Must be `twilioAccountSid` or `twilioAccountSid-