route_provider 4.0.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 59

dart-routeprovider #

Installation #

Add it to your dependencies

dependencies:
  route_provider: ^4.0.0

and install the package

$ pub get

Usage #

import 'dart:io';
import 'package:route_provider/route_provider.dart';
main() {
    Auth freeForAll = new StaticAuth(authed: true);
    Auth userAuth = new MyAuth();
    Responser jsonResponser = new JsonResponse();
    HttpServer.bind(InternetAddress.loopbackIPv4,8080).then((HttpServer server){
        new Router(server)
        ..route(
            url: "/",
            responser: new FileResponse("docroot/index.html"),
            auth: freeForAll
        )
        ..route(
            url: '/assets/**',
            responser: new FolderResponse("docroot/assets/"),
            auth: freeForAll
        )
        ..route(
            url: "/impress",
            responser: new FileResponse("docroot/impress.html"),
            auth: freeForAll
        )
        ..route(
            url: "/api/data/:id",
            controller: new DataRestApiController(),
            responser: jsonResponser,
            auth: userAuth
        )
        ..start();
    }).catchError((e) => print(e.toString()));
}

Contributing #

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request

Credits #

Robert Beyer 4sternrb@googlemail.com

License #

MIT

4.0.0 (2019-07-01) #

  • add tests
  • rename classes
  • RestApiController set CORS-header as default

3.1.6+5 (2019-06-26) #

  • add .editorconfig and change homepage url to https

3.1.6+4 (2019-06-26) #

  • add pedantic dev_dependency, dartfmt, dartanalyzer

3.1.6+3 (2019-06-25) #

  • more clean up to increase pub scoring

3.1.6+2 (2019-06-25) #

  • clean up with dartanalyzer

3.1.6+1 (2019-06-25) #

  • Prepare for upcoming change to File.openRead()

3.1.6 (2019-01-13) #

  • add analysis_options

3.1.5 (2019-01-13) #

  • change description in pubspec

3.1.4 (2019-01-13) #

  • bugfix

3.1.3 (2019-01-13) #

  • add example
  • change description in pubspec

3.1.2 (2019-01-13) #

  • reformat the code

3.1.1 (2019-01-13) #

  • bugfixing

3.1.0 (2019-01-13) #

  • add FolderResponse Class

3.0.0 (2019-01-09) #

  • static content is now handled on anonther way

2.0.5 (2019-01-06) #

  • dependency update: increase mime_type to version ^0.2.2

2.0.4 (2019-01-06) #

  • if auth if null, we send an 401 instead of 403 now

2.0.3 (2019-01-05) #

  • bugfixing

2.0.2 (2019-01-05) #

  • bugfixing

2.0.1 (2019-01-05) #

  • bugfixing

2.0.0 (2019-01-05) #

  • change to dart 2.1.0

1.0.1 (2018-01-14) #

  • remove basepath handling while static-content detection

1.0.0 (2018-01-14) #

  • add RedirectResponse to make simple redirects
  • dependency upgrade: increase mime_type to version 0.2.1
  • add enviroment sdk constraint to dart ">=1.8.0 <2.0.0"

0.3.6 (2016-05-13) #

  • add WebSocketController for handle websockets

0.3.5 (2016-05-13) #

  • add NoneResponse responsehandler

0.3.4 (2015-10-21) #

  • add AuthResponse to ApiRestController

0.3.3 (2015-10-16) #

  • change auth signature and handling

0.3.2 (2015-10-16) #

  • update tests

0.3.1 (2015-10-16) #

  • parsing http-request and params to auth handler

0.3.0 (2015-10-16) #

  • add the optional named parameter 'auth' (interface Auth) to the route method for authentication possibilities. it checks before the controller and responsehandler do there work if the call is authenticated - default is true.

0.2.0 (2015-08-27) #

  • rename RouteControllerEmpty to EmptyRouteController
  • add JsonResponser
  • add ApiRestController
  • change method signature of .route by using named parameters instead of map

0.1.13 #

  • Adding helfer functions to route-controller for parsing body-data like post-values

0.1.9 (2015-05-03) #

Feature:

  • add RouteError for transporting http-status-codes and messages to this layer; to handle error output not for your own

0.1.7 (2015-05-03) #

Feature:

  • add async/await handling

0.1.6 (2015-04-24) #

Feature:

  • RouteControllers execute-methode gets the request as parameter

Test:

  • add await expressions and async methods

0.1.5 (2015-04-23) #

Feature:

  • FileResponseHandler now sends file content as stream to response (@4stern)

0.1.4 (2015-04-23) #

