import {z} from 'zod' export type GetApiProcessingV1WorkflowsArgs = {after?: string | undefined, before?: string | undefined, size?: number | undefined, status?: string | undefined, type?: string | undefined, name?: string | undefined, sort?: string | undefined}; export type GetApiProcessingV1WorkflowsOptions = {baseUrl?: string | undefined, JWT?: string | undefined, fetch?: typeof fetch | undefined}; export const cursorPaginationMetadata = z.object({prev: z.string().optional(), next: z.string().optional(), total: z.number().int().optional()}); export const processingWorkflowUrn = z.string(); export const processingJobUrn = z.string(); export const processingWorkflow = z.object({urn: processingWorkflowUrn, type: z.string(), name: z.string().optional(), owner: z.string().optional(), startedAt: z.string(), finishedAt: z.string().optional(), duration: z.number().int().optional(), status: z.string(), errors: z.array(z.object({job: processingJobUrn, reason: z.string()})).optional()}); export const getApiProcessingV1WorkflowsResponse = z.object({meta: cursorPaginationMetadata, data: z.array(processingWorkflow)}); 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 }; /** * List workflows * * GET /processing/v1/workflows */ export const getApiProcessingV1Workflows = async (args: GetApiProcessingV1WorkflowsArgs, options?: GetApiProcessingV1WorkflowsOptions) => { const {baseUrl = "https://your-environment.ataccama.one/api", fetch: fetchFn = fetch, JWT} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.after != null) query.append("after", String(args.after)) if (args.before != null) query.append("before", String(args.before)) if (args.size != null) query.append("size", String(args.size)) if (args.status != null) query.append("status", String(args.status)) if (args.type != null) query.append("type", String(args.type)) if (args.name != null) query.append("name", String(args.name)) if (args.sort != null) query.append("sort", String(args.sort)) if (JWT !== undefined) headers.set('authorization', `Bearer ${JWT}`) const url = `${baseUrl.replace(/\/+$/, '')}/processing/v1/workflows${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(getApiProcessingV1WorkflowsResponse, await res.json()) };