Skip to main content

Nestipy is a Python framework built on top of FastAPI/BlackSheep that follows the modular architecture of NestJS

Project description

Nestipy Logo

Version Python License

Description

Nestipy is a Python framework built on top of FastAPI that follows the modular architecture of NestJS

Under the hood, Nestipy makes use of FastAPI, but also provides compatibility with a wide range of other libraries, like Blacksheep, allowing for easy use of the myriad of third-party plugins which are available.

Getting started

    pip install nestipy-cli
    nestipy new my_app
    cd my_app
    nestipy start --dev
    ├── src
    │    ├── __init__.py
    ├── app_module.py
    ├── app_controller.py
    ├── app_service.py
    ├── main.py
    ├── cli.py
    ├── pyproject.yml
    ├── uv.lock
    ├── README.md
    
       

The main.py file contains an instance of the application and bootstraps it with app.listen() (Granian under the hood).

from granian.constants import Interfaces

from nestipy.core import NestipyFactory

from app_module import AppModule

app = NestipyFactory.create(AppModule)

if __name__ == '__main__':
    app.listen(
        "main:app",
        host="0.0.0.0",
        port=8000,
        interface=Interfaces.ASGI,
        reload=True,
    )

Embed mode (no import string, fewer options like reload/workers):

if __name__ == '__main__':
    app.listen(host="0.0.0.0", port=8000)

Inside module, we got,

from app_command import AppCommand
from app_controller import AppController
from app_service import AppService

from nestipy.common import Module


@Module(
    controllers=[AppController],
    providers=[AppService, AppCommand]
)
class AppModule: ...

For controller, we got something like

from typing import Annotated

from nestipy.common import Controller, Get, Post, Put, Delete
from nestipy.ioc import Inject, Body, Param

from app_service import AppService


@Controller()
class AppController:
    service: Annotated[AppService, Inject()]

    @Get()
    async def get(self) -> str:
        return await self.service.get()

    @Post()
    async def post(self, data: Annotated[dict, Body()]) -> str:
        return await self.service.post(data=data)

    @Put("/{app_id}")
    async def put(
        self, app_id: Annotated[int, Param("app_id")], data: Annotated[dict, Body()]
    ) -> str:
        return await self.service.put(id_=app_id, data=data)

    @Delete("/{app_id}")
    async def delete(self, app_id: Annotated[int, Param("app_id")]) -> None:
        await self.service.delete(id_=app_id)

And, for app_service.py

from nestipy.common import Injectable


@Injectable()
class AppService:
    @classmethod
    async def get(cls):
        return "test"

    @classmethod
    async def post(cls, data: dict):
        return "test"

    @classmethod
    async def put(cls, id_: int, data: dict):
        return "test"

    @classmethod
    async def delete(cls, id_: int):
        return "test"

Web Actions Security

Nestipy Web actions support guard-based security similar to NestJS. You can enable default guards globally or attach guards per action.

Enable security presets globally:

from nestipy.common import Module
from nestipy.web import ActionsModule, ActionsOption


@Module(
    imports=[
        ActionsModule.for_root(
            ActionsOption(
                path="/_actions",
                csrf_enabled=True,
            )
        )
    ],
)
class AppModule: ...

Attach guards/permissions per action:

from nestipy.web import (
    action,
    ActionAuth,
    ActionPermissions,
    UseActionGuards,
    OriginActionGuard,
    CsrfActionGuard,
    ActionSignatureGuard,
)


class AppActions:
    @action()
    @ActionAuth("admin", guards=[OriginActionGuard, CsrfActionGuard])
    async def secure_action(self, payload: dict) -> str:
        return "ok"

    @action()
    @UseActionGuards(ActionSignatureGuard)
    async def signed_action(self, payload: dict) -> str:
        return "ok"

When CSRF protection is enabled, call the CSRF endpoint once on the client to set the cookie:

import { fetchCsrfToken } from './actions';

await fetchCsrfToken('/_actions/csrf');

Environment presets (read at import time):

  • NESTIPY_ACTION_SECURITY=1 enable defaults
  • NESTIPY_ACTION_ALLOWED_ORIGINS=http://localhost:5173,http://127.0.0.1:5173
  • NESTIPY_ACTION_CSRF=1 / 0
  • NESTIPY_ACTION_SIGNATURE_SECRET=...
  • NESTIPY_ACTION_PERMISSIONS=1

CLI convenience flags map to these env vars:

nestipy start --action-security --action-origins "http://localhost:5173" --action-csrf

Documentation

View full documentation from here.

Support

Nestipy is an MIT-licensed open source project. It can grow thanks to the sponsors and support from the amazing backers. If you'd like to join them, please [read more here].

Stay in touch

License

Nestipy is MIT licensed.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nestipy-0.10.0.tar.gz (6.1 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nestipy-0.10.0-py3-none-any.whl (6.2 MB view details)

Uploaded Python 3

File details

Details for the file nestipy-0.10.0.tar.gz.

File metadata

  • Download URL: nestipy-0.10.0.tar.gz
  • Upload date:
  • Size: 6.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.21

File hashes

Hashes for nestipy-0.10.0.tar.gz
Algorithm Hash digest
SHA256 d1d781a6138976213b036b62d83b0a4be7e1d9369a9358ccafca64beead48cfb
MD5 ce07e43310ab22a44cb7df1057e6466d
BLAKE2b-256 287a40b358943533471492418dbc387e40769acdaabf9510baa538d612447a44

See more details on using hashes here.

File details

Details for the file nestipy-0.10.0-py3-none-any.whl.

File metadata

  • Download URL: nestipy-0.10.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.21

File hashes

Hashes for nestipy-0.10.0-py3-none-any.whl
Algorithm Hash digest
SHA256 04dcf7edbd23c0f59546480adb7f774ae58988c7ee2f925a18b2fdf7ac2304e9
MD5 d4f2d3f948392e61676558995f0320f0
BLAKE2b-256 4b5762eebe06610aab8e54ce59362d6fc644fc45e1edba668fbd6394e948b941

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page