import {z} from 'zod' export type GetApiContainersFetchAllArgs = {'X-Phantombuster-Org'?: string | undefined, agentId: string, beforeEndedAt?: string | undefined, limit?: string | undefined, mode?: ('all' | 'finalized') | undefined, withRuntimeEvents?: string | undefined}; export type GetApiContainersFetchAllOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiContainersFetchAllResponse = z.object({maxLimitReached: z.boolean(), containers: z.array(z.object({id: z.string(), status: z.enum(["starting", "running", "finished", "unknown", "launch error"]), createdAt: z.number(), launchType: z.enum(["manual", "user api call", "scheduled once", "scheduled repeatedly", "scheduled after agent", "retry", "scheduled before", "browser extension api call"]), endType: z.enum(["finished", "killed", "global timeout", "org timeout", "agent timeout", "unknown", "no log timeout"]).optional(), endedAt: z.number().optional(), exitCode: z.number().nullable().optional(), retryNumber: z.number(), runtimeEvents: z.array(z.object({timestamp: z.number().optional(), slug: z.string().max(100), icon: z.string().max(100).optional(), type: z.string().max(100).optional(), category: z.string().max(100).optional(), title: z.string().max(500).optional(), text: z.string().max(500).optional(), primaryValue: z.union([z.number(), z.string().max(100), z.boolean()]).optional(), secondaryValue: z.union([z.number(), z.string().max(100), z.boolean()]).optional(), props: z.object({}).optional(), setupLink: z.string().max(100).optional(), redirectPage: z.string().max(100).optional()})).optional(), internalMetadata: z.record(z.string(), z.unknown()).nullable().optional(), userCustomMetadata: z.record(z.string(), z.unknown()).nullable().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 }; /** * Gets all containers associated to a specified agent. * * GET /containers/fetch-all */ export const getApiContainersFetchAll = async (args: GetApiContainersFetchAllArgs, options?: GetApiContainersFetchAllOptions) => { const {baseUrl = "https://api.phantombuster.com/api/v2", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['X-Phantombuster-Org'] != null) headers.set("X-Phantombuster-Org", String(args['X-Phantombuster-Org'])) if (args.agentId != null) query.append("agentId", String(args.agentId)) if (args.beforeEndedAt != null) query.append("beforeEndedAt", String(args.beforeEndedAt)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.mode != null) query.append("mode", String(args.mode)) if (args.withRuntimeEvents != null) query.append("withRuntimeEvents", String(args.withRuntimeEvents)) if (apiKeyAuth !== undefined) headers.set("X-Phantombuster-Key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/containers/fetch-all${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(getApiContainersFetchAllResponse, await res.json()) };