Closed jerrypopsoff closed 1 year ago
To clarify, the desired output here is only type definitions, correct. Not actual proto byte serialization/deserialization?
If so, this feels different from the other first-party implementations within Wire which are complete solutions. Nothing wrong with that, but we may want to categorize it differently.
Two thoughts:
Given that (1) involves a lot of work that no one’s actually asking for right now, (2) feels like the better choice, and could be promoted to (1) in the future if needed. Only minor gotcha is that custom handlers are still technically beta and we should probably do whatever’s necessary to move them official first.
I suspect it won't generate as nice of types, but simply compiling to JS will soon emit TS declarations: https://kotlinlang.org/docs/js-ir-compiler.html#preview-generation-of-typescript-declaration-files-d-ts
I think this is intended for use by JavaScript & TypeScript developers who aren’t otherwise using Kotlin or JVM tools. I think that’s a reasonable thing to add with the right person driving the work and design.
A good use case for a CustomerHandler?
To clarify, the desired output here is only type definitions, correct. Not actual proto byte serialization/deserialization?
Yes. Perhaps we can extend the initial solution to provide support for run-time code generation such as serialization/deserialization and type-checking if there is demand, but for now there are existing open source solutions available that can be leveraged by consumers to help accomplish this (if desired).
Additionally, the Android design explicitly omits serialization/deserialization as a design decision with reasoning that is equivalently applicable for TypeScript. From the Why Wire:
Wire avoids case mapping. A field declared as
picture_urls
in a schema yields a Java fieldpicture_urls
and not the conventionalpictureUrls
camel case. Though the name feels awkward at first, it's fantastic whenever you usegrep
or more sophisticated search tools. No more mapping when navigating between schema, Java source code, and data. It also provides a gentle reminder to calling code that proto messages are a bit special.
Overall I agree that the custom handler approach seems most reasonable, but I'm relatively new to Wire. @efirestone Is there an issue tracking the work to officially support custom handlers that are currently in beta? I'm seeing https://github.com/square/wire/issues/2023 but am unsure if this is sufficient for supporting the desired TypeScript code generation.
You can now implementation your own TypeScript generator using custom handlers
Also, I wrote a custom handler that generates TypeScript awhile back. It's likely using a slightly outdated custom handler API, but it should get you started.
Add support for developers to generate TypeScript based on Wire protocol buffer definitions. See Generating Code with Wire for details pertaining to existing code generation support.
Several developers/teams have expressed interest in generating TypeScript code corresponding to Wire protocol buffers, so that developers can be empowered to leverage type-safe interfaces, enums, etc. corresponding to Wire protocol buffers within framework-agnostic web and Node environments. While there are multiple existing solutions (e.g. WireTypeScriptGenerator), none are particularly well-maintained, have flexible build tooling/environment requirements, or are officially supported/recommended as a mechanism to produce consistent results. Additionally, existing solutions manually create files and append individual lines of code instead of leveraging a tool like typescriptpoet.
Implementing TypeScript code generation within the Wire repository would foster discoverability, consistency, and likely other benefits.
Example
CLI usage
A
--typescript
flag can be exposed to the compiler similarly to the--android
flag to indicate TypeScript output is desired.Output
Type definition example
Service definition example
Usage
The output can be leveraged by consumers alongside libraries like Axios to minimize the level of effort to invoke service endpoints from browser and Node environments.