dartness 0.3.1

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

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);
}

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 a library

1. Depend on it

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


dependencies:
  dartness: ^0.3.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.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
0.0.14 May 21, 2018 Go to the documentation of dartness 0.0.14 Download dartness 0.0.14 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
26
Health:
Code health derived from static analysis. [more]
90
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
60
Learn more about scoring.

We analyzed this package on Dec 5, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.7

Platforms

Detected platforms: other

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

Health issues and suggestions

Document public APIs (-10 points)

77 out of 77 API elements (library, class, field or method) have no adequate dartdoc content. Good documentation improves code readability and discoverability through search.

Format lib/src/callable.dart.

Run dartfmt to format lib/src/callable.dart.

Format lib/src/context.dart.

Run dartfmt to format lib/src/context.dart.

Format lib/src/httpMethod.dart.

Run dartfmt to format lib/src/httpMethod.dart.

Fix additional 3 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/src/meta.dart (Run dartfmt to format lib/src/meta.dart.)
  • lib/src/middlewares/cors.dart (Run dartfmt to format lib/src/middlewares/cors.dart.)
  • lib/src/router.dart (Run dartfmt to format lib/src/router.dart.)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
body_parser ^1.1.0 1.1.1
http_parser ^3.0.0 3.1.3
mapper ^0.2.0 0.2.0
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.6
mime 0.9.6+2
path 1.6.2
source_span 1.4.1
string_scanner 1.0.4
typed_data 1.1.6
Dev dependencies
test ^1.0.0