From 7cee2ecb4776e800341cf89cd2905e5731103a29 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sun, 12 Nov 2023 17:06:03 -0500 Subject: [PATCH] fleshing out the plugin --- lib/Dancer2/Plugin/JsonApi.pm | 53 ++++++++++++++++++++++++++++++++++- t/plugin.t | 10 +++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 t/plugin.t diff --git a/lib/Dancer2/Plugin/JsonApi.pm b/lib/Dancer2/Plugin/JsonApi.pm index c40a4bd..59d04fc 100644 --- a/lib/Dancer2/Plugin/JsonApi.pm +++ b/lib/Dancer2/Plugin/JsonApi.pm @@ -1,5 +1,52 @@ +use 5.38.0; + package Dancer2::Plugin::JsonApi; +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'; +} + 1; __END__ @@ -8,7 +55,11 @@ __END__ Dancer2::Plugin::JsonAPI -=head1 SEE ALSO +=head2 DESCRIPTION + +If the serializer is not already explicitly set, the plugin will configure it to be L. + +=head2 SEE ALSO =over diff --git a/t/plugin.t b/t/plugin.t new file mode 100644 index 0000000..a1d4220 --- /dev/null +++ b/t/plugin.t @@ -0,0 +1,10 @@ +use 5.38.0; + +use Test2::V0; + +use Dancer2; +use Dancer2::Plugin::JsonApi; + +pass 'we compile!'; + +done_testing;