AdonisJS {{ version }} x Inertia x Vue.js
-
Go to Home
+
Learn more about AdonisJS and Inertia.js by visiting the
AdonisJS documentation.
From c1ce5970b64a781665eae95531121e4e316715c4 Mon Sep 17 00:00:00 2001
From: Julien
Date: Thu, 20 Feb 2025 23:28:21 +0100
Subject: [PATCH 2/3] chore: add changeset
---
.changeset/ninety-jeans-clean.md | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
create mode 100644 .changeset/ninety-jeans-clean.md
diff --git a/.changeset/ninety-jeans-clean.md b/.changeset/ninety-jeans-clean.md
new file mode 100644
index 0000000..53b59ba
--- /dev/null
+++ b/.changeset/ninety-jeans-clean.md
@@ -0,0 +1,18 @@
+---
+'@tuyau/inertia': patch
+---
+
+Add `useRouter` hook/composable that expose a typesafe `visit` method to manually visit a route.
+
+```tsx
+import { useRouter } from '@tuyau/inertia/vue'
+
+const router = useRouter()
+
+router.visit({
+ name: 'users.posts.show',
+ params: { id: 1, postId: 2 }
+}, { preserveState: true })
+```
+
+Also available for React.
From e2083db185b7f73e77a4fa7f9fc60f755ac94947 Mon Sep 17 00:00:00 2001
From: Julien
Date: Thu, 20 Feb 2025 23:28:44 +0100
Subject: [PATCH 3/3] test: add typing tests for useRouter
---
packages/inertia/tests/react.spec.ts | 15 ++++++++++++++-
packages/inertia/tests/vue.spec.ts | 16 +++++++++++++++-
2 files changed, 29 insertions(+), 2 deletions(-)
diff --git a/packages/inertia/tests/react.spec.ts b/packages/inertia/tests/react.spec.ts
index c1af97d..5897d32 100644
--- a/packages/inertia/tests/react.spec.ts
+++ b/packages/inertia/tests/react.spec.ts
@@ -2,7 +2,7 @@ import { test } from '@japa/runner'
import { createTuyau } from '@tuyau/client'
import type { ApiDefinition } from '@tuyau/client'
-import { Link, TuyauProvider } from '../src/react/index.js'
+import { Link, TuyauProvider, useRouter } from '../src/react/index.js'
const routes = [
{
@@ -68,4 +68,17 @@ test.group('React | Typings', () => {
const client = createTuyau({ api, baseUrl: 'http://localhost' })
TuyauProvider({ client, children: null })
})
+
+ test('useRouter typing', () => {
+ const router = useRouter()
+
+ router.visit({ route: 'users.index' })
+ router.visit({ route: 'users.comments.edit', params: ['1', '2'] })
+
+ // @ts-expect-error inexistent route
+ router.visit({ route: 'foo' })
+
+ // @ts-expect-error missing params
+ router.visit({ route: 'users.comments.edit' })
+ }).fails()
})
diff --git a/packages/inertia/tests/vue.spec.ts b/packages/inertia/tests/vue.spec.ts
index a69668f..ec01f7e 100644
--- a/packages/inertia/tests/vue.spec.ts
+++ b/packages/inertia/tests/vue.spec.ts
@@ -2,7 +2,7 @@ import { test } from '@japa/runner'
import { createTuyau } from '@tuyau/client'
import type { ApiDefinition } from '@tuyau/client'
-import { Link, TuyauPlugin } from '../src/vue/index.js'
+import { useRouter, Link, TuyauPlugin } from '../src/vue/index.js'
const routes = [
{
@@ -70,4 +70,18 @@ test.group('Vue | Typings', () => {
client: createTuyau({ baseUrl: 'http://localhost', api }),
})
}).fails()
+
+ test('useRouter typing', () => {
+ const router = useRouter()
+
+ router.visit({ route: 'users.index' })
+ router.visit({ route: 'users.comments.edit', params: ['1', '2'] })
+ router.visit({ route: 'users.comments.edit', params: ['1', '2'] }, { preserveScroll: true })
+
+ // @ts-expect-error inexistent route
+ router.visit({ route: 'foo' })
+
+ // @ts-expect-error missing params
+ router.visit({ route: 'users.comments.edit' })
+ }).fails()
})