shelf_response_formatter 0.1.0

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 24

Shelf Response Formatter

A small class that, given a shelf request and some simple data, will determine a correct response format (eg.: Json or XML) and convert the data to the appropriate format.

This package is a fork of tentacle_response_formatter.

How to use

// ResponseFormatter is a Singleton so you will always get the same instance
// with
new ResponseFormatter formatter = new ResponseFormatter();

// Create a result from a shelf.Request and data
FormatResult result = formatter.formatResponse(request, {"message": "hello"});

// Body contains the generated response as String
print(result.body) // -> '{"message":"hello"}' or
    '<response><message>hello</message></response>' or 'hello'

// contentType is a suggestion to set as the HttpHeaders.CONTENT_TYPE in the
// shelf.Response you create
print(result.contentType) // -> "application/json" or "application/xml" or "text/plain"

How is the response format detected

There are currently 3 values that influence the response format detection. They are applied in the following order:

  1. Format query param: "http://example.com/some?format=json" would force the response to be converted to Json.
  2. Format file extension: "http://example.com/some.xml" would force the response to be converted to XML.
  3. Accept Header: "accept: application/json, text/xml" would force the response to be converted to Json.

Accept headers are used in the order they are defined. So first format has precedence over second and so forth.

If no format can be detected that has a valid encoder registered the response is encoded as String. If data contains a message field it is taken for String response otherwise the result will be an empty String. The string formatter is not a real formatter but rather a fallback. Read later on how to add your own formatter and register it for content types.

Add your own response formatter

You may want to add your own formatters or content-type mappings to the formatter. To do so you have to provide a formatter function which takes dynamic data and returns a String. Furthermore you have to provide a unique name and may provide your custom content-types that should be handled by your formatter.

// Add a simple formatter for csv
formatter.registerFormatter("csv", "text/csv", (dynamic data) {
  return "Your CSV result here";
});
// Now ?format=csv, file.csv and accept header "text/csv" are formatted using
// your formatter

// Override additional accept-headers
formatter.registerFormatter("allmighty", "text/allmighty", (dynamic data) {
      return "allmighty response body";
}, ["application/json", "application/xhtml+xml", "*/*", "text/plain", "application/xml"]);

// Replace existing formatter
formatter.registerFormatter("json", "application/json", (dynamic data) {
  return "Your json result here";
});
// Json is now handled by your formatter including all accept-headers json was
// initially registered for

License

Apache 2.0

0.1.0

  • fork from tentacle_response_formatter
  • apply pull requests to work with XML >0.2.0
  • apply dartformat

Use this package as a library

1. Depend on it

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


dependencies:
  shelf_response_formatter: ^0.1.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

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

3. Import it

Now in your Dart code, you can use:


import 'package:shelf_response_formatter/shelf_response_formatter.dart';
  
Version Uploaded Documentation Archive
0.1.0 Apr 1, 2015 Go to the documentation of shelf_response_formatter 0.1.0 Download shelf_response_formatter 0.1.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
4
Health:
Code health derived from static analysis. [more]
74
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
0
Overall:
Weighted score of the above. [more]
24
Learn more about scoring.

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

  • Dart: 2.0.0-dev.69.0
  • pana: 0.11.7

Platforms

Detected platforms: Flutter, other

Primary library: package:shelf_response_formatter/shelf_response_formatter.dart with components: isolate.

Issues and suggestions

Fix lib/xml_converter.dart.

Analysis of lib/xml_converter.dart failed with 1 error:

line 39 col 21: The expression here has a type of 'void', and therefore cannot be used.

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see https://www.dartlang.org/dart-2#migration.

Package is too old.

The package was released more than two years ago.

Package is pre-v1 release.

While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction of the API.

Maintain an example.

Create a short demo in the example/ directory to show how to use this package. Common file name patterns include: main.dart, example.dart or you could also use shelf_response_formatter.dart.

Fix lib/shelf_response_formatter.dart.

Analysis of lib/shelf_response_formatter.dart reported 2 hints:

line 86 col 12: The value of the local variable 'acceptHeaders' isn't used.

line 138 col 10: 'JSON' is deprecated and shouldn't be used.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.0.0 <2.0.0
shelf >=0.6.0 <0.7.0 0.6.8 0.7.3+3
xml >=2.1.0 <3.0.0 2.6.0 3.0.0
Transitive dependencies
async 1.13.3 2.0.8
charcode 1.1.2
collection 1.14.11
http_parser 3.1.3
matcher 0.12.4
path 1.6.2
petitparser 1.7.6
source_span 1.4.1
stack_trace 1.9.3
stream_channel 1.6.8
string_scanner 1.0.3
typed_data 1.1.6
Dev dependencies
analyzer >=0.24.0 <0.26.0
grinder >=0.7.0-dev.1 <0.8.0
linter >=0.0.2 <0.1.0
unittest any