import {z} from 'zod' export type UpdateApiConversationsArgs = {body: {conversation_id: number, title?: string | undefined, open_invite?: boolean | undefined, history_open?: boolean | undefined, allow_edit_messages?: boolean | undefined, allow_sticky_messages?: boolean | undefined, allow_delete_own_messages?: boolean | undefined}}; export type UpdateApiConversationsOptions = {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 updateApiConversationsResponse = z.object({conversation: respConversationModel, 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 }; /** * Edit Conversation * * PUT /conversations */ export const updateApiConversations = async (args: UpdateApiConversationsArgs, options?: UpdateApiConversationsOptions) => { const {baseUrl = "https://api.lolz.team", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/conversations${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(updateApiConversationsResponse, await res.json()) };