import {z} from 'zod' export type CreateApiBaseBaseIdWorkflowWorkflowIdLogicArgs = {body: {name?: string | undefined, description?: string | undefined, config: unknown, parentNodeId: string, type: 'condition' | 'Repeat'}}; export type CreateApiBaseBaseIdWorkflowWorkflowIdLogicOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiBaseBaseIdWorkflowWorkflowIdLogicResponse = z.object({logic: z.union([z.object({name: z.string().optional(), description: z.string().optional(), config: z.object({conditions: z.object({})}), id: z.string(), category: z.literal("logic"), testResult: z.object({spent: z.number().optional(), inputRaw: z.unknown(), outputRaw: z.unknown(), inputVariables: z.object({}).optional(), outputVariables: z.object({}).optional(), errorMsg: z.string().optional(), createdTime: z.string()}).optional(), outputVariables: z.object({}).optional(), inputVariables: z.object({}).optional(), createdTime: z.string().nullable().optional(), lastModifiedTime: z.string().nullable().optional(), type: z.literal("condition")}), z.object({name: z.string().optional(), description: z.string().optional(), config: z.object({fact: z.union([z.object({resolvable: z.literal(true), type: z.literal("literal"), value: z.unknown()}), z.object({resolvable: z.literal(true), type: z.literal("fact"), path: z.string().optional(), keyStack: z.array(z.string()).optional(), params: z.object({pipes: z.array(z.enum(["uppercase", "lowercase", "capitalize", "trim", "length", "toString", "jsonStringify", "encodeUrlComponent"])).optional()}).optional(), fact: z.string()}), z.object({resolvable: z.literal(true), type: z.literal("array"), nodes: z.array(z.union([z.object({resolvable: z.literal(true), type: z.literal("literal"), value: z.unknown()}), z.object({resolvable: z.literal(true), type: z.literal("fact"), path: z.string().optional(), keyStack: z.array(z.string()).optional(), params: z.object({pipes: z.array(z.enum(["uppercase", "lowercase", "capitalize", "trim", "length", "toString", "jsonStringify", "encodeUrlComponent"])).optional()}).optional(), fact: z.string()})]))}), z.unknown()])}), id: z.string(), category: z.literal("logic"), testResult: z.object({spent: z.number().optional(), inputRaw: z.unknown(), outputRaw: z.unknown(), inputVariables: z.object({}).optional(), outputVariables: z.object({}).optional(), errorMsg: z.string().optional(), createdTime: z.string()}).optional(), outputVariables: z.object({}).optional(), inputVariables: z.object({}).optional(), createdTime: z.string().nullable().optional(), lastModifiedTime: z.string().nullable().optional(), type: z.literal("Repeat")})]), controls: z.array(z.union([z.object({name: z.string().optional(), config: z.object({sourceNodeId: z.string()}), id: z.string(), category: z.literal("control"), createdTime: z.string().nullable().optional(), lastModifiedTime: z.string().nullable().optional(), type: z.enum(["conditionEnd", "triggerEnd", "repeatEnd"])}), z.object({name: z.string().optional(), config: z.object({truthy: z.boolean(), sourceNodeId: z.string()}), id: z.string(), category: z.literal("control"), outputVariables: z.object({}).optional(), inputVariables: z.object({}).optional(), createdTime: z.string().nullable().optional(), lastModifiedTime: z.string().nullable().optional(), type: z.literal("conditionBranch")})]))}); 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 }; /** * Create a automation workflow logic * * POST /base/{baseId}/workflow/{workflowId}/logic */ export const createApiBaseBaseIdWorkflowWorkflowIdLogic = async (args: CreateApiBaseBaseIdWorkflowWorkflowIdLogicArgs, options?: CreateApiBaseBaseIdWorkflowWorkflowIdLogicOptions) => { const {baseUrl = "https://app.teable.ai/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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/base/${encodeURIComponent(String(args.baseId))}/workflow/${encodeURIComponent(String(args.workflowId))}/logic${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(createApiBaseBaseIdWorkflowWorkflowIdLogicResponse, await res.json()) };