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 AddRowRequest = {id: string, buttons: Array, conditions?: (ButtonConditions | null) | undefined, max_per_row?: number | undefined, position?: (number | null) | undefined}; export type CreateApiMenuLayoutRowsArgs = {body: AddRowRequest}; export type CreateApiMenuLayoutRowsOptions = {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 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 }; /** * Add Row * * POST /menu-layout/rows */ export const createApiMenuLayoutRows = async (args: CreateApiMenuLayoutRowsArgs, options: CreateApiMenuLayoutRowsOptions) => { 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/rows${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(menuRowConfig, await res.json()) };