dartapi_core 0.0.5 copy "dartapi_core: ^0.0.5" to clipboard
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)]),
1
likes
160
points
21
downloads

Publisher

verified publisherakashgk.com

Weekly Downloads

Core utilities for building typed, structured REST APIs in Dart, including routing, validation, and middleware support.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

shelf

More

Packages that depend on dartapi_core