import {z} from 'zod' export type UserSubscriptionCreateRequest = {is_trial?: boolean | undefined, duration_days?: (number | null) | undefined, traffic_limit_gb?: (number | null) | undefined, device_limit?: (number | null) | undefined, squad_uuid?: (string | null) | undefined, connected_squads?: (Array | null) | undefined, replace_existing?: boolean | undefined}; export type CreateApiUsersUserIdSubscriptionArgs = {user_id: number, body: UserSubscriptionCreateRequest}; export type CreateApiUsersUserIdSubscriptionOptions = {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 }; /** * Create User Subscription * * POST /users/{user_id}/subscription */ export const createApiUsersUserIdSubscription = async (args: CreateApiUsersUserIdSubscriptionArgs, options: CreateApiUsersUserIdSubscriptionOptions) => { 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))}/subscription${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(appWebapiSchemasUsersUserResponse, await res.json()) };