API: types schema improve typescript performance

This commit is contained in:
Johannes Kirschbauer
2024-09-02 17:40:23 +02:00
parent 3f4c0a06ef
commit 9eb56f274b
9 changed files with 38 additions and 180 deletions

View File

@@ -7,10 +7,7 @@ from clan_cli.api import API
def main() -> None:
schema = API.to_json_schema()
print(
f"""export const schema = {json.dumps(schema, indent=2)} as const;
"""
)
print(f"""{json.dumps(schema, indent=2)}""")
if __name__ == "__main__":

View File

@@ -163,6 +163,7 @@ API.register(open_file)
"properties": {},
}
err_type = None
for name, func in self._registry.items():
hints = get_type_hints(func)
@@ -175,6 +176,15 @@ API.register(open_file)
return_type = serialized_hints.pop("return")
if err_type is None:
err_type = next(
t
for t in return_type["oneOf"]
if ("error" in t["properties"]["status"]["enum"])
)
return_type["oneOf"][1] = {"$ref": "#/$defs/error"}
sig = signature(func)
required_args = []
for n, param in sig.parameters.items():
@@ -196,6 +206,8 @@ API.register(open_file)
},
}
api_schema["$defs"] = {"error": err_type}
return api_schema
def get_method_argtype(self, method_name: str, arg_name: str) -> Any:

View File

@@ -92,7 +92,7 @@ def type_to_dict(
required.add(pn)
elif pv.get("oneOf") is not None:
if "null" not in [i["type"] for i in pv.get("oneOf", [])]:
if "null" not in [i.get("type") for i in pv.get("oneOf", [])]:
required.add(pn)
required_fields = {

View File

@@ -6,7 +6,11 @@
}:
{
perSystem =
{ self', pkgs, ... }:
{
self',
pkgs,
...
}:
let
flakeLock = lib.importJSON (self + /flake.lock);
flakeInputs = builtins.removeAttrs inputs [ "self" ];
@@ -63,7 +67,6 @@
src = ./.;
buildInputs = [
# TODO: see postFixup clan-cli/default.nix:L188
pkgs.python3
self'.packages.clan-cli.propagatedBuildInputs
@@ -84,17 +87,28 @@
buildInputs = [
pkgs.python3
self'.packages.json2ts
# TODO: see postFixup clan-cli/default.nix:L188
self'.packages.clan-cli.propagatedBuildInputs
];
installPhase = ''
${self'.packages.classgen}/bin/classgen ${self'.packages.inventory-schema}/schema.json ./clan_cli/inventory/classes.py
python api.py > $out
mkdir -p $out
python api.py > $out/API.json
${self'.packages.json2ts}/bin/json2ts --input $out/API.json > $out/API.ts
'';
};
json2ts = pkgs.buildNpmPackage {
name = "json2ts";
src = pkgs.fetchFromGitHub {
owner = "bcherny";
repo = "json-schema-to-typescript";
rev = "118d6a8e7a5a9397d1d390ce297f127ae674a623";
hash = "sha256-ldAFfw3E0A0lIJyDSsshgPRPR7OmV/FncPsDhC3waT8=";
};
npmDepsHash = "sha256-kLKau4SBxI9bMAd7X8/FQfCza2sYl/+0bg2LQcOQIJo=";
};
default = self'.packages.clan-cli;
};