import {z} from 'zod' export type GetApiV2AccountsAccountIdSubscriptionsArgs = {account_id: unknown}; export type GetApiV2AccountsAccountIdSubscriptionsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const accountAddress = z.object({address: z.string(), icon: z.string().optional(), is_scam: z.boolean(), is_wallet: z.boolean(), name: z.string().optional()}); export const metadata = z.object({decryption_key: z.string().optional(), encrypted_binary: z.string()}); export const currencyType = z.enum(["native", "extra_currency", "jetton", "fiat"]); export const trustType = z.enum(["whitelist", "graylist", "blacklist", "none"]); export const price = z.object({currency_type: currencyType, decimals: z.number().int(), image: z.string(), jetton: z.string().optional(), token_name: z.string(), value: z.string(), verification: trustType}); export const subscription = z.object({address: z.string().optional(), admin: accountAddress.optional(), beneficiary: accountAddress.optional(), metadata: metadata, next_charge_at: z.number().int(), payment_per_period: price, period: z.number().int(), status: z.enum(["not_ready", "active", "suspended", "cancelled"]), subscription_id: z.string(), type: z.string(), wallet: accountAddress}); export const subscriptions = z.object({subscriptions: z.array(subscription)}); 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 }; /** * Get all subscriptions by wallet address * * GET /v2/accounts/{account_id}/subscriptions */ export const getApiV2AccountsAccountIdSubscriptions = async (args: GetApiV2AccountsAccountIdSubscriptionsArgs, options?: GetApiV2AccountsAccountIdSubscriptionsOptions) => { const {baseUrl = "https://tonapi.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v2/accounts/${encodeURIComponent(String(args.account_id))}/subscriptions${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(subscriptions, await res.json()) };