import {z} from 'zod' export type GetApiV1ExternalMessagesHistoryBotIdArgs = {botId: string, limit?: number | undefined, page?: number | undefined, startAt?: string | undefined, endAt?: string | undefined, clientId?: string | undefined, messageId?: string | undefined}; export type GetApiV1ExternalMessagesHistoryBotIdOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const pagination = z.object({limit: z.number().int().optional(), page: z.number().int().optional(), total: z.number().int().optional(), offset: z.number().int().optional(), totalPages: z.number().int().optional()}); export const chatMessage = z.object({recipient: z.object({id: z.string().optional(), names: z.string().optional()}).optional(), sender: z.object({id: z.string().optional(), name: z.string().optional(), type: z.string().optional()}).optional(), by: z.enum(["bot", "user"]).optional(), messageId: z.string().optional(), status: z.string().optional(), bubble: z.object({type: z.string().optional(), text: z.string().optional(), mediaUrl: z.string().optional()}).optional(), createdAt: z.string().optional()}); export const chatHistoryResponse = z.object({pagination: pagination.optional(), _metadata: z.object({}).optional(), results: z.array(chatMessage).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 }; /** * Get Chat History * * GET /v1/external/messages/history/{botId} */ export const getApiV1ExternalMessagesHistoryBotId = async (args: GetApiV1ExternalMessagesHistoryBotIdArgs, options?: GetApiV1ExternalMessagesHistoryBotIdOptions) => { const {baseUrl = "https://api.jelou.ai", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) if (args.startAt != null) query.append("startAt", String(args.startAt)) if (args.endAt != null) query.append("endAt", String(args.endAt)) if (args.clientId != null) query.append("clientId", String(args.clientId)) if (args.messageId != null) query.append("messageId", String(args.messageId)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/external/messages/history/${encodeURIComponent(String(args.botId))}${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(chatHistoryResponse, await res.json()) };