Skip to content

Commit 31741ff

Browse files
committed
feat: export route utility types
1 parent 7d4d354 commit 31741ff

File tree

4 files changed

+29
-22
lines changed

4 files changed

+29
-22
lines changed

resources/function-arguments.blade.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ args{!! when($parameters->every->optional, '?') !!}: {
2323
@endif
2424
,
2525
@endif
26-
options?: { query?: QueryParams, mergeQuery?: QueryParams }
26+
options?: RouteQueryOptions

resources/js/wayfinder.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,21 @@ export type QueryParams = Record<
99
| Record<string, string | number | boolean>
1010
>;
1111

12-
export const queryParams = (options?: {
12+
export type RouteDefinition<TMethod extends string | string[]> = {
13+
url: string;
14+
} & (TMethod extends string[] ? { methods: TMethod } : { method: TMethod });
15+
16+
export type RouteFormDefinition<TMethod extends string> = {
17+
action: string;
18+
method: TMethod;
19+
};
20+
21+
export type RouteQueryOptions = {
1322
query?: QueryParams;
1423
mergeQuery?: QueryParams;
15-
}) => {
24+
}
25+
26+
export const queryParams = (options?: RouteQueryOptions) => {
1627
if (!options || (!options.query && !options.mergeQuery)) {
1728
return "";
1829
}

resources/method.blade.ts

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
@include('wayfinder::docblock')
2-
{!! when(($export ?? true) && !$isInvokable, 'export ') !!}const {!! $method !!} = (@include('wayfinder::function-arguments')): {
3-
url: string,
4-
method: @js($verbs->first()->actual),
5-
} => ({
2+
{!! when(($export ?? true) && !$isInvokable, 'export ') !!}const {!! $method !!} = (@include('wayfinder::function-arguments')): RouteDefinition<@js($verbs->first()->actual)> => ({
63
url: {!! $method !!}.url({!! when($parameters->isNotEmpty(), 'args, ') !!}options),
74
method: @js($verbs->first()->actual),
85
})
96

7+
@php
8+
$verbsArray = $verbs->map(fn($verb) => $verb->actual)->join("','");
9+
@endphp
10+
1011
{!! $method !!}.definition = {
11-
methods: [@foreach ($verbs as $verb)@js($verb->actual){!! when(! $loop->last, ',') !!}@endforeach],
12+
methods: ['{!! $verbsArray !!}'],
1213
url: {!! $uri !!},
13-
}
14+
} satisfies RouteDefinition<['{!! $verbsArray !!}']>
1415

1516
@include('wayfinder::docblock')
1617
{!! $method !!}.url = (@include('wayfinder::function-arguments')) => {
@@ -69,21 +70,15 @@
6970

7071
@foreach ($verbs as $verb)
7172
@include('wayfinder::docblock')
72-
{!! $method !!}.{!! $verb->actual !!} = (@include('wayfinder::function-arguments')): {
73-
url: string,
74-
method: @js($verb->actual),
75-
} => ({
73+
{!! $method !!}.{!! $verb->actual !!} = (@include('wayfinder::function-arguments')): RouteDefinition<@js($verb->actual)> => ({
7674
url: {!! $method !!}.url({!! when($parameters->isNotEmpty(), 'args, ') !!}options),
7775
method: @js($verb->actual),
7876
})
7977
@endforeach
8078

8179
@if ($withForm)
8280
@include('wayfinder::docblock')
83-
const {!! $method !!}Form = (@include('wayfinder::function-arguments')): {
84-
action: string,
85-
method: @js($verbs->first()->formSafe),
86-
} => ({
81+
const {!! $method !!}Form = (@include('wayfinder::function-arguments')): RouteFormDefinition<@js($verbs->first()->formSafe)> => ({
8782
action: {!! $method !!}.url(
8883
{!! when($parameters->isNotEmpty(), 'args, ') !!}
8984
@if ($verbs->first()->formSafe === $verbs->first()->actual)
@@ -102,10 +97,7 @@
10297

10398
@foreach ($verbs as $verb)
10499
@include('wayfinder::docblock')
105-
{!! $method !!}Form.{!! $verb->actual !!} = (@include('wayfinder::function-arguments')): {
106-
action: string,
107-
method: @js($verb->formSafe),
108-
} => ({
100+
{!! $method !!}Form.{!! $verb->actual !!} = (@include('wayfinder::function-arguments')): RouteFormDefinition<@js($verb->formSafe)> => ({
109101
action: {!! $method !!}.url(
110102
{!! when($parameters->isNotEmpty(), 'args, ') !!}
111103
@if ($verb->formSafe === $verb->actual)

src/GenerateCommand.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,11 @@ private function writeNamedFile(Collection $routes, string $namespace): void
222222

223223
private function appendCommonImports(Collection $routes, string $path, string $namespace): void
224224
{
225-
$imports = ['queryParams', 'type QueryParams'];
225+
$imports = ['queryParams', 'type RouteQueryOptions', 'type RouteDefinition'];
226+
227+
if ($this->option('with-form') === true) {
228+
$imports[] = 'type RouteFormDefinition';
229+
}
226230

227231
if ($routes->contains(fn (Route $route) => $route->parameters()->contains(fn (Parameter $parameter) => $parameter->optional))) {
228232
$imports[] = 'validateParameters';

0 commit comments

Comments
 (0)