Skip to content

Commit

Permalink
Adding typescript type endpoint and fixing ts types
Browse files Browse the repository at this point in the history
  • Loading branch information
sagojez committed Jul 24, 2024
1 parent 9c000a8 commit 2622ac0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 21 deletions.
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

0 comments on commit 2622ac0

Please sign in to comment.