Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: medz/routingkit
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v4.1.1
Choose a base ref
...
head repository: medz/routingkit
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v5.0.0
Choose a head ref
  • 10 commits
  • 21 files changed
  • 1 contributor

Commits on Mar 3, 2025

  1. chore: WIP

    medz committed Mar 3, 2025
    Copy the full SHA
    f9dce16 View commit details
  2. Copy the full SHA
    fccc1d2 View commit details
  3. Copy the full SHA
    fc1990d View commit details
  4. Copy the full SHA
    18e7750 View commit details
  5. Copy the full SHA
    5e32f6b View commit details
  6. Copy the full SHA
    f508ad8 View commit details
  7. Copy the full SHA
    5d16d1d View commit details
  8. Update documentation for 5.0.0 release: - Revised README with OO API …

    …examples - Translated CHANGELOG to English - Added llms.txt for AI assistance
    medz committed Mar 3, 2025
    Copy the full SHA
    d41018e View commit details
  9. Remove unused test utility import from test files

    medz committed Mar 3, 2025
    Copy the full SHA
    f52a2dc View commit details
  10. Merge pull request #10 from medz/v5-refactor

    RoutingKit 5.0.0: OO API Redesign with Enhanced Documentation
    medz authored Mar 3, 2025

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    297b0c5 View commit details
39 changes: 38 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,41 @@
## v4.1.1
## 5.0.0

### Breaking Changes

- Complete API restructuring from functional to object-oriented style
- Core operations are now methods of the Router class instead of standalone functions
- Removed exported operation functions such as addRoute, findRoute, etc.

### New Features

- Introduced Router class as the main entry point
- Provided a more concise chainable API
- Optimized internal implementation with clearer code structure

### Migration Guide

Migrating from v4.x to v5.0.0:

```diff
import 'package:routingkit/routingkit.dart';

- const router = createRouter();
+ final router = createRouter<String>();

- addRoute(router, 'get', '/path', 'data');
+ router.add('get', '/path', 'data');

- findRoute(router, 'get', '/path');
+ router.find('get', '/path');

- findAllRoutes(router, 'get', '/path');
+ router.findAll('get', '/path');

- removeRoute(router, 'get', '/path');
+ router.remove('get', '/path');
```

## 4.1.1

- **fix**: fix: remove named wildcard routes
- **chrome**: bump lints from 4.0.0 to 5.1.0
153 changes: 132 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -9,9 +9,17 @@
</p>

<p align="center">
Routing Kit - Lightweight and fast router for Dart.
RoutingKit - A lightweight, high-performance router for Dart with an elegant object-oriented API.
</p>

## Features

- 🚀 **High Performance**: Optimized route matching using a trie-based structure
- 🧩 **Flexible Routes**: Support for static, parameterized, and wildcard routes
- 💪 **Type Safety**: Generic typing for your route handlers
- 🔍 **Comprehensive Matching**: Find single or all matching routes
- 🧰 **Object-Oriented API**: Clean, chainable methods for route management

## Installation

Run this command:
@@ -28,40 +36,143 @@ flutter pub add routingkit

## Usage

### Create a router instance and insert routes
### Creating a Router

```dart
import 'package:routingkit/routingkit.dart';
// Create a typed router (recommended)
final router = createRouter<String>();
// For handling different data types
final dynamicRouter = createRouter<dynamic>();
```

### Adding Routes

RoutingKit supports various route patterns:

```dart
import "routingkit";
// Static routes
router.add('GET', '/users', 'Users list handler');
const router = createRouter();
// Parameter routes (with named parameters)
router.add('GET', '/users/:id', 'User details handler');
addRoute(router, 'get', '/path', 'Static path');
addRoute(router, 'get', '/path/:name', 'Param route');
addRoute(router, 'get', '/path/*', 'Unnamed param route');
addRoute(router, 'get', '/path/**', 'Wildcard Route');
addRoute(router, 'get', '/path/**:rset', 'Named wildcard route');
addRoute(router, 'get', '/files/:dir/:filename.:format,v:version', 'Mixed Route');
// Multiple parameters
router.add('GET', '/users/:id/posts/:postId', 'User post handler');
// Optional format parameter
router.add('GET', '/files/:filename.:format?', 'File handler');
// Wildcard routes
router.add('GET', '/assets/**', 'Static assets handler');
// Named wildcard segments
router.add('GET', '/docs/**:path', 'Documentation handler');
// Method-specific routes
router.add('POST', '/users', 'Create user handler');
router.add('PUT', '/users/:id', 'Update user handler');
router.add('DELETE', '/users/:id', 'Delete user handler');
```

### Match route to access matched data
### Matching Routes

Find the first matching route:

```dart
// {data: Static path}
findRoute(router, 'get', '/path')
final match = router.find('GET', '/users/123');
// {data: Param route, params: {name: seven}}
findRoute(router, 'get', '/path/seven')
if (match != null) {
print('Handler: ${match.data}');
print('Parameters: ${match.params}'); // {id: 123}
}
```

// {data: Wildcard Route, params: {_: foo/bar/baz}}
findRoute(router, 'get', '/path/foo/bar/baz')
Find all matching routes (useful for middleware):

