compose-payload.js 1.14 KB
const paramsIsArray = (methodName, params) => `<?xml version="1.0"?>
<methodCall>
    <methodName>${methodName}</methodName>
    <params>
        <param>
            <value>
                <struct>
${(params || []).map((param) => `
                    <member>
                        <name>${param.name}</name>
                        <value><${param.type || 'string'}>${param.value}</${param.type || 'string'}></value>
                    </member>
`).join('')}
                </struct>
            </value>
        </param>
    </params>
</methodCall>`;

const paramsIsNotArray = (methodName, params) => {
    const paramsInArray = [];

    // eslint-disable-next-line no-restricted-syntax
    for (const [keyword, value] of Object.entries(params)) {
        paramsInArray.push({
            name: keyword,
            value: (typeof value === 'object' && value.value) || value,
            type: value.type || 'string',
        });
    }

    return paramsIsArray(methodName, paramsInArray);
};

module.exports = (methodName, params) => (
    Array.isArray(params)
        ? paramsIsArray(methodName, params)
        : paramsIsNotArray(methodName, params)
);