Skip to content
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

feat: generate code as side effect of build #5

Closed
Tracked by #2
kkharji opened this issue Jul 16, 2022 · 2 comments
Closed
Tracked by #2

feat: generate code as side effect of build #5

kkharji opened this issue Jul 16, 2022 · 2 comments

Comments

@kkharji
Copy link

kkharji commented Jul 16, 2022

Hey @oscartbeaumont, I'm super excited about this library.

I believe it would be cleaner and simpler if .build() would generate the typescript definitions as side effect instead of having to call router.export("..").unwrap();

Additionally, it may be a good idea to have a default name for the exported file or dir {ts_root}/rspc/index.ts/{ts_src_root}/rspc.ts;

For that to be configurable, a method like set_export_ts_root. I'm adding ts here because maybe in the future another languages can be supported, for example I'm hoping to add lua support somehow

@oscartbeaumont
Copy link
Owner

The idea of automatically exporting the bindings is great but it's worth remembering that when you build the app into a binary for release it would continue exporting the bindings which wouldn't be great for most use cases. I have been doing the following in most of my projects but a helper for it would be great.

let router = Router::new().build();

#[cfg(debug_assertions)] // This will only export the bindings when the app is in development mode
router.export(TRPC_BINDING_EXPORT_PATH).unwrap();

I just pushed an commit which lets you do:

let router = <Router>::new()
        .config(Config::new().export_ts_bindings("./ts"))
        .query("version", |_, _: ()| env!("CARGO_PKG_VERSION"))
        .build();

This feature will only export the bindings in debug mode like the sample I showed above. If you need to export the bindings in a release build you will have to do them manually. We couldn't have a default file name as the system exports to a directory and I don't really think a default directory makes sense. People will end up structing their projects differently and I don't think providing a path is too much work to warrant a default but if you do really disagree with this I would be interested to hear why.

@kkharji
Copy link
Author

kkharji commented Jul 17, 2022

The idea of automatically exporting the bindings is great but it's worth remembering that when you build the app into a binary for release it would continue exporting the bindings which wouldn't be great for most use cases. I have been doing the following in most of my projects but a helper for it would be great.

let router = Router::new().build();



#[cfg(debug_assertions)] // This will only export the bindings when the app is in development mode

router.export(TRPC_BINDING_EXPORT_PATH).unwrap();

I just pushed an commit which lets you do:

let router = <Router>::new()

        .config(Config::new().export_ts_bindings("./ts"))

        .query("version", |_, _: ()| env!("CARGO_PKG_VERSION"))

        .build();

This feature will only export the bindings in debug mode like the sample I showed above. If you need to export the bindings in a release build you will have to do them manually. We couldn't have a default file name as the system exports to a directory and I don't really think a default directory makes sense. People will end up structing their projects differently and I don't think providing a path is too much work to warrant a default but if you do really disagree with this I would be interested to hear why.

I agree, awesome turn around, I like it 😍 thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants