import {z} from 'zod' export type GetApiConversationsArgs = {folder?: ('all' | 'unread' | 'groups' | 'market' | 'market_replacements' | 'staff' | 'giveaways' | 'p2p') | undefined, page?: number | undefined, limit?: number | undefined}; export type GetApiConversationsOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const respConversationModel = z.object({conversation_id: z.number().int(), conversation_title: z.string(), creator_user_id: z.number().int(), creator_username: z.string(), creator_username_html: z.string(), conversation_create_date: z.number().int(), conversation_update_date: z.number().int(), conversation_last_read_date: z.number().int(), conversation_online_count: z.number().int(), is_starred: z.number().int(), is_group: z.number().int(), is_unread: z.number().int(), alerts: z.number().int(), permissions: z.object({view: z.boolean(), reply: z.boolean(), invite: z.boolean(), manage_invite_links: z.boolean(), kick: z.boolean(), upload_avatar: z.boolean(), editOwnPost: z.boolean(), stickyMessages: z.boolean()}), conversation_message_count: z.number().int(), conversation_is_new: z.boolean(), creator_is_ignored: z.boolean(), conversation_is_open: z.boolean(), conversation_is_deleted: z.boolean(), recipient: z.object({user_id: z.number().int(), username: z.string(), username_html: z.string(), last_activity: z.number().int(), is_online: z.boolean(), contacts_changed: z.boolean(), avatar: z.string()}), recipients: z.array(z.object({user_id: z.number().int(), username: z.string(), username_html: z.string(), last_activity: z.number().int(), is_online: z.boolean(), contacts_changed: z.boolean(), avatar: z.string()})), links: z.object({permalink: z.string(), detail: z.string(), messages: z.string(), avatar: z.string()})}); export const respSystemInfo = z.object({visitor_id: z.number().int(), time: z.number().int(), log_id: z.number().int()}); export const getApiConversationsResponse = z.object({conversations: z.array(respConversationModel), can_start: z.boolean(), folders: z.array(z.object({id: z.string(), title: z.string(), name: z.string()})), links: z.object({pages: z.number().int(), page: z.number().int(), next: z.string()}), system_info: respSystemInfo}); 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 Conversations * * GET /conversations */ export const getApiConversations = async (args: GetApiConversationsArgs, options?: GetApiConversationsOptions) => { const {baseUrl = "https://api.lolz.team", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.folder != null) query.append("folder", String(args.folder)) if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) 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(getApiConversationsResponse, await res.json()) };