dartapi_core 0.0.5
dartapi_core: ^0.0.5 copied to clipboard
Core utilities for building typed, structured REST APIs in Dart, including routing, validation, and middleware support.
π DartAPI - A Lightweight FastAPI-like Framework for Dart #
DartAPI is a lightweight and developer-friendly framework for building fast, modern, and scalable APIs using Dart.
π Features #
β
Fast and lightweight - Minimal dependencies, optimized for speed.
β
Easy to use - Simple setup and minimal boilerplate.
β
Configurable port - Start the server with a custom port (--port=<number>).
β
Dynamic routing - Automatically registers controllers and their routes.
β
Middleware support - Includes logging and future authentication middleware.
β
CLI Tool - Generate projects, controllers, and models using the dartapi CLI.
π§ Installation #
To use DartAPI globally, install it via Dart's package manager:
dart pub global activate dartapi
After activation, you can use the dartapi CLI to create projects and manage your API.
π¦ Creating a New API Project
dartapi create my_project
cd my_project
dart pub get
π Running the Server
You can start the API server using:
1οΈβ£ Default Port (8080)
dartapi run
1οΈβ£ Custom Port (8080)
dartapi run --port=3000
Alternatively, run it directly via Dart:
dart run bin/main.dart --port=3000
β Expected Output:
π Server running on http://localhost:3000
π₯ API Routes
The boilerplate comes with the following methods
| Method | Route | Description |
|---|---|---|
| GET | /users | Fetch list of users |
| POST | /users | Create a new user |
Example Request (Using cURL)
curl -X GET http://localhost:8080/users
β Response:
{"users": ["Christy", "Akash"]}
π Generating a Controller
dartapi generate controller Product
β Creates:
lib/src/controllers/product_controller.dart
The generated controller includes:
import 'package:shelf/shelf.dart';
import 'base_controller.dart';
class ProductController extends BaseController {
@override
List<RouteDefinition> get routes => [
RouteDefinition('GET', '/products', getAllProducts),
RouteDefinition('POST', '/products', createProduct),
];
Response getAllProducts(Request request) {
return Response.ok('{"products": ["Laptop", "Phone"]}', headers: {'Content-Type': 'application/json'});
}
Response createProduct(Request request) {
return Response.ok('{"message": "Product created"}', headers: {'Content-Type': 'application/json'});
}
}
β Now accessible at:
β’ GET /products
β’ POST /products
π Middleware
DartAPI includes middleware support. The default logging middleware logs all requests:
Example Middleware (lib/src/middleware/logging.dart)
import 'package:shelf/shelf.dart';
Middleware loggingMiddleware() {
return (Handler innerHandler) {
return (Request request) async {
print("π Request: \${request.method} \${request.requestedUri}");
final response = await innerHandler(request);
return response;
};
};
}
β Adding Middleware in server.dart:
final handler = Pipeline()
.addMiddleware(loggingMiddleware())
.addHandler(_router.handler.call);
π Database Setup (Planned Feature)
Currently, DartAPI provides a placeholder for database connections:
class Database {
static void connect() {
print('π Connecting to database...');
}
}
In future versions, we will support:
- β
PostgreSQL, SQLite, MongoDB
- β
Database models with dartapi generate model User
- β
Migrations (dartapi migrate db)
π― Planned Features
- π Swagger UI (/docs route for API documentation)
- π Authentication System (JWT Middleware)
- π WebSocket Support (/ws for real-time communication)
- π Database ORM Integration (PostgreSQL, SQLite, MongoDB)
- π Task Scheduling (Cron Jobs, Background Tasks)
- π Deployment Support (docker and dartapi deploy)
π License This package is open-source and licensed under the BSD-3-Clause License. #
π Get Started Now!
dartapi create my_project
cd my_project
dart pub get
dartapi run --port=8080
β Start building APIs with Dart! πππ
β Adding Auth! πππ
Add the dartapi_auth package.
dart pub add dartapi_auth
Currently there is support for JWT using Auth Middleware.
final jwtService = JwtService(
accessTokenSecret: 'super-secret-key',
refreshTokenSecret: 'super-refresh-secret',
issuer: 'dartapi',
audience: 'dartapi-users',
);
Add the authMiddleware Middle ware to the route definition.
RouteDefinition('GET', '/users', getAllUsers, middlewares: [authMiddleware(jwtService)]),