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

make HEAD/GET method rewrite configurable #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/router/BaseRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
use function Facebook\AutoloadMap\Generated\is_dev;

abstract class BaseRouter<+TResponder> {
public function __construct(
private BaseRouterOptions $options = shape('use_get_responder_for_head' => true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps ?BaseRouterOptions, and make all the fields optional?

This will be needed when a second option is added - it doesn't really matter now, but doing it would make it clearer what the 'right way' is to add one.

This would also need something like:

$this->options = shape(
  'use_get_responder_for_head' => $options['use_get_responder_for_head'] ?? true
);

) {}

abstract protected function getRoutes(
): KeyedContainer<HttpMethod, KeyedContainer<string, TResponder>>;

Expand All @@ -33,7 +37,7 @@ final public function routeMethodAndPath(
}

if (
$method === HttpMethod::HEAD && $allowed === keyset[HttpMethod::GET]
$this->options['use_get_responder_for_head'] && $method === HttpMethod::HEAD && $allowed === keyset[HttpMethod::GET]
) {
list($responder, $data) = $resolver->resolve(HttpMethod::GET, $path);
$data = Dict\map($data, $value ==> \urldecode($value));
Expand Down
16 changes: 16 additions & 0 deletions src/router/BaseRouterOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?hh // strict

/*
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

namespace Facebook\HackRouter;

type BaseRouterOptions = shape(
'use_get_responder_for_head' => bool,
);
1 change: 1 addition & 0 deletions tests/lib/TestRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function __construct(
private dict<string, T> $routes,
private ?IResolver<T> $resolver = null,
) {
parent::__construct();
}

<<__Override>>
Expand Down