@adonisjs/inertia 5.0.0-next.0 → 5.0.0-next.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/build/commands/make_page.js +15 -0
- package/build/debug-Ca0ekg3M.js +25 -0
- package/build/factories/inertia_factory.d.ts +25 -0
- package/build/factories/main.js +144 -5
- package/build/headers-B-5pLwyD.js +77 -0
- package/build/index.js +3 -4
- package/build/inertia-ZqLiRJME.js +141 -0
- package/build/inertia_manager-BeqpMsuN.js +1770 -0
- package/build/providers/inertia_provider.js +69 -3
- package/build/src/client/helpers.js +28 -0
- package/build/src/client/react/index.js +144 -1
- package/build/src/client/vue/index.js +95 -1
- package/build/src/headers.d.ts +13 -0
- package/build/src/inertia.d.ts +58 -2
- package/build/src/inertia_middleware.d.ts +37 -1
- package/build/src/inertia_middleware.js +72 -5
- package/build/src/plugins/edge/plugin.js +122 -1
- package/build/src/plugins/japa/api_client.js +50 -1
- package/build/src/props.d.ts +118 -28
- package/build/src/symbols.d.ts +33 -0
- package/build/src/types.d.ts +398 -8
- package/build/tests/helpers.d.ts +26 -0
- package/build/tests/merges.spec.d.ts +1 -0
- package/build/tests/once_props.spec.d.ts +1 -0
- package/build/tests/rescued_deferred_props.spec.d.ts +1 -0
- package/build/tests/scroll.spec.d.ts +1 -0
- package/package.json +28 -26
- package/build/debug-CBMTuPUm.js +0 -3
- package/build/define_config-Dv9Dx2Yq.js +0 -56
- package/build/headers-DafWEpBh.js +0 -11
- package/build/inertia_manager-BdcjpKFf.js +0 -402
|
@@ -1,18 +1,28 @@
|
|
|
1
1
|
import { readdir } from "node:fs/promises";
|
|
2
2
|
import { BaseCommand, args, flags } from "@adonisjs/core/ace";
|
|
3
3
|
import { join } from "node:path";
|
|
4
|
+
//#region \0@oxc-project+runtime@0.139.0/helpers/esm/decorate.js
|
|
4
5
|
function __decorate(decorators, target, key, desc) {
|
|
5
6
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
6
7
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
7
8
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
8
9
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
9
10
|
}
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region commands/make_page.ts
|
|
10
13
|
const stubsRoot = join(import.meta.dirname, "../stubs");
|
|
11
14
|
var MakePage = class extends BaseCommand {
|
|
12
15
|
static commandName = "make:page";
|
|
13
16
|
static description = "Create a new Inertia page component";
|
|
14
17
|
static options = { allowUnknownFlags: true };
|
|
18
|
+
/**
|
|
19
|
+
* Directory under which the inertia pages are stored
|
|
20
|
+
*/
|
|
15
21
|
pagesDir = "inertia/pages";
|
|
22
|
+
/**
|
|
23
|
+
* Detect the framework by scanning existing files in the
|
|
24
|
+
* inertia/pages directory.
|
|
25
|
+
*/
|
|
16
26
|
async #detectFramework() {
|
|
17
27
|
try {
|
|
18
28
|
const files = await readdir(this.app.makePath(this.pagesDir), { recursive: true });
|
|
@@ -25,6 +35,10 @@ var MakePage = class extends BaseCommand {
|
|
|
25
35
|
return null;
|
|
26
36
|
}
|
|
27
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* Resolve which framework to use. Flags take priority,
|
|
40
|
+
* then auto-detection, and finally prompt the user.
|
|
41
|
+
*/
|
|
28
42
|
async #resolveFramework() {
|
|
29
43
|
if (this.vue) return "vue";
|
|
30
44
|
if (this.react) return "react";
|
|
@@ -51,4 +65,5 @@ __decorate([flags.boolean({
|
|
|
51
65
|
description: "Forcefully overwrite existing files",
|
|
52
66
|
alias: "f"
|
|
53
67
|
})], MakePage.prototype, "force", void 0);
|
|
68
|
+
//#endregion
|
|
54
69
|
export { MakePage as default };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { debuglog } from "node:util";
|
|
2
|
+
//#region src/debug.ts
|
|
3
|
+
/**
|
|
4
|
+
* Debug logger for Inertia.js package
|
|
5
|
+
*
|
|
6
|
+
* Provides debugging functionality using Node.js built-in debuglog.
|
|
7
|
+
* Enable debugging by setting the NODE_DEBUG environment variable to 'adonisjs:inertia'.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```js
|
|
11
|
+
* import debug from './debug.js'
|
|
12
|
+
*
|
|
13
|
+
* debug('Processing Inertia request')
|
|
14
|
+
* debug('Component: %s, Props: %o', componentName, props)
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```bash
|
|
19
|
+
* # Enable debugging
|
|
20
|
+
* NODE_DEBUG=adonisjs:inertia node server.js
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
var debug_default = debuglog("adonisjs:inertia");
|
|
24
|
+
//#endregion
|
|
25
|
+
export { debug_default as t };
|
|
@@ -97,6 +97,31 @@ export declare class InertiaFactory<Pages extends Record<string, ComponentProps>
|
|
|
97
97
|
*/
|
|
98
98
|
create(): Inertia<Pages>;
|
|
99
99
|
};
|
|
100
|
+
/**
|
|
101
|
+
* Simulates a client that already holds fresh, cached values for the given
|
|
102
|
+
* once-keys by setting the `X-Inertia-Except-Once-Props` request header.
|
|
103
|
+
*
|
|
104
|
+
* @param keys - The once-keys the client reports as cached
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```typescript
|
|
108
|
+
* const inertia = factory.withExceptOnceProps(['lookups']).create()
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
withExceptOnceProps(keys: string[]): this;
|
|
112
|
+
/**
|
|
113
|
+
* Simulates the infinite-scroll merge intent the client sends on follow-up
|
|
114
|
+
* scroll requests by setting the `X-Inertia-Infinite-Scroll-Merge-Intent`
|
|
115
|
+
* header. Absence is treated as `append`.
|
|
116
|
+
*
|
|
117
|
+
* @param intent - The merge direction the client requests
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```typescript
|
|
121
|
+
* const inertia = factory.withMergeIntent('prepend').create()
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
withMergeIntent(intent: 'append' | 'prepend'): this;
|
|
100
125
|
/**
|
|
101
126
|
* Sets the assets version for cache busting
|
|
102
127
|
*
|
package/build/factories/main.js
CHANGED
|
@@ -1,50 +1,167 @@
|
|
|
1
|
-
import { n as ServerRenderer, r as Inertia } from "../inertia_manager-
|
|
2
|
-
import { t as InertiaHeaders } from "../headers-
|
|
3
|
-
import "../
|
|
4
|
-
import { t as defineConfig } from "../define_config-Dv9Dx2Yq.js";
|
|
5
|
-
import "../index.js";
|
|
1
|
+
import { n as ServerRenderer, r as Inertia } from "../inertia_manager-BeqpMsuN.js";
|
|
2
|
+
import { t as InertiaHeaders } from "../headers-B-5pLwyD.js";
|
|
3
|
+
import { t as defineConfig } from "../inertia-ZqLiRJME.js";
|
|
6
4
|
import { HttpContextFactory } from "@adonisjs/core/factories/http";
|
|
5
|
+
//#region factories/inertia_factory.ts
|
|
6
|
+
/**
|
|
7
|
+
* Inertia factory to quickly create a new instance of Inertia
|
|
8
|
+
* for testing purposes
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const factory = new InertiaFactory()
|
|
13
|
+
* const inertia = factory
|
|
14
|
+
* .merge({ config: { ssr: { enabled: true } } })
|
|
15
|
+
* .withVersion('1.0.0')
|
|
16
|
+
* .create()
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
7
19
|
var InertiaFactory = class {
|
|
20
|
+
/** Optional Vite instance for asset handling */
|
|
8
21
|
#vite;
|
|
22
|
+
/** Internal parameters for factory configuration */
|
|
9
23
|
#parameters = {
|
|
10
24
|
ctx: new HttpContextFactory().create(),
|
|
11
25
|
config: defineConfig({})
|
|
12
26
|
};
|
|
27
|
+
/**
|
|
28
|
+
* Creates a new InertiaFactory instance with default Inertia headers
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* const factory = new InertiaFactory()
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
13
35
|
constructor() {
|
|
14
36
|
this.#parameters.ctx.request.request.headers[InertiaHeaders.Inertia] = "true";
|
|
15
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* Merges additional parameters into the factory configuration
|
|
40
|
+
*
|
|
41
|
+
* @param parameters - Partial factory parameters to merge
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* factory.merge({
|
|
46
|
+
* config: { ssr: { enabled: true } },
|
|
47
|
+
* ctx: customContext
|
|
48
|
+
* })
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
16
51
|
merge(parameters) {
|
|
17
52
|
if (parameters.ctx) this.#parameters.ctx = parameters.ctx;
|
|
18
53
|
if (parameters.config) this.#parameters.config = defineConfig(parameters.config);
|
|
19
54
|
this.#parameters.ctx.request.request.headers[InertiaHeaders.Inertia] = "true";
|
|
20
55
|
return this;
|
|
21
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Removes the X-Inertia header from the request headers
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* const inertia = factory.withoutInertia().create()
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
22
65
|
withoutInertia() {
|
|
23
66
|
delete this.#parameters.ctx.request.request.headers[InertiaHeaders.Inertia];
|
|
24
67
|
return this;
|
|
25
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Configures the factory for partial reloads of a specific component
|
|
71
|
+
*
|
|
72
|
+
* @param component - Name of the component to partially reload
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* const inertia = factory
|
|
77
|
+
* .partialReload('UserProfile')
|
|
78
|
+
* .only(['name', 'email'])
|
|
79
|
+
* .create()
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
26
82
|
partialReload(component) {
|
|
27
83
|
const self = this;
|
|
28
84
|
const request = this.#parameters.ctx.request;
|
|
29
85
|
request.request.headers[InertiaHeaders.PartialComponent] = component;
|
|
30
86
|
return {
|
|
87
|
+
/**
|
|
88
|
+
* Specifies which props to include in the partial reload
|
|
89
|
+
*
|
|
90
|
+
* @param props - Array of property names to include
|
|
91
|
+
*/
|
|
31
92
|
only(props) {
|
|
32
93
|
request.request.headers[InertiaHeaders.PartialOnly] = props.join(",");
|
|
33
94
|
return this;
|
|
34
95
|
},
|
|
96
|
+
/**
|
|
97
|
+
* Specifies which props to exclude from the partial reload
|
|
98
|
+
*
|
|
99
|
+
* @param props - Array of property names to exclude
|
|
100
|
+
*/
|
|
35
101
|
except(props) {
|
|
36
102
|
request.request.headers[InertiaHeaders.PartialExcept] = props.join(",");
|
|
37
103
|
return this;
|
|
38
104
|
},
|
|
105
|
+
/**
|
|
106
|
+
* Specifies which props should be reset during the partial reload
|
|
107
|
+
*
|
|
108
|
+
* @param props - Array of property names to reset
|
|
109
|
+
*/
|
|
39
110
|
reset(props) {
|
|
40
111
|
request.request.headers[InertiaHeaders.Reset] = props.join(",");
|
|
41
112
|
return this;
|
|
42
113
|
},
|
|
114
|
+
/**
|
|
115
|
+
* Creates the Inertia instance with partial reload configuration
|
|
116
|
+
*/
|
|
43
117
|
create() {
|
|
44
118
|
return self.create();
|
|
45
119
|
}
|
|
46
120
|
};
|
|
47
121
|
}
|
|
122
|
+
/**
|
|
123
|
+
* Simulates a client that already holds fresh, cached values for the given
|
|
124
|
+
* once-keys by setting the `X-Inertia-Except-Once-Props` request header.
|
|
125
|
+
*
|
|
126
|
+
* @param keys - The once-keys the client reports as cached
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```typescript
|
|
130
|
+
* const inertia = factory.withExceptOnceProps(['lookups']).create()
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
withExceptOnceProps(keys) {
|
|
134
|
+
this.#parameters.ctx.request.request.headers[InertiaHeaders.ExceptOnceProps] = keys.join(",");
|
|
135
|
+
return this;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Simulates the infinite-scroll merge intent the client sends on follow-up
|
|
139
|
+
* scroll requests by setting the `X-Inertia-Infinite-Scroll-Merge-Intent`
|
|
140
|
+
* header. Absence is treated as `append`.
|
|
141
|
+
*
|
|
142
|
+
* @param intent - The merge direction the client requests
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```typescript
|
|
146
|
+
* const inertia = factory.withMergeIntent('prepend').create()
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
withMergeIntent(intent) {
|
|
150
|
+
this.#parameters.ctx.request.request.headers[InertiaHeaders.InfiniteScrollMergeIntent] = intent;
|
|
151
|
+
return this;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Sets the assets version for cache busting
|
|
155
|
+
*
|
|
156
|
+
* @param version - Version string or function for asset versioning
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```typescript
|
|
160
|
+
* factory.withVersion('1.0.0')
|
|
161
|
+
* // or
|
|
162
|
+
* factory.withVersion(() => Date.now().toString())
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
48
165
|
withVersion(version) {
|
|
49
166
|
this.#parameters.config = {
|
|
50
167
|
...this.#parameters.config,
|
|
@@ -52,12 +169,34 @@ var InertiaFactory = class {
|
|
|
52
169
|
};
|
|
53
170
|
return this;
|
|
54
171
|
}
|
|
172
|
+
/**
|
|
173
|
+
* Sets the Vite instance for asset handling
|
|
174
|
+
*
|
|
175
|
+
* @param options - Vite configuration object
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```typescript
|
|
179
|
+
* factory.withVite(viteInstance)
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
55
182
|
withVite(options) {
|
|
56
183
|
this.#vite = options;
|
|
57
184
|
return this;
|
|
58
185
|
}
|
|
186
|
+
/**
|
|
187
|
+
* Creates a new Inertia instance with the configured parameters
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```typescript
|
|
191
|
+
* const inertia = factory
|
|
192
|
+
* .merge({ config: customConfig })
|
|
193
|
+
* .withVersion('1.0.0')
|
|
194
|
+
* .create()
|
|
195
|
+
* ```
|
|
196
|
+
*/
|
|
59
197
|
create() {
|
|
60
198
|
return new Inertia(this.#parameters.ctx, this.#parameters.config, this.#vite, this.#vite ? new ServerRenderer(this.#parameters.config, this.#vite) : void 0);
|
|
61
199
|
}
|
|
62
200
|
};
|
|
201
|
+
//#endregion
|
|
63
202
|
export { InertiaFactory };
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
//#region src/headers.ts
|
|
2
|
+
/**
|
|
3
|
+
* List of possible headers used by Inertia.js for client-server communication.
|
|
4
|
+
* These headers control various aspects of Inertia requests and responses,
|
|
5
|
+
* enabling features like partial reloads, version checking, and error handling.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```javascript
|
|
9
|
+
* // Check if request is an Inertia request
|
|
10
|
+
* const isInertiaRequest = request.header(InertiaHeaders.Inertia) === 'true'
|
|
11
|
+
*
|
|
12
|
+
* // Set version header in response
|
|
13
|
+
* response.header(InertiaHeaders.Version, '1.0.0')
|
|
14
|
+
*
|
|
15
|
+
* // Handle partial data requests
|
|
16
|
+
* if (request.header(InertiaHeaders.PartialOnly)) {
|
|
17
|
+
* const onlyProps = request.header(InertiaHeaders.PartialOnly).split(',')
|
|
18
|
+
* // Return only specified props
|
|
19
|
+
* }
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
const InertiaHeaders = {
|
|
23
|
+
/**
|
|
24
|
+
* Header to identify Inertia.js requests.
|
|
25
|
+
* Set to 'true' by Inertia.js client to indicate an Inertia request.
|
|
26
|
+
*/
|
|
27
|
+
Inertia: "x-inertia",
|
|
28
|
+
/**
|
|
29
|
+
* Header to drop a prop from the merge and deep merge object.
|
|
30
|
+
*/
|
|
31
|
+
Reset: "x-inertia-reset",
|
|
32
|
+
/**
|
|
33
|
+
* Header containing the current asset version.
|
|
34
|
+
* Used for cache busting - if versions don't match, Inertia performs a full page reload.
|
|
35
|
+
*/
|
|
36
|
+
Version: "x-inertia-version",
|
|
37
|
+
/**
|
|
38
|
+
* Header containing the target URL for redirects.
|
|
39
|
+
* Used when the server wants to redirect to a different URL than the current request.
|
|
40
|
+
*/
|
|
41
|
+
Location: "x-inertia-location",
|
|
42
|
+
/**
|
|
43
|
+
* Header specifying the error bag name for validation errors.
|
|
44
|
+
* Allows multiple forms on the same page to have separate error handling.
|
|
45
|
+
*/
|
|
46
|
+
ErrorBag: "x-inertia-error-bag",
|
|
47
|
+
/**
|
|
48
|
+
* Header containing comma-separated list of props to include in partial data requests.
|
|
49
|
+
* Only the specified props will be returned in the response.
|
|
50
|
+
*/
|
|
51
|
+
PartialOnly: "x-inertia-partial-data",
|
|
52
|
+
/**
|
|
53
|
+
* Header containing comma-separated list of props to exclude in partial data requests.
|
|
54
|
+
* All props except the specified ones will be returned in the response.
|
|
55
|
+
*/
|
|
56
|
+
PartialExcept: "x-inertia-partial-except",
|
|
57
|
+
/**
|
|
58
|
+
* Header specifying the component name for partial reloads.
|
|
59
|
+
* Used to identify which component is being partially reloaded.
|
|
60
|
+
*/
|
|
61
|
+
PartialComponent: "x-inertia-partial-component",
|
|
62
|
+
/**
|
|
63
|
+
* Header containing comma-separated list of once-keys the client already holds
|
|
64
|
+
* a fresh, cached value for. The server skips re-resolving those once props and
|
|
65
|
+
* omits their values from the response.
|
|
66
|
+
*/
|
|
67
|
+
ExceptOnceProps: "x-inertia-except-once-props",
|
|
68
|
+
/**
|
|
69
|
+
* Header sent by the client on infinite-scroll follow-up requests, declaring
|
|
70
|
+
* whether the incoming page should be appended (loading the next page) or
|
|
71
|
+
* prepended (loading the previous page) to the cached items. Absence is
|
|
72
|
+
* treated as `append`.
|
|
73
|
+
*/
|
|
74
|
+
InfiniteScrollMergeIntent: "x-inertia-infinite-scroll-merge-intent"
|
|
75
|
+
};
|
|
76
|
+
//#endregion
|
|
77
|
+
export { InertiaHeaders as t };
|
package/build/index.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { i as symbols_exports, n as ServerRenderer, r as Inertia, t as InertiaManager } from "./inertia_manager-
|
|
2
|
-
import { t as InertiaHeaders } from "./headers-
|
|
3
|
-
import "./
|
|
4
|
-
import { n as indexPages, t as defineConfig } from "./define_config-Dv9Dx2Yq.js";
|
|
1
|
+
import { i as symbols_exports, n as ServerRenderer, r as Inertia, t as InertiaManager } from "./inertia_manager-BeqpMsuN.js";
|
|
2
|
+
import { t as InertiaHeaders } from "./headers-B-5pLwyD.js";
|
|
3
|
+
import { n as indexPages, t as defineConfig } from "./inertia-ZqLiRJME.js";
|
|
5
4
|
export { Inertia, InertiaHeaders, InertiaManager, ServerRenderer, defineConfig, indexPages, symbols_exports as symbols };
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import "./inertia_manager-BeqpMsuN.js";
|
|
2
|
+
import "./headers-B-5pLwyD.js";
|
|
3
|
+
import lodash from "@poppinss/utils/lodash";
|
|
4
|
+
//#region src/index_pages.ts
|
|
5
|
+
/**
|
|
6
|
+
* File glob patterns for different frontend frameworks.
|
|
7
|
+
* Defines which file extensions should be scanned for each supported framework.
|
|
8
|
+
*/
|
|
9
|
+
const GLOB = {
|
|
10
|
+
vue3: ["**/*.vue"],
|
|
11
|
+
react: ["**/*.ts", "**/*.tsx"]
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* List of supported frontend frameworks for page type generation.
|
|
15
|
+
* Extracted from the GLOB configuration object.
|
|
16
|
+
*/
|
|
17
|
+
const SUPPORTED_FRAMEWORKS = Object.keys(GLOB);
|
|
18
|
+
/**
|
|
19
|
+
* TypeScript helper code templates for extracting component props.
|
|
20
|
+
* Contains framework-specific type extraction logic that gets injected
|
|
21
|
+
* into the generated type definition files.
|
|
22
|
+
*/
|
|
23
|
+
const TYPES_EXTRACTION_HELPER = {
|
|
24
|
+
vue3: `import type { VNodeProps, AllowedComponentProps, ComponentInstance } from 'vue'
|
|
25
|
+
|
|
26
|
+
type ExtractProps<T> = Omit<
|
|
27
|
+
ComponentInstance<T>['$props'],
|
|
28
|
+
keyof VNodeProps | keyof AllowedComponentProps
|
|
29
|
+
>`,
|
|
30
|
+
react: `import type React from 'react'
|
|
31
|
+
import type { Prettify } from '@adonisjs/core/types/common'
|
|
32
|
+
|
|
33
|
+
type ExtractProps<T> =
|
|
34
|
+
T extends React.FC<infer Props>
|
|
35
|
+
? Prettify<Omit<Props, 'children'>>
|
|
36
|
+
: T extends React.Component<infer Props>
|
|
37
|
+
? Prettify<Omit<Props, 'children'>>
|
|
38
|
+
: never`
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Creates an AdonisJS assembler hook to automatically generate TypeScript definitions
|
|
42
|
+
* for Inertia.js pages based on the specified framework.
|
|
43
|
+
*
|
|
44
|
+
* This function scans page components in the 'inertia/pages' directory and generates
|
|
45
|
+
* type definitions that map page names to their component props.
|
|
46
|
+
*
|
|
47
|
+
* @param config - Configuration object specifying the frontend framework
|
|
48
|
+
* @param config.framework - The frontend framework ('vue3' or 'react')
|
|
49
|
+
* @param config.source - The path to Inertia pages (default: inertia/pages)
|
|
50
|
+
* @returns Assembler hook object with run method for generating page types
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```js
|
|
54
|
+
* // In your adonisrc.ts file
|
|
55
|
+
* export default defineConfig({
|
|
56
|
+
* assembler: {
|
|
57
|
+
* onBuildStarting: [indexPages({ framework: 'vue3' })]
|
|
58
|
+
* }
|
|
59
|
+
* })
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
const indexPages = function(config) {
|
|
63
|
+
if (!SUPPORTED_FRAMEWORKS.includes(config.framework)) throw new Error(`Unsupported framework "${config.framework}". Types generation is available only for ${SUPPORTED_FRAMEWORKS.join(",")}`);
|
|
64
|
+
return {
|
|
65
|
+
/**
|
|
66
|
+
* Executes the page indexing process to generate TypeScript definitions.
|
|
67
|
+
*
|
|
68
|
+
* @param _ - Unused first parameter (assembler context)
|
|
69
|
+
* @param __ - Unused second parameter (hooks instance)
|
|
70
|
+
* @param indexGenerator - The index generator instance used to register the pages type generation
|
|
71
|
+
*/
|
|
72
|
+
run(_, __, indexGenerator) {
|
|
73
|
+
indexGenerator.add("inertiaPages", {
|
|
74
|
+
source: config.source ?? "inertia/pages",
|
|
75
|
+
glob: GLOB[config.framework],
|
|
76
|
+
output: ".adonisjs/server/pages.d.ts",
|
|
77
|
+
/**
|
|
78
|
+
* Generates the TypeScript module declaration for Inertia pages.
|
|
79
|
+
*
|
|
80
|
+
* @param vfs - Virtual file system containing the scanned page files
|
|
81
|
+
* @param buffer - Buffer instance for writing the generated TypeScript code
|
|
82
|
+
* @param ___ - Unused third parameter
|
|
83
|
+
* @param helpers - Helper utilities for path manipulation and imports
|
|
84
|
+
*/
|
|
85
|
+
as(vfs, buffer, ___, helpers) {
|
|
86
|
+
const filesList = vfs.asList();
|
|
87
|
+
buffer.writeLine(`import '@adonisjs/inertia/types'`);
|
|
88
|
+
buffer.writeLine(TYPES_EXTRACTION_HELPER[config.framework]);
|
|
89
|
+
buffer.write(`declare module '@adonisjs/inertia/types' {`).indent();
|
|
90
|
+
buffer.write(`export interface InertiaPages {`).indent();
|
|
91
|
+
Object.keys(filesList).forEach((key) => {
|
|
92
|
+
buffer.write(`'${key}': ExtractProps<(typeof import('${helpers.toImportPath(filesList[key])}'))['default']>`);
|
|
93
|
+
});
|
|
94
|
+
buffer.dedent().write(`}`);
|
|
95
|
+
buffer.dedent().write(`}`);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
} };
|
|
99
|
+
};
|
|
100
|
+
//#endregion
|
|
101
|
+
//#region src/define_config.ts
|
|
102
|
+
/**
|
|
103
|
+
* Define the Inertia configuration with default values
|
|
104
|
+
*
|
|
105
|
+
* This function merges user-provided configuration with sensible defaults
|
|
106
|
+
* to create a complete Inertia configuration object.
|
|
107
|
+
*
|
|
108
|
+
* @param config - User configuration input to override defaults
|
|
109
|
+
* @returns Complete Inertia configuration object with defaults applied
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```js
|
|
113
|
+
* const config = defineConfig({
|
|
114
|
+
* rootView: 'layouts/app',
|
|
115
|
+
* ssr: {
|
|
116
|
+
* enabled: true,
|
|
117
|
+
* entrypoint: 'inertia/ssr.tsx'
|
|
118
|
+
* }
|
|
119
|
+
* })
|
|
120
|
+
* ```
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```js
|
|
124
|
+
* // Minimal configuration
|
|
125
|
+
* const config = defineConfig({
|
|
126
|
+
* rootView: 'app'
|
|
127
|
+
* })
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
function defineConfig(config) {
|
|
131
|
+
return lodash.merge({
|
|
132
|
+
rootView: "inertia_layout",
|
|
133
|
+
history: { encrypt: false },
|
|
134
|
+
ssr: {
|
|
135
|
+
enabled: false,
|
|
136
|
+
entrypoint: "inertia/ssr.tsx"
|
|
137
|
+
}
|
|
138
|
+
}, config);
|
|
139
|
+
}
|
|
140
|
+
//#endregion
|
|
141
|
+
export { indexPages as n, defineConfig as t };
|