import {z} from 'zod' export type UserIDModel = string | number; export type CreateApiSearchArgs = {body: {q?: string | undefined, tag?: string | undefined, forum_id?: number | undefined, user_id?: UserIDModel | undefined, page?: number | undefined, limit?: number | undefined, before?: number | undefined}}; export type CreateApiSearchOptions = {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 respSearchResultThread = z.object({content_type: z.literal("thread"), content_id: z.number().int(), node_title: z.string(), 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(), 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 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 respSearchResultPost = z.object({content_type: z.literal("post"), content_id: z.number().int(), node_title: z.string(), thread_prefixes: z.array(z.object({prefix_id: z.number().int(), css_class: z.string(), prefix_title: z.string()})), thread: respThreadModel, 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 respUserModel = z.object({user_id: z.number().int(), username: z.string(), username_html: z.string(), user_message_count: z.number().int(), user_register_date: z.number().int(), user_like_count: z.number().int(), user_like2_count: z.number().int(), contest_count: z.number().int(), trophy_count: z.number().int(), short_link: z.string(), custom_title: z.string(), is_banned: z.number().int(), display_banner_id: z.number().int(), display_icon_group_id: z.number().int(), balance: z.string(), hold: z.string(), currency: z.string(), user_email: z.string(), user_unread_notification_count: z.number().int(), user_unread_conversation_count: z.number().int(), conv_welcome_message: z.string(), show_dob_dateshow_dob_date: z.number().int().optional(), show_dob_year: z.number().int(), user_title: z.string(), user_deposit: z.number().int(), user_is_valid: z.boolean(), user_is_verified: z.boolean(), user_is_followed: z.boolean(), user_last_seen_date: z.number().int(), links: z.object({permalink: z.string(), detail: z.string(), avatar: z.string(), avatar_big: z.string(), avatar_small: z.string(), followers: z.string(), followings: z.string(), ignore: z.string(), background_l: z.string(), background_m: z.string(), timeline: z.string()}), permissions: z.object({edit: z.boolean(), follow: z.boolean(), create_conversation: z.boolean(), ignore: z.boolean(), profile_post: z.boolean()}), user_is_ignored: z.boolean(), user_is_visitor: z.boolean(), user_group_id: z.number().int(), curator_titles: z.array(z.string()), user_groups: z.array(z.object({user_group_id: z.number().int(), user_group_title: z.string(), user_group_title_en: z.string(), user_group_banner_css_class: z.string(), user_group_banner_text: z.string(), user_group_banner_text_en: z.string(), display_group_selectable: z.boolean(), display_banner_selectable: z.boolean(), display_icon_selectable: z.boolean(), is_primary_group: z.boolean(), user_group_icon_class: z.string()})), fields: z.array(z.object({id: z.string(), title: z.string(), description: z.string(), position: z.string(), is_required: z.boolean(), is_system: z.boolean(), value: z.string().optional(), editable: z.boolean(), is_multi_choice: z.boolean(), choices: z.array(z.object({key: z.string(), value: z.string()})), values: z.array(z.unknown())})), user_timezone_offset: z.number().int(), user_external_authentications: z.array(z.object({provider: z.string(), provider_key: z.string()})), self_permissions: z.object({create_conversation: z.boolean()}), edit_permissions: z.object({password: z.boolean(), user_email: z.boolean(), username: z.boolean(), user_title: z.boolean(), short_link: z.boolean(), hide_username_logs: z.boolean(), primary_group_id: z.boolean(), secondary_group_ids: z.boolean(), user_dob_day: z.boolean(), user_dob_month: z.boolean(), user_dob_year: z.boolean(), fields: z.boolean()}), birthday: z.object({age: z.number().int(), timeStamp: z.object({date: z.string(), timezone_type: z.number().int(), timezone: z.string()}), format: z.string()}).nullable(), secret_answer_rendered: z.string(), secret_answer_first_letter: z.string(), user_following: z.object({users: z.array(z.object({user_id: z.number().int(), username: z.string(), username_html: z.string(), avatar: z.string()})), count: z.number().int()}), user_followers: z.object({users: z.array(z.object({user_id: z.number().int(), username: z.string(), username_html: z.string(), avatar: z.string()})), count: z.number().int()}), profile_threads: z.array(z.object({thread_id: z.number().int(), forum_id: z.number().int(), thread_title: z.string(), thread_view_count: z.number().int(), creator_user_id: z.number().int(), creator_username: z.string(), creator_username_html: z.string(), thread_create_date: z.number().int(), thread_update_date: z.number().int(), thread_reply_group_id: z.number().int(), thread_comment_ignore_group: z.number().int(), thread_last_bump_date: z.number().int(), thread_hide_contacts: z.number().int(), thread_allow_ask_hidden_content: z.number().int(), user_is_ignored: z.boolean(), thread_post_count: z.number().int(), thread_is_bumped: z.boolean(), thread_is_published: z.boolean(), thread_is_deleted: z.boolean(), thread_is_sticky: z.boolean(), thread_is_closed: z.boolean(), thread_is_followed: z.boolean(), thread_is_starred: z.boolean(), thread_prefixes: z.array(z.unknown()), thread_tags: z.object({"38": z.string(), "523": z.string(), "1403": z.string(), "1953": z.string(), "8176": z.string()}), links: z.object({permalink: z.string(), detail: z.string(), followers: z.string(), forum: z.string(), posts: z.string(), first_poster: z.string(), first_poster_avatar: z.string(), first_post: z.string(), last_post: z.string()}), permissions: z.object({view: z.boolean(), delete: z.boolean(), follow: z.boolean(), post: z.boolean(), bump: z.object({can: z.boolean(), available_count: z.number().int(), error: z.unknown(), next_available_time: z.unknown()})}), node_title: z.string()})), banner: z.string()}); export const respSearchResultProfilePost = z.object({content_type: z.literal("profile_post"), content_id: z.number().int(), node_title: z.string(), can_comment: z.boolean(), can_edit: z.boolean(), can_hard_delete: z.boolean(), can_like: z.boolean(), can_report: z.boolean(), can_soft_delete: z.boolean(), can_stick: z.boolean(), can_undelete: z.boolean(), can_view: z.boolean(), comment_count: z.number().int(), disable_comments: z.boolean(), first_comment_date: z.number().int(), is_ignored: z.boolean(), is_liked: z.boolean(), is_sticked: z.boolean(), last_comment_date: 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(), post_date: z.number().int(), profile_post_id: z.number().int(), profile_user: respUserModel, profile_user_id: z.number().int(), sticky_user_id: z.number().int(), user: respUserModel, user_id: z.number().int(), username: z.string(), view_url: z.string(), warning_id: z.number().int(), warning_message: z.string()}); export const respSearchResultProfilePostComment = z.object({content_type: z.literal("profile_post_comment"), content_id: z.number().int(), node_title: z.string(), can_edit: z.boolean(), can_hard_delete: z.boolean(), can_like: z.boolean(), can_report: z.boolean(), can_soft_delete: z.boolean(), can_undelete: z.boolean(), can_view: z.boolean(), comment_date: z.number().int(), is_ignored: z.boolean(), is_liked: z.boolean(), 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(), profile_post_comment_id: z.number().int(), profile_post_id: z.number().int(), user: respUserModel, user_id: z.number().int(), username: z.string(), view_url: z.string(), warning_id: z.number().int(), warning_message: z.string()}); export const respSystemInfo = z.object({visitor_id: z.number().int(), time: z.number().int(), log_id: z.number().int()}); export const createApiSearchResponse = z.object({data: z.array(z.discriminatedUnion("content_type", [respSearchResultThread, respSearchResultPost, respSearchResultProfilePost, respSearchResultProfilePostComment])), data_total: z.number().int(), users: z.array(respUserModel).optional(), 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 }; /** * Search * * POST /search */ export const createApiSearch = async (args: CreateApiSearchArgs, options?: CreateApiSearchOptions) => { 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(/\/+$/, '')}/search${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(createApiSearchResponse, await res.json()) };