diff --git a/autocomplete.js b/autocomplete.js index 93e7c35..dcf379d 100644 --- a/autocomplete.js +++ b/autocomplete.js @@ -32,8 +32,14 @@ function mapAutocompleteFuncParamsToObject(params) { function findMatchingMethodDefinition(sortedParamNames) { const config = loadConfiguration(); - const accountParamNames = config.auth ? _.map(config.auth.params, "name") : []; - const cleanParamNames = _.difference(sortedParamNames, accountParamNames); + const accountParamNames = new Set(config.auth ? _.map(config.auth.params, "name") : []); + const cleanParamNames = sortedParamNames.filter((paramName) => { + if (accountParamNames.has(paramName)) { + accountParamNames.delete(paramName); + return false; + } + return true; + }); const matchingMethodDefinition = config.methods.find((method) => ( _.isEqual( @@ -49,6 +55,13 @@ function readAutocompleteFunctionArguments(params, settings, autocompleteFunctio const paramNames = _.sortBy(_.map(params, "name")); const methodDefinition = findMatchingMethodDefinition(paramNames); + if (!methodDefinition) { + return { + ...mapAutocompleteFuncParamsToObject(settings), + ...mapAutocompleteFuncParamsToObject(params), + }; + } + const autocompleteParamIndex = methodDefinition.params.findIndex((param) => ( param.type === "autocomplete" && param.functionName === autocompleteFunctionName )); diff --git a/helpers.js b/helpers.js index 8cc0631..c131aca 100644 --- a/helpers.js +++ b/helpers.js @@ -9,6 +9,7 @@ const validators = require("./validators"); const { loadMethodFromConfiguration, loadAccountFromConfiguration, + loadConfiguration, } = require("./config-loader"); const CREATE_TEMPORARY_FILE_LINUX_COMMAND = "mktemp -p /tmp kaholo_plugin_library.XXXXXX"; @@ -22,6 +23,7 @@ async function readActionArguments( methodDefinition = loadMethodFromConfiguration(action.method.name), ) { const accountDefinition = loadAccountFromConfiguration(); + const paramValues = removeUndefinedAndEmpty(action.params); const settingsValues = removeUndefinedAndEmpty(settings); @@ -29,8 +31,25 @@ async function readActionArguments( throw new Error(`Could not find a method "${action.method.name}" in config.json`); } + const settingsParamsDefinition = loadConfiguration()?.settings ?? []; + const settingsParsingPromises = settingsParamsDefinition.map(async (settingDefinition) => { + settingsValues[settingDefinition.name] = await parseParameter( + settingDefinition, + settingsValues[settingDefinition.name], + ); + + const { validationType } = settingDefinition; + if (validationType) { + validateParamValue( + settingsValues[settingDefinition.name], + validationType, + ); + } + }); + await Promise.all(settingsParsingPromises); + const paramsParsingPromises = methodDefinition.params.map(async (paramDefinition) => { - paramValues[paramDefinition.name] = await parseMethodParameter( + paramValues[paramDefinition.name] = await parseParameter( paramDefinition, paramValues[paramDefinition.name], settingsValues[paramDefinition.name], @@ -44,12 +63,11 @@ async function readActionArguments( ); } }); - await Promise.all(paramsParsingPromises); if (accountDefinition) { const accountParsingPromises = accountDefinition.params.map(async (paramDefinition) => { - paramValues[paramDefinition.name] = await parseMethodParameter( + paramValues[paramDefinition.name] = await parseParameter( paramDefinition, paramValues[paramDefinition.name], settingsValues[paramDefinition.name], @@ -63,11 +81,13 @@ async function readActionArguments( ); } }); - await Promise.all(accountParsingPromises); } - return removeUndefinedAndEmpty(paramValues); + return removeUndefinedAndEmpty({ + ...settingsValues, + ...paramValues, + }); } async function temporaryFileSentinel(fileDataArray, functionToWatch) { @@ -151,7 +171,7 @@ function removeUndefinedAndEmpty(object) { return _.omitBy(object, (value) => value === "" || _.isNil(value) || (_.isObjectLike(value) && _.isEmpty(value))); } -function parseMethodParameter(paramDefinition, paramValue, settingsValue) { +function parseParameter(paramDefinition, paramValue, settingsValue) { const valueToParse = paramValue ?? settingsValue ?? paramDefinition.default; if (_.isNil(valueToParse)) { if (paramDefinition.required) { diff --git a/package.json b/package.json index ab1c83c..07cd51d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kaholo/plugin-library", - "version": "2.2.0", + "version": "2.2.1", "description": "Kaholo library for plugins", "main": "kaholo-plugin-library.js", "scripts": {