-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9e91bf7
commit 8cf9659
Showing
4 changed files
with
163 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,98 @@ | ||
use crate::{lang::js_ts, ExportContext, LanguageExt}; | ||
use heck::ToLowerCamelCase; | ||
use specta::datatype::FunctionResultVariant; | ||
use specta_typescript as ts; | ||
use specta_typescript::{self as ts, Typescript}; | ||
use specta_typescript::{js_doc, ExportError}; | ||
|
||
const GLOBALS: &str = include_str!("./globals.ts"); | ||
|
||
impl LanguageExt for specta_typescript::Typescript { | ||
fn render_commands(&self, cfg: &ExportContext) -> Result<String, ExportError> { | ||
let commands = cfg | ||
.commands | ||
fn render(&self, cfg: &ExportContext) -> Result<String, ExportError> { | ||
let dependant_types = cfg | ||
.type_map | ||
.iter() | ||
.map(|function| { | ||
let arg_defs = function | ||
.args() | ||
.map(|(name, typ)| { | ||
ts::datatype( | ||
self, | ||
&FunctionResultVariant::Value(typ.clone()), | ||
&cfg.type_map, | ||
) | ||
.map(|ty| format!("{}: {}", name.to_lower_camel_case(), ty)) | ||
}) | ||
.collect::<Result<Vec<_>, _>>()?; | ||
.map(|(_sid, ndt)| ts::export_named_datatype(&self, ndt, &cfg.type_map)) | ||
.collect::<Result<Vec<_>, _>>() | ||
.map(|v| v.join("\n"))?; | ||
|
||
js_ts::render_all_parts::<Self>( | ||
cfg, | ||
&dependant_types, | ||
GLOBALS, | ||
&self.header, | ||
render_commands(self, cfg)?, | ||
render_events(self, cfg)?, | ||
) | ||
} | ||
} | ||
|
||
let ret_type = js_ts::handle_result(function, &cfg.type_map, self)?; | ||
fn render_commands(ts: &Typescript, cfg: &ExportContext) -> Result<String, ExportError> { | ||
let commands = cfg | ||
.commands | ||
.iter() | ||
.map(|function| { | ||
let arg_defs = function | ||
.args() | ||
.map(|(name, typ)| { | ||
ts::datatype( | ||
ts, | ||
&FunctionResultVariant::Value(typ.clone()), | ||
&cfg.type_map, | ||
) | ||
.map(|ty| format!("{}: {}", name.to_lower_camel_case(), ty)) | ||
}) | ||
.collect::<Result<Vec<_>, _>>()?; | ||
|
||
let docs = { | ||
let mut builder = js_doc::Builder::default(); | ||
let ret_type = js_ts::handle_result(function, &cfg.type_map, ts)?; | ||
|
||
if let Some(d) = &function.deprecated() { | ||
builder.push_deprecated(d); | ||
} | ||
let docs = { | ||
let mut builder = js_doc::Builder::default(); | ||
|
||
if !function.docs().is_empty() { | ||
builder.extend(function.docs().split("\n")); | ||
} | ||
if let Some(d) = &function.deprecated() { | ||
builder.push_deprecated(d); | ||
} | ||
|
||
builder.build() | ||
}; | ||
Ok(js_ts::function( | ||
&docs, | ||
&function.name().to_lower_camel_case(), | ||
&arg_defs, | ||
Some(&ret_type), | ||
&js_ts::command_body(&cfg.plugin_name, function, true), | ||
)) | ||
}) | ||
.collect::<Result<Vec<_>, ExportError>>()? | ||
.join(",\n"); | ||
if !function.docs().is_empty() { | ||
builder.extend(function.docs().split("\n")); | ||
} | ||
|
||
builder.build() | ||
}; | ||
Ok(js_ts::function( | ||
&docs, | ||
&function.name().to_lower_camel_case(), | ||
&arg_defs, | ||
Some(&ret_type), | ||
&js_ts::command_body(&cfg.plugin_name, function, true), | ||
)) | ||
}) | ||
.collect::<Result<Vec<_>, ExportError>>()? | ||
.join(",\n"); | ||
|
||
Ok(format! { | ||
r#" | ||
Ok(format! { | ||
r#" | ||
export const commands = {{ | ||
{commands} | ||
}}"# | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
fn render_events(&self, cfg: &ExportContext) -> Result<String, ExportError> { | ||
if cfg.events.is_empty() { | ||
return Ok(Default::default()); | ||
} | ||
fn render_events(ts: &Typescript, cfg: &ExportContext) -> Result<String, ExportError> { | ||
if cfg.events.is_empty() { | ||
return Ok(Default::default()); | ||
} | ||
|
||
let (events_types, events_map) = | ||
js_ts::events_data(&cfg.events, self, &cfg.plugin_name, &cfg.type_map)?; | ||
let (events_types, events_map) = | ||
js_ts::events_data(&cfg.events, ts, &cfg.plugin_name, &cfg.type_map)?; | ||
|
||
let events_types = events_types.join(",\n"); | ||
let events_types = events_types.join(",\n"); | ||
|
||
Ok(format! { | ||
r#" | ||
Ok(format! { | ||
r#" | ||
export const events = __makeEvents__<{{ | ||
{events_types} | ||
}}>({{ | ||
{events_map} | ||
}})"# | ||
}) | ||
} | ||
|
||
fn render(&self, cfg: &ExportContext) -> Result<String, ExportError> { | ||
let dependant_types = cfg | ||
.type_map | ||
.iter() | ||
.map(|(_sid, ndt)| ts::export_named_datatype(&self, ndt, &cfg.type_map)) | ||
.collect::<Result<Vec<_>, _>>() | ||
.map(|v| v.join("\n"))?; | ||
|
||
js_ts::render_all_parts::<Self>(self, cfg, &dependant_types, GLOBALS, &self.header) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters