import {z} from 'zod' export type UserGroupId = 'Nothing' | 'Limited' | 'Regular' | 'Trusted' | 'Moderator' | 'Admin'; export type UserLanguageProficiency = 'Nothing' | 'Basics' | 'Intermediate' | 'Advanced' | 'Native'; export type UserKnownLanguageContract = {cultureCode?: (string | null) | undefined, proficiency?: UserLanguageProficiency | undefined}; export type EntryThumbForApiContract = {mime?: (string | null) | undefined, name?: (string | null) | undefined, urlOriginal?: (string | null) | undefined, urlSmallThumb?: (string | null) | undefined, urlThumb?: (string | null) | undefined, urlTinyThumb?: (string | null) | undefined}; export type OldUsernameContract = {date?: string | undefined, oldName?: (string | null) | undefined}; export type UserForApiContract = {active?: boolean | undefined, groupId?: UserGroupId | undefined, knownLanguages?: (Array | null) | undefined, mainPicture?: EntryThumbForApiContract | undefined, memberSince?: string | undefined, oldUsernames?: (Array | null) | undefined, verifiedArtist?: boolean | undefined, id?: number | undefined, name?: (string | null) | undefined}; export type DiscussionFolderContract = {description?: (string | null) | undefined, id?: number | undefined, lastTopicAuthor?: UserForApiContract | undefined, lastTopicDate?: (string | null) | undefined, name?: (string | null) | undefined, topicCount?: number | undefined}; export type CreateApiApiDiscussionsFoldersArgs = {body: DiscussionFolderContract}; export type CreateApiApiDiscussionsFoldersOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const userGroupId = z.enum(["Nothing", "Limited", "Regular", "Trusted", "Moderator", "Admin"]); export const userLanguageProficiency = z.enum(["Nothing", "Basics", "Intermediate", "Advanced", "Native"]); export const userKnownLanguageContract = z.object({cultureCode: z.string().nullable().optional(), proficiency: userLanguageProficiency.optional()}); export const entryThumbForApiContract = z.object({mime: z.string().nullable().optional(), name: z.string().nullable().optional(), urlOriginal: z.string().nullable().optional(), urlSmallThumb: z.string().nullable().optional(), urlThumb: z.string().nullable().optional(), urlTinyThumb: z.string().nullable().optional()}); export const oldUsernameContract = z.object({date: z.string().optional(), oldName: z.string().nullable().optional()}); export const userForApiContract = z.object({active: z.boolean().optional(), groupId: userGroupId.optional(), knownLanguages: z.array(userKnownLanguageContract).nullable().optional(), mainPicture: entryThumbForApiContract.optional(), memberSince: z.string().optional(), oldUsernames: z.array(oldUsernameContract).nullable().optional(), verifiedArtist: z.boolean().optional(), id: z.number().int().optional(), name: z.string().nullable().optional()}); export const discussionFolderContract = z.object({description: z.string().nullable().optional(), id: z.number().int().optional(), lastTopicAuthor: userForApiContract.optional(), lastTopicDate: z.string().nullable().optional(), name: z.string().nullable().optional(), topicCount: z.number().int().optional()}); 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 }; /** * POST /api/discussions/folders */ export const createApiApiDiscussionsFolders = async (args: CreateApiApiDiscussionsFoldersArgs, options: CreateApiApiDiscussionsFoldersOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/discussions/folders${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(discussionFolderContract, await res.json()) };