2023-11-12 22:06:03 +00:00
|
|
|
use 5.38.0;
|
|
|
|
|
2023-10-31 14:22:36 +00:00
|
|
|
package Dancer2::Plugin::JsonApi;
|
|
|
|
|
2023-11-12 22:06:03 +00:00
|
|
|
use Dancer2::Plugin::JsonApi::Registry;
|
|
|
|
use Dancer2::Plugin;
|
|
|
|
use Dancer2::Serializer::JsonApi;
|
|
|
|
|
|
|
|
use experimental qw/ try /;
|
|
|
|
|
|
|
|
has registry => (
|
|
|
|
plugin_keyword => 'jsonapi_registry',
|
|
|
|
is => 'ro',
|
|
|
|
default => sub ($self) {
|
|
|
|
Dancer2::Plugin::JsonApi::Registry->new( app => $self->app );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
sub jsonapi : PluginKeyword ( $plugin, $type, $sub ) {
|
|
|
|
|
|
|
|
return sub {
|
|
|
|
my $result = $sub->();
|
|
|
|
|
|
|
|
return [
|
|
|
|
$type => $result,
|
|
|
|
{ vars => $plugin->app->request->vars,
|
|
|
|
request => $plugin->app->request
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub BUILD ( $self, @ ) {
|
|
|
|
my $serializer = do {
|
|
|
|
try { $self->app->serializer_engine } catch ($e) {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
unless ($serializer) {
|
|
|
|
$self->app->set_serializer_engine(
|
|
|
|
Dancer2::Serializer::JsonApi->new );
|
|
|
|
$serializer = $self->app->serializer_engine;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$serializer->registry( $self->registry )
|
|
|
|
if ref $serializer eq 'Dancer2::Serializer::JsonApi';
|
|
|
|
}
|
|
|
|
|
2023-10-31 14:22:36 +00:00
|
|
|
1;
|
|
|
|
|
|
|
|
__END__
|
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
Dancer2::Plugin::JsonAPI
|
|
|
|
|
2023-11-12 22:06:03 +00:00
|
|
|
=head2 DESCRIPTION
|
|
|
|
|
|
|
|
If the serializer is not already explicitly set, the plugin will configure it to be L<Dancer2::Serializer::JsonApi>.
|
|
|
|
|
|
|
|
=head2 SEE ALSO
|
2023-10-31 14:22:36 +00:00
|
|
|
|
|
|
|
=over
|
|
|
|
|
|
|
|
=item * The L<JSON:API|https://jsonapi.org> specs, natch.
|
|
|
|
|
|
|
|
=item * L<json-api-serializer|https://www.npmjs.com/package/json-api-serializer> My go to for serializing JSON API documents in JavaScript-land. Also, inspiration for this module.
|
|
|
|
|
|
|
|
=back
|
|
|
|
|