パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v0.1.0

annotated_shelf

アノテーションを使用してREST APIを生成するための強力なDartライブラリです。シンプルで直感的なインターフェースにより、高速で効率的かつ使いやすいAPIを簡単に構築できます。

macOSWindowsLinux
30日間ダウンロード
126
いいね数
23
Pub ポイント
140

パッケージをインストール

flutter pub add annotated_shelf

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

Annotated_Shelf は、アノテーションを使用して REST API の作成を簡素化する強力な Dart ライブラリです。ルート、リクエストハンドラー、レスポンスをコードのアノテーションを通じて直接定義できる、洗練された直感的なインターフェースを提供します。Shelf フレームワークの上に構築されており、最小限のボイラープレートで効率的かつ型安全な API を生成できます。迅速な API 開発に最適で、OpenAPI/Swagger ドキュメントとのシームレスな統合もサポートしています。macOS、Windows、Linux で動作するため、クロスプラットフォームのバックエンドサービスに適しています。

用途

  • 最小限のセットアップで RESTful API を構築する
  • コードからOpenAPIドキュメントを生成する
  • バックエンドサービスの迅速なプロトタイピング
  • アノテーションを使用して型安全なエンドポイントを作成する
  • 既存のDartプロジェクトとの統合

主な機能

  • アノテーションベースのルート定義
  • 高性能のためのシェルフ上に構築
  • 自動 OpenAPI/Swagger ドキュメント
  • リクエスト/レスポンスモデリングをサポート
  • クロスプラットフォーム互換性

適している対象

  • Dart開発者がバックエンドAPIを構築しています
  • 迅速なAPIスcaffoldが必要なチーム
  • 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 件

ソースリポジトリ

このパッケージを使用しているプロジェクト

データはまだありません。