diff --git a/.gitignore b/.gitignore index 99e7978e..fe4c4cfa 100644 --- a/.gitignore +++ b/.gitignore @@ -54,3 +54,5 @@ com_crashlytics_export_strings.xml crashlytics.properties crashlytics-build.properties fabric.properties + +.dart_tool \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000..324effd8 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,2 @@ +# 2.0.0 +* Angel 2 + Dart 2 updates. \ No newline at end of file diff --git a/README.md b/README.md index 2bb57ee8..cdf92e78 100644 --- a/README.md +++ b/README.md @@ -46,10 +46,10 @@ The `renderHtml` function does all the magic for you. ```dart configureServer(Angel app) async { // Wire it up! - app.before.add(renderHtml()); + app.fallback(renderHtml()); // You can pass a custom StringRenderer if you need more control over the output. - app.before.add(renderHtml(renderer: new StringRenderer(html5: false))); + app.fallback(renderHtml(renderer: new StringRenderer(html5: false))); app.get('/greet/:name', (RequestContext req) { return html(lang: 'en', c: [ @@ -76,7 +76,7 @@ client doesn't accept `*/*` or `text/html`. ```dart configureServer(Angel app) async { // Wire it up! - app.before.add(renderHtml(enforceAcceptHeader: true)); + app.fallback(renderHtml(enforceAcceptHeader: true)); // ... } diff --git a/analysis_options.yaml b/analysis_options.yaml new file mode 100644 index 00000000..c230cee7 --- /dev/null +++ b/analysis_options.yaml @@ -0,0 +1,4 @@ +include: package:pedantic/analysis_options.yaml +analyzer: + strong-mode: + implicit-casts: false \ No newline at end of file diff --git a/example/main.dart b/example/main.dart new file mode 100644 index 00000000..d1570ec3 --- /dev/null +++ b/example/main.dart @@ -0,0 +1,46 @@ +import 'package:angel_framework/angel_framework.dart'; +import 'package:angel_framework/http.dart'; +import 'package:angel_html/angel_html.dart'; +import 'package:html_builder/elements.dart'; +import 'package:logging/logging.dart'; + +main() async { + var app = Angel(), http = AngelHttp(app); + app.logger = Logger('angel_html') + ..onRecord.listen((rec) { + print(rec); + if (rec.error != null) print(rec.error); + if (rec.stackTrace != null) print(rec.stackTrace); + }); + + app.fallback(renderHtml()); + + app.get('/html', (req, res) { + return html(c: [ + head(c: [ + title(c: [text('ok')]) + ]) + ]); + }); + + app.get( + '/strict', + chain([ + renderHtml( + enforceAcceptHeader: true, + renderer: StringRenderer( + doctype: null, + pretty: false, + ), + ), + (req, res) { + return div(c: [text('strict')]); + }, + ]), + ); + + app.fallback((req, res) => throw AngelHttpException.notFound()); + + await http.startServer('127.0.0.1', 3000); + print('Listening at ${http.uri}'); +} diff --git a/lib/angel_html.dart b/lib/angel_html.dart index 99563629..81b7c853 100644 --- a/lib/angel_html.dart +++ b/lib/angel_html.dart @@ -7,8 +7,7 @@ import 'package:html_builder/html_builder.dart'; /// You can provide a custom [renderer]. The default renders minified HTML5 pages. /// /// Set [enforceAcceptHeader] to `true` to throw a `406 Not Acceptable` if the client doesn't accept HTML responses. -RequestMiddleware renderHtml( - {StringRenderer renderer, bool enforceAcceptHeader}) { +RequestHandler renderHtml({StringRenderer renderer, bool enforceAcceptHeader}) { renderer ??= new StringRenderer(pretty: false, html5: true); return (RequestContext req, ResponseContext res) { @@ -21,11 +20,11 @@ RequestMiddleware renderHtml( if (enforceAcceptHeader == true && !req.accepts('text/html')) throw new AngelHttpException.notAcceptable(); - var content = renderer.render(data); + var content = renderer.render(data as Node); res ..headers['content-type'] = 'text/html' - ..write(content) - ..end(); + ..write(content); + res.close(); return ''; } }; diff --git a/pubspec.yaml b/pubspec.yaml index 7cfb322a..4851fd05 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,14 +1,16 @@ name: angel_html -version: 1.0.0 -description: Easily render html_builder AST's as responses in Angel. +version: 2.0.0 +description: Support for rendering html_builder AST's as responses in Angel. author: Tobe O homepage: https://github.com/angel-dart/html_builder environment: - sdk: ">=1.19.0" + sdk: ">=2.0.0-dev <3.0.0" dependencies: - angel_framework: ^1.0.0 + angel_framework: ^2.0.0-alpha html_builder: ^1.0.0 dev_dependencies: - angel_test: ^1.0.0 + angel_test: ^2.0.0-alpha html: ^0.13.2 - test: ^0.12.0 \ No newline at end of file + logging: ^0.11.0 + test: ^1.0.0 + pedantic: ^1.0.0 \ No newline at end of file diff --git a/test/all_test.dart b/test/all_test.dart index 92795a83..e6777418 100644 --- a/test/all_test.dart +++ b/test/all_test.dart @@ -12,9 +12,9 @@ main() { setUp(() async { app = new Angel(); - app.before.add(renderHtml()); + app.fallback(renderHtml()); - app.get('/html', () { + app.get('/html', (req, res) { return html(c: [ head(c: [ title(c: [text('ok')]) @@ -22,14 +22,21 @@ main() { ]); }); - app - .chain(renderHtml( - enforceAcceptHeader: true, - renderer: new StringRenderer(doctype: null, pretty: false))) - .get('/strict', () { - return div(c: [text('strict')]); - }); - + app.get( + '/strict', + chain([ + renderHtml( + enforceAcceptHeader: true, + renderer: new StringRenderer( + doctype: null, + pretty: false, + ), + ), + (req, res) { + return div(c: [text('strict')]); + }, + ]), + ); client = await connectTo(app); }); @@ -41,8 +48,10 @@ main() { expect( response, - allOf(hasContentType('text/html'), - hasBody('ok'))); + allOf( + hasContentType('text/html'), + hasBody( + 'ok'))); }); group('enforce accept header', () { @@ -62,8 +71,7 @@ main() { test('throws if incorrect or no accept', () async { var response = await client.get('/strict'); print('Response: ${response.body}'); - expect(response, - isAngelHttpException(statusCode: 406, message: '406 Not Acceptable')); + expect(response, hasStatus(406)); response = await client .get('/strict', headers: {'accept': 'application/json,text/xml'});