SoftInstigate / restheart

Rapid API Development with MongoDB
https://restheart.org
GNU Affero General Public License v3.0
807 stars 171 forks source link

How to customize return stucture #32

Closed ivoneijr closed 9 years ago

ivoneijr commented 9 years ago

can I customize the return JSON structure? For example: remove structure of curies, links, or etag ?

ivoneijr commented 9 years ago

??

ujibang commented 9 years ago

You can only transform the document properties using the (still to be documented) resource representation transformers. I'll give you here an example; if you add the following 'rts' metadata property to a collection:

rts: [{"name":"filterProperties", "phase":"RESPONSE", "scope":"CHILDREN", "args":["password"]},   {"name":"addRequestProperties", "phase":"REQUEST", "scope":"CHILDREN", "args":{ "log": ["userName", "userRoles", "dateTime", "epochTimeStamp", "remoteIp", "requestMethod"] } } ]

This adds two transformers (filterProperties and addRequestProperties) that (server-side) filter out properties from the response and adds few properties to the request respectively. Basically this filters out the 'password' property when GETting documents and add some log information (username, roles, timestamp, ip and request method) when POSTing/PUTting/PATCHing documents

There are few transformers that can be used and you can also implement your own. These are configured in the restheart.yml configuration files (look at the metadata-named-singletons property, group 'transformers' in https://github.com/SoftInstigate/restheart/blob/develop/etc/restheart.yml)

However the HAL fields (_links, _ebedded, _curies) and the _etag cannot be customized unless you modify the code; have a look at Representation class:

https://github.com/SoftInstigate/restheart/blob/develop/src/main/java/org/restheart/hal/Representation.java

You can easily modify it so that the toString() method will not return the unnecessary fields.

For the _etag, you can filter it out with representation transformers, however this won't allow you to modify data. You will have to modify the DAO code commenting out the logic that injects the _etag property (have a look at https://github.com/SoftInstigate/restheart/blob/develop/src/main/java/org/restheart/db/DocumentDAO.java)