A little late to the party – but here is an answer.
While I agree with @Chris answer that a REST API is different from the PHP API available for themes, sometimes we still build the same frontends from the data, right?
If I want to show links on my blog to the next and previous post, I don't want to send 3 requests to the API.
As a solution I included this in my plugin containing the API adjustments for the specific project:
// Add filter to respond with next and previous post in post response.
add_filter( 'rest_prepare_post', function( $response, $post, $request ) {
// Only do this for single post requests.
if( $request->get_param('per_page') === 1 ) {
global $post;
// Get the so-called next post.
$next = get_adjacent_post( false, '', false );
// Get the so-called previous post.
$previous = get_adjacent_post( false, '', true );
// Format them a bit and only send id and slug (or null, if there is no next/previous post).
$response->data['next'] = ( is_a( $next, 'WP_Post') ) ? array( "id" => $next->ID, "slug" => $next->post_name ) : null;
$response->data['previous'] = ( is_a( $previous, 'WP_Post') ) ? array( "id" => $previous->ID, "slug" => $previous->post_name ) : null;
}
return $response;
}, 10, 3 );
Which gives you something like this:
[
{
"ID": 123,
...
"next": {
"id": 212,
"slug": "ea-quia-fuga-sit-blanditiis"
},
"previous": {
"id": 171,
"slug": "blanditiis-sed-id-assumenda"
},
...
}
]