Merge branch 'example'

releases
Yanick Champoux 2023-10-31 16:19:05 -04:00
commit d9769d985e
1 changed files with 77 additions and 0 deletions

77
t/example.t Normal file
View File

@ -0,0 +1,77 @@
use 5.32.0;
use Test2::V0;
use Dancer2::Plugin::JsonApi::Registry;
use experimental qw/ signatures /;
# example taken straight from https://www.npmjs.com/package/json-api-serializer
my $data = [
{
id=> "1",
title=> "JSON API paints my bikeshed!",
body=> "The shortest article. Ever.",
created=> "2015-05-22T14:56:29.000Z",
updated=> "2015-05-22T14:56:28.000Z",
author=> {
id=> "1",
firstName=> "Kaley",
lastName=> "Maggio",
email=> "Kaley-Maggio\@example.com",
age=> "80",
gender=> "male"
},
tags=> ["1", "2"],
photos=> [
"ed70cf44-9a34-4878-84e6-0c0e4a450cfe",
"24ba3666-a593-498c-9f5d-55a4ee08c72e",
"f386492d-df61-4573-b4e3-54f6f5d08acf"
],
comments=> [
{
_id=> "1",
body=> "First !",
created=> "2015-08-14T18:42:16.475Z"
},
{
_id=> "2",
body=> "I Like !",
created=> "2015-09-14T18:42:12.475Z"
},
{
_id=> "3",
body=> "Awesome",
created=> "2015-09-15T18:42:12.475Z"
}
]
}
];
my $registry = Dancer2::Plugin::JsonApi::Registry->new;
$registry->add_type(
'article', {
top_level_meta => sub($data,$xtra) {
return +{
count => $xtra->{count},
total => 0+ @$data,
}
}
}
);
my $output = $registry->serialize('article', $data, { count => 2 } );
like $output => {
"jsonapi"=> {
"version"=> "1.0"
},
"meta"=> {
"count"=> 2,
"total"=> 1
},
};
done_testing();