Shelf

The Shelf package is a popular Dart package which acts as a middleware for incoming HTTP requests. There is a rich ecosystem of packages around Shelf, including routers and middleware.

Dart Edge provides a lightweight wrapper for interfacing with a Shelf Request and handling a returned Shelf Response, enalbing you take take advantage of this ecosystem of Shelf packages.

Usage#

Vercel Edge#

Install the shelf and shelf_router packages from pub.dev:

dart pub add shelf
dart pub add shelf_router

Import the VercelEdgeShelf class from the vercel_edge/vercel_edge_shelf.dart package, and route the request to a Shelf Router:

import 'package:vercel_edge/vercel_edge_shelf.dart';
import 'package:shelf_router/shelf_router.dart';
import 'package:shelf/shelf.dart';

void main() {
  VercelEdgeShelf(
    fetch: (request) async {
      final app = Router();

      app.get('/', (request) async {
        return Response.ok('Welcome to Dart Edge!');
      });

      app.all('/<ignored|.*>', (request) {
        return Response.notFound('Resource not found');
      });

      return app(request);
    },
  );
}

Alternatively, you could return a Pipeline handler to apply middleware to your requests.