laravel-json-api / laravel

JSON:API for Laravel applications
MIT License
538 stars 42 forks source link

Is there a way to consume the api's without an actual http request ? #144

Open Mina-R-Meshriky opened 2 years ago

Mina-R-Meshriky commented 2 years ago

Something like the tests $this->jsonapi()->get('/api/v1/posts') but to be used within the app, like maybe \JsonApi::server('v1')->get('api/v1/posts') which will return the json response content ?

lindyhopchris commented 2 years ago

Thanks for raising this. I definitely intended to do something like this to allow you to get JSON:API documents outside of a HTTP request, but didn't get round to adding it. It's just a matter of wiring up a few different parts of the package behind a fluent interface, and then documenting it.

RaymondAtivie commented 2 years ago

Really looking forward to this

vrusua commented 2 years ago

Useful feature, looking forward to getting this update as well.

carlosjac commented 2 years ago

This is exactly what I need!

lindyhopchris commented 2 years ago

Ok great, looks like a few people need this so will try to prioritise this.

Out of interest, are people expecting it to be authorised and validated? At the moment that's coupled to a HTTP request due to the way Laravel's form requests work. Or are you not expecting authorisation/validation?

FYI in a future version I'm going to have to decouple authorisation and validation from the HTTP request, so that we can support JSON:API Atomic Operations - but I'm not quite at that stage yet.

carlosjac commented 2 years ago

@lindyhopchris Thank you very much for paying attention to this request.

In my case, I don't need validation nor authorization because I'll use it from my controller which is already authorizated and validated.

Greetings

bbprojectnet commented 2 years ago

I'm also interesting that feature.

lindyhopchris commented 2 years ago

Yeah this is likely to be something I work on soon - as I need to decouple everything from HTTP requests to prepare for Atomic Operations, which I'm super excited to use (and is a feature that's been frequently requested!)

bbprojectnet commented 2 years ago

Yes, i'm also looking forward to atomic operations :)

lindyhopchris commented 10 months ago

In the linked testing issue (which isn't a problem with testing), the solution was to provide the server name to the facade when getting the resource container; however then you also needed to call withServer() on the resource class and give it the same server name.

If a container is derived from a particular server, it should either inject the server throughout whatever it creates, or at least inject the server name - so that any other server dependencies are derived from the same server as the container originated.