Skip to content

Commit 169f8a2

Browse files
committed
centralize all ts safe method logic
1 parent cd17ae4 commit 169f8a2

File tree

5 files changed

+30
-25
lines changed

5 files changed

+30
-25
lines changed

src/GenerateCommand.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -202,18 +202,8 @@ private function writeNamedFile(Collection $routes, string $namespace): void
202202
$imports = $routes->map(fn (Route $route) => $route->namedMethod())->implode(', ');
203203

204204
$basename = basename($path, '.ts');
205-
$base = Str::of($basename)->when(
206-
str_contains($basename, '-'),
207-
fn ($s) => $s->camel()
208-
)->toString();
209205

210-
if (in_array($base, TypeScript::RESERVED_KEYWORDS)) {
211-
$base = $base.'Route';
212-
}
213-
214-
if (is_numeric($base)) {
215-
$base = 'route'.$base;
216-
}
206+
$base = TypeScript::safeMethod($basename, 'Route');
217207

218208
if ($base !== $imports) {
219209
$this->appendContent($path, "const {$base} = { {$imports} }\n");

src/Route.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,7 @@ public function originalJsMethod()
5656

5757
public function namedMethod(): string
5858
{
59-
$base = Str::afterLast($this->name(), '.');
60-
61-
return $this->finalJsMethod(
62-
str_contains($base, '-') ?
63-
Str::camel($base) : $base
64-
);
59+
return $this->finalJsMethod(Str::afterLast($this->name(), '.'));
6560
}
6661

6762
public function controller(): string
@@ -165,13 +160,7 @@ public function controllerMethodLineNumber(): int
165160

166161
private function finalJsMethod(string $method): string
167162
{
168-
$method = in_array($method, TypeScript::RESERVED_KEYWORDS) ? $method.'Method' : $method;
169-
170-
if (is_numeric($method)) {
171-
return 'method'.$method;
172-
}
173-
174-
return $method;
163+
return TypeScript::safeMethod($method, 'Method');
175164
}
176165

177166
private function relativePath(string $path)

src/TypeScript.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,27 @@ class TypeScript
4444
'with',
4545
];
4646

47+
public static function safeMethod(string $method, string $suffix): string
48+
{
49+
$method = str($method);
50+
51+
if ($method->contains('-')) {
52+
$method = $method->camel();
53+
}
54+
55+
$suffix = strtolower($suffix);
56+
57+
if (in_array($method, self::RESERVED_KEYWORDS)) {
58+
return $method->append(ucfirst($suffix));
59+
}
60+
61+
if (is_numeric((string) $method)) {
62+
return $method->prepend($suffix);
63+
}
64+
65+
return $method;
66+
}
67+
4768
public static function cleanUp(string $view): string
4869
{
4970
$replacements = [

tests/DisallowedMethodNames.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import DisallowedMethodNameController, {
33
deleteMethod,
44
method404,
55
} from "../workbench/resources/js/actions/App/Http/Controllers/DisallowedMethodNameController";
6+
import route404 from "../workbench/resources/js/routes/disallowed/404";
67

78
test("will append `method` to invalid methods", () => {
89
expect(method404.url()).toBe("/disallowed/404");
@@ -12,3 +13,7 @@ test("will append `method` to invalid methods", () => {
1213
);
1314
expect(DisallowedMethodNameController[404].url()).toBe("/disallowed/404");
1415
});
16+
17+
test("will append `method` to invalid methods", () => {
18+
expect(route404.method404.url()).toBe("/disallowed/404");
19+
});

workbench/routes/web.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
Route::get('/two-routes-one-action-2', [TwoRoutesSameActionController::class, 'same']);
6767

6868
Route::get('/disallowed/delete', [DisallowedMethodNameController::class, 'delete']);
69-
Route::get('/disallowed/404', [DisallowedMethodNameController::class, '404']);
69+
Route::get('/disallowed/404', [DisallowedMethodNameController::class, '404'])->name('disallowed.404');
7070

7171
Route::get('/anonymous-middleware', [AnonymousMiddlewareController::class, 'show']);
7272

0 commit comments

Comments
 (0)