import {z} from 'zod' export type ChannelType = 1 | 2 | 3 | 4 | 5 | 8 | 10 | 11 | 13 | 14 | 15 | 16; export type ViewType = 0 | 1; export type GetApiV1ChatWidgetHistoriesArgs = {ApiKey?: string | undefined, VisitorId?: string | undefined, Email?: string | undefined, Phone?: string | undefined, UserHash?: string | undefined, RoomTokens?: string | undefined, Channel?: ChannelType | undefined, ViewType?: ViewType | undefined, Page?: number | undefined, ItemsPerPage?: number | undefined, ReturnTotalItems?: boolean | undefined}; export type GetApiV1ChatWidgetHistoriesOptions = {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 chatHistoryResponse = z.object({statusCode: z.number().int().nullable().optional(), success: z.boolean().optional(), message: z.string().nullable().optional(), resource: chatMessageItem.optional()}); export const chatHistoryResponseQueryResult = z.object({totalItems: z.number().int().optional(), items: z.array(chatHistoryResponse).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 }; /** * Lists all chat histories * * GET /v1/chat/widget/histories * * Also accepts auth: API Key */ export const getApiV1ChatWidgetHistories = async (args: GetApiV1ChatWidgetHistoriesArgs, options: GetApiV1ChatWidgetHistoriesOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.ApiKey != null) query.append("ApiKey", String(args.ApiKey)) if (args.VisitorId != null) query.append("VisitorId", String(args.VisitorId)) if (args.Email != null) query.append("Email", String(args.Email)) if (args.Phone != null) query.append("Phone", String(args.Phone)) if (args.UserHash != null) query.append("UserHash", String(args.UserHash)) if (args.RoomTokens != null) query.append("RoomTokens", String(args.RoomTokens)) if (args.Channel != null) query.append("Channel", String(args.Channel)) 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 (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/chat/widget/histories${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(chatHistoryResponseQueryResult, await res.json()) };