// {data: Mixed Route, params: {dir: dart, filename: pubspec, format: yaml, version: 1}}
findRoute(router, 'get', '/files/dart/pubspec.yaml,v1')
```dart
final matches = router.findAll('GET', '/users/123/settings');
// `null`, No match.
findRoute(router, 'get', '/')
for (final match in matches) {
print('Handler: ${match.data}');
print('Parameters: ${match.params}');
}
```

### Removing Routes

```dart
// Remove a specific route
router.remove('GET', '/users/:id');
// Remove all routes for a specific method
router.remove('POST', null);
```

## Example Applications

### HTTP Server Routing

```dart
import 'dart:io';
import 'package:routingkit/routingkit.dart';
void main() async {
final router = createRouter<Function>();
// Define routes with handler functions
router.add('GET', '/', (req, res) => res.write('Home page'));
router.add('GET', '/users', (req, res) => res.write('Users list'));
router.add('GET', '/users/:id', (req, res) => res.write('User ${req.params['id']}'));
final server = await HttpServer.bind('localhost', 8080);
print('Server running on http://localhost:8080');
await for (final request in server) {
final response = request.response;
final path = request.uri.path;
final method = request.method;
final match = router.find(method, path);
if (match != null) {
final handler = match.data;
// Add params to request for handler access
(request as dynamic).params = match.params;
handler(request, response);
} else {
response.statusCode = HttpStatus.notFound;
response.write('404 Not Found');
}
await response.close();
}
}
```

## For AI Assistance

RoutingKit includes an `llms.txt` file at the root of the repository. This file is specifically designed to help Large Language Models (LLMs) understand the project structure and functionality.

If you're using AI tools like GitHub Copilot, Claude, or ChatGPT to work with this codebase, you can point them to the `llms.txt` file for better context and more accurate assistance.

```dart
// Example: Asking an AI about the project
"Please read the llms.txt file in this repository to understand the RoutingKit structure"
```

The file provides AI-friendly documentation including:
- Project overview and core concepts
- Code structure explanation
- API reference and examples
- Links to relevant implementation files

## Migration from v4.x

See the [Migration Guide](https://github.com/medz/routingkit/blob/main/CHANGELOG.md#migration-guide) in the changelog.

## License

RoutingKit is open-sourced software licensed under the [MIT license](https://github.com/medz/routingkit?tab=MIT-1-ov-file).
19 changes: 10 additions & 9 deletions example/main.dart
Original file line number Diff line number Diff line change
@@ -3,14 +3,15 @@ import 'package:routingkit/routingkit.dart';
void main() {
final router = createRouter<String>();

addRoute(router, 'GET', '/path', 'static route');
addRoute(router, 'POST', '/path/:name', 'name route');
addRoute(router, 'GET', '/path/foo/**', 'wildcard route');
addRoute(router, 'GET', '/path/foo/**:name', 'named wildcard route');
router.add('GET', '/path', 'static route');
router.add('POST', '/path/:name', 'name route');
router.add('GET', '/path/foo/**', 'wildcard route');
router.add('GET', '/path/foo/**:name', 'named wildcard route');

print(findRoute(router, 'GET', '/path')); // => { data: static route }
print(findRoute(router, 'POST', '/path/cady')); // => { data: name route, }
print(findRoute(
router, 'GET', '/path/foo/bar/baz')); // => { data: wildcard route }
print(findRoute(router, 'GET', '/')); // => null, not found.
print(router.find('GET', '/path')); // => { data: static route }
print(router.find(
'POST', '/path/cady')); // => { data: name route, params: {name: cady} }
print(router.find('GET',
'/path/foo/bar/baz')); // => { data: wildcard route, params: {_: bar/baz} }
print(router.find('GET', '/')); // => null, not found.
}
24 changes: 12 additions & 12 deletions lib/routingkit.dart
Original file line number Diff line number Diff line change
@@ -9,19 +9,19 @@
/// - Type-safe route handlers
///
/// Key components:
/// - [RouterContext]: The main router instance
/// - [addRoute]: Add new routes to the router
/// - [findRoute]: Find a single matching route
/// - [findAllRoutes]: Find all matching routes
/// - [removeRoute]: Remove a route from the router
/// - [Router]: The main router class for managing routes
/// - [createRouter]: Creates a new router instance
///
/// Example usage:
/// ```dart
/// final router = createRouter<String>();
/// router.add('GET', '/path', 'static route');
/// router.add('POST', '/path/:name', 'name route');
/// print(router.find('GET', '/path')); // => {data: static route}
/// ```
///
/// ![Pub version](https://img.shields.io/pub/v/routingkit?logo=dart)
library routingkit;

export 'src/types.dart';
export 'src/context.dart';

export 'src/operations/add_route.dart';
export 'src/operations/find_all_routes.dart';
export 'src/operations/find_route.dart';
export 'src/operations/remove_route.dart';
export 'src/router.dart';
export 'src/types.dart' show MatchedRoute;
26 changes: 0 additions & 26 deletions lib/src/_internal/node_impl.dart

This file was deleted.

38 changes: 0 additions & 38 deletions lib/src/_internal/utils.dart

This file was deleted.

17 changes: 0 additions & 17 deletions lib/src/context.dart

This file was deleted.

Loading