|
| 1 | +import type { |
| 2 | + FunctionDefinitionConstructor, |
| 3 | + HerculesFunctionDefinition, |
| 4 | + HerculesFunctionDefinitionParameter, RegisteredFunction, |
| 5 | + SdkState |
| 6 | +} from "../../types"; |
| 7 | +import {buildRuntimeFunctionDefinition} from "./builder"; |
| 8 | +import {constructValue} from "@code0-tech/tucana/helpers"; |
| 9 | + |
| 10 | +export function registerFunctionDefinitionClass(config: { |
| 11 | + authToken: string; |
| 12 | + aquilaUrl: string; |
| 13 | + actionId: string; |
| 14 | + version: string |
| 15 | +}, state: SdkState) { |
| 16 | + return <T>(klass: FunctionDefinitionConstructor<T>): Promise<void> => { |
| 17 | + const parentClass = Object.getPrototypeOf(klass) |
| 18 | + const runtimeFunction = buildRuntimeFunctionDefinition(parentClass, config); |
| 19 | + const runtimeDefinition = runtimeFunction.definition |
| 20 | + |
| 21 | + const functionParameters: HerculesFunctionDefinitionParameter[] = Reflect.getMetadata('hercules:function_parameters', klass) |
| 22 | + const names: HerculesFunctionDefinition["name"] = Reflect.getMetadata('hercules:name', klass) |
| 23 | + const displayMessage: HerculesFunctionDefinition["displayMessage"] = Reflect.getMetadata('hercules:display_message', klass) |
| 24 | + const description: HerculesFunctionDefinition["description"] = Reflect.getMetadata('hercules:description', klass) |
| 25 | + const deprecationMessage: HerculesFunctionDefinition["deprecationMessage"] = Reflect.getMetadata('hercules:deprecation_message', klass) |
| 26 | + const alias: HerculesFunctionDefinition["alias"] = Reflect.getMetadata('hercules:alias', klass) |
| 27 | + const documentation: HerculesFunctionDefinition["documentation"] = Reflect.getMetadata('hercules:documentation', klass) |
| 28 | + const signature: HerculesFunctionDefinition["signature"] = Reflect.getMetadata('hercules:signature', klass) |
| 29 | + const linkedDataTypeIdentifiers: HerculesFunctionDefinition["linkedDataTypes"] = Reflect.getMetadata('hercules:linked_data_type_identifiers', klass) |
| 30 | + const version: HerculesFunctionDefinition["version"] = Reflect.getMetadata('hercules:version', klass) |
| 31 | + const displayIcon: HerculesFunctionDefinition["displayIcon"] = Reflect.getMetadata('hercules:display_icon', klass) |
| 32 | + const throwsError: HerculesFunctionDefinition["throwsError"] = Reflect.getMetadata('hercules:throws_error', klass) |
| 33 | + |
| 34 | + runtimeDefinition.runtimeParameterDefinitions.forEach(runtimeDefinition => { |
| 35 | + if (functionParameters.find((param: HerculesFunctionDefinitionParameter) => param.runtimeName === runtimeDefinition.runtimeName)) { |
| 36 | + return; |
| 37 | + } |
| 38 | + functionParameters.push({ |
| 39 | + ...runtimeDefinition, |
| 40 | + runtimeDefinitionName: runtimeDefinition.runtimeName |
| 41 | + }) |
| 42 | + }) |
| 43 | + |
| 44 | + state.functions.push({ |
| 45 | + identifier: runtimeFunction.identifier, |
| 46 | + definition: { |
| 47 | + runtimeDefinitionName: runtimeDefinition.runtimeName, |
| 48 | + runtimeName: runtimeDefinition.runtimeName || runtimeDefinition.runtimeName, |
| 49 | + signature: signature || runtimeDefinition.signature, |
| 50 | + throwsError: throwsError || runtimeDefinition.throwsError, |
| 51 | + alias: alias || runtimeDefinition.alias, |
| 52 | + version: version || runtimeDefinition.version, |
| 53 | + description: description || runtimeDefinition.description, |
| 54 | + name: names || runtimeDefinition.name, |
| 55 | + documentation: documentation || runtimeDefinition.documentation, |
| 56 | + deprecationMessage: deprecationMessage || runtimeDefinition.deprecationMessage, |
| 57 | + displayMessage: displayMessage || runtimeDefinition.displayMessage, |
| 58 | + displayIcon: displayIcon || runtimeDefinition.displayIcon, |
| 59 | + definitionSource: "action", |
| 60 | + linkedDataTypeIdentifiers: linkedDataTypeIdentifiers || runtimeDefinition.linkedDataTypeIdentifiers, |
| 61 | + parameterDefinitions: functionParameters.map(value => { |
| 62 | + return { |
| 63 | + ...value, |
| 64 | + runtimeDefinitionName: value.runtimeDefinitionName || value.runtimeName, |
| 65 | + name: value.name || [], |
| 66 | + description: value.description || [], |
| 67 | + documentation: value.documentation || [], |
| 68 | + hidden: value.hidden || false, |
| 69 | + optional: value.optional || false, |
| 70 | + defaultValue: value.defaultValue ? constructValue(value.defaultValue || null) : undefined, |
| 71 | + } |
| 72 | + }) |
| 73 | + }, |
| 74 | + } as RegisteredFunction) |
| 75 | + |
| 76 | + return Promise.resolve(); |
| 77 | + }; |
| 78 | +} |
0 commit comments