import {z} from 'zod' export type ButtonVisibility = 'all' | 'admins' | 'moderators' | 'subscribers'; 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 ButtonOpenMode = 'callback' | 'direct'; export type ButtonUpdateRequest = {text?: ({[key: string]: string} | null) | undefined, icon?: (string | null) | undefined, enabled?: (boolean | null) | undefined, visibility?: (ButtonVisibility | null) | undefined, conditions?: (ButtonConditions | null) | undefined, action?: (string | null) | undefined, dynamic_text?: (boolean | null) | undefined, open_mode?: (ButtonOpenMode | null) | undefined, webapp_url?: (string | null) | undefined, description?: (string | null) | undefined, sort_order?: (number | null) | undefined}; export type PatchApiMenuLayoutButtonsButtonIdArgs = {button_id: string, body: ButtonUpdateRequest}; export type PatchApiMenuLayoutButtonsButtonIdOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const buttonType = z.enum(["builtin", "url", "mini_app", "callback"]); export const buttonVisibility = z.enum(["all", "admins", "moderators", "subscribers"]); 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 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 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 Button * * PATCH /menu-layout/buttons/{button_id} */ export const patchApiMenuLayoutButtonsButtonId = async (args: PatchApiMenuLayoutButtonsButtonIdArgs, options: PatchApiMenuLayoutButtonsButtonIdOptions) => { 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/buttons/${encodeURIComponent(String(args.button_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(menuButtonConfig, await res.json()) };