How to call client with dynamic method? #1568
-
As you can see, I want to call the client with a dynamic method, but I can't do it because the type of the method is not inferred correctly. Many thanks
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
This is intentionally difficult to do 🙂. The reason is that you not only have to assert the endpoint dynamically; you also have to handle parameters dynamically, and when you get a response you’re dealing with a union of all your endpoints, which will be an incredibly-hard data shape (don’t forget your error shapes could be different as well as your successes!). I’d also recommend not using Type safety is all about static analysis, and what you’re doing by its nature is not statically-analyzable (or at least, not without a lot of discriminated union logic that probably won’t make it worth it). Our recommendation is to write code to manually handle the different methods (esp since most endpoints won’t have more than 3 or 4 methods, but each of those methods will require different requests and return different responses; usually in different parts of the application. |
Beta Was this translation helpful? Give feedback.
This is intentionally difficult to do 🙂. The reason is that you not only have to assert the endpoint dynamically; you also have to handle parameters dynamically, and when you get a response you’re dealing with a union of all your endpoints, which will be an incredibly-hard data shape (don’t forget your error shapes could be different as well as your successes!).
I’d also recommend not using
as
at all; that’s a footgun that will probably introduce errors that openapi-fetch solves for you, because you’re overriding the type inference.Type safety is all about static analysis, and what you’re doing by its nature is not statically-analyzable (or at least, not without a lot of discriminated un…