import {z} from 'zod' export type ItemIDModel = number; export type CreateApiClaimsArgs = {body: {item_id: ItemIDModel, post_body: string}}; export type CreateApiClaimsOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const respSystemInfo = z.object({rate_limit: z.object({limit: z.number().int(), remaining: z.number().int(), reset: z.number().int(), bucket: z.string().optional()}), visitor_id: z.number().int(), time: z.number().int(), log_id: z.number().int()}); export const createApiClaimsResponse = z.object({thread: 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(), thread_create_date: z.number().int(), thread_update_date: z.number().int(), user_is_ignored: z.boolean(), thread_post_count: z.number().int(), thread_is_published: z.boolean(), thread_is_deleted: z.boolean(), thread_is_sticky: z.boolean(), thread_is_followed: z.boolean(), first_post: z.object({post_id: z.number().int(), thread_id: z.number().int(), poster_user_id: z.number().int(), poster_username: z.string(), post_create_date: z.number().int(), post_body: z.string(), post_body_html: z.string(), post_body_plain_text: z.string(), signature: z.string(), signature_html: z.string(), signature_plain_text: z.string(), post_like_count: z.number().int(), post_attachment_count: z.number().int(), like_users: z.array(z.object({user_id: z.number().int(), username: z.string(), display_style_group_id: z.number().int().optional(), is_banned: z.number().int().optional(), uniq_username_css: z.string().optional()})), user_is_ignored: z.boolean(), post_is_published: z.boolean(), post_is_deleted: z.boolean(), post_update_date: z.number().int(), post_is_first_post: z.boolean(), links: z.object({permalink: z.string(), detail: z.string(), thread: z.string(), poster: z.string(), likes: z.string(), report: z.string(), attachments: z.string(), poster_avatar: z.string()}), permissions: z.object({view: z.boolean(), edit: z.boolean(), delete: z.boolean(), reply: z.boolean(), like: z.boolean(), report: z.boolean(), upload_attachment: z.boolean()})}), thread_prefixes: z.array(z.unknown()), thread_tags: z.array(z.unknown()), 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_poster: z.string(), last_post: z.string()}), permissions: z.object({view: z.boolean(), delete: z.boolean(), follow: z.boolean(), post: z.boolean(), upload_attachment: z.boolean(), edit: z.boolean()}), 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.object({group_title: z.string(), group_prefixes: z.array(z.object({prefix_id: z.number().int(), prefix_title: z.string()}))})), 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()})}), 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 }; /** * Create Claim * * POST /claims */ export const createApiClaims = async (args: CreateApiClaimsArgs, options?: CreateApiClaimsOptions) => { const {baseUrl = "https://api.lzt.market", 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(/\/+$/, '')}/claims${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(createApiClaimsResponse, await res.json()) };