A package for transitioning HTTP requests and responses based on a version header.
Release updates to your API schema without breaking existing clients by creating Transition classes that transition the request and/or response to match the previously expected result. Each layer of transitions only needs to convert the current version to match the previous version, from there the request and response will be piped through the subsequent layers until they match the version requested in the Api-Version header.
Largely based on Stripe's API versioning article.
$ composer require bbrothers/http-transitionsIn your app/Http/Kernel.php file, add:
protected $middleware = [
Transitions\TransitionMiddleware::class
];For Laravel 5.4, in your config/app.php file, in the providers array, add:
Transitions\TransitionProvider::classphp artisan vendor:publish --provider="Transitions\TransitionProvider"Add version numbers and an array of Transition classes to the transitions.php file.
return [
'headerKey' => 'Api-Version',
'transitions' => [
'20160101' => [
FullNameToNameTransition::class,
NameToFirstNameLastNameTransition::class,
BirthDateTransition::class,
],
'20150101' => [
FirstNameLastNameToFullNameTransition::class,
],
],
]Create a transition:
class NameToFirstNameLastNameTransition extends Transition
{
public function transformResponse(Response $response) : Response
{
$content = json_decode($response->getContent(), true);
$content = array_diff_key(array_merge($content, $content['name']), ['name' => true]);
return $response->setContent(json_encode($content));
}
}$ php artisan make:transition NameToFirstNameLastNameTransitionOptionally, the --request-only or --response-only flags can be added to create a transform that only generates a transformRequest or transformResponse method respectively.
Please see CHANGELOG for more information on what has changed recently.
The MIT License (MIT). Please see License File for more information.