dartness 0.4.1

  • readme.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 49

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';
  
Version Uploaded Documentation Archive
0.4.1 Jan 12, 2019 Go to the documentation of dartness 0.4.1 Download dartness 0.4.1 archive
0.4.0 Jan 9, 2019 Go to the documentation of dartness 0.4.0 Download dartness 0.4.0 archive
0.3.1 Nov 25, 2018 Go to the documentation of dartness 0.3.1 Download dartness 0.3.1 archive
0.3.0 Nov 25, 2018 Go to the documentation of dartness 0.3.0 Download dartness 0.3.0 archive
0.2.0 Nov 16, 2018 Go to the documentation of dartness 0.2.0 Download dartness 0.2.0 archive
0.1.0 Sep 9, 2018 Go to the documentation of dartness 0.1.0 Download dartness 0.1.0 archive
0.0.18 Sep 6, 2018 Go to the documentation of dartness 0.0.18 Download dartness 0.0.18 archive
0.0.17 Jul 5, 2018 Go to the documentation of dartness 0.0.17 Download dartness 0.0.17 archive
0.0.16 May 24, 2018 Go to the documentation of dartness 0.0.16 Download dartness 0.0.16 archive
0.0.15 May 23, 2018 Go to the documentation of dartness 0.0.15 Download dartness 0.0.15 archive

All 11 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
97
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
49
Learn more about scoring.

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

  • Dart: 2.2.0
  • pana: 0.12.14

Platforms

Detected platforms: other

Primary library: package:dartness/dartness.dart with components: io, mirrors.

Health issues and suggestions

Document public APIs. (-1 points)

114 out of 114 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/dartness.dart. (-1 points)

Analysis of lib/dartness.dart reported 2 hints:

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

line 70 col 9: Future results in async function bodies must be awaited or marked unawaited using package:pedantic.

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

Analysis of lib/src/cli/command.dart reported 1 hint:

line 95 col 3: The class 'Future' was not exported from 'dart:core' until version 2.1, but this code is required to be able to run on earlier versions.

Fix lib/src/middlewares/static.dart. (-0.50 points)

Analysis of lib/src/middlewares/static.dart reported 1 hint:

line 62 col 7: Future results in async function bodies must be awaited or marked unawaited using package:pedantic.

Fix lib/src/router.dart. (-0.50 points)

Analysis of lib/src/router.dart reported 1 hint:

line 18 col 26: Use = to separate a named parameter from its default value.

Format lib/src/errors/base.dart.

Run dartfmt to format lib/src/errors/base.dart.

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.3+2
mapper ^0.2.0 0.2.0
mime ^0.9.6 0.9.6+2
Transitive dependencies
charcode 1.1.2
collection 1.14.11
dart2_constant 1.0.2+dart2
http_server 0.9.8+1
meta 1.1.7
path 1.6.2
source_span 1.5.5
string_scanner 1.0.4
term_glyph 1.1.0
typed_data 1.1.6
Dev dependencies
test ^1.0.0