Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.

Commit 031136c

Browse files
committed
make HEAD/GET method rewrite configurable
1 parent c71cb58 commit 031136c

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

src/router/BaseRouter.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
use function Facebook\AutoloadMap\Generated\is_dev;
1515

1616
abstract class BaseRouter<+TResponder> {
17+
public function __construct(
18+
private RouterOptions $options = shape('head_rewrite' => true)
19+
) {}
20+
1721
abstract protected function getRoutes(
1822
): ImmMap<HttpMethod, ImmMap<string, TResponder>>;
1923

@@ -33,7 +37,7 @@ final public function routeMethodAndPath(
3337
}
3438

3539
if (
36-
$method === HttpMethod::HEAD && $allowed === keyset[HttpMethod::GET]
40+
$this->options['head_rewrite'] && $method === HttpMethod::HEAD && $allowed === keyset[HttpMethod::GET]
3741
) {
3842
list($responder, $data) = $resolver->resolve(HttpMethod::GET, $path);
3943
$data = Dict\map($data, $value ==> \urldecode($value));

src/router/RouterOptions.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?hh // strict
2+
3+
/*
4+
* Copyright (c) 2015-present, Facebook, Inc.
5+
* All rights reserved.
6+
*
7+
* This source code is licensed under the MIT license found in the
8+
* LICENSE file in the root directory of this source tree.
9+
*
10+
*/
11+
12+
namespace Facebook\HackRouter;
13+
14+
type RouterOptions = shape(
15+
'head_rewrite' => bool,
16+
);

tests/lib/TestRouter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public function __construct(
2121
private dict<string, T> $routes,
2222
private ?IResolver<T> $resolver = null,
2323
) {
24+
parent::__construct();
2425
}
2526

2627
<<__Override>>

0 commit comments

Comments
 (0)