All files / src/controllers ConfigurationController.ts

100% Statements 16/16
100% Branches 0/0
100% Functions 9/9
100% Lines 12/12

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  5x                 5x       8x           8x                 1x 1x                 4x 1x               2x 1x               1x 1x      
import { EditorAPI } from '../types/CommonTypes';
import { getEditorResponseData } from '../utils/EditorResponseData';
import { StudioOptionsDeltaUpdate, WellKnownConfigurationKeys } from '../types/ConfigurationTypes';
 
/**
 * The ConfigurationController allows setting editor session data. This data is not stored in the document and
 * can only be used at runtime. Amongst others, the configuration store is available to the editor connectors and
 * Javascript actions running in the editor.
 * Methods inside this controller can be called by `window.SDK.configuration.{method-name}`
 */
export abstract class ConfigurationController {
    /**
     * @ignore
     */
    #editorAPI: EditorAPI;
 
    /**
     * @ignore
     */
    protected constructor(editorAPI: EditorAPI) {
        this.#editorAPI = editorAPI;
    }
 
    /**
     * This method returns the value for a given configuration key. If a value was not found in the configuration store
     * this method returns an error. The key cannot be null.
     * @returns value for a given configuration key
     */
    async getValue(key: WellKnownConfigurationKeys | string) {
        const res = await this.#editorAPI;
        return res.getConfigValue(key).then((result) => getEditorResponseData<string>(result));
    }
 
    /**
     * This method sets, or overrides the value for a given key. Null values are not allowed for both key and value,
     * using them will result in an error.
     * @returns
     */
    async setValue(key: WellKnownConfigurationKeys | string, value: string) {
        const res = await this.#editorAPI;
        return res.setConfigValue(key, value).then((result) => getEditorResponseData<null>(result));
    }
 
    /**
     * This method updates the studio options. Only defined options are updated, null values are ignored.
     * @returns
     */
    async updateStudioOptions(options: StudioOptionsDeltaUpdate) {
        const res = await this.#editorAPI;
        return res.updateStudioOptions(JSON.stringify(options)).then((result) => getEditorResponseData<null>(result));
    }
 
    /**
     * This method returns the engine session id. This id is unique for this engine session
     * @returns the engine session id
     */
    async getEngineSessionId() {
        const res = await this.#editorAPI;
        return res.getEngineSessionId().then((result) => getEditorResponseData<string>(result));
    }
}