Search packages, repositories and app categories

Package detailsv0.1.0

annotated_shelf

powerful Dart library for generating REST APIs using annotations. With a simple and intuitive interface, you can easily build APIs that are fast, efficient, and easy to use.

macOSWindowsLinux
30-day downloads
126
Likes
23
Pub points
140

Install package

flutter pub add annotated_shelf

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

Annotated_Shelf is a powerful Dart library that simplifies the creation of REST APIs using annotations. It provides a clean, intuitive interface for defining routes, request handlers, and responses directly through code annotations. Built on top of the Shelf framework, it enables developers to generate efficient, type-safe APIs with minimal boilerplate. Ideal for rapid API development, it supports seamless integration with OpenAPI/Swagger documentation. The library runs on macOS, Windows, and Linux, making it suitable for cross-platform backend services.

Use cases

  • Building RESTful APIs with minimal setup
  • Generating OpenAPI documentation from code
  • Rapid prototyping of backend services
  • Creating type-safe endpoints using annotations
  • Integrating with existing Dart projects

Key features

  • Annotation-based route definition
  • Built on Shelf for high performance
  • Automatic OpenAPI/Swagger documentation
  • Supports request/response modeling
  • Cross-platform compatibility

Suitable for

  • Dart developers building backend APIs
  • Teams needing fast API scaffolding
  • Projects requiring OpenAPI integration
  • Developers preferring declarative coding style
  • Backend services targeting multiple platforms

Considerations

  • Requires familiarity with Dart annotations
  • Limited to Shelf-based server architecture
  • May need additional configuration for complex routing
  • Best suited for smaller to medium-sized APIs
  • Documentation generation depends on annotation accuracy

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

Annotated Shelf

Powerful, Lightweight, Fast development, Easy to learn

Documentation:

https://annotated-shelf.readthedocs.io/en/features-add_docs/index.html

Annotated Shelf is a powerful and lightweight Dart framework for generating REST APIs using annotations. Based on the popular Shelf library. Annotated Shelf provides a simple and intuitive interface for building APIs fast, efficiently, and easy to use without losing what Shelf can offer.

Features

  • Support for multiple HTTP methods and request types.
  • Support for File upload.
  • Automatic validation of request parameters.
  • Automatic Error responses based on exceptions.

Installation

To install Annotated Shelf, add it as a dependency in your pubspec.yaml file:

dependencies:
  annotated_shelf: ^0.1.0

Then, run pub get to install the package.

Getting Started

To use Annotated Shelf to create a REST API, import the library and annotate your models and controllers with the following annotations:

@RestAPI,
@GET,
@POST,
@PUT,
@DELETE,
@PATCH,

Annotated Shelf will automatically generate the necessary routes and endpoints based on the annotations you have provided.

Example

import 'package:annotated_shelf/annotated_shelf.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as io;

const _hostname = 'localhost';
const _port = 8080;
var itemsList = [Item("first item"), Item("second Item")];

class Item extends Payload {
  final String? name;

  Item(this.name);

  @override
  factory Item.fromJson(Map<String, dynamic> json) {
    return Item(json["name"]);
  }
  @override
  Map<String, dynamic> toJson() => {"name": name};
}

class TestForm extends Form {
  String name;
  int number;
  File image;

  TestForm(this.name, this.number, this.image);

  @override
  factory TestForm.fromJson(Map<String, dynamic> json) {
    return TestForm(
      json['name'] as String,
      json['number'] as int,
      File.fromJson(json['image'] as Map<String, dynamic>),
    );
  }

  @override
  Map<String, dynamic> toJson() => <String, dynamic>{
        'name': name,
        'number': number,
        'image': image,
      };
}

@RestAPI(baseUrl: '/to-do/list')
class ItemsAdaptor {
  @GET()
  List<Item> getAllItems(Request request) {
    return itemsList;
  }

  @GET(url: "/<itemName>")
  Item getItemByName(String itemName) {
    var index = itemsList.lastIndexWhere((element) => element.name == itemName);
    if (index >= 0) {
      return itemsList[index];
    } else {
      throw NotFoundError('item not found'); // this creates a 404 response
    }
  }

  @PUT(url: "/<itemName>")
  Item updateItem(Item item, String itemName) {
    var index = itemsList.lastIndexWhere((element) => element.name == itemName);
    if (index >= 0) {
      itemsList[index] = item;
      return getItemByName(item.name ?? '');
    } else {
      throw NotFoundError('item not found'); // this creates a 404 response
    }
  }

  @POST(url: "/")
  Response createNewItem(Item item) {
    var index =
        itemsList.lastIndexWhere((element) => element.name == item.name);
    if (index == -1) {
      itemsList.add(item);
      return Response(201); // pass a shelf response
    } else {
      throw BadRequestError('item with name in list');
    }
  }

  // examplo of uploading a file
  @POST(url: '/upload')
  Future<RestResponse> upload(TestForm form) async {
    print(form);
    return new RestResponse(201, {"msj": 'ok'}, "application/json");
  }
}

Future<void> main(List<String> args) async {
  var router = Cascade();

  router = await mount(ItemsAdaptor(), router);

  var server = await io.serve(router.handler, _hostname, _port);
  print('Serving at http://${server.address.host}:${server.port}');
}

/* 
-------------------------------------
mounting Instance of 'ItemsAdaptor'
adding get to-do/list
adding get to-do/list/<iteName>
adding put to-do/list/<iteName>
adding post to-do/list/
adding post to-do/list/upload
-------------------------------------
Serving at http://localhost:8080
*/

Contributing

We welcome contributions to Annotation Shelf! If you have an idea for a new feature or have found a bug, please open an issue on GitHub.

License

Annotation Shelf is released under the BSD-3-Clause. See LICENSE for details.

thanks

Special thanks to the Shelf project team for providing us with the opportunity to create servers using this beautiful language.

Categories

Dependencies

Dependency list 3 items

Source repository

Projects using this package

No data available yet.