diff --git a/GUI/src/components/FlowElementsPopup/index.tsx b/GUI/src/components/FlowElementsPopup/index.tsx index 764022c4..9b76c784 100644 --- a/GUI/src/components/FlowElementsPopup/index.tsx +++ b/GUI/src/components/FlowElementsPopup/index.tsx @@ -4,7 +4,7 @@ import React, { useEffect, useMemo, useState } from 'react'; import { DndProvider } from 'react-dnd'; import { HTML5Backend } from 'react-dnd-html5-backend'; import { useTranslation } from 'react-i18next'; -import useServiceStore from 'store/new-services.store'; +import useServiceStore, { extractMapValues, getEndpointBody } from 'store/new-services.store'; import useServiceListStore from 'store/services.store'; import useToastStore from 'store/toasts.store'; import { DynamicChoices } from 'types/dynamic-choices'; @@ -318,7 +318,7 @@ const FlowElementsPopup: React.FC = () => { url: endpoint.url, method: endpoint.methodType, headers: extractMapValues(endpoint.headers), - body: extractMapValues(endpoint.body), + body: getEndpointBody(endpoint), params: extractMapValues(endpoint.params), }, ], @@ -330,20 +330,6 @@ const FlowElementsPopup: React.FC = () => { } }; - function extractMapValues(element: any) { - if (element?.rawData && element?.rawData?.length > 0) { - return element.rawData.value; - } - - let result: any = {}; - if (element?.variables) { - for (const entry of element.variables) { - result = { ...result, [entry.name]: entry.value }; - } - } - return result; - } - const getJsonRequestButtonTitle = () => { if (!isUserDefinedNode || selectedTab === t('serviceFlow.tabs.test')) return ''; if (isJsonRequestVisible) return t('serviceFlow.popup.hideJsonRequest'); diff --git a/GUI/src/store/new-services.store.ts b/GUI/src/store/new-services.store.ts index 431af448..1ace8196 100644 --- a/GUI/src/store/new-services.store.ts +++ b/GUI/src/store/new-services.store.ts @@ -26,7 +26,13 @@ import { saveFlowClick } from 'services/service-builder'; import { EndpointDefinitionJson, Service, ServiceState, Step, StepType } from 'types'; import { Assign } from 'types/assign'; import { Chip } from 'types/chip'; -import { EndpointData, EndpointEnv, EndpointTab, PreDefinedEndpointEnvVariables } from 'types/endpoint'; +import { + EndpointData, + EndpointDefinition, + EndpointEnv, + EndpointTab, + PreDefinedEndpointEnvVariables, +} from 'types/endpoint'; import { EndpointResponseVariable } from 'types/endpoint/endpoint-response-variables'; import { EndpointType } from 'types/endpoint/endpoint-type'; import { RequestVariablesTabsRawData, RequestVariablesTabsRowsData } from 'types/request-variables'; @@ -259,7 +265,7 @@ const useServiceStore = create((set, get) => ({ url: endpoint.url, method: endpoint.methodType, headers: extractMapValues(endpoint.headers), - body: extractMapValues(endpoint.body), + body: getEndpointBody(endpoint), params: extractMapValues(endpoint.params), })), ); @@ -282,6 +288,12 @@ const useServiceStore = create((set, get) => ({ }); } + chips.push({ + name: 'Base Response', + value: `${endpoint?.name.replaceAll(' ', '_')}_res.response.body`, + data: `${endpoint?.name.replaceAll(' ', '_')}_res.response.body`, + }); + chips.push({ name: 'Status Code', value: `${endpoint?.name.replaceAll(' ', '_')}_res.response.statusCodeValue`, @@ -904,7 +916,24 @@ const useServiceStore = create((set, get) => ({ canRedo: () => get().historyIndex < get().history.length - 1, })); -function extractMapValues(element: any) { +export function getEndpointBody(endpoint: EndpointDefinition): any { + const isRawBodySelected = endpoint?.body?.isRawSelected ?? false; + const rawBody = endpoint?.body?.rawData ?? {}; + let body: any = extractMapValues(endpoint.body); + + if (isRawBodySelected) { + try { + const rawJson = JSON.parse(rawBody?.value ?? ''); + body = rawJson; + } catch (e: any) { + body = extractMapValues(endpoint.body); + console.log(`Unable to save JSON to Yaml. ${e.message}`); + } + } + return body; +} + +export function extractMapValues(element: any) { if (!element) return {}; if (element.rawData && element.rawData.length > 0) { diff --git a/GUI/src/utils/json-request-utils.ts b/GUI/src/utils/json-request-utils.ts index 4b60fabf..08beb72b 100644 --- a/GUI/src/utils/json-request-utils.ts +++ b/GUI/src/utils/json-request-utils.ts @@ -1,45 +1,18 @@ +import { extractMapValues, getEndpointBody } from 'store/new-services.store'; + import { servicesRequestsExplain } from '../resources/api-constants'; import api from '../services/api-dev'; import { EndpointDefinition } from '../types/endpoint'; -export const extractMapValues = (element: any) => { - if (element?.rawData && element?.rawData?.length > 0) { - return element.rawData.value; - } - - let result: any = {}; - if (element?.variables) { - for (const entry of element.variables) { - result = { ...result, [entry.name]: entry.value }; - } - } - return result; -}; - export const generateJsonRequest = async (endpoint: EndpointDefinition) => { try { - console.log('Generating JSON request for endpoint: ', endpoint); - const isRawBodySelected = endpoint?.body?.isRawSelected ?? false; - const rawBody = endpoint?.body?.rawData ?? {}; - let body: any = extractMapValues(endpoint.body); - - if (isRawBodySelected) { - try { - const rawJson = JSON.parse(rawBody?.value ?? ''); - body = rawJson; - } catch (e: any) { - body = extractMapValues(endpoint.body); - console.log(`Unable to save JSON to Yaml. ${e.message}`); - } - } - const response = await api.post(servicesRequestsExplain(), { requests: [ { url: endpoint.url, method: endpoint.methodType, headers: extractMapValues(endpoint.headers), - body: body, + body: getEndpointBody(endpoint), params: extractMapValues(endpoint.params), }, ],