This commit is contained in:
Tobe O 2019-01-06 13:11:11 -05:00
parent 57f212eecc
commit 62d29b45fd
8 changed files with 91 additions and 28 deletions

2
.gitignore vendored
View file

@ -54,3 +54,5 @@ com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
.dart_tool

2
CHANGELOG.md Normal file
View file

@ -0,0 +1,2 @@
# 2.0.0
* Angel 2 + Dart 2 updates.

View file

@ -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));
// ...
}

4
analysis_options.yaml Normal file
View file

@ -0,0 +1,4 @@
include: package:pedantic/analysis_options.yaml
analyzer:
strong-mode:
implicit-casts: false

46
example/main.dart Normal file
View file

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

View file

@ -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 '';
}
};

View file

@ -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 <thosakwe@gmail.com>
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
logging: ^0.11.0
test: ^1.0.0
pedantic: ^1.0.0

View file

@ -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('<!DOCTYPE html><html><head><title>ok</title></head></html>')));
allOf(
hasContentType('text/html'),
hasBody(
'<!DOCTYPE html><html><head><title>ok</title></head></html>')));
});
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'});