A FastAPI extension for dynamic and automatic route registration
Project description
FastAPI Dynamic Router
FastAPI extension that provides automatic and dynamic route registration for FastAPI applications, inspired by Next.js's file-system based routing.
Installation
pip install fastapi-dynamic-router
How to Use
Basic Setup
from fastapi import FastAPI
from fastapi_dynamic_router import DynamicRouter
# Create FastAPI app
app = FastAPI()
# Initialize the router
router = DynamicRouter(app)
# Register routes from a directory
router.register_routes('routes')
if __name__ == '__main__':
import uvicorn
uvicorn.run(app)
Route Examples
- Basic Route (
routes/root/__init__.py):
from fastapi import APIRouter
router = APIRouter()
@router.get('/')
def index():
return {'message': 'Welcome to the API'}
- Dynamic Parameter (
routes/users/[user_id]/__init__.py):
from fastapi import APIRouter
router = APIRouter()
@router.get('/')
def get_user(user_id: str):
return {'message': f'User details for ID: {user_id}'}
- Nested Routes (
routes/products/[productID]/reviews/__init__.py):
from fastapi import APIRouter
router = APIRouter()
@router.get('/')
def get_reviews(product_id: str):
return {'message': f'Reviews for product ID: {product_id}'}
Configuration Options
# Make routes case-insensitive
app.state.dynamic_router_config['case_sensitive'] = False
# Add a global prefix to all routes
app.state.dynamic_router_config['prefix'] = '/api/v1'
Directory Structure
The router follows a convention-based approach where your directory structure maps directly to URL routes:
๐ฆ routes
โโโ ๐ Root
โ โโโ ๐ __init__.py โ ๐ /
โ โโโ ๐ version
โ โ โโโ ๐ __init__.py โ ๐ /version
โ โโโ ๐ about
โ โโโ ๐ __init__.py โ ๐ /about
โ
โโโ ๐ users
โ โโโ ๐ __init__.py โ ๐ /users
โ โโโ ๐ [userID] โ ๐ซ Dynamic Parameter
โ โ โโโ ๐ __init__.py โ ๐ /users/{user_id}
โ โ โโโ ๐ profile
โ โ โโโ ๐ __init__.py โ ๐ /users/{user_id}/profile
โ โโโ ๐ settings
โ โโโ ๐ __init__.py โ ๐ /users/settings
โ
โโโ ๐ products
โโโ ๐ __init__.py โ ๐ /products
โโโ ๐ [productID] โ ๐ซ Dynamic Parameter
โโโ ๐ __init__.py โ ๐ /products/{product_id}
Route Mapping Examples
| Directory Structure | Generated Route | Type |
|---|---|---|
Root/__init__.py |
/ |
Static Route |
users/__init__.py |
/users |
Static Route |
users/[userID]/__init__.py |
/users/{user_id} |
Dynamic Route |
Products/[productID]/__init__.py |
/products/{product_id} |
Dynamic Route |
Dynamic Parameters
Dynamic parameters are defined using square brackets in directory names:
- ๐
[userID]โ{user_id} - ๐
[productID]โ{product_id} - ๐
[categoryName]โ{category_name}
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_dynamic_router-0.1.4.tar.gz.
File metadata
- Download URL: fastapi_dynamic_router-0.1.4.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
207bdf65115ddb5927d1de1c8bfd32934d357e15c887465fbdeb1f64f0e7622f
|
|
| MD5 |
a4538a25c5bad9232af49d8879faffe5
|
|
| BLAKE2b-256 |
4b0c65b9bf11c59a2aae0b34cf4104f8fa5ee418f0300fb96130f9a356c8c32a
|
File details
Details for the file fastapi_dynamic_router-0.1.4-py3-none-any.whl.
File metadata
- Download URL: fastapi_dynamic_router-0.1.4-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0a13994e61b0d0f8e138f329d2d1030e8ab9b8d75b5c8c96c71ba121cb24950c
|
|
| MD5 |
a4ed9697c95a50aa24fd48634eca24a7
|
|
| BLAKE2b-256 |
7ae13e476670956f6c58acb53cb30ffb42920082bb8294fa62b6fc6efaa8b23b
|