Skip to content

Commit

Permalink
chore: fix read value from ListVariable
Browse files Browse the repository at this point in the history
  • Loading branch information
psamusev committed Feb 10, 2025
1 parent bdc0efa commit 061f1f8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
32 changes: 29 additions & 3 deletions src/tests/utils/connectors.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import EditorSDK, { ConnectorMappingDirection } from '@chili-publish/studio-sdk';
import EditorSDK, { ConnectorMappingDirection, VariableType } from '@chili-publish/studio-sdk';
import axios from 'axios';
import { mock } from 'jest-mock-extended';
import {
Expand All @@ -16,14 +16,15 @@ const GRAFX_ENV_API = 'http://env-1.com/';
describe('utils connectors', () => {
const mockSDK = mock<EditorSDK>();
mockSDK.next.connector = {} as any;
mockSDK.next.variable = {} as any;

mockSDK.next.connector.getById = jest.fn().mockResolvedValue({
parsedData: {
source: { url: 'http://deploy.com/media-connector' },
},
});
mockSDK.mediaConnector.query = jest.fn();
mockSDK.variable.getById = jest.fn();
mockSDK.next.variable.getById = jest.fn();
mockSDK.connector.getMappings = jest.fn();

window.StudioUISDK = mockSDK;
Expand Down Expand Up @@ -83,15 +84,30 @@ describe('utils connectors', () => {
});

it('should "getConnectorConfigurationOptions" correctly', async () => {
(mockSDK.variable.getById as jest.Mock)
(mockSDK.next.variable.getById as jest.Mock)
.mockResolvedValueOnce({
parsedData: {
value: 'text',
type: VariableType.shortText,
},
})
.mockResolvedValueOnce({
parsedData: {
value: false,
type: VariableType.boolean,
},
})
.mockResolvedValueOnce({
parsedData: {
selected: {
value: 'list-item-1',
},
type: VariableType.list,
},
})
.mockResolvedValueOnce({
parsedData: {
type: VariableType.list,
},
});

Expand All @@ -113,6 +129,14 @@ describe('utils connectors', () => {
name: 'option-4',
value: 'var.var2',
},
{
name: 'option-5',
value: 'var.var3',
},
{
name: 'option-6',
value: 'var.var4',
},
],
});

Expand All @@ -127,6 +151,8 @@ describe('utils connectors', () => {
'option-2': 'text',
'option-3': true,
'option-4': false,
'option-5': 'list-item-1',
'option-6': null,
});
});
});
2 changes: 1 addition & 1 deletion src/types/OutputGenerationTypes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type DataConnectorConfiguration = {
dataConnectorId: string;
dataConnectorParameters: {
context: Record<string, string | boolean> | null;
context: Record<string, string | boolean | null> | null;
};
};
24 changes: 14 additions & 10 deletions src/utils/connectors.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
BooleanVariable,
ConnectorMappingDirection,
EngineToConnectorMapping,
ShortTextVariable,
} from '@chili-publish/studio-sdk';
import { ConnectorMappingDirection, EngineToConnectorMapping, Variable } from '@chili-publish/studio-sdk';
import {
ConnectorGrafxRegistration,
ConnectorInstance,
Expand All @@ -12,6 +7,7 @@ import {
ConnectorUrlRegistration,
} from '@chili-publish/studio-sdk/lib/src/next';
import axios from 'axios';
import { isBooleanVariable, isListVariable, isTextVariable } from '../components/variablesComponents/Variable';
import { RemoteConnector } from './ApiTypes';

const isConnectorUrlRegistration = (
Expand Down Expand Up @@ -75,7 +71,15 @@ export function getEnvId(connector: ConnectorInstance) {

export type TextEngineToConnectorMapping = Omit<EngineToConnectorMapping, 'value'> & { value: string };

type LinkedVariable = ShortTextVariable | BooleanVariable;
function readValueFromLinkedVariable(variable: Variable) {
if (isTextVariable(variable) || isBooleanVariable(variable)) {
return variable.value;
}
if (isListVariable(variable)) {
return variable.selected?.value ?? null;
}
throw new Error('Unsupported variable type for link the value');
}

export function isLinkToVariable(mapping: EngineToConnectorMapping): mapping is TextEngineToConnectorMapping {
return typeof mapping.value === 'string' && mapping.value.startsWith('var.');
Expand All @@ -100,9 +104,9 @@ export async function getConnectorConfigurationOptions(connectorId: string) {
const mappingValues = await Promise.all(
parsedData.map((m) => {
if (isLinkToVariable(m)) {
return window.StudioUISDK.variable.getById(fromLinkToVariableId(m.value)).then((v) => ({
return window.StudioUISDK.next.variable.getById(fromLinkToVariableId(m.value)).then((v) => ({
name: m.name,
value: (v.parsedData as LinkedVariable).value,
value: readValueFromLinkedVariable(v.parsedData as Variable),
}));
}
return m;
Expand All @@ -112,5 +116,5 @@ export async function getConnectorConfigurationOptions(connectorId: string) {
// eslint-disable-next-line no-param-reassign
config[mapping.name] = mapping.value;
return config;
}, {} as Record<string, string | boolean>);
}, {} as Record<string, string | boolean | null>);
}

0 comments on commit 061f1f8

Please sign in to comment.