blib/lib/PAGI/Nano/Context/HTTP.pm

filter: Statementstmt 100% 28 / 28 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 100% 41 / 41 CC 2 SCAR 1.8
CC
2
Cov
100%
params
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.2
« examples/contact-form/app.pl ↑ Summary examples/quickstart/app.pl »
1 package PAGI::Nano::Context::HTTP;
2 12 $PAGI::Nano::Context::HTTP::VERSION = '0.001001';
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use parent -norequire, 'PAGI::Context::HTTP', 'PAGI::Nano::Context';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use PAGI::Context; # PAGI::Context::HTTP declares @ISA but does not load
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use PAGI::Context::HTTP; # the base; pull both in when used outside the factory
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use PAGI::Nano::Context; # the shared mixin (uri_for)
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use PAGI::StructuredParameters;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # The HTTP context Nano vends. It is a genuine PAGI::Context::HTTP (so the
12 # inherited request/response/respond/json/text/html/redirect/state sugar all
13 # work and there is no silo) and adds $c->params, the strong-parameters entry
14 # point. We bless directly rather than going through PAGI::Context->new so the
15 # base factory's scope-type resolution does not down-cast us back to the plain
16 # context class.
17  
18 129 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
19 129 my ($class, $scope, $receive, $send) = @_;
20 129 return bless {
21 scope => $scope,
22 receive => $receive,
23 send => $send,
24 }, $class;
25 }
26  
27 19 ▶sub params {
Subroutine params: called CC=2 SCAR=6.9
Pod documented
28 19 my ($self) = @_;
29 19 my $req = $self->req;
30 19 ▶ return $req->is_json
Branch: 1 branch
BranchTrueFalseTotal
$req->is_json ? : 13 6 19
31 ? PAGI::StructuredParameters->from_data($req, $self)
32 : PAGI::StructuredParameters->from_body($req, $self);
33 }
34  
35 12 1;
36  
37 =encoding utf8
38  
39 =head1 NAME
40  
41 PAGI::Nano::Context::HTTP - The HTTP request context vended by PAGI::Nano
42  
43 =head1 SYNOPSIS
44  
45 get '/' => sub ($c) {
46 my $name = $c->req->query_param('name');
47 $c->json({ hello => $name });
48 };
49  
50 post '/tasks' => sub ($c) {
51 my $attrs = await $c->params->required(
52 'title',
53 sub ($c, $missing) { $c->json({ error => 'missing', fields => $missing }, status => 400) },
54 );
55 ...
56 };
57  
58 =head1 DESCRIPTION
59  
60 A subclass of L<PAGI::Context::HTTP>, so every method of the base HTTP context
61 is available — C<req>/C<request>, C<response>/C<resp>, C<respond>, C<method>,
62 C<state>, C<path_param>, and the response sugar C<json>/C<text>/C<html>/
63 C<redirect>. A Nano handler receives one of these as C<$c>.
64  
65 The additions are L</params> and L</uri_for>. The inherited
66 C<< $c->raw_send >> (the raw PAGI C<$send> coderef; see
67 L<PAGI::Context/raw_send>) is also available for handlers that drop to the
68 channel directly.
69  
70 =head1 METHODS
71  
72 =head2 params
73  
74 my $params = $c->params; # PAGI::StructuredParameters::Request
75 my $clean = await $params->permitted(@rules);
76 my $clean = await $params->required(@rules, $on_missing);
77  
78 The strong-parameters entry point. Returns a
79 L<PAGI::StructuredParameters::Request> bound to the current request, selecting
80 the source by content-type: a JSON request uses C<from_data> (nested data,
81 arrays kept as-is); any other request uses C<from_body> (form parameters, array
82 values flattened). The context C<$c> is passed through so C<required>'s
83 on-missing callback receives it as its first argument.
84  
85 Because reading a request body is asynchronous, the terminal C<permitted> and
86 C<required> calls must be awaited.
87  
88 =head2 uri_for
89  
90 my $url = $c->uri_for($name);
91 my $url = $c->uri_for($name, \%path_params);
92 my $url = $c->uri_for($name, \%path_params, \%query_params);
93  
94 Builds the URL for a route named with L<PAGI::Nano/name>. Path placeholders
95 (C<:id>, C<{id}>, C<{id:regex}>, C<*splat>) are filled from C<%path_params>, and
96 C<%query_params> (if any) is appended as a percent-encoded query string.
97 Resolution is against the flat name registry PAGI::Nano injects on the scope, so
98 names defined anywhere in the app — including across a C<mount>, in either
99 direction — are reachable, with mount prefixes applied. Dies if the name is
100 unknown.
101  
102 =head1 SEE ALSO
103  
104 L<PAGI::Nano>, L<PAGI::StructuredParameters>, L<PAGI::Context::HTTP>.
105  
106 =head1 AUTHOR
107  
108 John Napiorkowski C<< <jjnapiork@cpan.org> >>
109  
110 =head1 COPYRIGHT & LICENSE
111  
112 Copyright 2026, John Napiorkowski. This library is free software; you can
113 redistribute it and/or modify it under the same terms as Perl itself.
114  
115 =cut
« examples/contact-form/app.pl ↑ Summary examples/quickstart/app.pl »