import {z} from 'zod' export type BodyDTO = {type?: string | undefined, format?: string | undefined, data?: string | undefined, fields?: {[key: string]: Record} | undefined}; export type RequestDTO = {id?: string | undefined, endpoint?: string | undefined, method?: string | undefined, header?: {[key: string]: string} | undefined, body?: BodyDTO | undefined}; export type ResultDTO = {id?: string | undefined, status?: string | undefined, header?: {[key: string]: string} | undefined, body?: BodyDTO | undefined}; export type ResponseDTO = {id?: string | undefined, name?: string | undefined, success?: ResultDTO | undefined, fail?: ResultDTO | undefined}; export type FunctionDTO = {name?: string | undefined, type?: string | undefined, request?: RequestDTO | undefined, response?: ResponseDTO | undefined}; export type InvokerDTO = {name?: string | undefined, description?: string | undefined, hint?: string | undefined, icon?: string | undefined, authType?: string | undefined, requiredData?: {[key: string]: string} | undefined, operations?: Array | undefined, hasManualSync?: boolean | undefined}; export type BLsvgItem = {id?: number | undefined, name?: string | undefined, axisX?: number | undefined, axisY?: number | undefined, items?: string | undefined, getbLayout?: BusinessLayout | undefined}; export type BLarrow = {from?: number | undefined, to?: number | undefined, getbLayout?: BusinessLayout | undefined}; export type BusinessLayout = {id?: number | undefined, name?: string | undefined, svgItems?: Array | undefined, arrows?: Array | undefined}; export type MethodConnectorDTO = {connectorId?: number | undefined, title?: string | undefined, invoker?: string | undefined}; export type MethodDTO = {id?: string | undefined, index?: string | undefined, name?: string | undefined, color?: string | undefined, label?: string | undefined, methodType?: ('CONNECTOR' | 'HTTP_REQUEST' | 'WEBHOOK') | undefined, dataAggregator?: number | undefined, request?: RequestDTO | undefined, response?: ResponseDTO | undefined, connector?: MethodConnectorDTO | undefined}; export type StatementDTO = {color?: string | undefined, field?: string | undefined, type?: string | undefined, rightPropertyValue?: string | undefined}; export type ConditionDTO = {relationalOperator?: string | undefined, leftStatement?: StatementDTO | undefined, rightStatement?: StatementDTO | undefined}; export type OperatorDTO = {id?: string | undefined, type?: string | undefined, index?: string | undefined, iterator?: string | undefined, condition?: ConditionDTO | undefined, uiId?: string | undefined, expression?: string | undefined}; export type ConnectorDTO = {connectorId?: number | undefined, title?: string | undefined, icon?: string | undefined, sslCert?: boolean | undefined, timeout?: number | undefined, invoker?: InvokerDTO | undefined, businessLayout?: BusinessLayout | undefined, methods?: Array | undefined, operators?: Array | undefined}; export type LinkedFieldDTO = {color?: string | undefined, type?: string | undefined, field?: string | undefined}; export type CodeDTO = {name?: string | undefined, value?: string | undefined, type?: string | undefined}; export type SimpleCodeDTO = {pointer?: number | undefined, code?: Array | undefined}; export type EnhancementDTO = {title?: string | undefined, description?: string | undefined, script?: string | undefined, args?: string | undefined, simpleCode?: SimpleCodeDTO | undefined, language?: string | undefined}; export type FieldBindingDTO = {id?: string | undefined, enhancementId?: number | undefined, from?: Array | undefined, enhancement?: EnhancementDTO | undefined, to?: Array | undefined}; export type BLayoutArrowResource = {from?: number | undefined, to?: number | undefined}; export type BLayoutSvgItemResource = {id?: number | undefined, name?: string | undefined, x?: number | undefined, y?: number | undefined, items?: Array | undefined}; export type BusinessLayoutResource = {id?: number | undefined, arrows?: Array | undefined, svgItems?: Array | undefined}; export type ConnectionDTO = {id?: string | undefined, connectionId?: number | undefined, title?: string | undefined, description?: string | undefined, icon?: string | undefined, fromConnector?: ConnectorDTO | undefined, toConnector?: ConnectorDTO | undefined, fieldBinding?: Array | undefined, businessLayout?: BusinessLayoutResource | undefined, categoryId?: number | undefined, ui?: {[key: string]: Record} | undefined}; export type CreateApiConnectionValidateArgs = {body: ConnectionDTO}; export type CreateApiConnectionValidateOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiConnectionValidateResponse = z.object({}); 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 }; /** * Validates a connection for correctly constructed structure * * POST /connection/validate */ export const createApiConnectionValidate = async (args: CreateApiConnectionValidateArgs, options?: CreateApiConnectionValidateOptions) => { const {baseUrl = "http://localhost:9090", 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(/\/+$/, '')}/connection/validate${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(createApiConnectionValidateResponse, await res.json()) };