aribouius / jsonapi-react

A minimal JSON:API client and React hooks for fetching, updating, and caching remote data.
MIT License
149 stars 28 forks source link

Client ignores the "fetch" option in config for fetch/mutate/delete #51

Open jetwiwo opened 2 years ago

jetwiwo commented 2 years ago

ApiClient's types indicate that a fetch implementation may be provided in the config argument for delete(), fetch() and mutate(). The ApiClient does not use this implementation. Instead it always uses the fetch which was provided to the constructor.

My use-case is that I'd like to hook "fetch" to intercept request/response data in the manner suggested by https://github.com/aribouius/jsonapi-react/issues/38#issuecomment-868887564

aribouius commented 2 years ago

hi @jetwiwo,

Yeah I'm noticing now that the declared types were implemented incorrectly - the config argument for the client constructor is being portrayed as the same as that for the delete/fetch/mutate methods, as you noted.

That being said, would you mind expanding on how providing a custom implementation to the client constructor does not fit your use case, i.e. what does also supporting it at the method level enable?

jetwiwo commented 2 years ago

Sure. Firstly, my project is only using the JSON API client part of jsonapi-react, not the caching part. The JSON API implementation my project uses (https://www.graphiti.dev/) supports some extended functionality beyond what JSON API provides as standard. As such I want to be able to modify the request body, while still retaining the ability to parse the response using jsonapi-react.

I've actually achieved this by putting a replaceBody function on the fetchOptions when calling mutate and providing a fetch implementation which uses fetchOptions. This works, but it seems like a fairly convoluted way of doing this.

Considering all of the use cases my project has, the thing which would really make me happy is a library with just your serialiser and deserialiser in it. Yours is the only (de)serialiser project I've found which actually works with deeply nested relationships (good job on that!). We need to be able to deserialise the data in both the browser (a React application) and NodeJS. Having access to your simple and reliable serialiser library would enable us to do this quite easily.