@adonisjs/inertia 1.0.0-27 → 1.0.0-29

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.
@@ -1,4 +1,5 @@
1
1
  // src/server_renderer.ts
2
+ import { pathToFileURL } from "node:url";
2
3
  var ServerRenderer = class {
3
4
  constructor(config, vite) {
4
5
  this.config = config;
@@ -17,7 +18,7 @@ var ServerRenderer = class {
17
18
  const runtime = await this.vite.createRuntime();
18
19
  render = await runtime.executeEntrypoint(this.config.ssr.entrypoint);
19
20
  } else {
20
- render = await import(this.config.ssr.bundle);
21
+ render = await import(pathToFileURL(this.config.ssr.bundle).href);
21
22
  }
22
23
  const result = await render.default(pageObject);
23
24
  return { head: result.head, body: result.body };
@@ -108,12 +109,18 @@ var Inertia = class {
108
109
  }
109
110
  return isSsrEnabledForPage;
110
111
  }
112
+ /**
113
+ * Resolve the root view
114
+ */
115
+ #resolveRootView() {
116
+ return typeof this.config.rootView === "function" ? this.config.rootView(this.ctx) : this.config.rootView;
117
+ }
111
118
  /**
112
119
  * Render the page on the server
113
120
  */
114
121
  async #renderOnServer(pageObject, viewProps) {
115
122
  const { head, body } = await this.#serverRenderer.render(pageObject);
