import {z} from 'zod' export type GetApiIngestJobsJobIdArgs = {'Amperity-Tenant': string, 'Api-Version': string, 'job-id': string, api_version?: string | undefined}; export type GetApiIngestJobsJobIdOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const ingestFileDetails = z.object({errors: z.number().int(), file_id: z.string(), name: z.string().optional(), read: z.number().int()}); export const ingestFeedDetails = z.object({ended_at: z.string(), errors: z.number().int(), feed_id: z.string(), files: z.array(ingestFileDetails), inserted: z.number().int(), job_id: z.string(), read: z.number().int(), rejected: z.number().int(), started_at: z.string(), state: z.string(), table_name: z.string(), updated: z.number().int()}); export const ingestJobDetails = z.object({id: z.string(), tables: z.array(ingestFeedDetails), workflow_id: z.string()}); 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 }; /** * Ingest job details * * GET /ingest/jobs/{job-id} */ export const getApiIngestJobsJobId = async (args: GetApiIngestJobsJobIdArgs, options?: GetApiIngestJobsJobIdOptions) => { const {baseUrl = "https://app.amperity.com/api", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Amperity-Tenant'] != null) headers.set("Amperity-Tenant", String(args['Amperity-Tenant'])) if (args['Api-Version'] != null) headers.set("Api-Version", String(args['Api-Version'])) if (args.api_version != null) query.append("api_version", String(args.api_version)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/ingest/jobs/${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(ingestJobDetails, await res.json()) };