Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add additional data to the root of the response? #96

Closed
vylink opened this issue Mar 7, 2019 · 5 comments
Closed

How to add additional data to the root of the response? #96

vylink opened this issue Mar 7, 2019 · 5 comments

Comments

@vylink
Copy link

vylink commented Mar 7, 2019

I'm using decorator to add some additional data into all responses...

class ResponseDecorator implements IResponseDecorator
{
    public function decorateResponse(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
    {
        $entity = $response->getEntity()->getData();
        $entity['somedata'] = ['id'=>1];

        return $response
            ->withHeader('Access-Control-Allow-Origin', '*')
            ->withHeader('Access-Control-Allow-Credentials', 'true')
            ->withHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, DELETE, PUT')
            ->withHeader('Access-Control-Allow-Headers', 'Authorization, Accept, Overwrite, Destination, Content-Type, Depth, User-Agent, Translate, Range, Content-Range, Timeout, X-Requested-With, If-Modified-Since, Cache-Control, Location')
            ->withHeader('Access-Control-Max-Age', 1728000)
            ->withEntity(ArrayEntity::from($entity));
    }

}

This returns all responses as following example:

{
    "status": "success",
    "data": {
        "test": "test",
        "somedata": {
            "id": 1
        }
    }
}

My question is if is there any way, how can I move "somedata" node to same level as "status" and "data" nodes.

Cheers

@mabar
Copy link
Contributor

mabar commented Mar 7, 2019

I think you could redefine JsonUnifyTransformer.

What is your usecase for that? Unified data format is mean to be used just for status, data and errors

@vylink
Copy link
Author

vylink commented Mar 7, 2019

I would like to send updated authentication token in all responses.... and I'd like to keep "data" node just for actual data... Can you point me how to "redefine JsonUnifyTransformer"?

Kind Regards,

M

@mabar
Copy link
Contributor

mabar commented Mar 7, 2019

Extend JsonUnifyTransfomer and replace service in DIC

services:
    api.negotiation.transformer.json:
        class: App\Api\Transformer\JsonUnifyTransformerTransformer
        alteration: true

@mabar
Copy link
Contributor

mabar commented Mar 7, 2019

You can add these informations to request and response through withAttribute() and get it with getAttribute()

@vylink
Copy link
Author

vylink commented Mar 7, 2019

Many thanks. It works!!!

@vylink vylink closed this as completed Mar 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants