import {z} from 'zod' export type UserUpdateRequest = {username?: (string | null) | undefined, first_name?: (string | null) | undefined, last_name?: (string | null) | undefined, language?: (string | null) | undefined, status?: (string | null) | undefined, promo_group_id?: (number | null) | undefined, referral_code?: (string | null) | undefined, has_had_paid_subscription?: (boolean | null) | undefined, has_made_first_topup?: (boolean | null) | undefined}; export type PatchApiUsersUserIdArgs = {user_id: number, body: UserUpdateRequest}; export type PatchApiUsersUserIdOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const promoGroupSummary = z.object({id: z.number().int(), name: z.string(), server_discount_percent: z.number().int(), traffic_discount_percent: z.number().int(), device_discount_percent: z.number().int(), apply_discounts_to_addons: z.boolean().optional()}); export const subscriptionSummary = z.object({id: z.number().int(), status: z.string(), actual_status: z.string(), is_trial: z.boolean(), start_date: z.string(), end_date: z.string(), traffic_limit_gb: z.number().int(), traffic_used_gb: z.number(), device_limit: z.number().int(), autopay_enabled: z.boolean(), autopay_days_before: z.number().int().nullable().optional(), subscription_url: z.string().nullable().optional(), subscription_crypto_link: z.string().nullable().optional(), connected_squads: z.array(z.string()).optional()}); export const appWebapiSchemasUsersUserResponse = z.object({id: z.number().int(), telegram_id: z.number().int().nullable().optional(), username: z.string().nullable().optional(), first_name: z.string().nullable().optional(), last_name: z.string().nullable().optional(), status: z.string(), language: z.string(), balance_kopeks: z.number().int(), balance_rubles: z.number(), referral_code: z.string().nullable().optional(), referred_by_id: z.number().int().nullable().optional(), has_had_paid_subscription: z.boolean(), has_made_first_topup: z.boolean(), created_at: z.string(), updated_at: z.string(), last_activity: z.string().nullable().optional(), promo_group: promoGroupSummary.nullable().optional(), subscription: subscriptionSummary.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 User Endpoint * * PATCH /users/{user_id} */ export const patchApiUsersUserId = async (args: PatchApiUsersUserIdArgs, options: PatchApiUsersUserIdOptions) => { 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(/\/+$/, '')}/users/${encodeURIComponent(String(args.user_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(appWebapiSchemasUsersUserResponse, await res.json()) };