import {z} from 'zod' export type WriteMode = 'normal' | 'debug' | 'preview'; export type ImportMode = 'normal' | 'force_reimport' | 'IMPORT_ONLY_MISSING_DAYS' | 'REIMPORT_ENTIRE_PERIOD'; export type FlowSettings = {force_from_date?: (string | null) | undefined, force_to_date?: (string | null) | undefined, write_mode?: WriteMode | undefined, import_mode?: ImportMode | undefined, verbose_logs?: boolean | undefined, extra?: {[key: string]: unknown} | undefined}; export type OnlyObjects = {capability: string, ids: Array, scope?: ({[key: string]: string} | null) | undefined}; export type ExecuteFlowRequest = {consumers?: (Array) | undefined, settings?: FlowSettings | undefined, preflight?: boolean | undefined, only?: (OnlyObjects | null) | undefined}; export type CreateApiSyncsSyncidFlowsFlowidExecuteArgs = {syncid: string, flowid: string, body: ExecuteFlowRequest}; export type CreateApiSyncsSyncidFlowsFlowidExecuteOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const writeMode = z.enum(["normal", "debug", "preview"]); export const importMode = z.enum(["normal", "force_reimport", "IMPORT_ONLY_MISSING_DAYS", "REIMPORT_ENTIRE_PERIOD"]); export const flowSettings = z.object({force_from_date: z.string().nullable().optional(), force_to_date: z.string().nullable().optional(), write_mode: writeMode.optional(), import_mode: importMode.optional(), verbose_logs: z.boolean().optional(), extra: z.record(z.string(), z.unknown()).optional()}); export const executionConsumerResult = z.object({consumerid: z.string(), status: z.string()}); export const executionWarning = z.object({code: z.string(), message: z.string(), blocking: z.boolean().optional()}); export const executeFlowResponse = z.object({resolved_settings: flowSettings, chainexecutionid: z.string().nullable().optional(), consumers: z.array(executionConsumerResult).optional(), preview_executionid: z.string().nullable().optional(), warnings: z.array(executionWarning).optional(), blocked: z.boolean().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 a flow now * * POST /syncs/{syncid}/flows/{flowid}/execute */ export const createApiSyncsSyncidFlowsFlowidExecute = async (args: CreateApiSyncsSyncidFlowsFlowidExecuteArgs, options?: CreateApiSyncsSyncidFlowsFlowidExecuteOptions) => { const {baseUrl = "https://api.chift.eu", 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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/syncs/${encodeURIComponent(String(args.syncid))}/flows/${encodeURIComponent(String(args.flowid))}/execute${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(executeFlowResponse, await res.json()) };