| 1 | package PAGI::FastAPI::Depends; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 22 | ▶ | use v5.38; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 22 | ▶ | use experimental 'class'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 22 | ▶ | use version; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 22 | our $VERSION = qv('v1.7.3'); | |||||||||||||||||||||||||||
| 8 | 22 | our $AUTHORITY = 'cpan:MANWAR'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 22 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 22 | our @EXPORT_OK = qw(Depends); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 22 | class PAGI::FastAPI::Depends { | |||||||||||||||||||||||||||
| 15 | 22 | field $code :param; | |||||||||||||||||||||||||||
| 16 | 22 | field $key :param = undef; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 7 | ▶ | ADJUST { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 19 | 7 | ▶ | die "Depends requires a CODE reference" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $code and ref $code eq "CODE"
MC/DC50%1/2
defined $code and ref $code eq "CODE"
defined $code
ref $code eq "CODE"
Truth table66%2/3
A and B
A= defined $codeB= ref $code eq "CODE"
| |||||||||||||||||||||||||||||
| 20 | unless defined $code && ref $code eq 'CODE'; | ||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 4 | ▶ | method code { $code } | ||||||||||||||||||||||||||
|
Subroutine
code: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 5 | ▶ | method key { $key } | ||||||||||||||||||||||||||
|
Subroutine
key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 5 | ▶ | sub Depends ($code, %opts) { | ||||||||||||||||||||||||||
|
Subroutine
Depends: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | 5 | return PAGI::FastAPI::Depends->new(code => $code, %opts); | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =encoding utf-8 | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =head1 NAME | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | PAGI::FastAPI::Depends - Dependency Injection Wrapper for PAGI::FastAPI | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =head1 VERSION | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | Version v1.7.3 | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | use PAGI::FastAPI::Depends qw(Depends); | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | my $get_db = async sub ($c) { | ||||||||||||||||||||||||||||
| 46 | return My::DB->connect; | ||||||||||||||||||||||||||||
| 47 | }; | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | my $auth_check = async sub ($c) { | ||||||||||||||||||||||||||||
| 50 | my $token = $c->header('Authorization'); | ||||||||||||||||||||||||||||
| 51 | unless ($token) { | ||||||||||||||||||||||||||||
| 52 | $c->status(401); | ||||||||||||||||||||||||||||
| 53 | return { detail => 'Unauthorized' }; | ||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | }; | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | $app->get('/items', | ||||||||||||||||||||||||||||
| 58 | dependencies => [ | ||||||||||||||||||||||||||||
| 59 | Depends($get_db, key => 'db'), # Store result in $c->stash->{db} | ||||||||||||||||||||||||||||
| 60 | Depends($auth_check), # Execution guard dependency | ||||||||||||||||||||||||||||
| 61 | ], | ||||||||||||||||||||||||||||
| 62 | handler => async sub ($c) { | ||||||||||||||||||||||||||||
| 63 | my $db = $c->stash->{db}; | ||||||||||||||||||||||||||||
| 64 | return { status => 'ok' }; | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | C<PAGI::FastAPI::Depends> provides a lightweight container object for wrapping | ||||||||||||||||||||||||||||
| 71 | async dependency subroutines in L<PAGI::FastAPI>. | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | When registered on a route, dependencies execute asynchronously before the | ||||||||||||||||||||||||||||
| 74 | primary route handler. If a C<key> option is specified, the return value of | ||||||||||||||||||||||||||||
| 75 | the dependency is automatically injected into the request context's stash | ||||||||||||||||||||||||||||
| 76 | (C<< $c->stash->{$key} >>). | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =head2 C<Depends($code_ref, [%options])> | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | Exportable helper function that constructs a new C<PAGI::FastAPI::Depends> instance. | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | my $dep = Depends($code_ref, key => 'user'); | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | Options: | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | C<key> - Optional scalar string key under which the dependency's return | ||||||||||||||||||||||||||||
| 89 | value will be stored in C<< $c->stash >>. | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =head1 METHODS | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =head2 C<new(code => $code_ref, [key => $key])> | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | Constructs a new C<PAGI::FastAPI::Depends> object instance. Dies if | ||||||||||||||||||||||||||||
| 96 | C<$code_ref> is missing or not a C<CODE> reference. | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =head2 C<code()> | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | Returns the underlying code reference for the dependency subroutine. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =head2 C<key()> | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | Returns the stash key string associated with the dependency, or C<undef> | ||||||||||||||||||||||||||||
| 105 | if none was defined. | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >> | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =head1 REPOSITORY | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | L<https://github.com/manwar/PAGI-FastAPI> | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =head1 BUGS | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/PAGI-FastAPI/issues>. | ||||||||||||||||||||||||||||
| 118 | I will be notified and then you'll automatically be notified of progress on your | ||||||||||||||||||||||||||||
| 119 | bug as I make changes. | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | perldoc PAGI::FastAPI::Depends | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | You can also look for information at: | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =over 4 | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =item * BUG Report | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | L<https://github.com/manwar/PAGI-FastAPI/issues> | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =item * Search MetaCPAN | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | L<https://metacpan.org/dist/PAGI-FastAPI/> | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =back | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | Copyright (C) 2026 Mohammad Sajid Anwar. | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | This program is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 146 | the terms of the Artistic License (2.0). | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =cut | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 21 | 1; # End of PAGI::FastAPI::Depends | |||||||||||||||||||||||||||