import {z} from 'zod' export type ScheduledCallbackDestinationPhoneNumber = {type: 'phone_number', phone_number: string}; export type ScheduledCallbackDestinationRelatedCall = {type: 'related_call', call_id: string}; export type ScheduledCallbackDestinationExternalSystem = {type: 'external_system', system: string, account_id: string}; export type CreateApiApiV4TasksScheduledCallbacksArgs = {body: {user_id: number, scheduled_at: string, caller_id_group_id?: (number | null) | undefined, timezone?: (string | null) | undefined, destination: ScheduledCallbackDestinationPhoneNumber | ScheduledCallbackDestinationRelatedCall | ScheduledCallbackDestinationExternalSystem, note?: (string | null) | undefined}}; export type CreateApiApiV4TasksScheduledCallbacksOptions = {baseUrl: string, StaticKey?: string | undefined, fetch?: typeof fetch | undefined}; export const userShort = z.object({id: z.number().int().optional(), name: z.string().optional(), email: z.string().optional()}); export const callerIDGroupShort = z.object({id: z.number().int().optional(), name: z.string().optional()}); export const scheduledCallback = z.object({id: z.number().int().optional(), phone_number: z.string().optional(), scheduled_at: z.string().optional(), user: userShort.optional(), caller_id_group: callerIDGroupShort.optional(), note: z.string().nullable().optional(), related_call: z.object({id: z.string().optional()}).nullable().optional(), source: z.object({system: z.string().optional(), account_id: z.string().optional()}).nullable().optional(), status: z.enum(["pending", "in_progress", "completed", "failed", "in_failover"]).optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const createApiApiV4TasksScheduledCallbacksResponse = z.object({scheduled_callback: scheduledCallback.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 }; /** * Create scheduled callback * * POST /api/v4/tasks/scheduled_callbacks */ export const createApiApiV4TasksScheduledCallbacks = async (args: CreateApiApiV4TasksScheduledCallbacksArgs, options: CreateApiApiV4TasksScheduledCallbacksOptions) => { const {baseUrl, fetch: fetchFn = fetch, StaticKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (StaticKey !== undefined) headers.set('authorization', `Bearer ${StaticKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v4/tasks/scheduled_callbacks${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(createApiApiV4TasksScheduledCallbacksResponse, await res.json()) };