import {z} from 'zod' export type ProxyTestRunCreateRequest = {proxies?: (Array<{proxy_id?: string | undefined, proxy_string: string | string | string | string | string}>) | undefined, proxy_tester_server_id?: Array | undefined, urls?: Array | undefined}; export type CreateApiPublicUserProxyTestRunCreateArgs = {body: ProxyTestRunCreateRequest}; export type CreateApiPublicUserProxyTestRunCreateOptions = {baseUrl?: string | undefined, APIPrivateKey?: string | undefined, APIPublicKey?: string | undefined, fetch?: typeof fetch | undefined}; export const proxyTestRunCreateResponse = z.object({created: z.array(z.string()).optional(), data: z.record(z.string(), z.object({results: z.array(z.object({proxy_test_result_id: z.string().optional(), proxy_test_run_id: z.string().optional(), proxy_tester_proxy_id: z.string().optional(), proxy_test_result_url: z.string().optional(), proxy_test_result_is_successful: z.boolean().optional(), proxy_test_result_error_code: z.string().optional(), proxy_test_result_error_source: z.string().optional(), proxy_test_result_protocol: z.string().optional(), proxy_test_result_response_time: z.number().int().optional(), proxy_test_server_id: z.string().optional(), proxy_test_server_city_id: z.number().int().optional(), proxy_test_server_city_name: z.string().optional(), proxy_test_server_country_id: z.string().optional(), proxy_test_result_creation_datetime: z.string().optional(), proxy_test_result_last_update_datetime: z.string().optional(), proxy_username: z.string().optional(), proxy_host: z.string().optional(), proxy_port: z.number().int().optional(), proxy_ip_address: z.string().optional(), proxy_country_id: z.string().optional(), proxy_city_id: z.number().int().optional(), proxy_city_name: z.string().optional(), proxy_asn_id: z.number().int().optional(), proxy_asn_name: z.string().optional()})).optional()})).optional(), message: z.string().optional(), proxy_test_result_created: z.array(z.string()).optional(), proxy_tester_proxy_created: z.array(z.string()).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 Proxy Test Run * * POST /public/user/proxy_test_run/create */ export const createApiPublicUserProxyTestRunCreate = async (args: CreateApiPublicUserProxyTestRunCreateArgs, options?: CreateApiPublicUserProxyTestRunCreateOptions) => { const {baseUrl = "https://api.byteful.com/1.0", fetch: fetchFn = fetch, APIPrivateKey, APIPublicKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIPrivateKey !== undefined) headers.set("X-API-Private-Key", APIPrivateKey) if (APIPublicKey !== undefined) headers.set("X-API-Public-Key", APIPublicKey) const url = `${baseUrl.replace(/\/+$/, '')}/public/user/proxy_test_run/create${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(proxyTestRunCreateResponse, await res.json()) };