import {z} from 'zod' export type CreateAgentInput = {displayName: string, email: string, phoneNumber?: string | undefined, additionalEmails?: Array | undefined, additionalPhoneNumbers?: Array | undefined, firstName?: string | undefined, lastName?: string | undefined, middleNames?: Array | undefined, avatarUrl?: string | undefined}; export type CreateAgentsInput = {data?: Array | undefined}; export type CreateApiBetaAgentsBulkArgs = {body: CreateAgentsInput}; export type CreateApiBetaAgentsBulkOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const emptyPatchSet = z.object({id: z.string(), message: z.string()}); export const errorResponse = z.object({message: z.string()}); export const infrastructure = z.object({id: z.string(), message: z.string()}); export const integrity = z.object({id: z.string(), message: z.string()}); export const invalidArgument = z.object({message: z.string()}); export const notFound1 = z.object({id: z.string(), message: z.string()}); export const validation = z.object({id: z.string().optional(), message: z.string()}); export const bulkError = z.union([emptyPatchSet, errorResponse, infrastructure, integrity, invalidArgument, notFound1, validation]); export const bulkActionFailureBulkErrorAgent = z.object({error: bulkError}); export const agent = z.object({id: z.string(), createdAt: z.string(), displayName: z.string(), email: z.string(), avatarUrl: z.string().optional(), phoneNumber: z.string().optional(), additionalEmails: z.array(z.string()).optional(), additionalPhoneNumbers: z.array(z.string()).optional(), firstName: z.string().optional(), lastName: z.string().optional(), middleNames: z.array(z.string()).optional(), roles: z.array(z.string()).optional()}); export const bulkActionSuccessBulkErrorAgent = z.object({data: agent}); export const bulkActionOutcomeBulkErrorAgent = z.union([bulkActionFailureBulkErrorAgent, bulkActionSuccessBulkErrorAgent]); export const createAgentsOutput = z.object({data: z.array(bulkActionOutcomeBulkErrorAgent).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 agents * * POST /beta/agents/bulk */ export const createApiBetaAgentsBulk = async (args: CreateApiBetaAgentsBulkArgs, options?: CreateApiBetaAgentsBulkOptions) => { const {baseUrl = "https://dev.dixa.io", 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("Authorization", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/beta/agents/bulk${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(createAgentsOutput, await res.json()) };