coder / backstage-plugins

Official Coder plugins for the Backstage platform
34 stars 2 forks source link

Coder plugin: Create `useCoderApiFunction` custom hook #109

Closed Parkreiner closed 3 months ago

Parkreiner commented 5 months ago

Part of umbrella issue #16. Cannot be started until #108 and #110 are done.

Realistically, this is going to be, by far, the hardest part of the entire umbrella issue. Depending on how things go, we might have to abandon it, or turn it into something that gets chipped away at over time.

If we're not careful, I could see this using up a lot of working hours, which is why I think it should always be treated as a lower-priority issue. This could end up being on the level of our type-generation logic in the main Coder codebase. Expect to need to pay a visit to Matt Pocock's TypeScript Wizards Discord server for help

Problem

Let's say that #108 is done, and we have CoderClient.arbitraryApiCall defined, and ready to go. There's just one problem: the method isn't really type-safe at all. The method needs to be able to work with any kind of data, so it always return type Promise<any>, and while you can add type parameters to make things more specific, specificity doesn't translate to more accuracy. The type parameters rely entirely on user input – it's easy to make a typo and pass in the wrong parameter, and TypeScript won't let you know. You'll only realize your mistake after having a runtime error

So, why not make a wrapper over the method which will bring in that type safety? The core logic will still live in the method, but the hook will provide a lot of TypeScript magic to catch typos and other mistakes, while addressing some React-specific gotchas

Requirements

Possible solutions

Unknowns and pitfalls