搜尋套件、儲存庫與 APP 分類

套件詳情v0.1.0

annotated_shelf

功能強大的 Dart 庫,可使用註解產生 REST API。透過簡單直覺的介面,您可輕鬆建立快速、高效且易於使用的 API。

macOSWindowsLinux
近 30 天下載
126
喜歡數
23
Pub 分數
140

安裝套件

flutter pub add annotated_shelf

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

Annotated_Shelf 是一個強大的 Dart 庫,透過註解簡化了 REST API 的建立。它提供一種乾淨直覺的介面,可直接透過程式碼註解定義路由、請求處理器與回應。建構於 Shelf 框架之上,讓開發者能以最少的重複程式碼產生高效且類型安全的 API。非常適合快速 API 開發,支援與 OpenAPI/Swagger 文件的無縫整合。該庫可在 macOS、Windows 與 Linux 上執行,適用於跨平台後端服務。

適用情境

  • 使用最少的設定建構 RESTful API
  • 從程式碼產生 OpenAPI 文件
  • 後端服務的快速原型設計
  • 使用註解創建類型安全的端點
  • 與現有 Dart 項目整合

主要特色

  • 基於註解的路由定義
  • 基於架構建,高性能
  • 自動 OpenAPI/Swagger 文件
  • 支援請求/回應建模
  • 跨平台相容性

適合對象

  • Dart 開發人員建構後端 API
  • 需要快速 API 腳手架的團隊
  • 需要 OpenAPI 集成的項目
  • 偏好宣告式編碼風格的開發者
  • 面向多個平台的後端服務

使用建議

  • 需要熟悉 Dart 注解
  • 受限於基於貨架的伺服器架構
  • 可能需要額外的設定才能實現複雜的路由
  • 最適合中小型 API
  • 文件生成依賴於註釋的準確性

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

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.

所屬分類

依賴

依賴清單 3 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。