Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 2 additions & 16 deletions GUI/src/components/FlowElementsPopup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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),
},
],
Expand All @@ -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');
Expand Down
35 changes: 32 additions & 3 deletions GUI/src/store/new-services.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -259,7 +265,7 @@ const useServiceStore = create<ServiceStoreState>((set, get) => ({
url: endpoint.url,
method: endpoint.methodType,
headers: extractMapValues(endpoint.headers),
body: extractMapValues(endpoint.body),
body: getEndpointBody(endpoint),
params: extractMapValues(endpoint.params),
})),
);
Expand All @@ -282,6 +288,12 @@ const useServiceStore = create<ServiceStoreState>((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`,
Expand Down Expand Up @@ -904,7 +916,24 @@ const useServiceStore = create<ServiceStoreState>((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) {
Expand Down
33 changes: 3 additions & 30 deletions GUI/src/utils/json-request-utils.ts
Original file line number Diff line number Diff line change
@@ -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),
},
],
Expand Down