@inertia-node/core 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +218 -0
- package/package.json +3 -2
package/README.md
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
# @inertia-node/core
|
|
2
|
+
|
|
3
|
+
Framework-agnostic Inertia.js protocol helpers for Node.js.
|
|
4
|
+
|
|
5
|
+
`@inertia-node/core` is the shared runtime used by the Inertia Node adapter packages. It does not depend on Express, NestJS, Fastify, React, Vue, or Svelte. Instead, it provides the protocol primitives that framework adapters use to turn server responses into valid Inertia pages.
|
|
6
|
+
|
|
7
|
+
Use this package directly when you are building a custom adapter, testing protocol behavior, or integrating Inertia into a framework that is not covered by the higher-level packages.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- Detects Inertia requests and handles protocol headers.
|
|
12
|
+
- Serializes page objects into safe HTML root views.
|
|
13
|
+
- Builds protocol responses for page renders, redirects, external locations, and asset-version reloads.
|
|
14
|
+
- Resolves shared props, lazy props, deferred props, merge metadata, and partial reloads.
|
|
15
|
+
- Provides typed auth, session, flash, validation error, and SSR contracts.
|
|
16
|
+
- Works with official Inertia client packages such as `@inertiajs/react`, `@inertiajs/vue3`, and `@inertiajs/svelte`.
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
pnpm add @inertia-node/core
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```sh
|
|
25
|
+
npm install @inertia-node/core
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Quick Start
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import {
|
|
32
|
+
always,
|
|
33
|
+
createInertia,
|
|
34
|
+
defer,
|
|
35
|
+
merge,
|
|
36
|
+
type InertiaRequest,
|
|
37
|
+
} from "@inertia-node/core";
|
|
38
|
+
|
|
39
|
+
const request: InertiaRequest = {
|
|
40
|
+
headers: {
|
|
41
|
+
"x-inertia": "true",
|
|
42
|
+
"x-inertia-version": "1",
|
|
43
|
+
},
|
|
44
|
+
method: "GET",
|
|
45
|
+
url: "/dashboard",
|
|
46
|
+
originalUrl: "/dashboard",
|
|
47
|
+
protocol: "https",
|
|
48
|
+
host: "example.com",
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const inertia = createInertia({
|
|
52
|
+
version: "1",
|
|
53
|
+
share: async ({ request }) => ({
|
|
54
|
+
auth: {
|
|
55
|
+
user: request.raw?.user ?? null,
|
|
56
|
+
},
|
|
57
|
+
}),
|
|
58
|
+
ssr: {
|
|
59
|
+
enabled: true,
|
|
60
|
+
url: "http://127.0.0.1:13714/render",
|
|
61
|
+
timeoutMs: 1500,
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
const response = await inertia.render(request, "Dashboard/Index", {
|
|
66
|
+
profile: merge(() => loadProfile()),
|
|
67
|
+
metrics: always(() => loadMetrics()),
|
|
68
|
+
report: defer(() => loadReport()),
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
sendStatus(response.status);
|
|
72
|
+
sendHeaders(response.headers);
|
|
73
|
+
sendBody(response.body);
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Framework adapters are responsible for mapping their request/response objects to this protocol shape. For most applications, use one of the adapter packages instead:
|
|
77
|
+
|
|
78
|
+
- `@inertia-node/express`
|
|
79
|
+
- `@inertia-node/nest`
|
|
80
|
+
- `@inertia-node/nest-fastify`
|
|
81
|
+
|
|
82
|
+
## Core Concepts
|
|
83
|
+
|
|
84
|
+
### `createInertia(options)`
|
|
85
|
+
|
|
86
|
+
Creates a small protocol runtime.
|
|
87
|
+
|
|
88
|
+
The returned instance exposes:
|
|
89
|
+
|
|
90
|
+
- `render(request, component, props, options)`
|
|
91
|
+
- `redirect(request, location, status?)`
|
|
92
|
+
- `location(location)`
|
|
93
|
+
|
|
94
|
+
`render` returns a `ProtocolResponse` instead of writing to a framework response directly. This keeps the core runtime portable across frameworks.
|
|
95
|
+
|
|
96
|
+
### Request Mapping
|
|
97
|
+
|
|
98
|
+
Adapters map incoming framework requests to `InertiaRequest`:
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
const inertiaRequest = {
|
|
102
|
+
headers: request.headers,
|
|
103
|
+
method: request.method,
|
|
104
|
+
url: request.url,
|
|
105
|
+
originalUrl: request.originalUrl,
|
|
106
|
+
protocol: request.protocol,
|
|
107
|
+
host: request.get("host"),
|
|
108
|
+
raw: request,
|
|
109
|
+
};
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
The `raw` value is intentionally untyped. Adapters can use it to expose framework-specific objects to shared props, auth handlers, and custom SSR logic.
|
|
113
|
+
|
|
114
|
+
### Shared Props
|
|
115
|
+
|
|
116
|
+
Shared props are merged into every rendered page:
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
createInertia({
|
|
120
|
+
share: async ({ request }) => ({
|
|
121
|
+
flash: request.raw?.flash ?? {},
|
|
122
|
+
auth: {
|
|
123
|
+
user: request.raw?.user ?? null,
|
|
124
|
+
},
|
|
125
|
+
}),
|
|
126
|
+
});
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
Page props override shared props when the same key exists.
|
|
130
|
+
|
|
131
|
+
### Lazy and Deferred Props
|
|
132
|
+
|
|
133
|
+
The package includes helpers that model Inertia prop behavior:
|
|
134
|
+
|
|
135
|
+
```ts
|
|
136
|
+
import { always, defer, merge, optional } from "@inertia-node/core";
|
|
137
|
+
|
|
138
|
+
const props = {
|
|
139
|
+
user: always(() => loadCurrentUser()),
|
|
140
|
+
notifications: optional(() => loadNotifications()),
|
|
141
|
+
auditLog: defer(() => loadAuditLog()),
|
|
142
|
+
feed: merge(() => loadFeedPage()),
|
|
143
|
+
};
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Use these helpers when you need partial reloads, deferred data, or merge metadata to be represented in the serialized page object.
|
|
147
|
+
|
|
148
|
+
## SSR
|
|
149
|
+
|
|
150
|
+
The core runtime can call an SSR endpoint before rendering the root HTML document:
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
createInertia({
|
|
154
|
+
ssr: {
|
|
155
|
+
enabled: true,
|
|
156
|
+
url: "http://127.0.0.1:13714/render",
|
|
157
|
+
timeoutMs: 1500,
|
|
158
|
+
throwOnError: false,
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
When SSR succeeds, the result is passed to the configured root view. When SSR fails and `throwOnError` is false, rendering falls back to the normal client-side shell.
|
|
164
|
+
|
|
165
|
+
## Exports
|
|
166
|
+
|
|
167
|
+
Runtime helpers:
|
|
168
|
+
|
|
169
|
+
- `createInertia`
|
|
170
|
+
- `getHeader`
|
|
171
|
+
- `isInertiaRequest`
|
|
172
|
+
- `parseCommaHeader`
|
|
173
|
+
- `requestHeader`
|
|
174
|
+
- `varyHeader`
|
|
175
|
+
- `defaultRootView`
|
|
176
|
+
- `escapeAttribute`
|
|
177
|
+
- `serializePage`
|
|
178
|
+
- `inertiaAuth`
|
|
179
|
+
- `always`
|
|
180
|
+
- `defer`
|
|
181
|
+
- `merge`
|
|
182
|
+
- `optional`
|
|
183
|
+
- `resolvePageProps`
|
|
184
|
+
- `resolveProps`
|
|
185
|
+
- `selectPartialProps`
|
|
186
|
+
- `validationError`
|
|
187
|
+
|
|
188
|
+
Common types:
|
|
189
|
+
|
|
190
|
+
- `CreateInertiaOptions`
|
|
191
|
+
- `InertiaInstance`
|
|
192
|
+
- `InertiaRequest`
|
|
193
|
+
- `InertiaPage`
|
|
194
|
+
- `InputProps`
|
|
195
|
+
- `RenderOptions`
|
|
196
|
+
- `ProtocolResponse`
|
|
197
|
+
- `SessionStore`
|
|
198
|
+
- `AuthConfig`
|
|
199
|
+
- `AuthRuntime`
|
|
200
|
+
- `SsrOptions`
|
|
201
|
+
- `SsrResult`
|
|
202
|
+
- `ValidationErrorInput`
|
|
203
|
+
- `ValidationErrorPayload`
|
|
204
|
+
|
|
205
|
+
## Compatibility
|
|
206
|
+
|
|
207
|
+
- Node.js `>=20`
|
|
208
|
+
- ESM projects
|
|
209
|
+
- TypeScript projects
|
|
210
|
+
- Inertia.js v3-compatible clients
|
|
211
|
+
|
|
212
|
+
## Documentation
|
|
213
|
+
|
|
214
|
+
- Repository: https://github.com/inertia-node/inertia-node-adapter
|
|
215
|
+
- Quick start: https://github.com/inertia-node/inertia-node-adapter/blob/main/docs/quickstart.md
|
|
216
|
+
- Auth: https://github.com/inertia-node/inertia-node-adapter/blob/main/docs/auth.md
|
|
217
|
+
- Sessions, flash, and validation: https://github.com/inertia-node/inertia-node-adapter/blob/main/docs/session-flash-validation.md
|
|
218
|
+
- SSR: https://github.com/inertia-node/inertia-node-adapter/blob/main/docs/ssr.md
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inertia-node/core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Framework-agnostic Inertia.js v3 protocol helpers for Node.js.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Inertia Node Adapter contributors",
|
|
@@ -31,7 +31,8 @@
|
|
|
31
31
|
}
|
|
32
32
|
},
|
|
33
33
|
"files": [
|
|
34
|
-
"dist"
|
|
34
|
+
"dist",
|
|
35
|
+
"README.md"
|
|
35
36
|
],
|
|
36
37
|
"engines": {
|
|
37
38
|
"node": ">=20"
|