Skip to content

Commit 126b2b7

Browse files
committed
TypeScript E2E tests
1 parent 4de3214 commit 126b2b7

18 files changed

+1006
-101
lines changed

Cargo.lock

Lines changed: 43 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cw-schema-codegen/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ frunk = "0.4.3"
1717
frunk_core = "0.4.3"
1818
heck = "0.5.0"
1919
log = "0.4.22"
20+
mimalloc = "0.1.43"
2021
serde_json = "1.0.128"
2122
simple_logger = { version = "5.0.0", features = ["stderr"] }
2223

2324
[dev-dependencies]
25+
arbitrary = { version = "=1.3.2", features = ["derive"] }
26+
derive_arbitrary = "=1.3.2"
2427
insta = "1.40.0"
28+
rand = { version = "0.8.5", features = ["min_const_gen"] }
2529
serde = { workspace = true, features = ["derive"] }
2630
serde_json = "1.0.128"
27-
tempfile = "3.14.0"

packages/cw-schema-codegen/src/main.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ use std::{
1212
path::PathBuf,
1313
};
1414

15+
#[global_allocator]
16+
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
17+
1518
#[derive(Clone, Copy, Debug, Default, PartialEq, ValueEnum)]
1619
pub enum Language {
1720
#[default]
@@ -54,6 +57,7 @@ fn generate_defs<W>(
5457
output: &mut W,
5558
language: Language,
5659
schema: &cw_schema::Schema,
60+
add_imports: bool,
5761
) -> anyhow::Result<()>
5862
where
5963
W: io::Write,
@@ -68,7 +72,7 @@ where
6872
match language {
6973
Language::Rust => cw_schema_codegen::rust::process_node(output, schema, node),
7074
Language::Typescript => {
71-
cw_schema_codegen::typescript::process_node(output, schema, node)
75+
cw_schema_codegen::typescript::process_node(output, schema, node, add_imports)
7276
}
7377
Language::Go | Language::Python => todo!(),
7478
}
@@ -105,23 +109,23 @@ fn main() -> anyhow::Result<()> {
105109
let mut output = opts.output()?;
106110

107111
if let Some(ref instantiate) = schema.instantiate {
108-
generate_defs(&mut output, opts.language, instantiate)?;
112+
generate_defs(&mut output, opts.language, instantiate, true)?;
109113
}
110114

111115
if let Some(ref execute) = schema.execute {
112-
generate_defs(&mut output, opts.language, execute)?;
116+
generate_defs(&mut output, opts.language, execute, false)?;
113117
}
114118

115119
if let Some(ref query) = schema.query {
116-
generate_defs(&mut output, opts.language, query)?;
120+
generate_defs(&mut output, opts.language, query, false)?;
117121
}
118122

119123
if let Some(ref migrate) = schema.migrate {
120-
generate_defs(&mut output, opts.language, migrate)?;
124+
generate_defs(&mut output, opts.language, migrate, false)?;
121125
}
122126

123127
if let Some(ref sudo) = schema.sudo {
124-
generate_defs(&mut output, opts.language, sudo)?;
128+
generate_defs(&mut output, opts.language, sudo, false)?;
125129
}
126130

127131
if let Some(ref responses) = schema.responses {
@@ -131,7 +135,7 @@ fn main() -> anyhow::Result<()> {
131135
.collect::<HashSet<_>>();
132136

133137
for response in responses {
134-
generate_defs(&mut output, opts.language, response)?;
138+
generate_defs(&mut output, opts.language, response, false)?;
135139
}
136140
}
137141

packages/cw-schema-codegen/src/typescript/mod.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,20 @@ fn expand_node_name<'a>(
1212
match node.value {
1313
cw_schema::NodeType::Array { items } => {
1414
let items = &schema.definitions[items];
15-
format!("{}[]", expand_node_name(schema, items)).into()
15+
format!("z.array({})", expand_node_name(schema, items)).into()
1616
}
17-
cw_schema::NodeType::Float => "number".into(),
18-
cw_schema::NodeType::Double => "number".into(),
19-
cw_schema::NodeType::Boolean => "boolean".into(),
20-
cw_schema::NodeType::String => "string".into(),
21-
cw_schema::NodeType::Integer { .. } => "string".into(),
22-
cw_schema::NodeType::Binary => "Uint8Array".into(),
17+
cw_schema::NodeType::Float => "z.number()".into(),
18+
cw_schema::NodeType::Double => "z.number()".into(),
19+
cw_schema::NodeType::Boolean => "z.boolean()".into(),
20+
cw_schema::NodeType::String => "z.string()".into(),
21+
cw_schema::NodeType::Integer { .. } => "z.string()".into(),
22+
cw_schema::NodeType::Binary => "z.instanceof(Uint8Array)".into(),
2323
cw_schema::NodeType::Optional { inner } => {
2424
let inner = &schema.definitions[inner];
25-
format!("{} | null", expand_node_name(schema, inner)).into()
25+
format!("{}.optional()", expand_node_name(schema, inner)).into()
2626
}
27-
cw_schema::NodeType::Struct(..) => node.name.as_ref().into(),
27+
28+
cw_schema::NodeType::Struct(..) => format!("{}Schema", node.name).into(),
2829
cw_schema::NodeType::Tuple { ref items } => {
2930
let items = items
3031
.iter()
@@ -34,14 +35,17 @@ fn expand_node_name<'a>(
3435

3536
format!("[{}]", items).into()
3637
}
37-
cw_schema::NodeType::Enum { .. } => node.name.as_ref().into(),
38+
cw_schema::NodeType::Enum { .. } => format!("{}Schema", node.name).into(),
3839

39-
cw_schema::NodeType::Decimal { .. } => "string".into(),
40-
cw_schema::NodeType::Address => "string".into(),
40+
cw_schema::NodeType::Decimal { .. } => "z.string()".into(),
41+
cw_schema::NodeType::Address => "z.string()".into(),
4142
cw_schema::NodeType::Checksum => todo!(),
4243
cw_schema::NodeType::HexBinary => todo!(),
43-
cw_schema::NodeType::Timestamp => todo!(),
44-
cw_schema::NodeType::Unit => Cow::Borrowed("void"),
44+
cw_schema::NodeType::Timestamp => {
45+
// ToDo: Replace with better type
46+
"z.string()".into()
47+
}
48+
cw_schema::NodeType::Unit => "z.void()".into(),
4549
}
4650
}
4751

@@ -54,6 +58,7 @@ pub fn process_node<O>(
5458
output: &mut O,
5559
schema: &cw_schema::SchemaV1,
5660
node: &cw_schema::Node,
61+
add_imports: bool,
5762
) -> io::Result<()>
5863
where
5964
O: io::Write,
@@ -82,6 +87,7 @@ where
8287
.collect(),
8388
),
8489
},
90+
add_imports,
8591
};
8692

8793
writeln!(output, "{structt}")?;
@@ -125,6 +131,7 @@ where
125131
},
126132
})
127133
.collect(),
134+
add_imports,
128135
};
129136

130137
writeln!(output, "{enumm}")?;

packages/cw-schema-codegen/src/typescript/template.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub struct EnumTemplate<'a> {
1414
pub name: Cow<'a, str>,
1515
pub docs: Cow<'a, [Cow<'a, str>]>,
1616
pub variants: Cow<'a, [EnumVariantTemplate<'a>]>,
17+
pub add_imports: bool,
1718
}
1819

1920
#[derive(Clone)]
@@ -38,4 +39,5 @@ pub struct StructTemplate<'a> {
3839
pub name: Cow<'a, str>,
3940
pub docs: Cow<'a, [Cow<'a, str>]>,
4041
pub ty: TypeTemplate<'a>,
42+
pub add_imports: bool,
4143
}
Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
// This code is @generated by cw-schema-codegen. Do not modify this manually.
22

3+
{% if add_imports %}
4+
import { z } from 'zod';
5+
{% endif %}
6+
37
/**
48
{% for doc in docs %}
59
* {{ doc }}
610
{% endfor %}
711
*/
812

9-
type {{ name }} =
13+
const {{ name }}Schema = z.union([
1014
{% for variant in variants %}
11-
|
12-
1315
/**
1416
{% for doc in variant.docs %}
1517
* {{ doc }}
@@ -18,27 +20,29 @@ type {{ name }} =
1820

1921
{% match variant.ty %}
2022
{% when TypeTemplate::Unit %}
21-
{ "{{ variant.name }}": {} }
23+
z.object({ "{{ variant.name }}": z.void() }),
2224
{% when TypeTemplate::Tuple with (types) %}
23-
{ "{{ variant.name }}": [{{ types|join(", ") }}] }
25+
z.object({ "{{ variant.name }}": z.tuple([{{ types|join(", ") }}]) }),
2426
{% when TypeTemplate::Named with { fields } %}
25-
{ "{{ variant.name }}": {
27+
z.object({ "{{ variant.name }}": z.object({
2628
{% for field in fields %}
2729
/**
2830
{% for doc in field.docs %}
2931
* {{ doc }}
3032
{% endfor %}
3133
*/
3234

33-
{{ field.name }}: {{ field.ty }};
35+
{{ field.name }}: {{ field.ty }},
3436
{% endfor %}
35-
} }
37+
}) }),
3638
{% endmatch %}
3739
{% endfor %}
3840

3941
{% if variants.len() == 0 %}
4042
never;
4143
{% endif %}
42-
;
44+
]);
45+
46+
type {{ name }} = z.infer<typeof {{ name }}Schema>;
4347

44-
export { {{ name }} };
48+
export { {{ name }}, {{ name }}Schema };
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
11
// This code is @generated by cw-schema-codegen. Do not modify this manually.
22

3+
{% if add_imports %}
4+
import { z } from 'zod';
5+
{% endif %}
6+
37
/**
48
{% for doc in docs %}
59
* {{ doc }}
610
{% endfor %}
711
*/
812

9-
type {{ name }} =
13+
const {{ name }}Schema =
1014
{% match ty %}
1115
{% when TypeTemplate::Unit %}
12-
void
16+
z.void()
1317
{% when TypeTemplate::Tuple with (types) %}
14-
[{{ types|join(", ") }}]
18+
z.tuple([{{ types|join(", ") }}])
1519
{% when TypeTemplate::Named with { fields } %}
16-
{
20+
z.object({
1721
{% for field in fields %}
1822
/**
1923
{% for doc in field.docs %}
2024
* {{ doc }}
2125
{% endfor %}
2226
*/
2327

24-
{{ field.name }}: {{ field.ty }};
28+
{{ field.name }}: {{ field.ty }},
2529
{% endfor %}
26-
}
30+
})
2731
{% endmatch %}
2832
;
2933

30-
export { {{ name }} };
34+
type {{ name }} = z.infer<typeof {{ name }}Schema>;
35+
36+
export { {{ name }}, {{ name }}Schema };

packages/cw-schema-codegen/tests/snapshots/typescript__codegen_snap-2.snap

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@ expression: output
44
---
55
// This code is @generated by cw-schema-codegen. Do not modify this manually.
66

7+
8+
import { z } from 'zod';
9+
10+
711
/**
812
913
*/
1014

11-
type Uwu =
15+
const UwuSchema =
1216

13-
[string, string]
17+
z.tuple([z.string(), z.string()])
1418

1519
;
1620

17-
export { Uwu };
21+
type Uwu = z.infer<typeof UwuSchema>;
22+
23+
export { Uwu, UwuSchema };

0 commit comments

Comments
 (0)