Do not include fields schema in rest api responses

Hello, I’m new to cockpit and I wonder if it’s possible not to have the schema of the fields in the rest api response : {"name":"title","type":"text","localize":false,"options":[]} for example.
I’ve found the simple=1 query parameter. It works great with most of the fields but not with the repeater field. Is there a solution? Because exposing the schema appears messy to me and consumes bandwidth. Thanks

What I’m doing for those scenarios is to filter out stuff don’t want on the api answers using a collections.find.after.<name> trigger

1 Like

Nice, I wasn’t aware of this option.

If you don’t need all options from the repeater field, maybe this custom field helps: tag file, usage

I wrote it three months ago to have a clean output from a repeater field. Than I didn’t use it, so I’m not sure, if it has any issues or which features from the original repeater field are missing.

I made this in a custom addon. A bit messy, I’m no php developper, but it gets the job done for me. Not 100% tested though.

if (COCKPIT_ADMIN && COCKPIT_API_REQUEST) {
    function remove_underscore_fields(&$array)
    {
        // don't remove "_pid", as it breaks sortable nested entries
        $toRemove = ["_o", "_by", "_mby", "_modified", "_created"];

        foreach ($toRemove as $name) {
            if (isset($array[$name])) {
                unset($array[$name]);
            }
        }
    }

    function make_simple(&$field)
    {
        if (is_array($field)) {
            remove_underscore_fields($field);

            foreach ($field as &$subentry) {
                // for fields like repeater, set...
                if (isset($subentry["field"])) {
                    unset($subentry['field']);
                }
                make_simple($subentry);
            }
        }
    }

    $simple_collection = function ($name, &$entries) use ($app) {
        $isSimple = $app->param('simple');
        if ($isSimple) {
            foreach ($entries as &$entry) {
                remove_underscore_fields($entry);
                foreach ($entry as &$field) {
                    make_simple($field);
                }
            }
        }
    };

    $simple_singleton = function ($singleton, &$data) use ($app) {
        $isSimple = $app->param('simple');
        if ($isSimple) {
            remove_underscore_fields($data);
            foreach ($data as &$field) {
                make_simple($field);
            }
        }
    };


    $app->on("collections.find.after", $simple_collection);
    $app->on("singleton.getData.after", $simple_singleton);
}
3 Likes

Wow! Thanks a lot. I’ll try that

1 Like

Works great anyway! Thanks!

1 Like