Leistungsstarke Dart-Bibliothek zum Erstellen von REST-APIs mithilfe von Anmerkungen. Mit einer einfachen und intuitiven Schnittstelle können Sie schnell, effiziente und benutzerfreundliche APIs erstellen.
Aus öffentlichen Paketdaten erstellte Orientierung.
Annotated_Shelf ist eine leistungsstarke Dart-Bibliothek, die die Erstellung von REST-APIs mithilfe von Anmerkungen vereinfacht. Sie bietet eine saubere, intuitive Schnittstelle zum Definieren von Routen, Anfragehandlern und Antworten direkt über Codeanmerkungen. Auf der Basis des Shelf-Frameworks ermöglicht sie Entwicklern die Erzeugung effizienter, typsicherer APIs mit minimalem Boilerplate. Ideal für schnelle API-Entwicklung unterstützt sie nahtlose Integration mit OpenAPI/Swagger-Dokumentation. Die Bibliothek läuft auf macOS, Windows und Linux und ist somit für plattformübergreifende Backend-Dienste geeignet.
Anwendungsfälle
RESTful-APIs mit minimalem Aufwand erstellen
OpenAPI-Dokumentation aus Code generieren
Schnellprototyping von Backend-Diensten
Typsichere Endpunkte mithilfe von Annotationen erstellen
Integration mit bestehenden Dart-Projekten
Hauptmerkmale
Routing-Definition basierend auf Annotationen
Auf der Shelf basiert für hohe Leistung
Automatische OpenAPI/Swagger-Dokumentation
Unterstützt Anfrage/Antwort-Modellierung
Plattformübergreifende Kompatibilität
Geeignet für
Dart-Entwickler, die Backend-APIs erstellen
Teams, die schnelles API-Scaffolding benötigen
Projekte, die eine OpenAPI-Integration erfordern
Entwickler, die einen deklarativen Programmierstil bevorzugen
Backend-Dienste für mehrere Plattformen
Hinweise
Erfahrung mit Dart-Anmerkungen erforderlich
Beschränkt auf Shelf-basierte Server-Architektur
Könnte zusätzliche Konfiguration für komplexes Routing erfordern
Am besten geeignet für kleinere bis mittlere APIs
Die Dokumentationserstellung hängt von der Genauigkeit der Annotation ab
Original-README
Originaler README-Schnappschuss aus dem verknüpften Quell-Repository. Aktuelle Inhalte finden Sie im Repository.
Powerful, Lightweight, Fast development, Easy to learn
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")];
classItemextendsPayload{
finalString? name;
Item(this.name);
@overridefactoryItem.fromJson(Map<String, dynamic> json) {
returnItem(json["name"]);
}
@overrideMap<String, dynamic> toJson() => {"name": name};
}
classTestFormextendsForm{
String name;
int number;
File image;
TestForm(this.name, this.number, this.image);
@overridefactoryTestForm.fromJson(Map<String, dynamic> json) {
returnTestForm(
json['name'] asString,
json['number'] asint,
File.fromJson(json['image'] asMap<String, dynamic>),
);
}
@overrideMap<String, dynamic> toJson() => <String, dynamic>{
'name': name,
'number': number,
'image': image,
};
}
@RestAPI(baseUrl: '/to-do/list')
classItemsAdaptor{
@GET()
List<Item> getAllItems(Request request) {
return itemsList;
}
@GET(url: "/<itemName>")
ItemgetItemByName(String itemName) {
var index = itemsList.lastIndexWhere((element) => element.name == itemName);
if (index >= 0) {
return itemsList[index];
} else {
throwNotFoundError('item not found'); // this creates a 404 response
}
}
@PUT(url: "/<itemName>")
ItemupdateItem(Item item, String itemName) {
var index = itemsList.lastIndexWhere((element) => element.name == itemName);
if (index >= 0) {
itemsList[index] = item;
returngetItemByName(item.name ?? '');
} else {
throwNotFoundError('item not found'); // this creates a 404 response
}
}
@POST(url: "/")
ResponsecreateNewItem(Item item) {
var index =
itemsList.lastIndexWhere((element) => element.name == item.name);
if (index == -1) {
itemsList.add(item);
returnResponse(201); // pass a shelf response
} else {
throwBadRequestError('item with name in list');
}
}
// examplo of uploading a file@POST(url: '/upload')
Future<RestResponse> upload(TestForm form) async {
print(form);
returnnewRestResponse(201, {"msj": 'ok'}, "application/json");
}
}
Future<void> main(List<String> args) async {
var router = Cascade();
router = awaitmount(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.