angel_shelf 1.2.0

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 65

shelf

Pub build status

Shelf interop with Angel. This package lets you run package:shelf handlers via a custom adapter. It also includes a plug-in that configures Angel to natively run shelf response handlers.

Use the code in this repo to embed existing shelf apps into your Angel applications. This way, you can migrate legacy applications without having to rewrite your business logic.

This will make it easy to layer your API over a production application, rather than having to port code.

Usage

embedShelf

This is a compliant shelf adapter that acts as an Angel request handler. You can use it as a middleware, or attach it to individual routes.

import 'dart:io';
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_shelf/angel_shelf.dart';
import 'package:shelf/shelf.dart' as shelf;
import 'api/api.dart';

main() async {
  final app = new Angel();
  
  // Angel routes on top
  await app.configure(new ApiController());
  
  // Re-route all other traffic to an
  // existing application.
  app.after.add(embedShelf(
    new shelf.Pipeline()
      .addMiddleware(shelf.logRequests())
      .addHandler(_echoRequest)
  ));
  
  // Only on a specific route
  app.get('/shelf', handler);
  
  await app.startServer(InternetAddress.LOOPBACK_IP_V4, 3000);
}

Communicating with Angel with embedShelf

You can communicate with Angel:

handleRequest(shelf.Request request) {
  // Access original Angel request...
  var req = request.context['angel_shelf.request'] as RequestContext;
  
  // ... And then interact with it.
  req.inject('from_shelf', new Foo());
  
  // `req.properties` are also available.
  var props = request.context['angel_shelf.properties'] as Map;
  
}

supportShelf

This plug-in takes advantage of Angel's middleware system and dependency injection to patch a server to run shelf request handlers as though they were Angel request handlers. Hooray for integration!

You'll want to run this before adding any other response finalizers that depend on the response content being effectively final, i.e. GZIP compression.

NOTE: Do not inject a shelf.Request into your request under the name req. If you do, Angel will automatically inject a RequestContext instead.

configureServer(Angel app) async {
  // Return a shelf Response
  app.get('/shelf', (shelf.Request request) => new shelf.Response.ok('Yay!'));
  
  // Return an arbitrary value.
  //
  // This will be serialized by Angel as per usual.
  app.get('/json', (shelf.Request request) => {'foo': 'bar'});
  
  // You can still throw Angel exceptions.
  //
  // Don't be fooled: just because this is a shelf handler, doesn't mean
  // it's not an Angel response handler too. ;)
  app.get('/error', (shelf.Request request) {
    throw new AngelHttpException.forbidden();
  });
  
  // Make it all happen!
  await app.configure(supportShelf());
}

Communicating with Angel with supportShelf

The following keys will be present in the shelf request's context:

  • angel_shelf.request - Original RequestContext
  • angel_shelf.response - Original ResponseContext
  • angel_shelf.properties - Original RequestContext's properties

If the original RequestContext contains a Map named shelf_context in its properties, then it will be merged into the shelf request's context.

If the handler returns a shelf.Response, then it will be present in ResponseContext.properties as shelf_response.

1.2.0

  • Upgraded for >=1.1.0 compatibility.

1. Depend on it

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


dependencies:
  angel_shelf: "^1.2.0"

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:angel_shelf/angel_shelf.dart';
        
Version Uploaded Documentation Archive
1.2.0 Jan 9, 2018 Go to the documentation of angel_shelf 1.2.0 Download angel_shelf 1.2.0 archive
1.1.1 Jun 20, 2017 Go to the documentation of angel_shelf 1.1.1 Download angel_shelf 1.1.1 archive
1.1.0 Jun 12, 2017 Go to the documentation of angel_shelf 1.1.0 Download angel_shelf 1.1.0 archive
1.0.0 Jan 13, 2017 Go to the documentation of angel_shelf 1.0.0 Download angel_shelf 1.0.0 archive

Analysis

This feature is new.
We welcome feedback.
More details: scoring.

We analyzed this package, and provided a score, details, and suggestions below.

  • completed on Feb 3, 2018
  • Dart: 2.0.0-dev.20.0
  • pana: 0.10.1

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
32 / 100
Health:
Code health derived from static analysis. [more]
99 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100 / 100
Overall score:
Weighted score of the above. [more]
65

Platforms

Detected platforms: other

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

Suggestions

  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

  • Maintain an example.

    None of the files in your example/ directory matches a known example patterns. Common file name patterns include: main.dart, example.dart or you could also use angel_shelf.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.19.0
angel_framework >=1.1.0-alpha <2.0.0 1.1.0+3
shelf ^0.6.0 0.6.8 0.7.2
Transitive dependencies
angel_http_exception 1.0.0
angel_model 1.0.0
angel_route 2.0.5
async 1.13.3 2.0.3
body_parser 1.0.3
charcode 1.1.1
collection 1.14.5
combinator 1.0.0-beta+7
container 0.1.2
http_parser 3.1.1
http_server 0.9.7
json_god 2.0.0-beta+1
logging 0.11.3+1
matcher 0.12.1+4
merge_map 1.0.0
meta 1.1.2
mime 0.9.6
path 1.5.1
pool 1.3.4
quiver_hashcode 1.0.0
random_string 0.0.1
source_span 1.4.0
stack_trace 1.9.1
stream_channel 1.6.3
string_scanner 1.0.2
tuple 1.0.1
typed_data 1.1.5
Dev dependencies
angel_test ^1.1.0
console ^2.2.4
shelf_proxy ^0.1.0
test ^0.12.0