import {z} from 'zod' export type GetApiVpnServersListArgs = {per_page?: number | undefined}; export type GetApiVpnServersListOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiVpnServersListResponse = z.object({success: z.literal(true), vpn_systems: z.object({data: z.array(z.object({id: z.number().int(), server_name: z.string(), ip_address: z.string(), ssh_port: z.number().int(), ssh_username: z.string(), max_connections: z.number().int().nullable(), vpn_type: z.enum(["openVPN", "wireguard", "amneziawg"]), flag_countries: z.string(), status_online: z.union([z.literal(0), z.literal(1)]), is_disabled: z.boolean(), connections_count: z.number().int().gte(0)})), current_page: z.number().int().gte(1), first_page_url: z.string(), from: z.number().int().nullable(), last_page: z.number().int().gte(1), last_page_url: z.string(), next_page_url: z.string().nullable(), path: z.string(), per_page: z.number().int().gte(1).lte(100), prev_page_url: z.string().nullable(), to: z.number().int().nullable(), total: z.number().int().gte(0)}), server_groups: z.array(z.object({id: z.string(), name: z.string(), servers: z.array(z.object({id: z.number().int(), server_name: z.string(), ip_address: z.string(), vpn_type: z.enum(["openVPN", "wireguard", "amneziawg"]), flag_countries: z.string(), status_online: z.boolean(), is_disabled: z.boolean(), connections_count: z.number().int().gte(0)}))}))}); 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 }; /** * List VPN servers * * GET /vpn/servers/list */ export const getApiVpnServersList = async (args: GetApiVpnServersListArgs, options?: GetApiVpnServersListOptions) => { const {baseUrl = "https://demo.onlinebillingform.com/api/v2", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.per_page != null) query.append("per_page", String(args.per_page)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/vpn/servers/list${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(getApiVpnServersListResponse, await res.json()) };