enonic / xp

Enonic XP
https://enonic.com
GNU General Public License v3.0
202 stars 34 forks source link

ModifyNodeParams doesn't allow using NodeIndexConfigTemplates #10509

Closed ComLock closed 7 months ago

ComLock commented 7 months ago

This runs fine:

import {run} from '/lib/xp/context';
import {connect} from '/lib/xp/node';

run({
    principals: ['role:system.admin'],
}, () => {
    const writeConnection = connect({
        repoId: '<snipped>',
        branch: 'master'
    });
    const modifiedNode = writeConnection.modify({
        key: '<snipped>',
        editor: (node) => {
            node._indexConfig = {
                default: 'byType',
            }
            return node;
        }
    });
    log.info('modifiedNode:%s', JSON.stringify(modifiedNode, null, 4))
});

But shows type error in code editor.

ComLock commented 7 months ago

The type is not wrong, one just have to write the code a bit different:

import type {ModifiedNode} from '/lib/xp/node';

import {run} from '/lib/xp/context';
import {connect} from '/lib/xp/node';

interface SpecificNodeData {
    key: string
}

run({
    principals: ['role:system.admin'],
}, () => {
    const writeConnection = connect({
        repoId: 'com.enonic.app.explorer.collection.a',
        branch: 'master'
    });
    const modifiedNode = writeConnection.modify<SpecificNodeData>({
        key: 'ed28007b-f297-4162-86eb-9df020d85ee3',
        editor: (node) => {
            const modifiedNode: ModifiedNode<SpecificNodeData> = node;
            modifiedNode._indexConfig = {
                default: 'byType',
            }
            return modifiedNode;
        }
    });
    log.info('modifiedNode:%s', JSON.stringify(modifiedNode, null, 4))
});