Giter VIP home page Giter VIP logo

request-migrations's Introduction

HTTP Request Migrations

Latest Version on Packagist Build Status Total Downloads StyleCI

This package is based on the API versioning scheme used at Stripe. Users pass a version header and you automatically migrate the request & response data to match the current version of your code.

Installation

You can install the package via composer:

Installation via Composer

composer require tomschlick/request-migrations

Service Provider & Facade

This package supports Laravel 5.5 autoloading so the service provider and facade will be loaded automatically.

If you are using an earlier version of Laravel or have autoloading disabled you need to add the service provider and facade to config/app.php.

'providers' => [
    \TomSchlick\RequestMigrations\RequestMigrationsServiceProvider.php,
]
'aliases' => [
    'RequestMigrations' => \TomSchlick\RequestMigrations\Facades\RequestMigrations::class,
]

Middleware

Add the middleware to your Http Kernel app/Http/Kernel.php.

protected $middleware = [
	\TomSchlick\RequestMigrations\RequestMigrationsMiddleware::class,
];

Configuration

Run the following Artisan command to publish the package configuration to config/request-migrations.php.

php artisan vendor:publish --provider="TomSchlick\RequestMigrations\RequestMigrationsServiceProvider"

Usage

Creating a Migration

You can generate a new request migration using the Artisan CLI.

php artisan make:request-migration ExampleMigration

The command will generate a request migration and publish it to App/Http/Migrations/*.

It will generate a migration, you can modify it like this:

class GroupNameMigration extends RequestMigration
{
    /**
     * Migrate the request for the application to "read".
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return \Illuminate\Http\Request
     */
    public function migrateRequest(Request $request) : Request
    {
        return $request;
    }

    /**
     * Migrate the response to display to the client.
     *
     * @param \Symfony\Component\HttpFoundation\Response $response
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function migrateResponse(Response $response) : Response
    {
        $content = json_decode($response->getContent(), true);

        $content['firstname'] = array_get($content, 'name.firstname');
        $content['lastname'] = array_get($content, 'name.lastname');
        unset($content['name']);

        return $response->setContent(json_encode($content));
    }

    /**
     * Define which named paths should this migration modify.
     *
     * @return array
     */
    public function paths() : array
    {
        return [
            'users/show',
        ];
    }
}

Override the Versions

use TomSchlick\RequestMigrations\Facades\RequestMigrations;

// set both response & request versions
RequestMigrations::setVersion('2017-01-01')

// set the request version
RequestMigrations::setRequestVersion('2017-01-01')

// set the response version
RequestMigrations::setResponseVersion('2017-01-01')

This can be useful if you are pinning the version to a user.

RequestMigrations::setVersion(auth()->user()->api_version);

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.

request-migrations's People

Contributors

cmgmyr avatar dependabot-preview[bot] avatar jbrooksuk avatar mbaeuerle avatar sixlive avatar tomschlick avatar vistik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

request-migrations's Issues

Documentation

  • How to modify a request
  • How to modify a response
  • Notate that using dates is not a requirement for versioning
  • Events

Add event tests

There should be tests to verify events are behaving correctly.

Migration Events

Broadcast 'read only' events passing the migration and the respective response/request.

  • RequestIsMigrating
  • RequestHasMigrated
  • ResponsetIsMigrating
  • ResponseHasMigrated

Should default to current version

Given

request-migrations.php

'current_version' => '2017-09-18',
'versions' => [
    '2017-09-18' => [],
    '2017-09-17' => [
        \App\Http\Migrations\FooMigration::class
    ]
],

It shouldn't run any migrations and default to the current version: 2017-09-18. Currently, it runs the migration for 2017-09-17.

Labeling as a 'Bug' because I think this was the intended functionality but I can't recall.

Support for Overriding Versions

Use Cases

  • Major API Versioning (e.g. api.example.com/v1)
  • Alternative storage for migration versions

Implementation

So I'm thinking an elegant solution for this would be some way of over riding the versions array, similar to ResposeMigrations::setVersion(). This would provide plenty of flexibility to pull the versions in from wherever you please, just using modifying the config as an example.

We would need a way to override the current version as well RequestMigrations::setCurrentVersion('YYYY-MM-DD') so we know what the current version is depending on which subset you use. We should also add it to RequestMigrations::setMigrations($migrations, $currentVersion). (open to some input here)

ExampleMiddleware.php

ResponseMigrations::setMigrations(config('request-migrations.versions.version_2'))`

config/request-migrations.php

...
'versions' => [
    'version_1' => [
        'YYYY-MM-DD' => [
            \App\Http\Migrations\ExampleChangeFieldName::class
        ],
    ],
    'version_2' => [
        'YYYY-MM-DD' => [
          \App\Http\Migrations\ExampleChangeFieldName::class
        ],
    ],
],
...

Middleware

Add a middleware that will migrate the request

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.