import {z} from 'zod' export type ViewType = 0 | 1; export type GetApiV1CallcenterVoicemailsArgs = {Id?: number | undefined, DepartmentId?: number | undefined, ListenStatus?: boolean | undefined, VoipUserId?: number | undefined, ListenUserId?: number | undefined, CallerNumber?: string | undefined, CalledNumber?: string | undefined, Dtmf?: string | undefined, Notes?: string | undefined, FromDate?: string | undefined, ToDate?: string | undefined, SipServerId?: number | undefined, ViewType?: ViewType | undefined, Page?: number | undefined, ItemsPerPage?: number | undefined, ReturnTotalItems?: boolean | undefined, sortCol?: string | undefined, sortDir?: string | undefined, isExport?: boolean | undefined}; export type GetApiV1CallcenterVoicemailsOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const voicemail = z.object({id: z.number().int().optional(), departmentId: z.number().int().optional(), voipUserId: z.number().int().optional(), callCenterQueueId: z.number().int().optional(), contactId: z.number().int().optional(), companyId: z.number().int().optional(), createdDate: z.string().optional(), callDuration: z.number().int().optional(), listenStatus: z.boolean().optional(), callerNumber: z.string().nullable().optional(), calledNumber: z.string().nullable().optional(), dtmf: z.string().nullable().optional(), notes: z.string().nullable().optional(), directory: z.string().nullable().optional(), s3BucketName: z.string().nullable().optional(), s3Key: z.string().nullable().optional(), sipServerId: z.number().int().optional()}); export const voicemailQueryResult = z.object({totalItems: z.number().int().optional(), items: z.array(voicemail).nullable().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 all existing voice mails * * GET /v1/callcenter/voicemails * * Also accepts auth: API Key */ export const getApiV1CallcenterVoicemails = async (args: GetApiV1CallcenterVoicemailsArgs, options: GetApiV1CallcenterVoicemailsOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Id != null) query.append("Id", String(args.Id)) if (args.DepartmentId != null) query.append("DepartmentId", String(args.DepartmentId)) if (args.ListenStatus != null) query.append("ListenStatus", String(args.ListenStatus)) if (args.VoipUserId != null) query.append("VoipUserId", String(args.VoipUserId)) if (args.ListenUserId != null) query.append("ListenUserId", String(args.ListenUserId)) if (args.CallerNumber != null) query.append("CallerNumber", String(args.CallerNumber)) if (args.CalledNumber != null) query.append("CalledNumber", String(args.CalledNumber)) if (args.Dtmf != null) query.append("Dtmf", String(args.Dtmf)) if (args.Notes != null) query.append("Notes", String(args.Notes)) if (args.FromDate != null) query.append("FromDate", String(args.FromDate)) if (args.ToDate != null) query.append("ToDate", String(args.ToDate)) if (args.SipServerId != null) query.append("SipServerId", String(args.SipServerId)) if (args.ViewType != null) query.append("ViewType", String(args.ViewType)) if (args.Page != null) query.append("Page", String(args.Page)) if (args.ItemsPerPage != null) query.append("ItemsPerPage", String(args.ItemsPerPage)) if (args.ReturnTotalItems != null) query.append("ReturnTotalItems", String(args.ReturnTotalItems)) if (args.sortCol != null) query.append("sortCol", String(args.sortCol)) if (args.sortDir != null) query.append("sortDir", String(args.sortDir)) if (args.isExport != null) query.append("isExport", String(args.isExport)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/callcenter/voicemails${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(voicemailQueryResult, await res.json()) };