import {z} from 'zod' export type CreateApiProfilesProfileIdTestProxyArgs = {profileId: string}; export type CreateApiProfilesProfileIdTestProxyOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const proxyTestResultStatus = z.enum(["success", "partial_success", "failure"]); export const proxyValidationStep = z.enum(["ssh_connection", "direct_ping", "direct_https", "proxy_ipv4", "proxy_ipv6", "proxy_website", "proxy_location"]); export const proxyTestStepResponse = z.object({step: proxyValidationStep, name: z.string(), successful: z.boolean(), comment: z.string().nullable().optional()}); export const testProxyResponse = z.object({result: proxyTestResultStatus, steps: z.array(proxyTestStepResponse)}); 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 }; /** * Tests the proxy connection configured on the given profile and returns every individual test step together with an overall summary. The * summary is 'success' when every step passes, 'partial_success' when the website could be reached through at least one of IPv4, IPv6 or DNS * name and the IP location lookup also succeeded, otherwise 'failure'. * * POST /profiles/{profileId}/test-proxy */ export const createApiProfilesProfileIdTestProxy = async (args: CreateApiProfilesProfileIdTestProxyArgs, options?: CreateApiProfilesProfileIdTestProxyOptions) => { const {baseUrl = "http://localhost:5050", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/profiles/${encodeURIComponent(String(args.profileId))}/test-proxy${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(testProxyResponse, await res.json()) };