import {z} from 'zod' export type GetApiMessengerV1MessagesIdGoToMessageArgs = {id: string, limit?: number | undefined}; export type GetApiMessengerV1MessagesIdGoToMessageOptions = {baseUrl: string, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const externalLineResponseDto = z.object({id: z.string(), name: z.string(), icon: z.string(), externalId: z.string(), timestamp: z.number(), portal: z.string(), phoneNumber: z.string()}); export const attachedFile = z.object({id: z.object({}), entityId: z.string(), entityType: z.string(), lastModified: z.number(), originalFilename: z.string(), size: z.string(), url: z.string()}); export const messageRelation = z.object({action: z.enum(["create", "update", "delete"]), service: z.literal("crm"), entity: z.enum(["lead", "contact", "deal"]), name: z.string(), pictureUrl: z.string(), entityId: z.string()}); export const reference = z.object({chatId: z.string(), authorId: z.number(), messageId: z.string()}); export const metaBody = z.object({text: z.string(), attachedFiles: z.array(z.string()), url: z.string()}); export const messageMeta = z.object({entity: z.string(), type: z.string(), accountName: z.string(), accountImage: z.string(), body: z.array(metaBody)}); export const messageResponseDto = z.object({id: z.string().optional(), timestamp: z.number().optional(), message: z.string(), type: z.enum(["MESSAGE", "AUDIO", "VIDEO", "GIF", "SYSTEM"]).optional(), chatId: z.string(), readBy: z.array(z.number()).optional(), externalLine: externalLineResponseDto, attachedFiles: z.array(attachedFile).optional(), relations: z.array(messageRelation).optional(), externalAuthorId: z.string().optional(), externalId: z.string().optional(), ref: reference.optional(), meta: messageMeta.optional()}); export const getApiMessengerV1MessagesIdGoToMessageResponse = z.array(messageResponseDto); 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 }; /** * Navigate to a specific message * * GET /messenger/v1/messages/{id}/goToMessage */ export const getApiMessengerV1MessagesIdGoToMessage = async (args: GetApiMessengerV1MessagesIdGoToMessageArgs, options: GetApiMessengerV1MessagesIdGoToMessageOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearer} = 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 (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/messenger/v1/messages/${encodeURIComponent(String(args.id))}/goToMessage${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(getApiMessengerV1MessagesIdGoToMessageResponse, await res.json()) };