import {z} from 'zod' export type GetApiBetaAgentsArgs = {pageLimit?: number | undefined, pageKey?: string | undefined, email?: string | undefined, phone?: string | undefined}; export type GetApiBetaAgentsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; 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 paginationLinks = z.object({previous: z.string().optional(), next: z.string().optional()}); export const listAgentsOutput = z.object({data: z.array(agent).optional(), meta: paginationLinks.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 agents * * GET /beta/agents */ export const getApiBetaAgents = async (args: GetApiBetaAgentsArgs, options?: GetApiBetaAgentsOptions) => { const {baseUrl = "https://dev.dixa.io", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.pageLimit != null) query.append("pageLimit", String(args.pageLimit)) if (args.pageKey != null) query.append("pageKey", String(args.pageKey)) if (args.email != null) query.append("email", String(args.email)) if (args.phone != null) query.append("phone", String(args.phone)) if (ApiKeyAuth !== undefined) headers.set("Authorization", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/beta/agents${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(listAgentsOutput, await res.json()) };