Skip to content

Commit

Permalink
docs: document @tuyau/inertia usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed May 12, 2024
1 parent 2da60a7 commit 280c502
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Set of tools to create typesafe APIs using AdonisJS. The monorepo includes the f
- `@tuyau/core` : Core package that you must install in your AdonisJS project.
- `@tuyau/client` : E2E typesafe client to consume your AdonisJS APIs.
- `@tuyau/utils` : Set of utilities and helpers for the other packages.
- `@tuyau/inertia` : Set of components and helpers for AdonisJS + Inertia projects.

## Goals of the project

Expand Down Expand Up @@ -454,6 +455,90 @@ Only one of `only` or `except` can be used at the same time. Both accept either

`definitions` will filter the generated types in the `ApiDefinition` interface. `routes` will the filter the route names that are generated in the `routes` object.

## Inertia package

Tuyau also provides a set of helpers for Inertia projects. The package is called `@tuyau/inertia`. First, make sure to have generated the API definition in your AdonisJS project using `@tuyau/core` and also have configured the client in your frontend project using `@tuyau/client`.

Then, you can install the package in your frontend project :

```bash
pnpm add @tuyau/inertia
```
### React usage

To use the Inertia helpers in your React x Inertia project, you must wrap your app with the `TuyauProvider` component :

```tsx
// inertia/app/app.tsx
import { TuyauProvider } from '@tuyau/inertia/react'
import { tuyau } from './tuyau'

createInertiaApp({
// ...

setup({ el, App, props }) {
hydrateRoot(
el,
<>
<TuyauProvider client={tuyau}>
<App {...props} />
</TuyauProvider>
</>
)
},
})
```

As you can see, you must pass an instance of the Tuyau client to the `TuyauProvider` component. Also, if you are using SSR, make sure to also wrap your app with the `TuyauProvider` component in your `inertia/app/ssr.tsx` file.

#### Link

The `Link` component is a wrapper around the Inertia `Link` component with some additional typesafety. Tuyau `Link` component will accept the same props as the Inertia `Link` component except for the `href` and `method` props. They are replaced by the `route` and `params` props.

```tsx
import { Link } from '@tuyau/inertia/react'

<Link route="users.posts.show" params={{ id: 1, postId: 2 }}>Go to post</Link>
```

### Vue usage

To use the Inertia helpers in your Vue x Inertia project, you must install the Tuyau plugin :

```ts
// inertia/app/app.ts

import { TuyauPlugin } from '@tuyau/inertia/vue'
import { tuyau } from './tuyau'

createInertiaApp({
// ...

setup({ el, App, props, plugin }) {
createSSRApp({ render: () => h(App, props) })
.use(plugin)
.use(TuyauPlugin, { client: tuyau })
.mount(el)
},
})
```

As you can see, you must pass an instance of the Tuyau client to the `TuyauPlugin` plugin. Also, if you are using SSR, make sure to also install the `TuyauPlugin` plugin in your `inertia/app/ssr.ts` file.

#### Link

The `Link` component is a wrapper around the Inertia `Link` component with some additional typesafety. Tuyau `Link` component will accept the same props as the Inertia `Link` component except for the `href` and `method` props. They are replaced by the `route` and `params` props.

```vue
<script setup lang="ts">
import { Link } from '@tuyau/inertia/vue'
</script>
<template>
<Link route="users.posts.show" :params="{ id: 1, postId: 2 }">Go to post</Link>
</template>
```

## Sponsors

![](https://github.com/julien-r44/static/blob/main/sponsorkit/sponsors.png?raw=true)
Expand Down

0 comments on commit 280c502

Please sign in to comment.