import {z} from 'zod' export type GetApiAgentIdArgs = {id: number, withScript?: boolean | undefined}; export type GetApiAgentIdOptions = {baseUrl?: string | undefined, internalApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiAgentIdResponse = z.object({status: z.string().optional(), data: z.object({id: z.number().optional(), name: z.string().optional(), scriptId: z.number().optional(), proxy: z.string().optional(), proxyAddress: z.object({}).optional(), proxyUsername: z.object({}).optional(), proxyPassword: z.object({}).optional(), disableWebSecurity: z.boolean().optional(), ignoreSslErrors: z.boolean().optional(), loadImages: z.boolean().optional(), launch: z.string().optional(), nbLaunches: z.number().optional(), showDebug: z.boolean().optional(), awsFolder: z.string().optional(), executionTimeLimit: z.number().optional(), fileMgmt: z.string().optional(), fileMgmtMaxFolders: z.number().optional(), lastEndMessage: z.string().optional(), lastEndStatus: z.string().optional(), maxParallelExecs: z.number().optional(), userAwsFolder: z.string().optional(), nonce: z.number().optional(), script: z.object({id: z.number().optional(), name: z.string().optional(), source: z.string().optional(), url: z.object({}).optional(), text: z.string().optional(), httpHeaders: z.object({}).optional()}).optional()}).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 }; /** * Get an agent record. * * GET /agent/{id} */ export const getApiAgentId = async (args: GetApiAgentIdArgs, options?: GetApiAgentIdOptions) => { const {baseUrl = "https://api.phantombuster.com/api/v2", fetch: fetchFn = fetch, internalApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.withScript != null) query.append("withScript", String(args.withScript)) if (internalApiKey !== undefined) headers.set("X-Phantombuster-Key-1", internalApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/agent/${encodeURIComponent(String(args.id))}${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(getApiAgentIdResponse, await res.json()) };