diff --git a/resources/method.blade.ts b/resources/method.blade.ts index 37218c6..127ab10 100644 --- a/resources/method.blade.ts +++ b/resources/method.blade.ts @@ -1,5 +1,5 @@ @include('wayfinder::docblock') -{!! when(($export ?? true) && !$isInvokable, 'export ') !!}const {!! $method !!} = (@include('wayfinder::function-arguments')): RouteDefinition<@js($verbs->first()->actual)> => ({ +{!! when($shouldExport, 'export ') !!}const {!! $method !!} = (@include('wayfinder::function-arguments')): RouteDefinition<@js($verbs->first()->actual)> => ({ url: {!! $method !!}.url({!! when($parameters->isNotEmpty(), 'args, ') !!}options), method: @js($verbs->first()->actual), }) diff --git a/resources/multi-method.blade.ts b/resources/multi-method.blade.ts index 5445290..d212d56 100644 --- a/resources/multi-method.blade.ts +++ b/resources/multi-method.blade.ts @@ -3,11 +3,11 @@ ...$route, 'method' => $route['tempMethod'], 'docblock_method' => $route['method'], - 'export' => false, + 'shouldExport' => false, ]) @endforeach -{!! when(!$isInvokable, 'export ') !!}const {!! $method !!} = { +{!! when($shouldExport, 'export ') !!}const {!! $method !!} = { @foreach ($routes as $route) {!! $route['uri'] !!}: {!! $route['tempMethod'] !!}, @endforeach diff --git a/src/GenerateCommand.php b/src/GenerateCommand.php index 40f3d1f..3215afa 100644 --- a/src/GenerateCommand.php +++ b/src/GenerateCommand.php @@ -179,11 +179,12 @@ private function writeMultiRouteControllerMethodExport(Collection $routes, strin 'path' => $routes->first()->controllerPath(), 'line' => $routes->first()->controllerMethodLineNumber(), 'controller' => $routes->first()->controller(), - 'isInvokable' => $routes->first()->hasInvokableController(), + 'isInvokable' => $isInvokable = $routes->first()->hasInvokableController(), + 'shouldExport' => ! $isInvokable, 'withForm' => $this->option('with-form') ?? false, 'routes' => $routes->map(fn ($r) => [ 'method' => $r->jsMethod(), - 'tempMethod' => $r->jsMethod().md5($r->uri()), + 'tempMethod' => $r->jsMethod().hash('xxh3', $r->uri()), 'parameters' => $r->parameters(), 'verbs' => $r->verbs(), 'uri' => $r->uri(), @@ -197,7 +198,8 @@ private function writeControllerMethodExport(Route $route, string $path): void 'controller' => $route->controller(), 'method' => $route->jsMethod(), 'original_method' => $route->originalJsMethod(), - 'isInvokable' => $route->hasInvokableController(), + 'isInvokable' => $isInvokable = $route->hasInvokableController(), + 'shouldExport' => ! $isInvokable, 'path' => $route->controllerPath(), 'line' => $route->controllerMethodLineNumber(), 'parameters' => $route->parameters(), @@ -245,10 +247,11 @@ private function appendCommonImports(Collection $routes, string $path, string $n private function writeNamedMethodExport(Route $route, string $path): void { $this->appendContent($path, $this->view->make('wayfinder::method', [ - 'controller' => $route->controller(), + 'controller' => $controller = $route->controller(), 'method' => $route->namedMethod(), 'original_method' => $route->originalJsMethod(), - 'isInvokable' => false, + 'isInvokable' => $isInvokable = $route->hasInvokableController(), + 'shouldExport' => (! $isInvokable) || str_contains($controller, '\\Closure'), 'path' => $route->controllerPath(), 'line' => $route->controllerMethodLineNumber(), 'parameters' => $route->parameters(), @@ -266,14 +269,12 @@ private function writeBarrelFiles(array|Collection $children, string $parent): v return; } - $normalizeToCamelCase = fn ($value) => str_contains($value, '-') ? Str::camel($value) : $value; - $indexPath = join_paths($this->base(), $parent, 'index.ts'); $childKeys = $children->keys()->mapWithKeys(fn ($child) => [ $child => [ - 'safe' => TypeScript::safeMethod($normalizeToCamelCase($child), 'Method'), - 'normalized' => $normalizeToCamelCase($child), + 'safe' => TypeScript::safeMethod($child, 'Method'), + 'normalized' => (string) str($child)->whenContains('-', fn ($s) => $s->camel()), ], ]); @@ -290,7 +291,7 @@ private function writeBarrelFiles(array|Collection $children, string $parent): v $keys = $childKeys->map(fn ($alias, $key) => str_repeat(' ', 4).implode(': ', array_unique([$alias['normalized'], $alias['safe']])))->implode(', '.PHP_EOL); - $varExport = $normalizeToCamelCase(Str::afterLast($parent, DIRECTORY_SEPARATOR)); + $varExport = TypeScript::safeMethod(Str::afterLast($parent, DIRECTORY_SEPARATOR), 'Method'); $this->appendContent($indexPath, <<append(ucfirst($suffix)); } - if (is_numeric((string) $method)) { + if ($method->match('/^[a-zA-Z_$]/')->isEmpty()) { return $method->prepend($suffix); }