Tests:

  • adding testfile to test the provider (@4stern)

Bugfixes:

  • fix content root bug (@4stern)

example/example.dart

import 'dart:async';
import 'dart:io';
import 'package:route_provider/route_provider.dart';

class RouteControllerError extends Controller {
  RouteControllerError();

  @override
  Future<Map> execute(HttpRequest request, Map params, {AuthResponse authResponse}) async {
    throw new RouteError(HttpStatus.notFound, "ERROR");
  }
}

class APIController extends RestApiController {
  @override
  Future<Map> onGet(HttpRequest request, Map params, {AuthResponse authResponse}) async {
    throw new RouteError(HttpStatus.internalServerError, 'Not supported');
  }
}

class MyAuth implements Auth {
  bool authed = false;
  MyAuth({this.authed = false});

  @override
  Future<AuthResponse> isAuthed(HttpRequest request, Map params) async => this.authed ? new StaticAuthResponse() : null;
}

Future main() async {
  HttpServer server = await HttpServer.bind(InternetAddress.loopbackIPv4, 4040);

  new Router(server)
    ..route(url: '/', responser: new FileResponse("docroot/home.html"), auth: new MyAuth(authed: true))
    ..route(url: '/', responser: new FolderResponse("docroot/"), auth: new MyAuth(authed: true))
    ..route(url: '/img', responser: new FolderResponse("docroot/assets/img"), auth: new MyAuth(authed: true))
    ..route(url: '/assets/**', responser: new FolderResponse("docroot/assets/"), auth: new MyAuth(authed: true))
    ..route(url: '/js/**', responser: new FolderResponse("docroot/code/"), auth: new MyAuth(authed: true))
    ..route(
        url: '/error',
        controller: new RouteControllerError(),
        responser: new FileResponse("docroot/home.html"),
        auth: new MyAuth(authed: true))
    ..route(
        url: '/error2',
        controller: new APIController(),
        responser: new FileResponse("docroot/home.html"),
        auth: new MyAuth(authed: true))
    ..route(
        url: '/noauth',
        controller: new APIController(),
        responser: new FileResponse("docroot/home.html"),
        auth: new MyAuth(authed: false))
    ..start();

  print('listening on localhost, port ${server.port}');
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  route_provider: ^4.0.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:route_provider/route_provider.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
40
Health:
Code health derived from static analysis. [more]
64
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
59
Learn more about scoring.

We analyzed this package on Nov 22, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, other

Primary library: package:route_provider/route_provider.dart with components: io.

Health issues and suggestions

Fix lib/src/Controller.dart. (-26.12 points)

Analysis of lib/src/Controller.dart failed with 1 error, 3 hints:

line 52 col 23: The argument type 'Utf8Decoder' can't be assigned to the parameter type 'StreamTransformer<Uint8List, dynamic>'.

line 50 col 36: Unnecessary new keyword.

line 57 col 13: Unnecessary new keyword.

line 77 col 33: Unnecessary new keyword.

Fix lib/src/controllers/RestApiController.dart. (-4.89 points)

Analysis of lib/src/controllers/RestApiController.dart reported 10 hints, including:

line 15 col 11: Unnecessary new keyword.

line 19 col 11: Unnecessary new keyword.

line 23 col 11: Unnecessary new keyword.

line 27 col 11: Unnecessary new keyword.

line 31 col 11: Unnecessary new keyword.

Fix lib/src/Router.dart. (-4.41 points)

Analysis of lib/src/Router.dart reported 9 hints, including:

line 7 col 37: Unnecessary new keyword.

line 8 col 39: Unnecessary new keyword.

line 16 col 20: Unnecessary new keyword.

line 19 col 14: Unnecessary new keyword.

line 28 col 38: Unnecessary new keyword.

Fix additional 7 files with analysis or formatting issues. (-5.49 points)

Additional issues in the following files:

  • lib/src/responsers/JsonResponse.dart (3 hints)
  • lib/src/controllers/WebSocketController.dart (2 hints)
  • lib/src/responsers/FileResponse.dart (2 hints)
  • lib/src/responsers/FolderResponse.dart (2 hints)
  • lib/src/Response.dart (1 hint)
  • lib/src/auth/StaticAuth.dart (1 hint)
  • lib/src/responsers/RedirectResponse.dart (Run dartfmt to format lib/src/responsers/RedirectResponse.dart.)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
mime_type ^0.2.2 0.2.4
Dev dependencies
build_runner ^1.6.1
build_web_compilers ^2.1.1
http ^0.12.0+2
pedantic ^1.7.0
test ^1.6.4