All files / src/controllers DataSourceController.ts

100% Statements 27/27
100% Branches 0/0
100% Functions 13/13
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71  5x                           5x       8x 8x           8x 8x               8x 1x 1x             8x 1x 1x             8x 1x 1x                 8x 2x 2x 2x      
import { EditorAPI } from '../types/CommonTypes';
import { getEditorResponseData } from '../utils/EditorResponseData';
import { ConnectorInstance } from '../next';
import { DataItem } from '../types/DataConnectorTypes';
import { DataItemMappingTools } from '../utils/DataItemMappingTools';
 
/**
 * The DataSourceController is responsible for all communication regarding data source.
 * Methods inside this controller can be called by `window.SDK.dataSource.{method-name}`
 *
 * A data source is a data connector selected to be used in the document.
 * Only one data source can be defined per template, this controller manages this
 * data source.
 *
 */
export class DataSourceController {
    /**
     * @ignore
     */
    #editorAPI: EditorAPI;
    #dataItemMappingTools: DataItemMappingTools;
 
    /**
     * @ignore
     */
    constructor(editorAPI: EditorAPI, dataItemMappingTools: DataItemMappingTools) {
        this.#editorAPI = editorAPI;
        this.#dataItemMappingTools = dataItemMappingTools;
    }
 
    /**
     * Sets the data source by id.
     * @param connectorId the id of your data connector
     * @returns
     */
    setDataSource = async (connectorId: string) => {
        const res = await this.#editorAPI;
        return res.setDataSource(connectorId).then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * Gets the data source.
     * @returns the connector object
     */
    getDataSource = async () => {
        const res = await this.#editorAPI;
        return res.getDataSource().then((result) => getEditorResponseData<ConnectorInstance>(result));
    };
 
    /**
     * Removes the data source if defined.
     * @returns
     */
    removeDataSource = async () => {
        const res = await this.#editorAPI;
        return res.removeDataSource().then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * Maps the data row values to variables by names (data row keys).
     * Variables must exist.
     * @param dataRow DataItem to set
     * @returns
     */
    setDataRow = async (dataRow: DataItem) => {
        const res = await this.#editorAPI;
        const engineDataItem = this.#dataItemMappingTools.mapDataItemToEngine(dataRow);
        return res.setDataRow(JSON.stringify(engineDataItem)).then((result) => getEditorResponseData<null>(result));
    };
}