import {z} from 'zod' export type GetApiConversationsArgs = {page_token?: string | undefined, limit?: number | undefined, updated_from?: string | undefined, entity_type?: 'ORDER' | undefined, entity_ids?: Array | undefined, conversation_ids?: Array | undefined}; export type GetApiConversationsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const conversationOrigin = z.object({channel_id: z.string(), channel_name: z.string(), channel_store_id: z.string(), channel_store_name: z.string()}); export const conversationParticipant = z.object({id: z.string().optional(), name: z.string(), type: z.enum(["CHANNEL", "CUSTOMER"])}); export const conversationResponse = z.object({anonymized_at: z.string().optional(), channel_conversation_id: z.string(), created_at: z.string(), entity: z.object({channel_entity_id: z.string(), id: z.string().optional(), type: z.literal("ORDER")}), id: z.string(), origin: conversationOrigin, participants: z.array(conversationParticipant), topic: z.object({type: z.literal("FREE_TEXT"), value: z.string().max(500)}), updated_at: z.string()}); export const conversationsPageResponse = z.object({next_page_token: z.string().min(1).optional(), previous_page_token: z.string().min(1).optional(), data: z.array(conversationResponse)}); 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 }; /** * listConversations - List all order conversations * * GET /conversations */ export const getApiConversations = async (args: GetApiConversationsArgs, options?: GetApiConversationsOptions) => { const {baseUrl = "https://miraklconnect.com/api", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page_token != null) query.append("page_token", String(args.page_token)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.updated_from != null) query.append("updated_from", String(args.updated_from)) if (args.entity_type != null) query.append("entity_type", String(args.entity_type)) for (const item of args.entity_ids ?? []) query.append("entity_ids", String(item)) for (const item of args.conversation_ids ?? []) query.append("conversation_ids", String(item)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/conversations${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(conversationsPageResponse, await res.json()) };