agentejo / cockpit

Add content management functionality to any site - plug & play / headless / api-first CMS
http://getcockpit.com
MIT License
5.39k stars 522 forks source link

Fetch: Exclude certain properties from API response by custom rules #1053

Open vincentrohde opened 5 years ago

vincentrohde commented 5 years ago

Is there a way to exclude/ include certain things from the API response by declaring custom rules.

For example I would like to exclude all additional information from the CMS. These properties usually start with an . Is there a way to exclude all properties **starting with an or the field** property?

underscore field

In the documentation, I can only find sparsely information about these two properties(filter and fields), in the JSON.stringify argument:

entries
pauloamgomes commented 5 years ago

You can do that in a event like:


$app->on('collections.find.after', function ($name, &$entries) use ($app) {
  $ufields = ['_o', '_pid', '_by', '_mby', '_modified', '_created'];
  foreach ($entries as $idx => $entry) {
    foreach ($ufields as $ufield) {
      if (array_key_exists($ufield, $entry)) {
        unset($entries[$idx][$ufield]);
      }
    }
  }
});
aheinze commented 5 years ago

@pauloamgomes but only if it is an api request, otherwise this can break the backend ;-)

pauloamgomes commented 5 years ago

@pauloamgomes but only if it is an api request, otherwise this can break the backend ;-)

Yes, for sure, but is the only situation I think that (exclude some fields) can be required.

aheinze commented 5 years ago

another option is

fields: {'_o':0, '_pid':0, '_by':0, '_mby':0, '_modified':0, '_created':0}

or adjust the fields property in the read rules (in the collection settings)

vincentrohde commented 5 years ago

Thank you for all the responses!

fields: {'_o':0, '_pid':0, '_by':0, '_mby':0, '_modified':0, '_created':0}

Where do you implement this? If I put this into my fetch argument, the fields are not excluded from the API response. Am I missing something here?

or adjust the fields property in the read rules (in the collection settings)

Is there some documentation on editing the read rules?

G-Rath commented 5 years ago

I've run into this myself - collections.find.before gives you &$options, & collections.find.after gives you &$entries, but not the "other", which greatly limits what you can do with them :/

I feel like something is missing from somewhere - is it maybe possible to get the options from $app somehow?


I don't think this is the best solution, but I've found using $_REQUEST is valid in the events, so thats something in the meanwhile.

G-Rath commented 5 years ago

Heres the first iteration of a plugin I made to solve this problem: https://gist.github.com/G-Rath/934fabe5ed91fefe61d6f3e656527cd5

It's rough, but the basic idea is there - hopefully it'll be helpful to someone :)

Credit appreciated if used, as I'm interested in seeing what it actually gets used for 😉

When/if I get some time, I plan to try and make it into a proper plugin that hopefully has a frontend component.

teamgroove commented 5 years ago

Also, when you send: { "simple": true } There will be no fields-schema on top of the response.