spanner 1.0.2 copy "spanner: ^1.0.2" to clipboard
spanner: ^1.0.2 copied to clipboard

Generic HTTP Router implementation, internally uses a Radix Tree (aka compact Prefix Tree), supports route params, wildcards.

spanner 🎢 #

Generic HTTP Router implementation, internally uses a Radix Tree (aka compact Prefix Tree), supports route params, wildcards.

import 'package:spanner/spanner.dart';
import 'package:test/test.dart';

void main() {
  test('spanner sample test', () {
    final routeHandler = () async {};

    final router = Spanner()
      ..addMiddleware('/user', #userMiddleware)
      ..addMiddleware('/a/<userId>', #anyUser)
      ..addRoute(HTTPMethod.GET, '/user', #currentUser)
      ..addRoute(HTTPMethod.GET, '/user/<userId>', 123)
      ..addRoute(HTTPMethod.GET, '/user/<file>.png/download', null)
      ..addRoute(HTTPMethod.GET, '/user/<file>.png/<user2>/hello', null)
      ..addRoute(HTTPMethod.GET, '/a/<userId>-static', routeHandler)
      ..addRoute(HTTPMethod.GET, '/b/<userId>.static', routeHandler);

    var result = router.lookup(HTTPMethod.GET, '/user');
    expect(result!.values, [#userMiddleware, #currentUser]);

    result = router.lookup(HTTPMethod.GET, '/user/24');
    expect(result?.params, {'userId': '24'});
    expect(result?.values, [#userMiddleware, 123]);

    result = router.lookup(HTTPMethod.GET, '/user/aws-image.png/download');
    expect(result?.params, {'file': 'aws-image'});

    result = router.lookup(HTTPMethod.GET, '/user/aws-image.png/A29384/hello');
    expect(result?.params, {'file': 'aws-image', 'user2': 'A29384'});

    result = router.lookup(HTTPMethod.GET, '/a/chima-static');
    expect(result?.values, [routeHandler]);
    expect(result?.params, {'userId': 'chima'});

    result = router.lookup(HTTPMethod.GET, '/b/codekeyz.static');
    expect(result?.values, [routeHandler]);
  });
}

7
likes
0
pub points
31%
popularity

Publisher

verified publisheryaroo.dev

Generic HTTP Router implementation, internally uses a Radix Tree (aka compact Prefix Tree), supports route params, wildcards.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

collection, equatable, meta

More

Packages that depend on spanner