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

chore: adding typescript type endpoint and fixing ts types #97

Merged
merged 7 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion integrationos-api/src/endpoints/schema_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ use axum::{
};
use bson::{doc, Document};
use futures::StreamExt;
use integrationos_domain::{ApplicationError, Id, IntegrationOSError, InternalError, Store};
use integrationos_domain::{
api_model_config::Lang, ApplicationError, Id, IntegrationOSError, InternalError, Store,
};
use mongodb::options::FindOptions;
use std::sync::Arc;

pub fn get_router() -> Router<Arc<AppState>> {
Router::new()
.route("/projection", get(get_common_model_proj))
.route("/:id", get(generate_schema))
.route("/types/:id", get(generate_types))
}

pub async fn get_common_model_proj(
Expand Down Expand Up @@ -61,6 +64,29 @@ pub async fn get_common_model_proj(
}))
}

pub async fn generate_types(
state: State<Arc<AppState>>,
Path(id): Path<Id>,
) -> Result<String, IntegrationOSError> {
let cm_store = state.app_stores.common_model.clone();
let ce_store = state.app_stores.common_enum.clone();

let common_model = cm_store
.get_one_by_id(&id.to_string())
.await
.map_err(IntegrationOSError::from)?
.ok_or(ApplicationError::not_found(
&format!("CommonModel with id {} not found", id),
None,
))?;

let schema = common_model
.generate_as_expanded(&Lang::TypeScript, &cm_store, &ce_store)
.await;

Ok(schema)
}

pub async fn generate_schema(
state: State<Arc<AppState>>,
Path(id): Path<Id>,
Expand Down
46 changes: 26 additions & 20 deletions integrationos-domain/src/domain/schema/common_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,32 @@ impl CommonEnum {
)
}

pub fn as_typescript_type(&self) -> String {
// let's add the value directly to the enum
format!(
"export const enum {} {{ {} }}\n",
replace_reserved_keyword(&self.name, Lang::TypeScript)
.replace("::", "")
.pascal_case(),
self.options
.iter()
.map(|option| {
let option_name = option.pascal_case();
let option_value = if option.chars().all(char::is_uppercase) {
option.to_lowercase()
} else {
option.kebab_case()
};

format!("{} = '{}'", option_name, option_value)
})
.collect::<HashSet<String>>()
.into_iter()
.collect::<Vec<_>>()
.join(", ")
)
}

/// Generates a effect Schema for the enum
pub fn as_typescript_schema(&self) -> String {
let name = replace_reserved_keyword(&self.name, Lang::TypeScript)
Expand Down Expand Up @@ -220,22 +246,6 @@ impl CommonEnum {

format!("{}\n{}", native_enum, schema)
}

pub fn as_typescript_type(&self) -> String {
format!(
"export const enum {} {{ {} }}\n",
replace_reserved_keyword(&self.name, Lang::TypeScript)
.replace("::", "")
.pascal_case(),
self.options
.iter()
.map(|option| option.pascal_case())
.collect::<HashSet<String>>()
.into_iter()
.collect::<Vec<_>>()
.join(", ")
)
}
}

impl DataType {
Expand Down Expand Up @@ -625,10 +635,6 @@ impl CommonModel {
}
}

pub fn generate_as_typescript_schema(&self) -> String {
self.as_typescript_schema()
}

/// Generates the model as a string in the specified language
/// with recursively expanded inner models and enums.
/// This is useful for generating the entire model and its
Expand Down
Loading