mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-05-24 01:12:54 -04:00
This PR does too many things :( 1. Major refactoring of the dev/scripts and dev/code-generation folders. Primarily this was removing duplicate code and cleaning up some poorly written code snippets as well as making them more idempotent so then can be re-run over and over again but still maintain the same results. This is working on my machine, but I've been having problems in CI and comparing diffs so running generators in CI will have to wait. 2. Re-Implement using the generated api routes for testing This was a _huge_ refactor that touched damn near every test file but now we have auto-generated typed routes with inline hints and it's used for nearly every test excluding a few that use classes for better parameterization. This should greatly reduce errors when writing new tests. 3. Minor Perf improvements for the All Recipes endpoint A. Removed redundant loops B. Uses orjson to do the encoding directly and returns a byte response instead of relying on the default jsonable_encoder. 4. Fix some TS type errors that cropped up for seemingly no reason half way through the PR. See this issue https://github.com/phillipdupuis/pydantic-to-typescript/issues/28 Basically, the generated TS type is not-correct since Pydantic will automatically fill in null fields. The resulting TS type is generated with a ? to indicate it can be null even though we _know_ that i can't be.
97 lines
2.5 KiB
Python
97 lines
2.5 KiB
Python
import re
|
|
from enum import Enum
|
|
from typing import Optional
|
|
|
|
from humps import camelize
|
|
from pydantic import BaseModel, Extra, Field
|
|
from slugify import slugify
|
|
|
|
|
|
class RouteObject:
|
|
def __init__(self, route_string) -> None:
|
|
self.prefix = "/" + route_string.split("/")[1]
|
|
self.route = "/" + route_string.split("/", 2)[2]
|
|
self.js_route = self.route.replace("{", "${")
|
|
self.parts = route_string.split("/")[1:]
|
|
self.var = re.findall(r"\{(.*?)\}", route_string)
|
|
self.is_function = "{" in self.route
|
|
self.router_slug = slugify("_".join(self.parts[1:]), separator="_")
|
|
self.router_camel = camelize(self.router_slug)
|
|
|
|
|
|
class RequestType(str, Enum):
|
|
get = "get"
|
|
put = "put"
|
|
post = "post"
|
|
patch = "patch"
|
|
delete = "delete"
|
|
|
|
|
|
class ParameterIn(str, Enum):
|
|
query = "query"
|
|
path = "path"
|
|
header = "header"
|
|
|
|
|
|
class RouterParameter(BaseModel):
|
|
required: bool = False
|
|
name: str
|
|
location: ParameterIn = Field(..., alias="in")
|
|
|
|
class Config:
|
|
extra = Extra.allow
|
|
|
|
|
|
class RequestBody(BaseModel):
|
|
required: bool = False
|
|
|
|
class Config:
|
|
extra = Extra.allow
|
|
|
|
|
|
class HTTPRequest(BaseModel):
|
|
request_type: RequestType
|
|
description: str = ""
|
|
summary: str
|
|
requestBody: Optional[RequestBody]
|
|
|
|
parameters: list[RouterParameter] = []
|
|
tags: list[str] | None = []
|
|
|
|
class Config:
|
|
extra = Extra.allow
|
|
|
|
def list_as_js_object_string(self, parameters, braces=True):
|
|
if len(parameters) == 0:
|
|
return ""
|
|
|
|
if braces:
|
|
return "{" + ", ".join(parameters) + "}"
|
|
else:
|
|
return ", ".join(parameters)
|
|
|
|
def payload(self):
|
|
return "payload" if self.requestBody else ""
|
|
|
|
def function_args(self):
|
|
all_params = [p.name for p in self.parameters]
|
|
if self.requestBody:
|
|
all_params.append("payload")
|
|
return self.list_as_js_object_string(all_params)
|
|
|
|
def query_params(self):
|
|
params = [param.name for param in self.parameters if param.location == ParameterIn.query]
|
|
return self.list_as_js_object_string(params)
|
|
|
|
def path_params(self):
|
|
params = [param.name for param in self.parameters if param.location == ParameterIn.path]
|
|
return self.list_as_js_object_string(parameters=params, braces=False)
|
|
|
|
@property
|
|
def summary_camel(self):
|
|
return camelize(slugify(self.summary))
|
|
|
|
@property
|
|
def js_docs(self):
|
|
return self.description.replace("\n", " \n * ")
|