format
This commit is contained in:
parent
0a67377791
commit
ea112b2bd5
159
t/example.t
159
t/example.t
@ -9,66 +9,60 @@ 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",
|
updated => "2015-05-22T14:56:28.000Z",
|
||||||
updated=> "2015-05-22T14:56:28.000Z",
|
author => {
|
||||||
author=> {
|
id => "1",
|
||||||
id=> "1",
|
firstName => "Kaley",
|
||||||
firstName=> "Kaley",
|
lastName => "Maggio",
|
||||||
lastName=> "Maggio",
|
email => "Kaley-Maggio\@example.com",
|
||||||
email=> "Kaley-Maggio\@example.com",
|
age => "80",
|
||||||
age=> "80",
|
gender => "male"
|
||||||
gender=> "male"
|
},
|
||||||
},
|
tags => [ "1", "2" ],
|
||||||
tags=> ["1", "2"],
|
photos => [
|
||||||
photos=> [
|
"ed70cf44-9a34-4878-84e6-0c0e4a450cfe",
|
||||||
"ed70cf44-9a34-4878-84e6-0c0e4a450cfe",
|
"24ba3666-a593-498c-9f5d-55a4ee08c72e",
|
||||||
"24ba3666-a593-498c-9f5d-55a4ee08c72e",
|
"f386492d-df61-4573-b4e3-54f6f5d08acf"
|
||||||
"f386492d-df61-4573-b4e3-54f6f5d08acf"
|
],
|
||||||
],
|
comments => [
|
||||||
comments=> [
|
{ _id => "1",
|
||||||
{
|
body => "First !",
|
||||||
_id=> "1",
|
created => "2015-08-14T18:42:16.475Z"
|
||||||
body=> "First !",
|
},
|
||||||
created=> "2015-08-14T18:42:16.475Z"
|
{ _id => "2",
|
||||||
},
|
body => "I Like !",
|
||||||
{
|
created => "2015-09-14T18:42:12.475Z"
|
||||||
_id=> "2",
|
},
|
||||||
body=> "I Like !",
|
{ _id => "3",
|
||||||
created=> "2015-09-14T18:42:12.475Z"
|
body => "Awesome",
|
||||||
},
|
created => "2015-09-15T18:42:12.475Z"
|
||||||
{
|
}
|
||||||
_id=> "3",
|
]
|
||||||
body=> "Awesome",
|
}
|
||||||
created=> "2015-09-15T18:42:12.475Z"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
];
|
];
|
||||||
|
|
||||||
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 => {
|
top_level_links => { self => '/articles', },
|
||||||
self => '/articles',
|
links => {
|
||||||
},
|
self => sub ( $data, @ ) {
|
||||||
links => {
|
|
||||||
self => sub($data,@) {
|
|
||||||
return "/articles/" . $data->{id};
|
return "/articles/" . $data->{id};
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
relationships => {
|
relationships => {
|
||||||
'tags' => { type => 'tag' },
|
'tags' => { type => 'tag' },
|
||||||
'comments' => { type => 'comment' },
|
'comments' => { type => 'comment' },
|
||||||
author => {
|
author => {
|
||||||
type => "people",
|
type => "people",
|
||||||
@ -91,48 +85,47 @@ $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" => {
|
||||||
},
|
"count" => 2,
|
||||||
"meta"=> {
|
"total" => 1
|
||||||
"count"=> 2,
|
|
||||||
"total"=> 1
|
|
||||||
},
|
|
||||||
"links"=> {
|
|
||||||
"self"=> "/articles"
|
|
||||||
},
|
|
||||||
"data"=> [{
|
|
||||||
"type"=> "article",
|
|
||||||
"id"=> "1",
|
|
||||||
"attributes"=> {
|
|
||||||
"title"=> "JSON API paints my bikeshed!",
|
|
||||||
"body"=> "The shortest article. Ever.",
|
|
||||||
"created"=> "2015-05-22T14:56:29.000Z"
|
|
||||||
},
|
},
|
||||||
"relationships"=> {
|
"links" => { "self" => "/articles" },
|
||||||
"tags"=> {
|
"data" => [
|
||||||
"data"=> [{
|
{ "type" => "article",
|
||||||
"type"=> "tag",
|
"id" => "1",
|
||||||
"id"=> "1"
|
"attributes" => {
|
||||||
}, {
|
"title" => "JSON API paints my bikeshed!",
|
||||||
"type"=> "tag",
|
"body" => "The shortest article. Ever.",
|
||||||
"id"=> "2"
|
"created" => "2015-05-22T14:56:29.000Z"
|
||||||
}]
|
},
|
||||||
},
|
"relationships" => {
|
||||||
}
|
"tags" => {
|
||||||
}],
|
"data" => [
|
||||||
|
{ "type" => "tag",
|
||||||
|
"id" => "1"
|
||||||
|
},
|
||||||
|
{ "type" => "tag",
|
||||||
|
"id" => "2"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
done_testing;
|
done_testing;
|
||||||
|
Loading…
Reference in New Issue
Block a user