import {z} from 'zod' export type RepeatSettings = {repeat_by: 'day' | 'week' | 'month' | 'year', step: number, times?: number | undefined, end_date?: string | undefined, days?: (Array<'Sunday' | 'Monday' | 'Tuesday' | 'Wednesday' | 'Thursday' | 'Friday' | 'Saturday'>) | undefined}; export type ContactMessageRequest = {body?: string | undefined, sender_name?: string | undefined, status_callback?: string | undefined, location_callback?: string | undefined, mms_media?: Array | undefined, schedule_date_utc?: string | undefined, repeat_settings?: RepeatSettings | undefined}; export type CreateApiDashboardsDashboardIdContactsPhoneNumberMessagesArgs = {dashboard_id: number, phone_number: string, body: ContactMessageRequest}; export type CreateApiDashboardsDashboardIdContactsPhoneNumberMessagesOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const message = z.object({message_id: z.string().optional(), body: z.string().optional(), message_direction: z.string().optional(), response_by_username: z.string().optional(), message_timestamp_utc: z.string().optional(), delivery_status: z.enum(["accepted", "queued", "sending", "scheduled", "error", "sent", "failed", "undelivered", "delivered"]).optional(), schedule_date_utc: z.string().optional(), delivery_error: z.string().optional(), mms_media: z.array(z.string()).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Send a message to the contact with the given phone number * * POST /dashboards/{dashboard_id}/contacts/{phone_number}/messages */ export const createApiDashboardsDashboardIdContactsPhoneNumberMessages = async (args: CreateApiDashboardsDashboardIdContactsPhoneNumberMessagesArgs, options?: CreateApiDashboardsDashboardIdContactsPhoneNumberMessagesOptions) => { const {baseUrl = "https://api.textrequest.com/api/v3", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/dashboards/${encodeURIComponent(String(args.dashboard_id))}/contacts/${encodeURIComponent(String(args.phone_number))}/messages${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(message, await res.json()) };