import {z} from 'zod' export type WorkflowRunRequest = {api_version?: string | undefined, config_id: string, range_from?: string | undefined, range_to?: string | undefined, run_mode?: string | undefined}; export type CreateApiWorkflowRunsArgs = {'Amperity-Tenant': string, 'Api-Version': string, body: WorkflowRunRequest}; export type CreateApiWorkflowRunsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const workflowErrorData = z.record(z.string(), z.unknown()); export const workflowError = z.object({attribution: z.string().optional(), data: workflowErrorData.optional(), message: z.string(), type: z.string()}); export const task = z.object({created_at: z.string(), ended_at: z.string().optional(), error: workflowError.optional(), execution_type: z.string().optional(), id: z.string(), label: z.string(), run_id: z.string().optional(), state: z.string(), state_changed_at: z.string().optional(), task_definition_id: z.string(), task_definition_type: z.string(), tenant: z.string(), timeout_ms: z.number().optional(), workflow_id: z.string()}); export const workflow = z.object({created_at: z.string(), current_version: z.string().optional(), ended_at: z.string().optional(), error: workflowError.optional(), id: z.string(), launched_version: z.string().optional(), name: z.string().optional(), principal_email: z.string().optional(), principal_id: z.string().optional(), principal_name: z.string().optional(), state: z.string(), task_instances: z.array(task).optional(), tenant: z.string(), type: z.string(), warn_after_ms: z.number().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 }; /** * Run workflow * * POST /workflow/runs */ export const createApiWorkflowRuns = async (args: CreateApiWorkflowRunsArgs, options?: CreateApiWorkflowRunsOptions) => { const {baseUrl = "https://app.amperity.com/api", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") 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 (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/workflow/runs${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(workflow, await res.json()) };