dartness 0.4.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 43

Dartness

Minimalist, middleware based, web framework

import 'package:dartness/dartness.dart';

void main() {

  final app = new Dartness();
  final router = new Router();
  
  router.get('/', (Context context) async => null);
  
  app.use(router);
  
  app.listen(port: 4040);

}
  • uses middleware to create working flow
  • simple

Basic features:

  • all parts is middleware
  • errors can be captured on any step
  • can be used simple dynamic routes
  • router skips followed routes, if one was matched
  • middleware can be grouped into modules

Roadmap:

  • add binding routes to class methods
  • add logger
  • add nester routes
  • add middleware chains
  • add post/get parameters as arguments to routes
  • function result as response
  • use isolates

Full Example:

import 'package:dartness/dartness.dart';
  
void main() {
 
  final app = new Dartness();
  
  app.use((Context context) async {
    // will be called first
  }, catchError: false);
  
  final router = new Router();
  
  router.get('/:param1/:param2/:param3', (String param3, Context context, String param1, String param2) async {
    // you can use params direct trough function arguments, or get from
    // context.req.params = map {param1: 'value1', param2: value2, param3: value3}
    print('GET /' + context.req.params.toString());
  });
  
  router.get(r'/:blogId(\d+$)', (String blogId, Context context) {
    // will match on route: /some-blog-title-1234/
    // regex params can be get as function arguments as well
    context.req.params['blogId']; // 1234
  });

  router.get(r'/secret/:id', (int id, Context context){
    if (id != 2) {
      // will be called first middleware with catchError = true
      throw new Error(); 
    }
  })
  .useBefore((Context context) { /* will be called before route */ })
  .useAfter((Context context) { /* will be called after route */ });

  // simple ger request
  router.get('/', (Context context) async => null);
    
  // simple post request
  // uses body_parser to decode:
  // application/json 
  // application/x-www-form-urlencoded
  // multipart/form-data
  router.post('/', (Context context) async => print(context.req.body['message']['text']));
 
  app.use(router); // you can use more than one router
 
  app.use((Context context) async {
    // will be called only if error will be thrown
  }, catchError: true);
  
  app.listen(port: 4040);
}

Class bonding example

import 'package:dartness/dartness.dart';

class A {
  @Get('/')
  void index(Context c) {
    c.res.write('index');
  }

  @Get('/:param')
  void param(Context c, String param) {
    c.res.write(param);
  }
}

void main() {
  final app = new Dartness();
  final route = new Router();

  route.bind(new A());

  app.use(route);
  app.listen(port: 3030);
}

Changelog #

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

[0.2.0]

added #

  • now you can use parameters from routes as function arguments

[0.1.0] - 2018-09-09

Added #

  • added regexp routes

Changed #

  • corrected deprecated
  • corrected docs

[0.0.18] - 2018-09-07

Added #

  • ability to attach middleware directly to routes (added middlewares are isolated in route)
  • added tests

[0.0.17] - 2018-07-05

Added #

  • added tests
  • added modules (middleware groups, see example) !!! module's route does not add to included routers

[0.0.16] - 2018-05-24

Added #

  • improved examples

Changed #

  • Added error to Context

[0.0.15] - 2018-05-23

Added #

  • improved example

Changed #

  • Formatted code according dart code styles

example/example.dart

import 'dart:io';

import 'package:dartness/dartness.dart';

void main() {
  final app = new Dartness();

  app.use((Context context) async {
    print('middleware 1');
  }, catchError: false);

  app.use((Context context) async {
    print('middleware 2');
  });

  final router = new Router();

  router.get('/:param1/:param2/:param3',
      (String param3, Context context, String param1, String param2) async {
    // you can use params direct trough function arguments, or get from
    // context.req.params = map {param1: 'value1', param2: value2, param3: value3}
    print('GET /' + context.req.params.toString());
  });

  router.get('/', (Context context) async => null);
  router.post(
      '/', (Context context) async => print(context.req.body)); // body is a map
  router
      .get('/secret',
          (Context context) async => context.res.write('secret word'))
      .useBefore((Context context) {
    if (context.req.headers.value('X-Secret-Code').isEmpty) {
      throw new StateError('You shall not pass!');
    }
  });
  router.get(r'/:blogId(\d+$)', (String blogId, Context context) {
    // will match on route: /some-blog-title-1234/
    // regex params can be used in function arguments as well
    final reqBlogId = context.req.params['blogId']; // 1234
    context.res.write('blogId is ' + blogId + ' and ' + reqBlogId);
  });

  app.use(router);

  app.use((Context context) async {
    // print('sending response');
    context.res
      ..headers.add(HttpHeaders.contentTypeHeader, 'application/json')
      ..write('{"qe": "asd", "zxc": 4}')
      ..close();
  });

  app.use((Context context) async {
    print('wow, here is was an error!');
    context.res.write('middleware 2');
  }, catchError: true);

  app.listen(host: InternetAddress.anyIPv4, port: 4040);
}

Use this package as an executable

1. Install it

You can install the package from the command line:


$ pub global activate dartness

2. Use it

The package has the following executables:


$ dartness

Use this package as a library

1. Depend on it

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


dependencies:
  dartness: ^0.4.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

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

3. Import it

Now in your Dart code, you can use:


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

We analyzed this package on Jan 20, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4

Health issues and suggestions

Document public APIs. (-1 points)

124 out of 124 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Fix lib/src/cli/command.dart. (-4.41 points)

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

line 12 col 16: Unnecessary new keyword.

line 14 col 16: Unnecessary new keyword.

line 21 col 11: Unnecessary new keyword.

line 34 col 13: Unnecessary new keyword.

line 57 col 29: Unnecessary new keyword.

Fix lib/dartness.dart. (-3.93 points)

Analysis of lib/dartness.dart reported 8 hints, including:

line 27 col 32: Unnecessary new keyword.

line 30 col 25: Unnecessary new keyword.

line 40 col 49: Use = to separate a named parameter from its default value.

line 41 col 22: Unnecessary new keyword.

line 50 col 23: Unnecessary new keyword.

Fix lib/src/routeMatcher.dart. (-2.48 points)

Analysis of lib/src/routeMatcher.dart reported 5 hints:

line 27 col 13: Unnecessary new keyword.

line 30 col 13: Unnecessary new keyword.

line 36 col 52: Unnecessary new keyword.

line 37 col 15: Unnecessary new keyword.

line 38 col 27: Unnecessary new keyword.

Fix additional 8 files with analysis or formatting issues. (-10.45 points)

Additional issues in the following files:

  • lib/src/module.dart (4 hints)
  • lib/src/routeItem.dart (4 hints)
  • bin/dartness.dart (3 hints)
  • lib/src/middlewares/static.dart (3 hints)
  • lib/src/router.dart (3 hints)
  • lib/src/callable.dart (2 hints)
  • lib/src/context.dart (2 hints)
  • lib/src/errors/base.dart (Run dartfmt to format lib/src/errors/base.dart.)

Maintenance suggestions

Package is getting outdated. (-1.92 points)

The package was last published 53 weeks ago.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
body_parser ^1.1.0 1.1.1
cli_util ^0.1.3+2 0.1.3+2
http_parser ^3.0.0 3.1.3
logging ^0.11.3+2 0.11.4
mapper ^0.2.0 0.2.0
mime ^0.9.6 0.9.6+3
Transitive dependencies
charcode 1.1.2
collection 1.14.12
dart2_constant 1.0.2+dart2
http_server 0.9.8+3
meta 1.1.8
path 1.6.4
source_span 1.6.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
Dev dependencies
test ^1.0.0