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

Add index.ts with export for all bindings. #133

Open
lab360-ch opened this issue Dec 1, 2022 · 4 comments · May be fixed by #304
Open

Add index.ts with export for all bindings. #133

lab360-ch opened this issue Dec 1, 2022 · 4 comments · May be fixed by #304
Labels
CLI This issue/PR is related to or requires the implementation of the CLI tool

Comments

@lab360-ch
Copy link

Hi

Would it be possible to also generate an index.ts inside the bindings directory?

example

When I have the following structs:

#[derive(Serialize, Debug, TS)]
#[ts(
    export,
    export_to = "bindings/admin/login-response.ts",
    rename = "AdminLoginResponse"
)]
struct LoginResponse {
    access_token: String,
    expires_in: i64,
    token_type: String,
}

#[derive(Deserialize, Validate, Debug, TS)]
#[ts(
    export,
    export_to = "bindings/admin/login-request.ts",
    rename = "AdminLoginRequest"
)]
struct LoginRequest {
    #[validate(email(message = "invalid email"))]
    email: String,
    #[validate(length(min = 8))]
    password: String,
}

I would like to have a index.ts generated inside the bindings directory, that looks like:

export * from './admin/login-request';
export * from './admin/login-response';

So I could add the following configuration to my tsconfig.json:

{
  ...
  "compilerOptions": {
    ...
    "paths": {
      "@my-project/backend": ["backend/bindings/index.ts"],
    }
  }
}

this allows me to import all the exported types in my typescript code like this:

import { AdminLoginRequest, AdminLoginResponse } from '@my-project/backend';
@wiredmatt
Copy link

wiredmatt commented Dec 11, 2022

Pretty sure we could just use export_to = '../bindings/index.ts' in every struct as per this.

@bram209
Copy link

bram209 commented Jan 22, 2023

as a workaround, I am currently doing this in build.rs:

use std::{
    ffi::OsStr,
    fs::{self, File},
    io::Write,
};

fn main() {
    let exports: Vec<_> = fs::read_dir("./bindings")
        .unwrap()
        .filter_map(Result::ok)
        .filter_map(|p| {
            p.path()
                .file_stem()
                .map(OsStr::to_str)
                .flatten()
                .map(str::to_owned)
        })
        .filter(|f| f != "index")
        .map(|f| format!("export * from \"./{}\"", f))
        .collect();

    let mut file = File::create("./bindings/index.ts").unwrap();
    file.write_all(exports.join("\n").as_bytes()).unwrap();
}

@escritorio-gustavo
Copy link
Collaborator

Currently the plan is to create a CLI tool to allow this

@escritorio-gustavo escritorio-gustavo added the CLI This issue/PR is related to or requires the implementation of the CLI tool label Mar 13, 2024
@escritorio-gustavo escritorio-gustavo linked a pull request Apr 24, 2024 that will close this issue
12 tasks
@escritorio-gustavo
Copy link
Collaborator

To anyone who sees this issue and is interested in the feature, please try the CLI being developed in #304 and tell us what you think.

To try it you need to clone the cli branch and build the project with cargo build --release. Then run ./target/release/cargo-ts --index --output <BINDINGS_DIRECTORY>.

When we decide the CLI is complete, it will be available through cargo install but for now, it must be downloaded and built manually

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLI This issue/PR is related to or requires the implementation of the CLI tool
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants