Skip to content

Commit

Permalink
feat MET-12: Generate TypeScript type definitions from jsonschema (#45)
Browse files Browse the repository at this point in the history
* Generate TypeScript type definitions from jsonschema

* Drop old type definitions
  • Loading branch information
Natoandro authored Jan 13, 2023
1 parent 560f1c5 commit a43ecfd
Show file tree
Hide file tree
Showing 25 changed files with 653 additions and 213 deletions.
22 changes: 17 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
[workspace]
members = ["typegate/native", "meta-cli", "libs/common", "libs/xtask"]
members = [
"typegate/native",
"meta-cli",
"libs/common",
"libs/typescript",
"libs/xtask",
]

[patch.crates-io]
# https://github.com/prisma/quaint/issues/392
Expand Down
21 changes: 15 additions & 6 deletions libs/common/src/typegraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ use std::collections::HashMap;
#[derive(Serialize, Deserialize, Debug)]
pub struct Typegraph {
pub types: Vec<TypeNode>,
#[serde(default)]
pub materializers: Vec<Materializer>,
pub runtimes: Vec<TGRuntime>,
#[serde(default)]
pub policies: Vec<Policy>,
pub meta: TypeMeta,
}
Expand All @@ -26,15 +24,26 @@ pub struct Cors {
pub allow_origin: Vec<String>,
pub allow_headers: Vec<String>,
pub expose_headers: Vec<String>,
#[serde(default)]
pub allow_methods: Vec<String>,
pub allow_credentials: bool,
pub max_age: Option<u32>,
}

#[cfg_attr(feature = "codegen", derive(JsonSchema))]
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "lowercase")]
pub enum AuthProtocol {
OAuth2,
Jwk,
Basic,
}

#[cfg_attr(feature = "codegen", derive(JsonSchema))]
#[derive(Serialize, Deserialize, Debug)]
pub struct Auth {
pub name: String,
pub protocol: String,
pub protocol: AuthProtocol,
pub auth_data: HashMap<String, Value>,
}

Expand Down Expand Up @@ -69,10 +78,10 @@ pub struct TypeNodeBase {
pub description: Option<String>,
#[serde(default)]
pub injection: Option<String>,
#[serde(default)]
pub inject: Option<serde_json::Value>,
#[serde(default, skip_serializing_if = "serde_json::Value::is_null")]
pub inject: Value,
#[serde(default, rename = "enum")]
pub enum_: Option<Vec<serde_json::Value>>,
pub enum_: Option<Vec<Value>>,
#[serde(default)]
pub config: HashMap<String, serde_json::Value>,
}
Expand Down
20 changes: 20 additions & 0 deletions libs/typescript/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "typescript"
version = "0.0.3-dev.0"
edition = "2021"

[dependencies]
anyhow = "1.0.68"
swc_common = { version = "0.29.10", features = ["tty-emitter"] }
swc_ecma_ast = "0.94.14"
swc_ecmascript = { version = "0.205.47", features = [
"visit",
"transforms",
"typescript-parser",
"parser",
"codegen",
] }
swc_ecma_transforms = { version = "0.198.26", features = ["typescript"] }
string_cache = "0.8.4"
dprint-plugin-typescript = "0.80.2"
lazy_static = "1.4.0"
32 changes: 32 additions & 0 deletions libs/typescript/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright Metatype OÜ under the Elastic License 2.0 (ELv2). See LICENSE.md for usage.

pub mod parser;

pub use dprint_plugin_typescript as dprint_plugin;
pub use string_cache;
pub use swc_common;
pub use swc_ecma_ast as ast;
pub use swc_ecmascript::*;

use anyhow::Result;
use dprint_plugin::configuration::Configuration;
use lazy_static::lazy_static;
use std::path::Path;

lazy_static! {
static ref TS_FORMAT_CONFIG: Configuration = {
use dprint_plugin_typescript::configuration::*;
ConfigurationBuilder::new()
.line_width(80)
.prefer_hanging(true)
.prefer_single_line(false)
.next_control_flow_position(NextControlFlowPosition::SameLine)
.union_and_intersection_type_prefer_hanging(false)
.union_and_intersection_type_prefer_single_line(false)
.build()
};
}

pub fn format_text<P: AsRef<Path>>(path: P, source: &str) -> Result<String> {
Ok(dprint_plugin::format_text(path.as_ref(), source, &TS_FORMAT_CONFIG)?.unwrap())
}
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ where

/// Parses a string as a TypeScript module and
/// generates its [`Module`] and [`SourceMap`].
fn parse_module_source(source: String) -> Result<(Module, Lrc<SourceMap>)> {
pub fn parse_module_source(source: String) -> Result<(Module, Lrc<SourceMap>)> {
let cm: Lrc<SourceMap> = Default::default();
let handler = Handler::with_tty_emitter(ColorConfig::Auto, true, false, Some(cm.clone()));

Expand Down
4 changes: 2 additions & 2 deletions libs/xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ name = "xtask"
version = "0.0.3-dev.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.68"
clap = { version = "4.0.32", features = ["derive"] }
common = { path = "../common", features = ["codegen"] }
typescript = { path = "../typescript" }
schemars = { version = "0.8.11", features = [] }
serde_json = "1.0.91"
project-root = "0.2.2"
1 change: 1 addition & 0 deletions libs/xtask/src/codegen/jsonschema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub fn run() -> Result<()> {
.create(true)
.open(path)
.context("Opening the output file")?;
file.set_len(0)?;
let schema = schema_for!(Typegraph);
serde_json::to_writer_pretty(&mut file, &schema)?;
writeln!(file)?;
Expand Down
6 changes: 5 additions & 1 deletion libs/xtask/src/codegen/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright Metatype OÜ under the Elastic License 2.0 (ELv2). See LICENSE.md for usage.

mod jsonschema;
mod typescript;

use anyhow::Result;
use clap::Parser;
Expand All @@ -12,7 +13,10 @@ pub struct Codegen {

type CodegenEntry = (&'static str, fn() -> Result<()>);

const CODEGEN_ENTRIES: &[CodegenEntry] = &[("jsonschema", jsonschema::run)];
const CODEGEN_ENTRIES: &[CodegenEntry] = &[
("jsonschema", jsonschema::run),
("typescript", typescript::run),
];

impl Codegen {
pub fn run(&self) -> Result<()> {
Expand Down
Loading

0 comments on commit a43ecfd

Please sign in to comment.