import {z} from 'zod' export type ViewType = 0 | 1; export type GetApiV1ChatMessagesArgs = {DepartmentId?: number | undefined, RoomId?: number | undefined, ChatId?: number | undefined, FromUser?: string | undefined, ContactId?: number | undefined, IsNote?: number | undefined, Content?: string | undefined, FromLogTime?: string | undefined, ToLogTime?: string | undefined, TargetLang?: string | undefined, ViewType?: ViewType | undefined, Page?: number | undefined, ItemsPerPage?: number | undefined, ReturnTotalItems?: boolean | undefined, sortCol?: string | undefined, sortDir?: string | undefined, isExport?: boolean | undefined}; export type GetApiV1ChatMessagesOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const channelType = z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(8), z.literal(10), z.literal(11), z.literal(13), z.literal(14), z.literal(15), z.literal(16)]); export const chatMessageItem = z.object({id: z.string().nullable().optional(), departmentId: z.number().int().optional(), chatId: z.number().int().optional(), roomId: z.number().int().optional(), channel: channelType.optional(), channelName: z.string().nullable().optional(), channelId: z.number().int().nullable().optional(), channelData: z.string().nullable().optional(), externalId: z.string().nullable().optional(), isFromAgent: z.number().int().optional(), aiSourceType: z.number().int().nullable().optional(), fromUser: z.string().nullable().optional(), visitor: z.string().nullable().optional(), agent: z.string().nullable().optional(), photo: z.string().nullable().optional(), content: z.string().nullable().optional(), fileUrls: z.string().nullable().optional(), sourceLang: z.string().nullable().optional(), targetLang: z.string().nullable().optional(), logTime: z.string().optional(), isNote: z.number().int().optional(), isEdited: z.number().int().optional(), isDeleted: z.number().int().optional(), readAt: z.string().nullable().optional(), replyToId: z.string().nullable().optional(), reaction: z.string().nullable().optional(), failedReason: z.string().nullable().optional(), failedCategory: z.string().nullable().optional(), mentionedUserIds: z.string().nullable().optional(), isTranslated: z.number().int().optional(), translatedContent: z.string().nullable().optional(), contactId: z.number().int().optional(), companyId: z.number().int().nullable().optional(), assignedTo: z.number().int().optional(), status: z.string().nullable().optional(), roomStatus: z.string().nullable().optional()}); export const chatMessageItemQueryResult = z.object({totalItems: z.number().int().optional(), items: z.array(chatMessageItem).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 chat messages * * GET /v1/chat/messages * * Also accepts auth: API Key */ export const getApiV1ChatMessages = async (args: GetApiV1ChatMessagesArgs, options: GetApiV1ChatMessagesOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.DepartmentId != null) query.append("DepartmentId", String(args.DepartmentId)) if (args.RoomId != null) query.append("RoomId", String(args.RoomId)) if (args.ChatId != null) query.append("ChatId", String(args.ChatId)) if (args.FromUser != null) query.append("FromUser", String(args.FromUser)) if (args.ContactId != null) query.append("ContactId", String(args.ContactId)) if (args.IsNote != null) query.append("IsNote", String(args.IsNote)) if (args.Content != null) query.append("Content", String(args.Content)) if (args.FromLogTime != null) query.append("FromLogTime", String(args.FromLogTime)) if (args.ToLogTime != null) query.append("ToLogTime", String(args.ToLogTime)) if (args.TargetLang != null) query.append("TargetLang", String(args.TargetLang)) 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/chat/messages${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(chatMessageItemQueryResult, await res.json()) };