Skip to content

Commit 6da9066

Browse files
committed
adds guide about shared data and types
1 parent f235c3a commit 6da9066

File tree

4 files changed

+133
-10
lines changed

4 files changed

+133
-10
lines changed

docs/.vitepress/config.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ export default defineConfig({
3030
{ text: 'Development Workflow', link: '/get-started/development-workflow' }
3131
]
3232
},
33-
// {
34-
// text: 'Guide',
35-
// items: [
36-
// { text: 'Configuration', link: '/guide/configuration' },
37-
// { text: 'Database Setup', link: '/guide/database' },
38-
// { text: 'Authentication', link: '/guide/authentication' }
39-
// ]
40-
// },
33+
{
34+
text: 'Guide',
35+
items: [
36+
{ text: 'Share Data', link: '/guide/share-data' },
37+
// { text: 'Database Setup', link: '/guide/database' },
38+
// { text: 'Authentication', link: '/guide/authentication' }
39+
]
40+
},
4141
{
4242
text: 'Deployment',
4343
items: [

docs/get-started/development-workflow.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,44 @@ This guide outlines the recommended development workflow to help you build featu
44

55
## Daily Development Flow
66

7+
::: code-group
8+
9+
```bash [pnpm]
10+
# Start everything
11+
pnpm dev
12+
13+
# Run tests (yes, we have tests!)
14+
pnpm test
15+
16+
# Lint your code (keep it clean)
17+
pnpm lint
18+
```
19+
20+
```bash [yarn]
21+
# Start everything
22+
yarn dev
23+
24+
# Run tests (yes, we have tests!)
25+
yarn test
26+
27+
# Lint your code (keep it clean)
28+
yarn lint
29+
```
30+
31+
```bash [npm]
32+
# Start everything
33+
npm start dev
34+
35+
# Run tests (yes, we have tests!)
36+
npm start test
37+
38+
# Lint your code (keep it clean)
39+
npm start lint
40+
```
41+
42+
:::
43+
44+
<!--
745
```bash
846
# Start everything
947
pnpm dev
@@ -13,4 +51,4 @@ pnpm test
1351
1452
# Lint your code (keep it clean)
1553
pnpm lint
16-
```
54+
``` -->

docs/guide/share-data.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# TypeScript Integration for Inertia.js
2+
3+
This directory contains TypeScript definitions for proper type safety between your AdonisJS backend and React frontend through Inertia.js.
4+
5+
## How It Works
6+
7+
The type synchronization works through several layers:
8+
9+
1. **Backend Types** (`shared/types.ts`) - Shared interfaces between backend and frontend
10+
2. **Inertia Config** (`config/inertia.ts`) - Module augmentation for Inertia shared props
11+
3. **Frontend Types** (`resources/js/types/inertia.ts`) - Re-exported types for frontend use
12+
4. **Middleware** (`app/middleware/inertia_middleware.ts`) - Runtime data sharing with type annotations
13+
14+
## Using Typed Props in Components
15+
16+
### Basic Page Component
17+
18+
```typescript
19+
import type { PageProps } from '@/types/inertia'
20+
21+
export default function MyPage({ auth, user, flash }: PageProps) {
22+
return (
23+
<div>
24+
{auth.isAuthenticated && (
25+
<h1>Welcome, {user?.name}!</h1>
26+
)}
27+
</div>
28+
)
29+
}
30+
```
31+
32+
### Page Component with Additional Props
33+
34+
```typescript
35+
import type { PageProps } from '@/types/inertia'
36+
37+
interface MyPageProps extends PageProps {
38+
posts: Array<{
39+
id: number
40+
title: string
41+
content: string
42+
}>
43+
meta: {
44+
total: number
45+
perPage: number
46+
}
47+
}
48+
49+
export default function MyPage({ auth, user, flash, posts, meta }: MyPageProps) {
50+
// Full type safety for both shared and page-specific props
51+
}
52+
```
53+
54+
## Available Shared Props
55+
56+
All page components automatically receive these typed props:
57+
58+
- `auth.isAuthenticated: boolean` - Authentication status
59+
- `user: User | null` - Current user object (null if not authenticated)
60+
- `flash.success?: string` - Success flash message
61+
- `flash.errors?: Record<string, string[]>` - Validation errors
62+
- `sidebarOpen: boolean` - Sidebar is open or not
63+
64+
## User Type Structure
65+
66+
```typescript
67+
interface User {
68+
id: number
69+
name: string | null
70+
email: string
71+
}
72+
```
73+
74+
## Best Practices
75+
76+
1. **Always extend `PageProps`** for page components that need shared data
77+
2. **Use destructuring** in component parameters for clean prop access
78+
3. **Check authentication** before accessing user data
79+
4. **Handle flash messages** appropriately in your UI
80+
5. **Keep shared types in sync** between backend and frontend
81+
82+
## Example Files
83+
84+
- `pages/dashboard.tsx` - Real-world example using shared props
85+
- `pages/auth/login.tsx` - Authentication page with proper typing

docs/whats-included.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
- Responsive UI components
1717
- Development tooling (ESLint, Prettier)
1818
- Testing setup
19-
- Internationalization with I18n
19+
- Internationalization with i18n
2020

2121
## Development Tools
2222
- Hot module replacement

0 commit comments

Comments
 (0)