This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Duck { | |
quack(): string; | |
} | |
function isDuck(value: unknown): value is Duck { | |
return !!( | |
value && | |
typeof value === 'object' && | |
typeof Reflect.get(value, 'quack') === 'function' | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isRecord<T = any>(value: unknown): value is Record<string | symbol, T> { | |
return !!(value && typeof value === 'object'); | |
} | |
function isPromise<T = unknown>(value: unknown): value is Promise<T> { | |
return isRecord(value) && Reflect.has(value, 'then'); | |
} | |
async function recursiveResolve<T>( | |
parsedObject: Record<string, any>, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
SPECS_DOWNLOAD_URL="https://url-of-api.com/specs" | |
FILE="$PWD/src/my-api-specs.ts" | |
if [[ -f "$FILE" ]]; then | |
rm "$FILE" | |
fi | |
# use dtsgen to generate the target typescript file | |
dtsgen --url $SPECS_DOWNLOAD_URL >> "$FILE" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Optional | |
from fastapi import FastAPI | |
app = FastAPI() | |
@app.get("/") | |
def read_root(): | |
return {"Hello": "World"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import List | |
from starlite import Partial, delete, get, patch, post, put | |
from my_app.models import Resource | |
@get(path="/resources") | |
def list_resources() -> List[Resource]: | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import List | |
from pydantic import UUID4 | |
from starlite import Controller, Partial, get, post, put, patch, delete | |
from my_app.models import User | |
class UserController(Controller): | |
path = "/users" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from os import environ | |
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine | |
# we first define a dependency. The dependency is a function or method (async or sync) whose return value will be injected | |
def create_postgres_connection() -> AsyncEngine: | |
postgres_connection_string = environ.get("POSTGRES_CONNECTION_STRING", "") | |
if not postgres_connection_string: | |
raise ValueError("Missing ENV Variable POSTGRES_CONNECTION_STRING") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from starlite import Starlite, LoggingConfig | |
from my_app.users import UserController | |
app = Starlite( | |
route_handlers=[UserController], | |
on_startup=[ | |
LoggingConfig(loggers={ | |
"my_app": { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from starlite import Starlite, MediaType, get | |
@get(path="/health-check", media_type=MediaType.TEXT) | |
def health_check() -> str: | |
return "healthy" | |
app = Starlite(route_handlers=[health_check]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from starlette.status import HTTP_200_OK | |
from starlite import create_test_client | |
from my_app.main import health_check | |
def test_health_check(): | |
with create_test_client(route_handlers=[health_check]) as client: | |
response = client.get("/health-check") | |
assert response.status_code == HTTP_200_OK |