import {z} from 'zod' export type GetApiThreadsArgs = {forum_id?: number | undefined, tab?: string | undefined, state?: ('active' | 'closed') | undefined, period?: ('day' | 'week' | 'month' | 'year') | undefined, title?: string | undefined, title_only?: boolean | undefined, creator_user_id?: number | undefined, sticky?: boolean | undefined, 'prefix_ids[]'?: Array | undefined, 'prefix_ids_not[]'?: Array | undefined, thread_tag_id?: number | undefined, page?: number | undefined, limit?: number | undefined, order?: ('post_date' | 'last_post_date' | 'reply_count' | 'reply_count_asc' | 'first_post_likes' | 'vote_count') | undefined, direction?: ('asc' | 'desc') | undefined, thread_create_date?: number | undefined, thread_update_date?: number | undefined, fields_include?: (Array<'*' | 'latest_posts'>) | undefined}; export type GetApiThreadsOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const respPostModel = z.object({avatar_url: z.string(), can_edit: z.boolean(), can_hard_delete: z.boolean(), can_like: z.boolean(), can_reply: z.boolean(), can_report: z.boolean(), can_soft_delete: z.boolean(), can_stick: z.boolean(), can_undelete: z.boolean(), can_unstick: z.boolean(), can_view: z.boolean(), edit_count: z.number().int(), is_banned: z.boolean(), is_first_post: z.boolean(), is_ignored: z.boolean(), is_last_post: z.boolean(), is_like2_node: z.boolean(), is_liked: z.boolean(), is_staff: z.boolean(), is_sticked: z.boolean(), is_unread: z.boolean(), last_edit_date: z.number().int(), last_edit_user_id: z.number().int(), like_date: z.number().int(), likes: z.number().int(), message: z.string(), message_html: z.string(), message_plain_text: z.string(), message_state: z.string(), node_id: z.number().int(), position: z.number().int(), post_date: z.number().int(), post_id: z.number().int(), signature: z.string(), signature_html: z.string(), signature_plain_text: z.string(), sticky_user_id: z.number().int(), thread_id: z.number().int(), thread_is_closed: z.boolean(), thread_is_deleted: z.boolean(), update_date: z.number().int(), user_id: z.number().int(), username: z.string(), username_html: z.string(), view_url: z.string(), warning_id: z.number().int(), warning_message: z.string(), comment_count: z.number().int()}); export const respThreadModel = z.object({allow_ask_hidden_content: z.boolean(), avatar_url: z.string(), bump: z.object({can: z.boolean(), available_count: z.number().int(), error: z.string(), next_available_time: z.number().int()}), can_edit: z.boolean(), can_edit_tags: z.boolean(), can_edit_title: z.boolean(), can_hard_delete: z.boolean(), can_reply: z.boolean(), can_soft_delete: z.boolean(), can_view: z.boolean(), can_watch: z.boolean(), comment_ignore_group: z.boolean(), discussion_open: z.boolean(), discussion_state: z.string(), discussion_type: z.string(), first_post: respPostModel, first_post_id: z.number().int(), first_post_likes: z.number().int(), hide_contacts: z.boolean(), is_bumped: z.boolean(), is_hidden: z.boolean(), is_hot: z.boolean(), is_ignored: z.boolean(), is_like2_node: z.boolean(), is_liked: z.boolean(), is_starred: z.boolean(), is_unread: z.boolean(), is_watched: z.boolean(), last_bump_date: z.number().int(), last_post_date: z.number().int(), last_post_id: z.number().int(), last_post_user_id: z.number().int(), last_post_username: z.string(), node_id: z.number().int(), node_title: z.string(), post_count: z.number().int(), post_date: z.number().int(), prefix_id: z.number().int(), prefix_ids: z.array(z.number().int()), reply_count: z.number().int(), reply_group_id: z.number().int(), review_negative_count: z.number().int(), review_paid_count: z.number().int(), review_positive_count: z.number().int(), review_score: z.number(), review_share: z.number(), review_total_count: z.number().int(), review_unproven_count: z.number().int(), sticky: z.boolean(), tags: z.array(z.string()), thread_id: z.number().int(), title: z.string(), title_en: z.string(), user_id: z.number().int(), username: z.string(), username_html: z.string(), view_count: z.number().int(), view_url: z.string(), visitor_post_count: z.number().int(), contest: z.object({type: z.string(), finish_date: z.number().int(), now_count_members: z.number().int(), needed_members: z.number().int(), is_finished: z.number().int(), count_winners: z.number().int(), require_like_count: z.number().int(), require_total_like_count: z.number().int(), prize_type: z.string(), prize_type_phrase: z.string(), prize_data: z.union([z.record(z.string(), z.number()), z.number()]), prize_cost: z.number().int(), is_money_places: z.number().int(), chance_to_win: z.number(), prize_per_winner: z.number().int().nullable(), prize_places: z.object({place: z.number().int(), amount: z.number().int()}).nullable(), already_participate: z.boolean(), permissions: z.object({can_finish: z.boolean(), can_participate: z.boolean(), can_participate_error: z.string(), can_view_user_list: z.boolean()})})}); export const respSystemInfo = z.object({visitor_id: z.number().int(), time: z.number().int(), log_id: z.number().int()}); export const getApiThreadsResponse = z.object({threads: z.array(respThreadModel), tabTitle: z.string().nullable(), forum: z.object({forum_id: z.number().int(), forum_title: z.string(), forum_description: z.string(), forum_thread_count: z.number().int(), forum_post_count: z.number().int(), forum_prefixes: z.array(z.unknown()), thread_default_prefix_id: z.number().int(), thread_prefix_is_required: z.boolean(), links: z.object({permalink: z.string(), detail: z.string(), "sub-categories": z.string(), "sub-forums": z.string(), threads: z.string(), followers: z.string()}), permissions: z.object({view: z.boolean(), edit: z.boolean(), delete: z.boolean(), create_thread: z.boolean(), upload_attachment: z.boolean(), tag_thread: z.boolean(), follow: z.boolean()}), forum_is_followed: z.boolean()}).optional(), threads_total: z.number().int(), 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 Threads * * GET /threads */ export const getApiThreads = async (args: GetApiThreadsArgs, options?: GetApiThreadsOptions) => { 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.forum_id != null) query.append("forum_id", String(args.forum_id)) if (args.tab != null) query.append("tab", String(args.tab)) if (args.state != null) query.append("state", String(args.state)) if (args.period != null) query.append("period", String(args.period)) if (args.title != null) query.append("title", String(args.title)) if (args.title_only != null) query.append("title_only", String(args.title_only)) if (args.creator_user_id != null) query.append("creator_user_id", String(args.creator_user_id)) if (args.sticky != null) query.append("sticky", String(args.sticky)) for (const item of args['prefix_ids[]'] ?? []) query.append("prefix_ids[]", String(item)) for (const item of args['prefix_ids_not[]'] ?? []) query.append("prefix_ids_not[]", String(item)) if (args.thread_tag_id != null) query.append("thread_tag_id", String(args.thread_tag_id)) if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.order != null) query.append("order", String(args.order)) if (args.direction != null) query.append("direction", String(args.direction)) if (args.thread_create_date != null) query.append("thread_create_date", String(args.thread_create_date)) if (args.thread_update_date != null) query.append("thread_update_date", String(args.thread_update_date)) if (args.fields_include != null) query.append("fields_include", args.fields_include.join(',')) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/threads${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(getApiThreadsResponse, await res.json()) };