Skip to content

Commit

Permalink
feat(rest): extend REST request to allow OPTIONS without error (#55)
Browse files Browse the repository at this point in the history
Co-authored-by: Evan Johnson <[email protected]>
  • Loading branch information
damienwebdev and thaddeusmt authored Jun 4, 2021
1 parent 7efabca commit eb1df2d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Request/RestRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* @copyright Copyright 2017 SplashLab
*/
/**
* Copyright © Graycore, LLC. All rights reserved.
* See LICENSE.md for details.
*/

namespace Graycore\Cors\Request;

use Magento\Framework\Webapi\Request;
use Magento\Framework\Exception\InputException;

class RestRequest
{
/**
* Triggers before original dispatch
*
* @param Request $subject
* @return void
* @throws InputException
*/
public function aroundGetHttpMethod(
Request $subject
) {
if (!$subject->isGet()
&& !$subject->isPost()
&& !$subject->isPut()
&& !$subject->isDelete()
&& !$subject->isOptions()
) {
throw new InputException(__('Request method is invalid.'));
}
return $subject->getMethod();
}
}
3 changes: 3 additions & 0 deletions etc/webapi_rest/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
<type name="Magento\Webapi\Controller\Rest">
<plugin name="graycoreCorsPreflightHandlerRest" type="Graycore\Cors\Response\Preflight\Rest\PreflightRequestHandler" />
</type>
<type name="Magento\Framework\Webapi\Rest\Request">
<plugin name="cors_request_options" type="Graycore\Cors\Request\RestRequest" />
</type>
<type name="Magento\Framework\Webapi\Rest\Response">
<plugin name="genericWebApiHeaderPlugin" type="Graycore\Cors\Response\HeaderManager"/>
</type>
Expand Down

0 comments on commit eb1df2d

Please sign in to comment.