format
This commit is contained in:
parent
0a67377791
commit
ea112b2bd5
55
t/example.t
55
t/example.t
@ -9,8 +9,7 @@ use experimental qw/ signatures /;
|
|||||||
# example taken straight from https://www.npmjs.com/package/json-api-serializer
|
# example taken straight from https://www.npmjs.com/package/json-api-serializer
|
||||||
|
|
||||||
my $data = [
|
my $data = [
|
||||||
{
|
{ id => "1",
|
||||||
id=> "1",
|
|
||||||
title => "JSON API paints my bikeshed!",
|
title => "JSON API paints my bikeshed!",
|
||||||
body => "The shortest article. Ever.",
|
body => "The shortest article. Ever.",
|
||||||
created => "2015-05-22T14:56:29.000Z",
|
created => "2015-05-22T14:56:29.000Z",
|
||||||
@ -30,18 +29,15 @@ my $data = [
|
|||||||
"f386492d-df61-4573-b4e3-54f6f5d08acf"
|
"f386492d-df61-4573-b4e3-54f6f5d08acf"
|
||||||
],
|
],
|
||||||
comments => [
|
comments => [
|
||||||
{
|
{ _id => "1",
|
||||||
_id=> "1",
|
|
||||||
body => "First !",
|
body => "First !",
|
||||||
created => "2015-08-14T18:42:16.475Z"
|
created => "2015-08-14T18:42:16.475Z"
|
||||||
},
|
},
|
||||||
{
|
{ _id => "2",
|
||||||
_id=> "2",
|
|
||||||
body => "I Like !",
|
body => "I Like !",
|
||||||
created => "2015-09-14T18:42:12.475Z"
|
created => "2015-09-14T18:42:12.475Z"
|
||||||
},
|
},
|
||||||
{
|
{ _id => "3",
|
||||||
_id=> "3",
|
|
||||||
body => "Awesome",
|
body => "Awesome",
|
||||||
created => "2015-09-15T18:42:12.475Z"
|
created => "2015-09-15T18:42:12.475Z"
|
||||||
}
|
}
|
||||||
@ -52,16 +48,14 @@ my $data = [
|
|||||||
my $registry = Dancer2::Plugin::JsonApi::Registry->new;
|
my $registry = Dancer2::Plugin::JsonApi::Registry->new;
|
||||||
|
|
||||||
$registry->add_type(
|
$registry->add_type(
|
||||||
'article', {
|
'article',
|
||||||
top_level_meta => sub($data,$xtra) {
|
{ top_level_meta => sub ( $data, $xtra ) {
|
||||||
return +{
|
return +{
|
||||||
count => $xtra->{count},
|
count => $xtra->{count},
|
||||||
total => 0 + @$data,
|
total => 0 + @$data,
|
||||||
}
|
};
|
||||||
},
|
|
||||||
top_level_links => {
|
|
||||||
self => '/articles',
|
|
||||||
},
|
},
|
||||||
|
top_level_links => { self => '/articles', },
|
||||||
links => {
|
links => {
|
||||||
self => sub ( $data, @ ) {
|
self => sub ( $data, @ ) {
|
||||||
return "/articles/" . $data->{id};
|
return "/articles/" . $data->{id};
|
||||||
@ -91,30 +85,27 @@ $registry->add_type( 'comment',
|
|||||||
$registry->add_type(
|
$registry->add_type(
|
||||||
'people',
|
'people',
|
||||||
{ links => sub ( $data, @ ) { '/peoples/' . $data->{id} }
|
{ links => sub ( $data, @ ) { '/peoples/' . $data->{id} }
|
||||||
} );
|
}
|
||||||
|
);
|
||||||
|
|
||||||
my $output = $registry->serialize( 'article', $data, { count => 2 } );
|
my $output = $registry->serialize( 'article', $data, { count => 2 } );
|
||||||
|
|
||||||
like $output->{data}[0]{relationships}{author}, {
|
like $output->{data}[0]{relationships}{author},
|
||||||
links => {
|
{ links => {
|
||||||
"self" => "/articles/1/relationships/author",
|
"self" => "/articles/1/relationships/author",
|
||||||
"related" => "/articles/1/author"
|
"related" => "/articles/1/author"
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
like $output => {
|
like $output => {
|
||||||
"jsonapi"=> {
|
"jsonapi" => { "version" => "1.0" },
|
||||||
"version"=> "1.0"
|
|
||||||
},
|
|
||||||
"meta" => {
|
"meta" => {
|
||||||
"count" => 2,
|
"count" => 2,
|
||||||
"total" => 1
|
"total" => 1
|
||||||
},
|
},
|
||||||
"links"=> {
|
"links" => { "self" => "/articles" },
|
||||||
"self"=> "/articles"
|
"data" => [
|
||||||
},
|
{ "type" => "article",
|
||||||
"data"=> [{
|
|
||||||
"type"=> "article",
|
|
||||||
"id" => "1",
|
"id" => "1",
|
||||||
"attributes" => {
|
"attributes" => {
|
||||||
"title" => "JSON API paints my bikeshed!",
|
"title" => "JSON API paints my bikeshed!",
|
||||||
@ -123,16 +114,18 @@ like $output => {
|
|||||||
},
|
},
|
||||||
"relationships" => {
|
"relationships" => {
|
||||||
"tags" => {
|
"tags" => {
|
||||||
"data"=> [{
|
"data" => [
|
||||||
"type"=> "tag",
|
{ "type" => "tag",
|
||||||
"id" => "1"
|
"id" => "1"
|
||||||
}, {
|
},
|
||||||
"type"=> "tag",
|
{ "type" => "tag",
|
||||||
"id" => "2"
|
"id" => "2"
|
||||||
}]
|
}
|
||||||
|
]
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}],
|
}
|
||||||
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
done_testing;
|
done_testing;
|
||||||
|
Loading…
Reference in New Issue
Block a user