xddq / ts2typebox

Creating TypeBox code from Typescript types
MIT License
43 stars 3 forks source link

The main module exports ts2typebox while evaluating the cli #19

Closed qurafi closed 1 year ago

qurafi commented 1 year ago

I was confused a little bit when importing the main import, it will also evaluate the cli.

import { ts2typebox } from "ts2typebox";

After debugging I found the main field also refers to the cli module in package.json. So I suggest to separate the cli and main field.

Current solution is to import it directly from ts2typebox/dist/src/programmatic-usage

xddq commented 1 year ago

@mhmd-22 Hey!

Ahh, I see, thanks for mentioning it. I feel like the programmatic usage is currently somewhat flawed. I got an initial idea to adapt it (and the options for programmatic usage), happy to hear your thoughts on that.

cli

I guess I will move the cli part into something like ./cli.ts and then have to adapt the bin options in package.json to link to /dist/src/cli.js instead. Inside index.ts I will simply reexport the schema2typebox function from programmativ usage then.

Programmatic usage

Also I feel like the ./programmatic-usage part should have the "outputStdout" removed, the "input" replaced by a string (or perhaps a stream) and should always return a string or a stream with the generated code instead.

Your use case

Would be helpful if you could further describe or even better, provide samples of your use case. This way I could understand and think about it accordingly.

I might be able to take a look at it this monday or tuesday, otherwise probably on saturday.

However, if you want to contribute, feel free to tell me and do so! :]

qurafi commented 1 year ago

So for my use case, I'm building a vite/rollup plugins that scan for schema files and compile them using Ajv. So I could import them as virtual modules. Then in my Sveltekit app I use the compiled schema to validate the requests automatically(very similar to what Fastify currently has).

For type generation I'm currently using json schema to typescript but after seeing the ts to json schema approach, I felt it's the way to go. Tried different solutions but they kinda didn't work for me. Because I need 1-1 mapping of the types and the generated module but they all generate a single schema and they didn't work well with refs and private types and don't forget they're too slow for me(1.3s for 6 files)

I found this module after Sinclair's recommendation as it's the same code used in the typebox-worbench.

Although this module only output typescript which I need to return pure JavaScript for rollup plugin but I could get around it by transforming it using esbuild. Not sure about the performance impact but I hope not a lot so.

For the current issue. I could send a quick PR later on the day. For other improvements, We could open another issue and discuss it further.

xddq commented 1 year ago

Mhh.. the idea sounds cool!

So I actually won't be able to publish a new release today. And if I got it correct, stuff is currently working, but you need to use a weird/unexpexted export.

I think I will then rather tackle thise whole issue in one go in the upcoming days I mentioned. No need to create a PR for the export adoption, thanks for the offer! Can add you to the PR once it is done if you want to verify it.

Otherwise I will just ping you once it is merged

qurafi commented 1 year ago

I think we should wait for the upcoming codegen module from Sinclair? I'm not sure when, but for now I think I'll just copy and paste the code from typebox-workbench because it's the same as the one here but I need some more freedom to tweak the code and it currently does not work for imported types but I think I might get it to work with some ts compiler api magic, I'm not sure how but I will try.

So I think I'm going to close this issue and wait for the new official module.

xddq commented 1 year ago

@mhmd-22 Hey!

Alright! I did update the api for the programmatic usage, in case you still need it.

I think I'll just copy and paste the code from typebox-workbench because it's the same as the one

Yeah, does not matter where to copy the code from, both are from the same repo.

but I need some more freedom to tweak the code and it currently does not work for imported types but I think I might get it to work with some ts compiler api magic, I'm not sure how but I will try.

It might be worth hitting up @sinclairzx81 once you are done with it incorporate your feature into the codegen repo! :]