-
Notifications
You must be signed in to change notification settings - Fork 326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Evening Hack: generate browser-compatible javascript #31
Conversation
This generates reasonably sane javascript that is fairly universally compatible. Usage is simple: Given this protobuf: ```protobuf syntax = "proto3"; package us.xeserv.api.printerfacts; option go_package = "proto"; service Printerfacts { rpc Fact(FactParams) returns (Facts) {}; } message FactParams { int32 count = 1; } message Facts { repeated string facts = 1; } ``` this tool will generate output javascript such as https://xena.greedo.xeserv.us/files/twirp_printerfacts/printerfacts_twirp.js usage is simple: ```shell protoc --proto_path=$GOPATH/src:. \ --twirp_out=. \ --twirp_jsbrowser_out=. \ --go_out=. \ ./printerfacts.proto ``` Usage from HTML is simple: ```html <script src="https://xena.greedo.xeserv.us/files/twirp_printerfacts/printerfacts_twirp.js"></script> <script> let onSuccess = (x) => { document.write(x.facts[0]); }; let onError = (x) => { console.log(x); }; Printerfacts_fact("https://printerfacts.herokuapp.com", {"count": 1}, onSuccess, onError); </script> ```
Nice |
This is very, very cool, and impressively lightweight. Thanks for sharing it! I think I'd like to keep the bar for inclusion in this repo pretty high. Clients here should do all of the following:
I can't say whether this is idiomatic - I'm not really a JavaScript pro. It clearly doesn't have any dependencies. But I'd like to see it speak in protobuf before accepting it. There's an official JavaScript protobuf compiler these days. I think a generated JavaScript client should depend upon its output. It looks like the official compiler has options that change the output, letting you pick how you want to import the generated code. The official Twirp JS generator should accept identically-named options to be compatible. I want to reiterate that this is just the bar for getting included in github.com/twitchtv/twirp. I don't think you should feel the bar is so high to host your generator elsewhere, but I'd like to keep the ones in here polished (and in fact the Python one needs a lot more polish right now). In order to make it easier for others to write generators and host them elsewhere, I'm strongly considering making |
I'm also looking to use twirp with a client from browser without dependencies - for a simple API I'm fine with a JSON only client. I would also like the typescript definition file to be generated. That way it's possible to have amazing quality of life as the need to write JavaScript to include a web frontend for a Go project is minimal, and my editor can tell me what fields are available and their types. If this doesn't fit the twirp proper, the twirp documentation/README should have a blurp link to how to find these extensions. |
@joneskoo I've moved this binary into https://github.com/Xe/twirp-codegens, I don't personally like typescript but I'd be more than happy to accept a PR that generates typescript type definitions. |
@spenczar I took your suggestions and produced this: thechriswalker@c379f7e I haven't made a PR yet, but would be interested in feedback. It takes the official google protobuf javascript compiler output and creates the twirp api client on the side. It generates code for both node and the browser, speaking JSON and protobuf and the node version passes the It does only generate commonjs modules currently. Edit: I have updated that fork since, and a more valid comment is in the issue: #61 |
https://github.com/twitchtv/protogen now exists, so you can import Twirp's protoc plugin library. Hooray! I'm going to close this issue as community-maintained generators seem like the best way forward right now. |
This generates reasonably sane javascript that is fairly universally compatible. Usage is simple:
Given this protobuf:
this tool will generate output javascript such as
https://xena.greedo.xeserv.us/files/twirp_printerfacts/printerfacts_twirp.js
usage is simple:
Usage from HTML is simple:
Ref #19
See example here. (source code at https://github.com/Xe/printerfacts)
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.