Skip to content

Commit a548ff6

Browse files
authored
Use hey-api (#1479)
Store the `openapi.json` of the backend in the repo and import it using https://heyapi.dev/ to generate TypeScript types for the backend API query params, responses, bodies.
1 parent 77e9150 commit a548ff6

39 files changed

+14465
-587
lines changed

backend/cli.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
#!/usr/bin/env python3
22
import asyncio
33
import functools
4+
import json
5+
from pathlib import Path
46
from typing import Any
57

68
import click
79
from heliclockter import datetime_utc
810

11+
from bracket.app import app
912
from bracket.config import config
1013
from bracket.database import database
1114
from bracket.logger import get_logger
@@ -18,6 +21,8 @@
1821
from bracket.utils.db_init import sql_create_dev_db
1922
from bracket.utils.security import hash_password
2023

24+
OPENAPI_JSON_PATH = "openapi/openapi.json"
25+
2126
logger = get_logger("cli")
2227

2328

@@ -49,7 +54,14 @@ def cli() -> None:
4954
pass
5055

5156

52-
@click.command()
57+
@cli.command()
58+
def generate_openapi() -> None:
59+
schema = app.openapi()
60+
Path("openapi/openapi.json").write_text(json.dumps(schema, indent=2, sort_keys=True))
61+
logger.info(f"OpenAPI schema saved to {OPENAPI_JSON_PATH}")
62+
63+
64+
@cli.command()
5365
def hash_password_cmd() -> None:
5466
if config.admin_password is None:
5567
logger.error("No admin password is given")
@@ -59,13 +71,13 @@ def hash_password_cmd() -> None:
5971
logger.info(hashed_pwd)
6072

6173

62-
@click.command()
74+
@cli.command()
6375
@run_async
6476
async def create_dev_db() -> None:
6577
await sql_create_dev_db()
6678

6779

68-
@click.command()
80+
@cli.command()
6981
@click.option("--email", prompt="Email", help="The email used to log into the account.")
7082
@click.option("--password", prompt="Password", help="The password used to log into the account.")
7183
@click.option("--name", prompt="Name", help="The name associated with the account.")
@@ -86,7 +98,4 @@ async def register_user(email: str, password: str, name: str) -> None:
8698

8799

88100
if __name__ == "__main__":
89-
cli.add_command(create_dev_db)
90-
cli.add_command(hash_password_cmd)
91-
cli.add_command(register_user)
92101
cli()

0 commit comments

Comments
 (0)