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

fetch: Docs misleading about per-request middleware #2005

Open
1 task done
Snurppa opened this issue Nov 14, 2024 · 0 comments
Open
1 task done

fetch: Docs misleading about per-request middleware #2005

Snurppa opened this issue Nov 14, 2024 · 0 comments
Assignees
Labels
enhancement New feature or request openapi-fetch Relevant to the openapi-fetch library

Comments

@Snurppa
Copy link

Snurppa commented Nov 14, 2024

Description

If you look at the openapi-fetch API docs, it says you can give middleware array as "fetch options" to the request methods.

The following options apply to all request methods (.GET(), .POST(), etc.)

But in source code of v0.13.0, we can see middlewares used are only the ones from the root createClient scope.

Took a while to realise this when debugging why my middleware didn't seem to do anything...

Proposal

I have a use case, where in certain frontend scenarios I wanted to attach "show generic error modal on error response" middleware for group of requests.

I don't want that middleware to be global (createClient level).
Because of mutable client state, I think I can't call use(myMw) and instantly eject(myMw) after the request has been handled, as nothing can't guarantee that during the async fetch request, no other request would be fired (and thus accidentally use this "temp" middleware) from event-loop while waiting for the async work to be finished.

I could handle this case request by request by checking the returned error and then show modal, but that feels not-so-elegant compared to middleware.

I could create another client which would vary only by this one added mw, but it feels too heavy weight.

On the other hand, I understand that it is not actually trivial to define "per request middleware"...
Should they replace the "global" middlewares? Or should they be pushed to the array after the global middleware?
Order of execution is not instantly clear without explicit documentation. I can imagine users might have different assumptions on what the middlewares' execution order would be if both global and per-request middleware are defined.

Axios didn't seem to have support for such either.

Summary

  1. I guess docs need fixing by removing part that suggests middleware can be set on per request basis. Or have I understood something totally wrong? Please enlighten me 😀
  2. Ideas/thoughts about "per request middleware"? Does it make any sense? I was initially happy to find from docs that I could solve my issue directly as a request option. Is there some obvious (semi-elegant) way to handle my use case that I just can't see right now?

Thanks for the lib(s), eagerly waiting for next progression steps 💪 👏

ps. I am not native in english, so might well be I am misunderstanding the docs.
pps. Best medium to ask stupid questions regarding the lib and especially type inference challenges? Do you guys have Discord or such? I also understand if such medium is not feasible because of the noise/nuisance it might cause. Just wanted to ask as I've had several situations where rubberducking with someone more experienced with the lib would've been both beneficial and educational!

Checklist

@Snurppa Snurppa added enhancement New feature or request openapi-fetch Relevant to the openapi-fetch library labels Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request openapi-fetch Relevant to the openapi-fetch library
Projects
Status: No status
Development

No branches or pull requests

2 participants