diff --git a/CHANGELOG.md b/CHANGELOG.md index 674a7f5..0a11568 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# 1.0.2 + +- Added more documentations +- Fixes here and there + # 1.0.1 - Minor meta info changes diff --git a/lib/shelf_limiter.dart b/lib/shelf_limiter.dart index ec0d353..83bc8b2 100644 --- a/lib/shelf_limiter.dart +++ b/lib/shelf_limiter.dart @@ -1,8 +1,61 @@ +/// # Shelf Limiter +/// +/// A powerful and highly customizable rate limiter for shelf library, allowing you to easily manage and control request rates in your Dart server. +/// +/// `shelf_limiter` is a middleware package for the [Shelf](https://pub.dev/packages/shelf) +/// library in Dart that provides rate limiting capabilities. It allows you to restrict +/// the number of requests a client can make to your server within a specified time window, +/// making it useful for preventing abuse and ensuring fair usage of your API. +/// +/// This package offers several customization options to suit your needs, including custom +/// headers and responses. It integrates seamlessly into your existing Shelf pipeline. +/// +/// ## Example +/// +/// ```dart +/// import 'dart:convert'; +/// import 'package:shelf/shelf.dart'; +/// import 'package:shelf/shelf_io.dart' as io; +/// import 'package:shelf_limiter/shelf_limiter.dart'; +/// +/// void main() async { +/// final options = RateLimiterOptions( +/// headers: { +/// 'X-Custom-Header': 'Rate limited', +/// }, +/// onRateLimitExceeded: (request) async { +/// return Response( +/// 429, +/// body: jsonEncode({ +/// 'status': false, +/// 'message': "Uh, hm! Wait a minute, that's a lot of request.", +/// }), +/// headers: { +/// 'Content-Type': 'application/json', +/// }, +/// ); +/// }, +/// ); +/// +/// final limiter = shelfLimiter( +/// maxRequests: 5, +/// windowSize: const Duration(minutes: 1), +/// options: options, +/// ); +/// +/// final handler = +/// const Pipeline().addMiddleware(limiter).addHandler(_echoRequest); +/// +/// var server = await io.serve(handler, 'localhost', 8080); +/// print('Server listening on port ${server.port}'); +/// } +/// +/// Response _echoRequest(Request request) => Response.ok('Request received'); +/// ``` library; import 'dart:async'; import 'dart:collection'; -import 'dart:io'; import 'package:shelf/shelf.dart'; part 'src/shelf_limiter_base.dart'; diff --git a/lib/src/shelf_limiter_base.dart b/lib/src/shelf_limiter_base.dart index e3a15f4..74c3405 100644 --- a/lib/src/shelf_limiter_base.dart +++ b/lib/src/shelf_limiter_base.dart @@ -89,8 +89,8 @@ Middleware shelfLimiter({ // Extract client identifier (IP by default) final clientIdentifier = options?.clientIdentifierExtractor != null ? options!.clientIdentifierExtractor!(request) - : (request.context['shelf.io.connection_info'] as HttpConnectionInfo) - .remoteAddress + : ((request.context['shelf.io.connection_info']) as dynamic) + ?.remoteAddress .address; // Check if the client has exceeded the rate limit diff --git a/pubspec.yaml b/pubspec.yaml index 1257654..c43433c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: shelf_limiter description: A powerful and highly customizable rate limiter for shelf library, allowing you to easily manage and control request rates in your Dart server. -version: 1.0.1 +version: 1.0.2 repository: https://github.com/xooniverse/shelf_limiter issue_tracker: https://github.com/xooniverse/shelf_limiter/issues documentation: https://pub.dev/documentation/shelf_limiter/latest