import {z} from 'zod' export type GetApiFlowsIdArgs = {id: unknown}; export type GetApiFlowsIdOptions = {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 flow = z.object({name: z.string(), description: z.string().optional(), tenant: 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(["active", "inactive", "starting", "stopping"]).optional(), createdAt: z.string(), updatedAt: z.string()}); export const getApiFlowsIdResponse = z.object({data: flow.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 }; /** * Find Flow by ID * * GET /flows/{id} */ export const getApiFlowsId = async (args: GetApiFlowsIdArgs, options?: GetApiFlowsIdOptions) => { const {baseUrl = "http://flow-repository.openintegrationhub.com", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/flows/${encodeURIComponent(String(args.id))}${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(getApiFlowsIdResponse, await res.json()) };