<commit_before>import kongState from './kongState';

export default async (adminApi) => {
    return Promise.all([kongState(adminApi), adminApi.fetchPluginSchemas()])
        .then(([state, schemas]) => {
            const prepareConfig = (plugin, config) => stripConfig(config, schemas.get(plugin));
            const parseApiPluginsForSchemes = plugins => parseApiPlugins(plugins, prepareConfig);
            const parsePluginsForSchemes = plugins => parseGlobalPlugins(plugins, prepareConfig);

            return {
                apis: parseApis(state.apis, parseApiPluginsForSchemes),
                consumers: parseConsumers(state.consumers),
                plugins: parsePluginsForSchemes(state.plugins)
            }
        })
};

function parseConsumers(consumers) {
    return consumers.map(({username, credentials, acls, ..._info}) => {
        return {
            username,
            _info,
            acls: Array.isArray(acls) ? acls.map(({group, ..._info}) => ({group, _info})) : [],
            credentials: zip(Object.keys(credentials), Object.values(credentials))
                .map(parseCredential)
                .reduce((acc, x) => acc.concat(x), [])
        };
    });
}

function zip(a, b) {
    return a.map((n, index) => [n, b[index]]);
}

function parseCredential([credentialName, credentials]) {
    if (!Array.isArray(credentials)) {
      return [];
    }

    return credentials.map(({consumer_id, id, created_at, ...attributes}) => {
        return {
            name: credentialName,
            attributes,
            _info: {id, consumer_id, created_at}
        }
    });
}

function parseApis(apis, parseApiPlugins) {
    return apis.map(({
        name, plugins,
        request_host, request_path, strip_request_path, preserve_host, upstream_url,
        id, created_at}) => {
        return {
            name,
            plugins: parseApiPlugins(plugins),
            attributes: {
                request_host,
                request_path,
                strip_request_path,
                preserve_host,
                upstream_url,
            },
            _info: {
                id,
                created_at
            }
        };
    });
}

function parseApiPlugins(plugins, prepareConfig) {<commit_msg>Add guard for non array for api plugins<commit_after>import kongState from './kongState';

export default async (adminApi) => {
    return Promise.all([kongState(adminApi), adminApi.fetchPluginSchemas()])
        .then(([state, schemas]) => {
            const prepareConfig = (plugin, config) => stripConfig(config, schemas.get(plugin));
            const parseApiPluginsForSchemes = plugins => parseApiPlugins(plugins, prepareConfig);
            const parsePluginsForSchemes = plugins => parseGlobalPlugins(plugins, prepareConfig);

            return {
                apis: parseApis(state.apis, parseApiPluginsForSchemes),
                consumers: parseConsumers(state.consumers),
                plugins: parsePluginsForSchemes(state.plugins)
            }
        })
};

function parseConsumers(consumers) {
    return consumers.map(({username, credentials, acls, ..._info}) => {
        return {
            username,
            _info,
            acls: Array.isArray(acls) ? acls.map(({group, ..._info}) => ({group, _info})) : [],
            credentials: zip(Object.keys(credentials), Object.values(credentials))
                .map(parseCredential)
                .reduce((acc, x) => acc.concat(x), [])
        };
    });
}

function zip(a, b) {
    return a.map((n, index) => [n, b[index]]);
}

function parseCredential([credentialName, credentials]) {
    if (!Array.isArray(credentials)) {
      return [];
    }

    return credentials.map(({consumer_id, id, created_at, ...attributes}) => {
        return {
            name: credentialName,
            attributes,
            _info: {id, consumer_id, created_at}
        }
    });
}

function parseApis(apis, parseApiPlugins) {
    return apis.map(({
        name, plugins,
        request_host, request_path, strip_request_path, preserve_host, upstream_url,
        id, created_at}) => {
        return {
            name,
            plugins: parseApiPlugins(plugins),
            attributes: {
                request_host,
                request_path,
                strip_request_path,
                preserve_host,
                upstream_url,
            },
            _info: {
                id,
                created_at
            }
        };
    });
}

function parseApiPlugins(plugins, prepareConfig) {
    if (!Array.isArray(plugins)) {
      return [];
    }

{'fraction_non_alphanumeric': 0.12254160363086233, 'fraction_numerical': 0.0, 'mean_word_length': 2.032765399737877, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7177015', 'n_tokens_mistral': 1301, 'n_tokens_neox': 1275, 'n_words': 244}