import {z} from 'zod' export type GetApiContainersFetchArgs = {'X-Phantombuster-Org'?: string | undefined, id: string, withResultObject?: string | undefined, withOutput?: string | undefined, withRuntimeEvents?: string | undefined, withNewerAndOlderContainerId?: string | undefined}; export type GetApiContainersFetchOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiContainersFetchResponse = 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(), launchedAt: z.number().nullable().optional(), exitCode: z.number().nullable().optional(), retryNumber: z.number(), output: z.string().nullable().optional(), resultObject: z.string().nullable().optional(), 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(), newerContainerId: z.string().optional(), olderContainerId: z.string().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 a container by id. * * GET /containers/fetch */ export const getApiContainersFetch = async (args: GetApiContainersFetchArgs, options?: GetApiContainersFetchOptions) => { 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.id != null) query.append("id", String(args.id)) if (args.withResultObject != null) query.append("withResultObject", String(args.withResultObject)) if (args.withOutput != null) query.append("withOutput", String(args.withOutput)) if (args.withRuntimeEvents != null) query.append("withRuntimeEvents", String(args.withRuntimeEvents)) if (args.withNewerAndOlderContainerId != null) query.append("withNewerAndOlderContainerId", String(args.withNewerAndOlderContainerId)) if (apiKeyAuth !== undefined) headers.set("X-Phantombuster-Key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/containers/fetch${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(getApiContainersFetchResponse, await res.json()) };