orval icon indicating copy to clipboard operation
orval copied to clipboard

MSW: overrideResponse should allow async functions

Open severinh opened this issue 9 months ago • 0 comments

What are the steps to reproduce this issue?

In MSW, a mock handler can access the response resolver info. For example, it can access the request body to change the response based on the request. Access to that request body is only possible through an async function call to json().

http.post("/some/path", async ({ request }) => {
  const requestJson = await request.json();
  // Do something with the request, such as producing a response based on it.
  return ...
});

In Orval, PR #1375 made it possible for the override in a MSW mock handler to be a function, and Orval passes the response resolver info to that function. However, Orval requires a synchronous method that returns a response object. It does not allow async functions.

Hence, it's not possible to migrate the above to Orval.

getSomePathMockHandler(async ({ request }) => {  // This does not type-check. Orval does not allow a Promise<SomeResponse> return type.
  const requestJson = await request.json();
  // Do something with the request, such as producing a response based on it.
  return ...
});

What happens?

The above code does not type-check, because Orval does not allow response type to be Promise<SomeResponse>.

What were you expecting to happen?

The above code should type-check. It should be possible to pass both a sync or async method, in the same way MSW allows this.

Any logs, error output, etc?

N/A

What versions are you using?

Operating System: MacOS Package Version: 0.29.1 Browser Version: Node.js 20

This should be trivial to fix, so I'll open a simple PR.

severinh avatar May 20 '24 19:05 severinh