import {z} from 'zod' export type FlowGraph = {nodes?: (Array<{id: string, componentId?: string | undefined, credentials_id?: string | undefined, function?: string | undefined, name?: string | undefined, description?: string | undefined, fields?: Record | undefined}>) | undefined, edges?: (Array<{id?: string | undefined, config?: ({condition?: string | undefined, mapper?: Record | undefined}) | undefined, source: string, target: string}>) | undefined}; export type Owner = {id: string, type: string}; export type MutableFlowTemplate = {name: string, description?: string | undefined, graph: FlowGraph, type: 'ordinary' | 'realtime', cron?: string | undefined, owners?: Array | undefined}; export type PatchApiTemplatesIdArgs = {id: unknown, body: MutableFlowTemplate}; export type PatchApiTemplatesIdOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const flowGraph = z.object({nodes: z.array(z.object({id: z.string(), componentId: z.string().optional(), credentials_id: z.string().optional(), function: z.string().optional(), name: z.string().optional(), description: z.string().optional(), fields: z.object({}).optional()})).optional(), edges: z.array(z.object({id: z.string().optional(), config: z.object({condition: z.string().optional(), mapper: z.object({}).optional()}).optional(), source: z.string(), target: z.string()})).optional()}); export const owner = z.object({id: z.string(), type: z.string()}); export const flowTemplate = z.object({name: z.string(), description: z.string().optional(), graph: flowGraph, type: z.enum(["ordinary", "realtime"]), cron: z.string().optional(), owners: z.array(owner).optional(), id: z.string(), status: z.enum(["published", "draft"]).optional(), createdAt: z.string(), updatedAt: z.string()}); export const patchApiTemplatesIdResponse = z.object({data: flowTemplate.optional(), meta: z.object({}).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 }; /** * Update an existing flow template * * PATCH /templates/{id} */ export const patchApiTemplatesId = async (args: PatchApiTemplatesIdArgs, options?: PatchApiTemplatesIdOptions) => { const {baseUrl = "http://template-repository.openintegrationhub.com", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/templates/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(patchApiTemplatesIdResponse, await res.json()) };