├── .gitignore ├── LICENSE ├── README.md ├── composer.json └── src └── Idempotency.php /.gitignore: -------------------------------------------------------------------------------- 1 | composer.phar 2 | /vendor/ 3 | .idea 4 | # Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file 5 | # You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file 6 | composer.lock 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) 2018 Javier Vidal 4 | 5 | > Permission is hereby granted, free of charge, to any person obtaining a copy 6 | > of this software and associated documentation files (the "Software"), to deal 7 | > in the Software without restriction, including without limitation the rights 8 | > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | > copies of the Software, and to permit persons to whom the Software is 10 | > furnished to do so, subject to the following conditions: 11 | > 12 | > The above copyright notice and this permission notice shall be included in 13 | > all copies or substantial portions of the Software. 14 | > 15 | > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | > THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Idempotent Requests 2 | 3 | First, what is Idempotency and why I need it? 4 | 5 | Checkout this awesome [post from Brandum Leach](https://stripe.com/blog/idempotency). 6 | 7 | ### Install 8 | 9 | Require this package with composer using the following command: 10 | 11 | ```bash 12 | composer require javidalpe/laravel-idempotency 13 | ``` 14 | 15 | ### Usage 16 | 17 | Register Idempotency middleware on your http kernel file: 18 | 19 | ```php 20 | 'api' => [ 21 | 'throttle:60,1', 22 | 'bindings', 23 | \Javidalpe\Idempotency\Idempotency::class, 24 | ], 25 | ``` 26 | 27 | To perform an idempotent request, provide an additional `Idempotency-Key: ` header to the request. 28 | 29 | ### How it works 30 | 31 | If the header `Idempotency-Key` is present on the request and the request method is different from GET, PUT and DELETE, the middleware stores the response on the cache. Next time you make a request with same idempotency key, the middleware will return the cached response. 32 | 33 | How you create unique keys is up to you, but I suggest using V4 UUIDs or another appropriately random string. It'll always send back the same response for requests made with the same key, and keys can't be reused with different request parameters. Keys expire after 24 hours. 34 | 35 | ### License 36 | 37 | The Laravel Idempotency is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT) 38 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "javidalpe/laravel-idempotency", 3 | "description": "Laravel Idempotency Middleware", 4 | "type": "library", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Javier Vidal", 9 | "email": "jvpenya@gmail.com" 10 | } 11 | ], 12 | "keywords": [ 13 | "laravel", 14 | "middleware", 15 | "idempotency", 16 | "api" 17 | ], 18 | "autoload": { 19 | "psr-4": { 20 | "Javidalpe\\Idempotency\\": "src" 21 | } 22 | }, 23 | "minimum-stability": "dev", 24 | "prefer-stable": true, 25 | "require": { 26 | "php": ">=5.4.0" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Idempotency.php: -------------------------------------------------------------------------------- 1 | method() == 'GET' || $request->method() == 'PUT' || $request->method() == 'DELETE') { 23 | return $next($request); 24 | } 25 | 26 | $requestId = $request->header(self::IDEMPOTENCY_HEADER); 27 | if (!$requestId) { 28 | return $next($request); 29 | } 30 | 31 | if (Cache::has($requestId)) { 32 | return Cache::get($requestId); 33 | } 34 | 35 | $response = $next($request); 36 | $response->header(self::IDEMPOTENCY_HEADER, $requestId); 37 | Cache::put($requestId, $response, self::EXPIRATION_IN_MINUTES); 38 | return $response; 39 | } 40 | } 41 | --------------------------------------------------------------------------------