import {z} from 'zod' export type DialerModeType = 1 | 2 | 3; export type DataSourceType = 1 | 2; export type DialerModel = {id?: number | undefined, departmentId?: number | undefined, name?: (string | null) | undefined, dialerMode?: DialerModeType | undefined, dataSourceType?: DataSourceType | undefined, dataSource?: (string | null) | undefined, dataParameters?: (string | null) | undefined, dialerType?: number | undefined, dialerTypeParameter?: (string | null) | undefined, outboundDialerPlanNumber?: (string | null) | undefined, callStartNumber?: (string | null) | undefined, callEndNumber?: (string | null) | undefined, startTime?: (string | null) | undefined, lastProcTime?: (string | null) | undefined, workingHourType?: number | undefined, periodType?: number | undefined, periodIndis?: number | undefined, maxRepeatCount?: number | undefined, status?: number | undefined, callWaitTimeSec?: number | undefined, maxRingTimeSec?: number | undefined, autoCallCount?: number | undefined, maxChannel?: number | undefined, wavFile?: (string | null) | undefined, sessionCount?: number | undefined, callCount?: number | undefined, errorCount?: number | undefined, busyCount?: number | undefined, wrongNumberCount?: number | undefined, successfulCount?: number | undefined, sipServerId?: number | undefined}; export type CreateApiV1DialersArgs = {body: DialerModel}; export type CreateApiV1DialersOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const dialerModeType = z.union([z.literal(1), z.literal(2), z.literal(3)]); export const dataSourceType = z.union([z.literal(1), z.literal(2)]); export const dialerModel = z.object({id: z.number().int().optional(), departmentId: z.number().int().optional(), name: z.string().nullable().optional(), dialerMode: dialerModeType.optional(), dataSourceType: dataSourceType.optional(), dataSource: z.string().nullable().optional(), dataParameters: z.string().nullable().optional(), dialerType: z.number().int().optional(), dialerTypeParameter: z.string().nullable().optional(), outboundDialerPlanNumber: z.string().nullable().optional(), callStartNumber: z.string().nullable().optional(), callEndNumber: z.string().nullable().optional(), startTime: z.string().nullable().optional(), lastProcTime: z.string().nullable().optional(), workingHourType: z.number().int().optional(), periodType: z.number().int().optional(), periodIndis: z.number().int().optional(), maxRepeatCount: z.number().int().optional(), status: z.number().int().optional(), callWaitTimeSec: z.number().int().optional(), maxRingTimeSec: z.number().int().optional(), autoCallCount: z.number().int().optional(), maxChannel: z.number().int().optional(), wavFile: z.string().nullable().optional(), sessionCount: z.number().int().optional(), callCount: z.number().int().optional(), errorCount: z.number().int().optional(), busyCount: z.number().int().optional(), wrongNumberCount: z.number().int().optional(), successfulCount: z.number().int().optional(), sipServerId: z.number().int().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 dialer (auto, predictive...) * * POST /v1/dialers * * Also accepts auth: API Key */ export const createApiV1Dialers = async (args: CreateApiV1DialersArgs, options: CreateApiV1DialersOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/dialers${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(dialerModel, await res.json()) };