import {z} from 'zod' export type GetApiApiImConversationsArgs = {withUnread?: boolean | undefined, unreadOnly?: boolean | undefined}; export type GetApiApiImConversationsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const iMConversation = z.object({id: z.string().optional(), type: z.enum(["direct", "group", "channel"]).optional(), title: z.string().optional(), description: z.string().optional(), status: z.enum(["active", "archived", "deleted"]).optional(), lastMessageAt: z.string().optional(), createdAt: z.string().optional(), unreadCount: z.number().int().optional()}); export const getApiApiImConversationsResponse = z.object({ok: z.boolean().optional(), data: z.array(iMConversation).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 conversations * * GET /api/im/conversations * * Also accepts auth: apiKey */ export const getApiApiImConversations = async (args: GetApiApiImConversationsArgs, options?: GetApiApiImConversationsOptions) => { const {baseUrl = "https://prismer.cloud", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.withUnread != null) query.append("withUnread", String(args.withUnread)) if (args.unreadOnly != null) query.append("unreadOnly", String(args.unreadOnly)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/im/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(getApiApiImConversationsResponse, await res.json()) };