import {z} from 'zod' export type GetApiV2JobsStatusJobIdArgs = {job_id: string}; export type GetApiV2JobsStatusJobIdOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const v2JobInput = z.object({list_id: z.string().optional(), row_count: z.number().int().optional(), entity_type: z.string().optional()}); export const v2JobTiming = z.object({created_at: z.string().optional(), started_at: z.string().optional(), finished_at: z.string().optional()}); export const v2JobCreditUsage = z.object({total_credits: z.number().int(), total_results: z.number().int()}); export const v2JobResults = z.object({format: z.literal("csv"), download_url: z.string(), expires_at: z.string(), file_expires_at: z.string().optional()}); export const v2JobStatusResponse = z.object({job_id: z.string(), status: z.string(), input: v2JobInput, timing: v2JobTiming, additional_data: z.object({}).optional(), credit_usage: v2JobCreditUsage.optional(), results: v2JobResults.optional(), error: z.object({}).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 Job Status * * GET /v2/jobs/status/{job_id} * * Also accepts auth: APIKeyHeader */ export const getApiV2JobsStatusJobId = async (args: GetApiV2JobsStatusJobIdArgs, options?: GetApiV2JobsStatusJobIdOptions) => { const {baseUrl = "https://api.explorium.ai", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("api_key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v2/jobs/status/${encodeURIComponent(String(args.job_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(v2JobStatusResponse, await res.json()) };