Skip to content

Commit 401d643

Browse files
Merge pull request #68 from istiak-tridip/feat/export-types
feat: introduce route utility types for improved DX
2 parents 7d4d354 + 63d48f7 commit 401d643

File tree

5 files changed

+28
-23
lines changed

5 files changed

+28
-23
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
},
2929
"require-dev": {
3030
"laravel/pint": "^1.21",
31-
"orchestra/testbench": "^10.1"
31+
"orchestra/testbench": "^10.1 | ^9.0"
3232
},
3333
"autoload": {
3434
"psr-4": {

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: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,23 @@ export type QueryParams = Record<
99
| Record<string, string | number | boolean>
1010
>;
1111

12-
export const queryParams = (options?: {
12+
type Method = "get" | "post" | "put" | "delete" | "patch" | "head";
13+
14+
export type RouteDefinition<TMethod extends Method | Method[]> = {
15+
url: string;
16+
} & (TMethod extends Method[] ? { methods: TMethod } : { method: TMethod });
17+
18+
export type RouteFormDefinition<TMethod extends Method> = {
19+
action: string;
20+
method: TMethod;
21+
};
22+
23+
export type RouteQueryOptions = {
1324
query?: QueryParams;
1425
mergeQuery?: QueryParams;
15-
}) => {
26+
}
27+
28+
export const queryParams = (options?: RouteQueryOptions) => {
1629
if (!options || (!options.query && !options.mergeQuery)) {
1730
return "";
1831
}

resources/method.blade.ts

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
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

107
{!! $method !!}.definition = {
11-
methods: [@foreach ($verbs as $verb)@js($verb->actual){!! when(! $loop->last, ',') !!}@endforeach],
8+
methods: {!! $verbs->pluck('actual')->toJson() !!},
129
url: {!! $uri !!},
13-
}
10+
} satisfies RouteDefinition<{!! $verbs->pluck('actual')->toJson() !!}>
1411

1512
@include('wayfinder::docblock')
1613
{!! $method !!}.url = (@include('wayfinder::function-arguments')) => {
@@ -69,21 +66,15 @@
6966

7067
@foreach ($verbs as $verb)
7168
@include('wayfinder::docblock')
72-
{!! $method !!}.{!! $verb->actual !!} = (@include('wayfinder::function-arguments')): {
73-
url: string,
74-
method: @js($verb->actual),
75-
} => ({
69+
{!! $method !!}.{!! $verb->actual !!} = (@include('wayfinder::function-arguments')): RouteDefinition<@js($verb->actual)> => ({
7670
url: {!! $method !!}.url({!! when($parameters->isNotEmpty(), 'args, ') !!}options),
7771
method: @js($verb->actual),
7872
})
7973
@endforeach
8074

8175
@if ($withForm)
8276
@include('wayfinder::docblock')
83-
const {!! $method !!}Form = (@include('wayfinder::function-arguments')): {
84-
action: string,
85-
method: @js($verbs->first()->formSafe),
86-
} => ({
77+
const {!! $method !!}Form = (@include('wayfinder::function-arguments')): RouteFormDefinition<@js($verbs->first()->formSafe)> => ({
8778
action: {!! $method !!}.url(
8879
{!! when($parameters->isNotEmpty(), 'args, ') !!}
8980
@if ($verbs->first()->formSafe === $verbs->first()->actual)
@@ -102,10 +93,7 @@
10293

10394
@foreach ($verbs as $verb)
10495
@include('wayfinder::docblock')
105-
{!! $method !!}Form.{!! $verb->actual !!} = (@include('wayfinder::function-arguments')): {
106-
action: string,
107-
method: @js($verb->formSafe),
108-
} => ({
96+
{!! $method !!}Form.{!! $verb->actual !!} = (@include('wayfinder::function-arguments')): RouteFormDefinition<@js($verb->formSafe)> => ({
10997
action: {!! $method !!}.url(
11098
{!! when($parameters->isNotEmpty(), 'args, ') !!}
11199
@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)