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 MenuLayoutUpdateRequest = {rows?: (Array | null) | undefined, buttons?: ({[key: string]: MenuButtonConfig} | null) | undefined}; export type UpdateApiMenuLayoutArgs = {body: MenuLayoutUpdateRequest}; export type UpdateApiMenuLayoutOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const buttonConditions = z.object({has_active_subscription: z.boolean().nullable().optional(), subscription_is_active: z.boolean().nullable().optional(), has_traffic_limit: z.boolean().nullable().optional(), is_admin: z.boolean().nullable().optional(), is_moderator: z.boolean().nullable().optional(), referral_enabled: z.boolean().nullable().optional(), contests_visible: z.boolean().nullable().optional(), support_enabled: z.boolean().nullable().optional(), language_selection_enabled: z.boolean().nullable().optional(), happ_enabled: z.boolean().nullable().optional(), simple_subscription_enabled: z.boolean().nullable().optional(), show_trial: z.boolean().nullable().optional(), show_buy: z.boolean().nullable().optional(), has_saved_cart: z.boolean().nullable().optional(), min_balance_kopeks: z.number().int().gte(0).nullable().optional(), max_balance_kopeks: z.number().int().gte(0).nullable().optional(), min_registration_days: z.number().int().gte(0).nullable().optional(), max_registration_days: z.number().int().gte(0).nullable().optional(), min_referrals: z.number().int().gte(0).nullable().optional(), has_referrals: z.boolean().nullable().optional(), promo_group_ids: z.array(z.string()).nullable().optional(), exclude_promo_group_ids: z.array(z.string()).nullable().optional(), has_subscription_days_left: z.number().int().gte(0).nullable().optional(), max_subscription_days_left: z.number().int().gte(0).nullable().optional(), is_trial_user: z.boolean().nullable().optional(), has_autopay: z.boolean().nullable().optional()}); export const menuRowConfig = z.object({id: z.string().min(1).max(50), buttons: z.array(z.string()), conditions: buttonConditions.nullable().optional(), max_per_row: z.number().int().gte(1).lte(4).optional()}); export const buttonType = z.enum(["builtin", "url", "mini_app", "callback"]); export const buttonVisibility = z.enum(["all", "admins", "moderators", "subscribers"]); export const buttonOpenMode = z.enum(["callback", "direct"]); export const menuButtonConfig = z.object({type: buttonType, builtin_id: z.string().nullable().optional(), text: z.record(z.string(), z.string()), icon: z.string().max(10).nullable().optional(), action: z.string(), enabled: z.boolean().optional(), visibility: buttonVisibility.optional(), conditions: buttonConditions.nullable().optional(), dynamic_text: z.boolean().optional(), open_mode: buttonOpenMode.optional(), webapp_url: z.string().nullable().optional(), description: z.string().max(200).nullable().optional(), sort_order: z.number().int().nullable().optional()}); export const menuLayoutResponse = z.object({version: z.number().int(), rows: z.array(menuRowConfig), buttons: z.record(z.string(), menuButtonConfig), is_enabled: z.boolean(), updated_at: z.string().nullable().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 }; /** * Update Menu Layout * * PUT /menu-layout */ export const updateApiMenuLayout = async (args: UpdateApiMenuLayoutArgs, options: UpdateApiMenuLayoutOptions) => { 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${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(menuLayoutResponse, await res.json()) };