Skip to content

Commit

Permalink
Fix and examples/add examples for vue and fix faulty ssr (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
hxjo authored Jul 18, 2024
1 parent 0f422ca commit 6c75289
Show file tree
Hide file tree
Showing 51 changed files with 9,950 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ jobs:
- name: Install dependencies
run: poetry install --no-ansi --no-interaction
- name: Run lint check
run: ruff check .
run: ruff check inertia
- name: Run format check
run: ruff format . --check
run: ruff format inertia --check
2 changes: 1 addition & 1 deletion .github/workflows/type_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ jobs:
- name: Install dependencies
run: poetry install --no-ansi --no-interaction
- name: Run type check
run: mypy .
run: mypy inertia
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.1] - 2024-07-18

- Fix: SSR failed when the inertia server responded with an empty array
- Examples: Add examples for both SSR and non-SSR in vue language.

## [1.0.0] - 2024-07-18

- [BREAKING CHANGE] Introduce templating via Jinja2 instead of a raw HTML string
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ The following options are available:

## Examples

You can see different full examples in the [following repository](https://github.com/hxjo/fastapi-inertia-examples).
You can see different full examples in the `examples` directory

## Usage

Expand Down
96 changes: 96 additions & 0 deletions examples/vue/classic/backend/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import os
from typing import Annotated
from fastapi.templating import Jinja2Templates
from pydantic import BaseModel, EmailStr

from fastapi import FastAPI, Depends
from fastapi.responses import RedirectResponse
from fastapi.exceptions import RequestValidationError
from starlette.staticfiles import StaticFiles
from starlette.middleware.sessions import SessionMiddleware
from inertia import (
InertiaResponse,
Inertia,
inertia_dependency_factory,
inertia_version_conflict_exception_handler,
inertia_request_validation_exception_handler,
InertiaVersionConflictException,
InertiaConfig,
lazy,
)

template_dir = os.path.join(os.path.dirname(__file__), "templates")
templates = Jinja2Templates(directory=template_dir)

app = FastAPI()
app.add_middleware(SessionMiddleware, secret_key="secret_key")
app.add_exception_handler(
InertiaVersionConflictException,
inertia_version_conflict_exception_handler, # type: ignore[arg-type]
)
app.add_exception_handler(
RequestValidationError,
inertia_request_validation_exception_handler, # type: ignore[arg-type]
)

manifest_json = os.path.join(
os.path.dirname(__file__), "..", "webapp", "dist", "manifest.json"
)
inertia_config = InertiaConfig(
templates=templates,
manifest_json_path=manifest_json,
environment="development",
use_flash_messages=True,
use_flash_errors=True,
entrypoint_filename="main.ts",
assets_prefix="/src",
)
InertiaDep = Annotated[Inertia, Depends(inertia_dependency_factory(inertia_config))]


vue_dir = (
os.path.join(os.path.dirname(__file__), "..", "webapp", "dist")
if inertia_config.environment != "development"
else os.path.join(os.path.dirname(__file__), "..", "webapp", "src")
)

app.mount("/src", StaticFiles(directory=vue_dir), name="src")
app.mount(
"/assets", StaticFiles(directory=os.path.join(vue_dir, "assets")), name="assets"
)


def some_dependency(inertia: InertiaDep) -> None:
inertia.share(message="hello from dependency")


@app.get("/", response_model=None)
async def index(inertia: InertiaDep) -> InertiaResponse:
props = {
"message": "hello from index",
"lazy_prop": lazy(lambda: "hello from lazy prop"),
}
return await inertia.render("IndexPage", props)


@app.get("/2", response_model=None)
async def other_page(inertia: InertiaDep) -> RedirectResponse:
inertia.flash("hello from index2 (through flash)", category="message")
return RedirectResponse(url="/3")


@app.get("/3", response_model=None, dependencies=[Depends(some_dependency)])
async def other_page_with_flashed_data(inertia: InertiaDep) -> InertiaResponse:
inertia.flash("hello from index3 (through flash)", category="message")
return await inertia.render("OtherPage")


class UserLogin(BaseModel):
email: EmailStr
password: str


@app.post("/login", response_model=None)
async def some_form(user: UserLogin, inertia: InertiaDep) -> RedirectResponse:
inertia.flash("form submitted", category="message")
return inertia.back()
Loading

0 comments on commit 6c75289

Please sign in to comment.