116
- return this.ctx.view.render(this.config.rootView, {
123
+ return this.ctx.view.render(this.#resolveRootView(), {
117
124
  ...viewProps,
118
125
  page: { ssrHead: head, ssrBody: body, ...pageObject }
119
126
  });
@@ -135,7 +142,7 @@ var Inertia = class {
135
142
  const shouldRenderOnServer = await this.#shouldRenderOnServer(component);
136
143
  if (shouldRenderOnServer)
137
144
  return this.#renderOnServer(pageObject, viewProps);
138
- return this.ctx.view.render(this.config.rootView, { ...viewProps, page: pageObject });
145
+ return this.ctx.view.render(this.#resolveRootView(), { ...viewProps, page: pageObject });
139
146
  }
140
147
  this.ctx.response.header("x-inertia", "true");
141
148
  return pageObject;
package/build/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import Configure from '@adonisjs/core/commands/configure';
2
2
  import { ConfigProvider } from '@adonisjs/core/types';
3
- import { I as InertiaConfig, R as ResolvedConfig } from './types-CyeRFf-Q.js';
3
+ import { S as SharedData, I as InertiaConfig, R as ResolvedConfig } from './types-fb05P61I.js';
4
4
  import '@adonisjs/core/http';
5
5
  import '@tuyau/utils/types';
6
6
  import '@adonisjs/vite';
@@ -13,7 +13,7 @@ declare function configure(command: Configure): Promise<void>;
13
13
  /**
14
14
  * Define the Inertia configuration
15
15
  */
16
- declare function defineConfig(config: InertiaConfig): ConfigProvider<ResolvedConfig>;
16
+ declare function defineConfig<T extends SharedData>(config: InertiaConfig<T>): ConfigProvider<ResolvedConfig<T>>;
17
17
 
18
18
  declare const stubsRoot: string;
19
19
 
package/build/index.js CHANGED
@@ -84,15 +84,10 @@ async function defineExampleRoute(command, codemods) {
84
84
  if (!routesFile) {
85
85
  return command.logger.warning("Unable to find the routes file");
86
86
  }
87
- const isAlreadyDefined = routesFile.getText().includes("/inertia");
88
- if (isAlreadyDefined) {
89
- command.logger.warning("/inertia route is already defined. Skipping");
90
- return;
91
- }
92
87
  const action = command.logger.action("update start/routes.ts file");
93
88
  try {
94
89
  routesFile?.addStatements((writer) => {
95
- writer.writeLine(`router.on('/inertia').renderInertia('home', { version: 6 })`);
90
+ writer.writeLine(`router.on('/').renderInertia('home', { version: 6 })`);
96
91
  });
97
92
  await tsMorph?.save();
98
93
  action.succeeded();
@@ -105,6 +100,7 @@ async function configure(command) {
105
100
  let adapter = command.parsedFlags.adapter;
106
101
  let ssr = command.parsedFlags.ssr;
107
102
  let shouldInstallPackages = command.parsedFlags.install;
103
+ let shouldSkipExampleRoute = command.parsedFlags["skip-example-route"];
108
104
  if (adapter === void 0) {
109
105
  adapter = await command.prompt.choice(
110
106
  "Select the Inertia adapter you want to use",
@@ -163,7 +159,9 @@ async function configure(command) {
163
159
  await codemods.registerVitePlugin(adonisjsPluginCall, [
164
160
  { isNamed: false, module: "@adonisjs/vite/client", identifier: "adonisjs" }
165
161
  ]);
166
- await defineExampleRoute(command, codemods);
162
+ if (shouldSkipExampleRoute !== true) {
163
+ await defineExampleRoute(command, codemods);
164
+ }
167
165
  const pkgToInstall = adapterInfo.dependencies;
168
166
  if (ssr && adapterInfo.ssrDependencies) {
169
167
  pkgToInstall.push(...adapterInfo.ssrDependencies);
@@ -182,6 +180,7 @@ async function configure(command) {
182
180
  }
183
181
 
184
182
  // src/define_config.ts
183
+ import { slash } from "@poppinss/utils";
185
184
  import { configProvider } from "@adonisjs/core";
186
185
 
187
186
  // src/version_cache.ts
@@ -284,7 +283,6 @@ var FilesDetector = class {
284
283
  };
285
284
 
286
285
  // src/define_config.ts
287
- import { slash } from "@poppinss/utils";
288
286
  function defineConfig(config) {
289
287
  return configProvider.create(async (app) => {
290
288
  const detector = new FilesDetector(app);
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  InertiaMiddleware
3
- } from "../chunk-Y7UMAJ3S.js";
3
+ } from "../chunk-QKSM72AR.js";
4
4
 
5
5
  // providers/inertia_provider.ts
6
6
  import { configProvider } from "@adonisjs/core";
@@ -1,6 +1,8 @@
1
1
  {{{
2
2
  exports({ to: app.makePath('inertia/app/app.tsx') })
3
3
  }}}
4
+ /// <reference path="../../adonisrc.ts" />
5
+
4
6
  import '../css/app.css';
5
7
 
6
8
  {{#if ssr}}
@@ -1,6 +1,8 @@
1
1
  {{{
2
2
  exports({ to: app.makePath('inertia/app/app.tsx') })
3
3
  }}}
4
+ /// <reference path="../../adonisrc.ts" />
5
+
4
6
  import '../css/app.css'
5
7
 
6
8
  {{#if ssr}}
@@ -1,7 +1,8 @@
1
1
  import { Vite } from '@adonisjs/vite';
2
2
  import { HttpContext } from '@adonisjs/core/http';
3
3
  import { NextFn } from '@adonisjs/core/types/http';
4
- import { a as Inertia, R as ResolvedConfig } from '../types-CyeRFf-Q.js';
4
+ import { a as Inertia, R as ResolvedConfig } from '../types-fb05P61I.js';
5
+ import '@adonisjs/core/types';
5
6
  import '@tuyau/utils/types';
6
7
 
7
8
  /**
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  InertiaMiddleware
3
- } from "../chunk-Y7UMAJ3S.js";
3
+ } from "../chunk-QKSM72AR.js";
4
4
  export {
5
5
  InertiaMiddleware as default
6
6
  };
@@ -1,6 +1,6 @@
1
1
  import { PluginFn } from '@japa/runner/types';
2
2
  import { ApplicationService } from '@adonisjs/core/types';
3
- import { P as PageProps } from '../../../types-CyeRFf-Q.js';
3
+ import { P as PageProps } from '../../../types-fb05P61I.js';
4
4
  import '@adonisjs/core/http';
5
5
  import '@tuyau/utils/types';
6
6
  import '@adonisjs/vite';
@@ -1,4 +1,5 @@
1
+ import '@adonisjs/core/types';
1
2
  import '@adonisjs/core/http';
2
3
  import '@tuyau/utils/types';
3
- export { A as AssetsVersion, D as Data, I as InertiaConfig, d as InferPageProps, M as MaybePromise, c as PageObject, P as PageProps, e as RenderInertiaSsrApp, R as ResolvedConfig, b as SharedData, S as SharedDatumFactory } from '../types-CyeRFf-Q.js';
4
+ export { A as AssetsVersion, D as Data, I as InertiaConfig, f as InferPageProps, d as InferSharedProps, M as MaybePromise, c as PageObject, P as PageProps, g as RenderInertiaSsrApp, R as ResolvedConfig, S as SharedData, b as SharedDatumFactory, e as SharedProps } from '../types-fb05P61I.js';
4
5
  import '@adonisjs/vite';
@@ -1,6 +1,8 @@
1
1
  {{{
2
2
  exports({ to: app.makePath('inertia/app/app.ts') })
3
3
  }}}
4
+ /// <reference path="../../adonisrc.ts" />
5
+
4
6
  import '../css/app.css';
5
7
 
6
8
  import { createInertiaApp } from '@inertiajs/svelte'
@@ -1,3 +1,4 @@
1
+ import { ConfigProvider } from '@adonisjs/core/types';
1
2
  import { HttpContext } from '@adonisjs/core/http';
2
3
  import { Simplify, Serialize } from '@tuyau/utils/types';
3
4
  import { Vite } from '@adonisjs/vite';
@@ -85,12 +86,12 @@ type SharedData = Record<string, Data | SharedDatumFactory>;
85
86
  * Allowed values for the assets version
86
87
  */
87
88
  type AssetsVersion = string | number | undefined;
88
- interface InertiaConfig {
89
+ interface InertiaConfig<T extends SharedData = SharedData> {
89
90
  /**
90
91
  * Path to the Edge view that will be used as the root view for Inertia responses.
91
92
  * @default root (resources/views/inertia_layout.edge)
92
93
  */
93
- rootView?: string;
94
+ rootView?: string | ((ctx: HttpContext) => string);
94
95
  /**
95
96
  * Path to your client-side entrypoint file.
96
97
  */
@@ -103,7 +104,7 @@ interface InertiaConfig {
103
104
  /**
104
105
  * Data that should be shared with all rendered pages
105
106
  */
106
- sharedData?: SharedData;
107
+ sharedData?: T;
107
108
  /**
108
109
  * Options to configure SSR
109
110
  */
@@ -129,10 +130,10 @@ interface InertiaConfig {
129
130
  /**
130
131
  * Resolved inertia configuration
131
132
  */
132
- interface ResolvedConfig {
133
- rootView: string;
133
+ interface ResolvedConfig<T extends SharedData = SharedData> {
134
+ rootView: string | ((ctx: HttpContext) => string);
134
135
  versionCache: VersionCache;
135
- sharedData: SharedData;
136
+ sharedData: T;
136
137
  ssr: {
137
138
  enabled: boolean;
138
139
  entrypoint: string;
@@ -158,10 +159,25 @@ type InferProps<T> = {
158
159
  } & {
159
160
  [K in keyof T as IsLazyProp<T[K]> extends true ? never : K]: T[K];
160
161
  };
162
+ type ReturnsTypesSharedData<T extends SharedData> = {} extends T ? {} : {
163
+ [K in keyof T]: T[K] extends (...args: any[]) => MaybePromise<infer U> ? U : T[K];
164
+ };
165
+ /**
166
+ * Infer shared data types from the config provider
167
+ */
168
+ type InferSharedProps<T extends ConfigProvider<ResolvedConfig>> = ReturnsTypesSharedData<Awaited<ReturnType<T['resolver']>>['sharedData']>;
169
+ /**
170
+ * The shared props inferred from the user config user-land.
171
+ * Should be module augmented by the user
172
+ */
173
+ interface SharedProps {
174
+ }
161
175
  /**
162
176
  * Helper for infering the page props from a Controller method that returns
163
177
  * inertia.render
164
178
  *
179
+ * InferPageProps will also include the shared props
180
+ *
165
181
  * ```ts
166
182
  * // Your Adonis Controller
167
183
  * class MyController {
@@ -175,7 +191,7 @@ type InferProps<T> = {
175
191
  * }
176
192
  * ```
177
193
  */
178
- type InferPageProps<Controller, Method extends keyof Controller> = Controller[Method] extends (...args: any[]) => any ? Simplify<Serialize<InferProps<Exclude<Awaited<ReturnType<Controller[Method]>>, string>['props']>>> : never;
194
+ type InferPageProps<Controller, Method extends keyof Controller> = Controller[Method] extends (...args: any[]) => any ? Simplify<Serialize<InferProps<Exclude<Awaited<ReturnType<Controller[Method]>>, string>['props']> & SharedProps>> : never;
179
195
  /**
180
196
  * Signature for the method in the SSR entrypoint file
181
197
  */
@@ -184,4 +200,4 @@ type RenderInertiaSsrApp = (page: PageObject) => Promise<{
184
200
  body: string;
185
201
  }>;
186
202
 
187
- export { type AssetsVersion as A, type Data as D, type InertiaConfig as I, type MaybePromise as M, type PageProps as P, type ResolvedConfig as R, type SharedDatumFactory as S, Inertia as a, type SharedData as b, type PageObject as c, type InferPageProps as d, type RenderInertiaSsrApp as e };
203
+ export { type AssetsVersion as A, type Data as D, type InertiaConfig as I, type MaybePromise as M, type PageProps as P, type ResolvedConfig as R, type SharedData as S, Inertia as a, type SharedDatumFactory as b, type PageObject as c, type InferSharedProps as d, type SharedProps as e, type InferPageProps as f, type RenderInertiaSsrApp as g };
@@ -1,6 +1,8 @@
1
1
  {{{
2
2
  exports({ to: app.makePath('inertia/app/app.ts') })
3
3
  }}}
4
+ /// <reference path="../../adonisrc.ts" />
5
+
4
6
  import '../css/app.css';
5
7
 
6
8
  {{#if ssr}}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@adonisjs/inertia",
3
3
  "description": "Official Inertia.js adapter for AdonisJS",
4
- "version": "1.0.0-27",
4
+ "version": "1.0.0-29",
5
5
  "engines": {
6
6
  "node": ">=20.6.0"
7
7
  },
@@ -38,7 +38,7 @@
38
38
  "prepublishOnly": "npm run build"
39
39
  },
40
40
  "devDependencies": {
41
- "@adonisjs/assembler": "^7.5.1",
41
+ "@adonisjs/assembler": "^7.6.1",
42
42
  "@adonisjs/core": "6.8.0",
43
43
  "@adonisjs/eslint-config": "^1.3.0",
44
44
  "@adonisjs/prettier-config": "^1.3.0",
@@ -52,8 +52,8 @@
52
52
  "@japa/plugin-adonisjs": "^3.0.1",
53
53
  "@japa/runner": "3.1.4",
54
54
  "@japa/snapshot": "^2.0.5",
55
- "@swc/core": "^1.5.3",
56
- "@types/node": "^20.12.10",
55
+ "@swc/core": "^1.5.24",
56
+ "@types/node": "^20.12.13",
57
57
  "@types/qs": "^6.9.15",
58
58
  "@types/supertest": "^6.0.2",
59
59
  "@vavite/multibuild": "^4.1.1",
@@ -65,16 +65,16 @@
65
65
  "eslint": "^8.57.0",
66
66
  "get-port": "^7.1.0",
67
67
  "prettier": "^3.2.5",
68
- "release-it": "^17.2.1",
68
+ "release-it": "^17.3.0",
69
69
  "supertest": "^7.0.0",
70
70
  "ts-node": "^10.9.2",
71
71
  "tsup": "^8.0.2",
72
72
  "typescript": "~5.4.5",
73
- "vite": "^5.2.11"
73
+ "vite": "^5.2.12"
74
74
  },
75
75
  "dependencies": {
76
76
  "@poppinss/utils": "^6.7.3",
77
- "@tuyau/utils": "^0.0.3",
77
+ "@tuyau/utils": "^0.0.4",
78
78
  "crc-32": "^1.2.2",
79
79
  "edge-error": "^4.0.1",
80
80
  "html-entities": "^2.5.2",