<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Web::Dispatch::Wrapper;

use strictures 1;
use Moo;
use Exporter 'import';

our @EXPORT = qw(dispatch_wrapper redispatch_to response_filter);

extends 'Plack::Middleware';

has 'wrapper' =&gt; (is =&gt; 'ro', required =&gt; 1);

sub dispatch_wrapper (&amp;) {
  my ($code) = @_;
  __PACKAGE__-&gt;from_code($code);
}

sub from_code {
  my ($class, $code) = @_;
  $class-&gt;new(wrapper =&gt; $code);
}

sub redispatch_to {
  my ($new_path) = @_;
  __PACKAGE__-&gt;from_code(sub {
    $_[1]-&gt;({ %{$_[0]}, PATH_INFO =&gt; $new_path });
  });
}

sub response_filter (&amp;) {
  my ($code) = @_;
  __PACKAGE__-&gt;from_code(sub {
    my @result = $_[1]-&gt;($_[0]);
    if (@result) {
      $code-&gt;(@result);
    } else {
      ()
    }
  });
}

sub to_app {
  my $code = $_[0]-&gt;wrapper;
  my $app = $_[0]-&gt;app;
  sub { $code-&gt;($_[0], $app) }
}

1;
</pre></body></html>