import {z} from 'zod' export type UIComponent = {component?: string | undefined, config?: {[key: string]: Record} | undefined, type?: string | undefined}; export type ParameterOptionDTO = {label?: string | undefined, value?: string | undefined}; export type FilterCriteriaDTO = {value?: string | undefined, name?: string | undefined}; export type ConfigDescriptionParameterDTO = {context?: string | undefined, defaultValue?: string | undefined, description?: string | undefined, label?: string | undefined, name?: string | undefined, required?: boolean | undefined, type?: ('TEXT' | 'INTEGER' | 'DECIMAL' | 'BOOLEAN') | undefined, min?: number | undefined, max?: number | undefined, stepsize?: number | undefined, pattern?: string | undefined, readOnly?: boolean | undefined, multiple?: boolean | undefined, multipleLimit?: number | undefined, groupName?: string | undefined, advanced?: boolean | undefined, verify?: boolean | undefined, limitToOptions?: boolean | undefined, unit?: string | undefined, unitLabel?: string | undefined, options?: Array | undefined, filterCriteria?: Array | undefined}; export type ConfigDescriptionParameterGroupDTO = {name?: string | undefined, context?: string | undefined, advanced?: boolean | undefined, label?: string | undefined, description?: string | undefined}; export type ConfigDescriptionDTO = {uri?: string | undefined, parameters?: Array | undefined, parameterGroups?: Array | undefined}; export type RootUIComponent = {component?: string | undefined, config?: {[key: string]: Record} | undefined, slots?: {[key: string]: Array} | undefined, uid?: string | undefined, tags?: Array | undefined, props?: ConfigDescriptionDTO | undefined, timestamp?: string | undefined, type?: string | undefined}; export type UpdateApiUiComponentsNamespaceComponentUIDArgs = {namespace: string, componentUID: string, body: RootUIComponent}; export type UpdateApiUiComponentsNamespaceComponentUIDOptions = {baseUrl: string, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const uIComponent = z.object({component: z.string().optional(), config: z.record(z.string(), z.object({})).optional(), type: z.string().optional()}); export const parameterOptionDTO = z.object({label: z.string().optional(), value: z.string().optional()}); export const filterCriteriaDTO = z.object({value: z.string().optional(), name: z.string().optional()}); export const configDescriptionParameterDTO = z.object({context: z.string().optional(), defaultValue: z.string().optional(), description: z.string().optional(), label: z.string().optional(), name: z.string().optional(), required: z.boolean().optional(), type: z.enum(["TEXT", "INTEGER", "DECIMAL", "BOOLEAN"]).optional(), min: z.number().optional(), max: z.number().optional(), stepsize: z.number().optional(), pattern: z.string().optional(), readOnly: z.boolean().optional(), multiple: z.boolean().optional(), multipleLimit: z.number().int().optional(), groupName: z.string().optional(), advanced: z.boolean().optional(), verify: z.boolean().optional(), limitToOptions: z.boolean().optional(), unit: z.string().optional(), unitLabel: z.string().optional(), options: z.array(parameterOptionDTO).optional(), filterCriteria: z.array(filterCriteriaDTO).optional()}); export const configDescriptionParameterGroupDTO = z.object({name: z.string().optional(), context: z.string().optional(), advanced: z.boolean().optional(), label: z.string().optional(), description: z.string().optional()}); export const configDescriptionDTO = z.object({uri: z.string().optional(), parameters: z.array(configDescriptionParameterDTO).optional(), parameterGroups: z.array(configDescriptionParameterGroupDTO).optional()}); export const rootUIComponent = z.object({component: z.string().optional(), config: z.record(z.string(), z.object({})).optional(), slots: z.record(z.string(), z.array(uIComponent)).optional(), uid: z.string().optional(), tags: z.array(z.string()).optional(), props: configDescriptionDTO.optional(), timestamp: z.string().optional(), type: z.string().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 a specific UI component in the specified namespace. * * PUT /ui/components/{namespace}/{componentUID} */ export const updateApiUiComponentsNamespaceComponentUID = async (args: UpdateApiUiComponentsNamespaceComponentUIDArgs, options: UpdateApiUiComponentsNamespaceComponentUIDOptions) => { const {baseUrl, fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/ui/components/${encodeURIComponent(String(args.namespace))}/${encodeURIComponent(String(args.componentUID))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(rootUIComponent, await res.json()) };