shelf_path 0.1.8

Path Parameters for Dart Shelf

Pub Version

Introduction

Provides a small library for Shelf to decouple producers of path parameters such as routers from consumers like your own handlers or other middleware like binding frameworks.

This supports interoperability so that you can plug and play which middleware you like as long as they support Dart Path.

In a nutshell, Shelf Path encapsulates the storage of path parameters in the Shelf Request context property. It exposes two sets of functions:

  1. Functions for consumers to Read path properties: getPathParameters, getPathParameter
  2. Functions for producers to Write path properties: setPathParameters, setPathParameter, addPathParameter

Example

Say we have an end point that serves up queries on a nomintated bank account and supports filtering by a minimum amount. The requests look like

GET /banking/account/123456/deposit?minAmount=20

One way to represent this is using the powerful Uri Template standard which is supported by the very useful Dart uri package. It supports both segment parameters and query parameters.

For example

/banking/account/{accountId}/deposit{?minAmount}

which has two path variables:

  1. accountId
  2. minAmount

Producer

Shelf Middleware such as routers can match the request path against this template and a map like

{
  'accountId': '123456',
  'minAmount': '20'
}

These path parameters can now be added to the context using the Shelf Path function addPathParameters

var pathParams = {
  'accountId': '123456',
  'minAmount': '20'
};

addPathParameters(pathParams);

Consumer

An end user can now write a handler and extract the path parameters using getPathParameter

depositSearchHandler(request) {
  var accountId = getPathParameter(request, 'accountId');
  var minAmount = getPathParameter(request, 'minAmount');

  return new Response.ok("Querying deposits for account $accountId for amounts >= $minAmount");
}  

Compatible Middleware

Producers

Consumers

0.1.3

  • removed dependency from uri package

0.1.2

  • increased upper bound on shelf

0.1.1+1

  • bug fix in addPathParametersToContext

0.1.1

  • deprecated some functions
  • upgraded minimum shelf version requirement
  • fixed bug due to immutable context

0.1.0

  • First release

1. Depend on it

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

dependencies:
  shelf_path: "^0.1.8"

2. Install it

You can install packages from the command line:

$ 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:shelf_path/shelf_path.dart';

Platforms

About

Path parameter helpers for Shelf

Author

Email andersmholmgren@gmail.com Anders Holmgren

Homepage

bitbucket.org/andersmholmgren/shelf_path

Documentation

www.dartdocs.org/documentation/shelf_path/0.1.8/

Uploader

andersmholmgren@gmail.com

License

BSD

Published

Apr 10, 2016

Share