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 MethodConnectorDTO = {connectorId?: number | undefined, title?: string | undefined, invoker?: string | undefined}; export type MethodOldDTO = {nodeId?: 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 OperatorOldDTO = {nodeId?: string | undefined, type?: string | undefined, index?: string | undefined, iterator?: string | undefined, condition?: ConditionDTO | undefined, uiId?: string | undefined, expression?: string | 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 ConnectorOldDTO = {connectorId?: number | undefined, title?: string | undefined, icon?: string | undefined, sslCert?: boolean | undefined, timeout?: number | undefined, invoker?: InvokerDTO | undefined, methods?: Array | undefined, operators?: Array | undefined, businessLayout?: BusinessLayout | 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 EnhancementOldDTO = {enhanceId?: number | undefined, name?: string | undefined, description?: string | undefined, expertCode?: string | undefined, expertVar?: string | undefined, simpleCode?: SimpleCodeDTO | undefined, language?: string | undefined}; export type FieldBindingOldDTO = {nodeId?: string | undefined, from?: Array | undefined, enhancement?: EnhancementOldDTO | 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 ConnectionOldDTO = {nodeId?: string | undefined, connectionId?: number | undefined, title?: string | undefined, description?: string | undefined, icon?: string | undefined, fromConnector?: ConnectorOldDTO | undefined, toConnector?: ConnectorOldDTO | undefined, fieldBinding?: Array | undefined, businessLayout?: BusinessLayoutResource | undefined, categoryId?: number | undefined, ui?: {[key: string]: Record} | undefined}; export type CreateApiConnectionArgs = {body: ConnectionOldDTO}; export type CreateApiConnectionOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const bodyDTO = z.object({type: z.string().optional(), format: z.string().optional(), data: z.string().optional(), fields: z.record(z.string(), z.object({})).optional()}); export const requestDTO = z.object({id: z.string().optional(), endpoint: z.string().optional(), method: z.string().optional(), header: z.record(z.string(), z.string()).optional(), body: bodyDTO.optional()}); export const resultDTO = z.object({id: z.string().optional(), status: z.string().optional(), header: z.record(z.string(), z.string()).optional(), body: bodyDTO.optional()}); export const responseDTO = z.object({id: z.string().optional(), name: z.string().optional(), success: resultDTO.optional(), fail: resultDTO.optional()}); export const functionDTO = z.object({name: z.string().optional(), type: z.string().optional(), request: requestDTO.optional(), response: responseDTO.optional()}); export const invokerDTO = z.object({name: z.string().optional(), description: z.string().optional(), hint: z.string().optional(), icon: z.string().optional(), authType: z.string().optional(), requiredData: z.record(z.string(), z.string()).optional(), operations: z.array(functionDTO).optional(), hasManualSync: z.boolean().optional()}); export const methodConnectorDTO = z.object({connectorId: z.number().int().optional(), title: z.string().optional(), invoker: z.string().optional()}); export const methodOldDTO = z.object({nodeId: z.string().optional(), index: z.string().optional(), name: z.string().optional(), color: z.string().optional(), label: z.string().optional(), methodType: z.enum(["CONNECTOR", "HTTP_REQUEST", "WEBHOOK"]).optional(), dataAggregator: z.number().int().optional(), request: requestDTO.optional(), response: responseDTO.optional(), connector: methodConnectorDTO.optional()}); export const statementDTO = z.object({color: z.string().optional(), field: z.string().optional(), type: z.string().optional(), rightPropertyValue: z.string().optional()}); export const conditionDTO = z.object({relationalOperator: z.string().optional(), leftStatement: statementDTO.optional(), rightStatement: statementDTO.optional()}); export const operatorOldDTO = z.object({nodeId: z.string().optional(), type: z.string().optional(), index: z.string().optional(), iterator: z.string().optional(), condition: conditionDTO.optional(), uiId: z.string().optional(), expression: z.string().optional()}); export const bLsvgItem = z.object({id: z.number().int().optional(), name: z.string().optional(), axisX: z.number().int().optional(), axisY: z.number().int().optional(), items: z.string().optional(), getbLayout: z.lazy(() => businessLayout.optional())}); export const bLarrow = z.object({from: z.number().int().optional(), to: z.number().int().optional(), getbLayout: z.lazy(() => businessLayout.optional())}); export const businessLayout: z.ZodType = z.object({id: z.number().int().optional(), name: z.string().optional(), svgItems: z.array(bLsvgItem).optional(), arrows: z.array(bLarrow).optional()}); export const connectorOldDTO = z.object({connectorId: z.number().int().optional(), title: z.string().optional(), icon: z.string().optional(), sslCert: z.boolean().optional(), timeout: z.number().int().optional(), invoker: invokerDTO.optional(), methods: z.array(methodOldDTO).optional(), operators: z.array(operatorOldDTO).optional(), businessLayout: businessLayout.optional()}); export const linkedFieldDTO = z.object({color: z.string().optional(), type: z.string().optional(), field: z.string().optional()}); export const codeDTO = z.object({name: z.string().optional(), value: z.string().optional(), type: z.string().optional()}); export const simpleCodeDTO = z.object({pointer: z.number().int().optional(), code: z.array(codeDTO).optional()}); export const enhancementOldDTO = z.object({enhanceId: z.number().int().optional(), name: z.string().optional(), description: z.string().optional(), expertCode: z.string().optional(), expertVar: z.string().optional(), simpleCode: simpleCodeDTO.optional(), language: z.string().optional()}); export const fieldBindingOldDTO = z.object({nodeId: z.string().optional(), from: z.array(linkedFieldDTO).optional(), enhancement: enhancementOldDTO.optional(), to: z.array(linkedFieldDTO).optional()}); export const bLayoutArrowResource = z.object({from: z.number().int().optional(), to: z.number().int().optional()}); export const bLayoutSvgItemResource = z.object({id: z.number().int().optional(), name: z.string().optional(), x: z.number().int().optional(), y: z.number().int().optional(), items: z.array(z.string()).optional()}); export const businessLayoutResource = z.object({id: z.number().int().optional(), arrows: z.array(bLayoutArrowResource).optional(), svgItems: z.array(bLayoutSvgItemResource).optional()}); export const connectionOldDTO = z.object({nodeId: z.string().optional(), connectionId: z.number().int().optional(), title: z.string().optional(), description: z.string().optional(), icon: z.string().optional(), fromConnector: connectorOldDTO.optional(), toConnector: connectorOldDTO.optional(), fieldBinding: z.array(fieldBindingOldDTO).optional(), businessLayout: businessLayoutResource.optional(), categoryId: z.number().int().optional(), ui: z.record(z.string(), 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 }; /** * Creates a connection from database by accepting connection data in request body. * * POST /connection */ export const createApiConnection = async (args: CreateApiConnectionArgs, options?: CreateApiConnectionOptions) => { 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${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(connectionOldDTO, await res.json()) };