import {z} from 'zod' export type GetApiApiV4TasksScheduledCallbacksArgs = {offset?: number | undefined, limit?: number | undefined}; export type GetApiApiV4TasksScheduledCallbacksOptions = {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 metadata = z.object({total: z.number().int().optional(), next_cursor: z.string().nullable().optional()}); export const getApiApiV4TasksScheduledCallbacksResponse = z.object({scheduled_callbacks: z.array(scheduledCallback).optional(), metadata: metadata.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 }; /** * List scheduled callbacks * * GET /api/v4/tasks/scheduled_callbacks */ export const getApiApiV4TasksScheduledCallbacks = async (args: GetApiApiV4TasksScheduledCallbacksArgs, options: GetApiApiV4TasksScheduledCallbacksOptions) => { const {baseUrl, fetch: fetchFn = fetch, StaticKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiApiV4TasksScheduledCallbacksResponse, await res.json()) };