Skip to content
This repository has been archived by the owner on Dec 21, 2021. It is now read-only.

[Question] Feature consumed by both API and Web #2

Open
yasalkhatib opened this issue Jun 18, 2020 · 1 comment
Open

[Question] Feature consumed by both API and Web #2

yasalkhatib opened this issue Jun 18, 2020 · 1 comment
Assignees
Labels
question Further information is requested

Comments

@yasalkhatib
Copy link

yasalkhatib commented Jun 18, 2020

Hello,
Thank you for this well explained demo of Lucid.

I have a question regarding RespondWithJsonJob and RespondWithViewJob.

If my feature should serve both API and Web Controllers. They have exactly the same logic except for the last line which returns either JSON or a view.

What is your recommended solution here?

I am thinking about creating a Middleware where I inject into the Request object a value (e.g. source [WEB, API]) which determines whether the request is an API or Web.
And then inside the feature, I have two options:

  1. Create an Operation e.g. RespondOperation, that takes the source variable, and based on that, it will run either RespondWithJsonJob or RespondWithViewJob.
  2. Add if; else inside the feature to determine which response job based on the source.
@Mulkave
Copy link
Member

Mulkave commented Jun 29, 2020

Ideally it should be a feature per service, one to serve the API and another to serve Web because if they currently do the same thing at some point you might start seeing differences in the way they're handled based on the request origin.

If you would still prefer to have the same feature, option n.1 is a good approach. And if you would like to take this further you can use the strategy pattern and have the response mutate itself.

@Mulkave Mulkave added the question Further information is requested label Jun 29, 2020
@Mulkave Mulkave self-assigned this Jun 29, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants