import {z} from 'zod' export type GetApiChatsArgs = {'filter[external_id]'?: string | undefined, 'filter[type]'?: ('chat' | 'email') | undefined, 'filter[title]'?: string | undefined, 'filter[participant_name]'?: string | undefined, 'filter[scorecard_id]'?: Array | undefined, 'filter[user_id]'?: Array | undefined, 'filter[team_id]'?: Array | undefined, from_date_time?: string | undefined, to_date_time?: string | undefined, exclude_transcript?: boolean | undefined, page?: number | undefined, size?: number | undefined}; export type GetApiChatsOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const chatParticipant = z.object({externalId: z.string().optional(), name: z.string().optional(), email: z.string().optional(), participantType: z.enum(["prospect", "user", "bot"]).optional(), userUuid: z.string().optional()}); export const chatTranscriptItem = z.object({sentence: z.string().optional(), author: z.string().optional(), authorEmail: z.string().optional(), datetime: z.string().optional()}); export const chat = z.object({id: z.string().optional(), externalId: z.string().optional(), applicationName: z.string().optional(), title: z.string().optional(), type: z.enum(["chat", "email"]).optional(), userId: z.string().optional(), teamId: z.string().optional(), participants: z.array(chatParticipant).optional(), transcript: z.array(chatTranscriptItem).optional(), dateStart: z.string().optional(), createdAt: z.string().optional()}); export const meta = z.object({pageCount: z.number().int().optional(), totalRecords: z.number().int().optional(), pageNumber: z.number().int().optional(), pageSize: z.number().int().optional()}); export const listChatsResponse = z.object({data: z.array(chat).optional(), meta: meta.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 Chats * * GET /chats */ export const getApiChats = async (args: GetApiChatsArgs, options?: GetApiChatsOptions) => { const {baseUrl = "https://api.attention.tech/v2", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['filter[external_id]'] != null) query.append("filter[external_id]", String(args['filter[external_id]'])) if (args['filter[type]'] != null) query.append("filter[type]", String(args['filter[type]'])) if (args['filter[title]'] != null) query.append("filter[title]", String(args['filter[title]'])) if (args['filter[participant_name]'] != null) query.append("filter[participant_name]", String(args['filter[participant_name]'])) if (args['filter[scorecard_id]'] != null) query.append("filter[scorecard_id]", args['filter[scorecard_id]'].join(',')) if (args['filter[user_id]'] != null) query.append("filter[user_id]", args['filter[user_id]'].join(',')) if (args['filter[team_id]'] != null) query.append("filter[team_id]", args['filter[team_id]'].join(',')) if (args.from_date_time != null) query.append("from_date_time", String(args.from_date_time)) if (args.to_date_time != null) query.append("to_date_time", String(args.to_date_time)) if (args.exclude_transcript != null) query.append("exclude_transcript", String(args.exclude_transcript)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (APIKeyHeader !== undefined) headers.set("Authorization", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/chats${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(listChatsResponse, await res.json()) };