import {z} from 'zod' export type ButtonConditions = {has_active_subscription?: (boolean | null) | undefined, subscription_is_active?: (boolean | null) | undefined, has_traffic_limit?: (boolean | null) | undefined, is_admin?: (boolean | null) | undefined, is_moderator?: (boolean | null) | undefined, referral_enabled?: (boolean | null) | undefined, contests_visible?: (boolean | null) | undefined, support_enabled?: (boolean | null) | undefined, language_selection_enabled?: (boolean | null) | undefined, happ_enabled?: (boolean | null) | undefined, simple_subscription_enabled?: (boolean | null) | undefined, show_trial?: (boolean | null) | undefined, show_buy?: (boolean | null) | undefined, has_saved_cart?: (boolean | null) | undefined, min_balance_kopeks?: (number | null) | undefined, max_balance_kopeks?: (number | null) | undefined, min_registration_days?: (number | null) | undefined, max_registration_days?: (number | null) | undefined, min_referrals?: (number | null) | undefined, has_referrals?: (boolean | null) | undefined, promo_group_ids?: (Array | null) | undefined, exclude_promo_group_ids?: (Array | null) | undefined, has_subscription_days_left?: (number | null) | undefined, max_subscription_days_left?: (number | null) | undefined, is_trial_user?: (boolean | null) | undefined, has_autopay?: (boolean | null) | undefined}; export type MenuRowConfig = {id: string, buttons: Array, conditions?: (ButtonConditions | null) | undefined, max_per_row?: number | undefined}; export type ButtonType = 'builtin' | 'url' | 'mini_app' | 'callback'; export type ButtonVisibility = 'all' | 'admins' | 'moderators' | 'subscribers'; export type ButtonOpenMode = 'callback' | 'direct'; export type MenuButtonConfig = {type: ButtonType, builtin_id?: (string | null) | undefined, text: {[key: string]: string}, icon?: (string | null) | undefined, action: string, enabled?: boolean | undefined, visibility?: ButtonVisibility | undefined, conditions?: (ButtonConditions | null) | undefined, dynamic_text?: boolean | undefined, open_mode?: ButtonOpenMode | undefined, webapp_url?: (string | null) | undefined, description?: (string | null) | undefined, sort_order?: (number | null) | undefined}; export type MenuLayoutValidateRequest = {rows?: (Array | null) | undefined, buttons?: ({[key: string]: MenuButtonConfig} | null) | undefined}; export type CreateApiMenuLayoutValidateArgs = {body: MenuLayoutValidateRequest}; export type CreateApiMenuLayoutValidateOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const validationError = z.object({loc: z.array(z.union([z.string(), z.number().int()])), msg: z.string(), type: z.string(), input: z.unknown(), ctx: z.object({}).optional()}); export const menuLayoutValidateResponse = z.object({is_valid: z.boolean(), errors: z.array(validationError).optional(), warnings: z.array(validationError).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 }; /** * Validate Menu Layout * * POST /menu-layout/validate */ export const createApiMenuLayoutValidate = async (args: CreateApiMenuLayoutValidateArgs, options: CreateApiMenuLayoutValidateOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("X-API-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/menu-layout/validate${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(menuLayoutValidateResponse, await res.json()) };