RickWong/fetch-plus

Asynchronous request modification in middleware

futpib opened this issue · 2 comments

Currently one can't modify request options asynchronously in middleware.

I think all of the following signatures should be supported as middlewares:
Request → Response → Response
Request → Response → Promise Response
Request → Promise (Response → Response)
Request → Promise (Response → Promise Response)

I like the idea. Do you have a use-case?

My actual use-case is a bit niche, I have configuration delivered asynchronously to the client and my middleware should change the request domain depending on the config. So instead of awaiting a client instance, I would like to construct client synchronously but each request would await the config promise.

client.addMiddleware(async request => {
  const { baseUrl } = await config;
  ... // mangle request
  return response => response;
});