import {z} from 'zod' export type GetApiFlowsArgs = {'page[size]'?: number | undefined, 'page[number]'?: number | undefined, 'filter[status]'?: string | undefined, 'filter[type]'?: string | undefined, 'filter[user]'?: string | undefined, sort?: string | undefined, search?: string | undefined}; export type GetApiFlowsOptions = {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 meta = z.object({page: z.number().int().optional(), perPage: z.number().int().optional(), total: z.number().int().optional(), totalPages: z.number().int().optional()}); export const getApiFlowsResponse = z.object({data: z.array(flow).optional(), meta: meta.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 }; /** * Retrieve all flows associated with the current user from the repository * * GET /flows */ export const getApiFlows = async (args: GetApiFlowsArgs, options?: GetApiFlowsOptions) => { 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 (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (args['page[number]'] != null) query.append("page[number]", String(args['page[number]'])) if (args['filter[status]'] != null) query.append("filter[status]", String(args['filter[status]'])) if (args['filter[type]'] != null) query.append("filter[type]", String(args['filter[type]'])) if (args['filter[user]'] != null) query.append("filter[user]", String(args['filter[user]'])) if (args.sort != null) query.append("sort", String(args.sort)) if (args.search != null) query.append("search", String(args.search)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/flows${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(getApiFlowsResponse, await res.json()) };