diff --git a/src/libs/actions/http-request.ts b/src/libs/actions/http-request.ts index a6d02f6e0..1231c54ae 100644 --- a/src/libs/actions/http-request.ts +++ b/src/libs/actions/http-request.ts @@ -6,7 +6,7 @@ import { registerActionCallback, registerNewAction, } from '../joystick/protocols/cockpit-actions' -import { getCockpitActionVariableData } from './data-lake' +import { getCockpitActionVariableData, getCockpitActionVariableInfo } from './data-lake' const httpRequestActionIdPrefix = 'http-request-action' @@ -111,43 +111,91 @@ export const saveHttpRequestActionConfigs = (): void => { export type HttpRequestActionCallback = () => void export const getHttpRequestActionCallback = (id: string): HttpRequestActionCallback => { - const action = getHttpRequestActionConfig(id) - if (!action) { - throw new Error(`Action with id ${id} not found.`) - } - - let parsedBody = action.body - const parsedUrlParams = action.urlParams + return () => { + const action = getHttpRequestActionConfig(id) + if (!action) { + throw new Error(`Action with id ${id} not found.`) + } - const cockpitInputsInBody = action.body.match(/{{\s*([^{}\s]+)\s*}}/g) - if (cockpitInputsInBody) { - for (const input of cockpitInputsInBody) { - const parsedInput = input.replace('{{', '').replace('}}', '').trim() - const inputData = getCockpitActionVariableData(parsedInput) - if (inputData) { - parsedBody = parsedBody.replace(input, inputData.toString()) + let parsedBody = action.body + const parsedUrlParams = { ...action.urlParams } + + const cockpitInputsInBody = action.body.match(/{{\s*([^{}\s]+)\s*}}/g) + if (cockpitInputsInBody) { + for (const input of cockpitInputsInBody) { + const parsedInput = input.replace('{{', '').replace('}}', '').trim() + const inputData = getCockpitActionVariableData(parsedInput) + const variableInfo = getCockpitActionVariableInfo(parsedInput) + + if (inputData !== undefined) { + let valueToReplace: string + + // Determine type either from variable info or by parsing the value + const type = + variableInfo?.type || + (() => { + const strValue = inputData.toString().toLowerCase() + // Check if it's a boolean + if (strValue === 'true' || strValue === 'false') { + return 'boolean' + } + // Check if it's a number + if (!isNaN(Number(strValue)) && strValue !== '') { + return 'number' + } + return 'string' + })() + + // Cast the value based on the determined type + switch (type) { + case 'number': + valueToReplace = inputData.toString() + // Remove quotes if they exist around the placeholder + parsedBody = parsedBody.replace(`"${input}"`, valueToReplace) + // If no quotes found, replace the placeholder directly + if (parsedBody.includes(input)) { + parsedBody = parsedBody.replace(input, valueToReplace) + } + break + case 'boolean': + valueToReplace = inputData.toString() + // Remove quotes if they exist around the placeholder + parsedBody = parsedBody.replace(`"${input}"`, valueToReplace) + // If no quotes found, replace the placeholder directly + if (parsedBody.includes(input)) { + parsedBody = parsedBody.replace(input, valueToReplace) + } + break + case 'string': + valueToReplace = `"${inputData.toString()}"` + // Replace the placeholder, maintaining the quotes if they exist + parsedBody = parsedBody.replace(`"${input}"`, valueToReplace) + // If no quotes found, replace and add them + if (parsedBody.includes(input)) { + parsedBody = parsedBody.replace(input, valueToReplace) + } + break + } + } } } - } - const cockpitInputsInUrlParams = Object.entries(action.urlParams).filter( - ([, value]) => typeof value === 'string' && value.startsWith('{{') && value.endsWith('}}') - ) - if (cockpitInputsInUrlParams) { - for (const [key, value] of cockpitInputsInUrlParams) { - const parsedInput = value.replace('{{', '').replace('}}', '').trim() - const inputData = getCockpitActionVariableData(parsedInput) - if (inputData) { - parsedUrlParams[key] = inputData.toString() + const cockpitInputsInUrlParams = Object.entries(action.urlParams).filter( + ([, value]) => typeof value === 'string' && value.startsWith('{{') && value.endsWith('}}') + ) + if (cockpitInputsInUrlParams) { + for (const [key, value] of cockpitInputsInUrlParams) { + const parsedInput = value.replace('{{', '').replace('}}', '').trim() + const inputData = getCockpitActionVariableData(parsedInput) + if (inputData) { + parsedUrlParams[key] = inputData.toString() + } } } - } - const url = new URL(action.url) + const url = new URL(action.url) + url.search = new URLSearchParams(parsedUrlParams).toString() - url.search = new URLSearchParams(parsedUrlParams).toString() - - return () => { fetch(url, { method: action.method, headers: action.headers,