Skip to content

Commit

Permalink
http-request-actions: Fix parsing of data-lake variables
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaellehmkuhl authored and ArturoManzoli committed Nov 21, 2024
1 parent 00a732f commit d0d57b0
Showing 1 changed file with 78 additions and 30 deletions.
108 changes: 78 additions & 30 deletions src/libs/actions/http-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit d0d57b0

Please sign in to comment.