import {z} from 'zod' export type GetApiPostsCommentsArgs = {post_id?: number | undefined, post_comment_id?: number | undefined, before?: number | undefined, before_comment?: number | undefined}; export type GetApiPostsCommentsOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const respPostCommentModel = 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_undelete: z.boolean(), can_view: z.boolean(), comment_date: z.number().int(), edit_count: z.number().int(), is_banned: z.boolean(), is_ignored: z.boolean(), is_like2_node: z.boolean(), is_liked: 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(), post_comment_id: z.number().int(), post_id: z.number().int(), signature: z.string(), signature_html: z.string(), signature_plain_text: z.string(), thread_id: z.number().int(), 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()}); export const respSystemInfo = z.object({visitor_id: z.number().int(), time: z.number().int(), log_id: z.number().int()}); export const getApiPostsCommentsResponse = z.object({comments: z.array(respPostCommentModel), 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 Post Comments * * GET /posts/comments */ export const getApiPostsComments = async (args: GetApiPostsCommentsArgs, options?: GetApiPostsCommentsOptions) => { 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.post_id != null) query.append("post_id", String(args.post_id)) if (args.post_comment_id != null) query.append("post_comment_id", String(args.post_comment_id)) if (args.before != null) query.append("before", String(args.before)) if (args.before_comment != null) query.append("before_comment", String(args.before_comment)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/posts/comments${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(getApiPostsCommentsResponse, await res.json()) };