@adonisjs/inertia 1.0.0-1 → 1.0.0-3

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.
@@ -61,11 +61,11 @@ var Inertia = class {
61
61
  /**
62
62
  * Render a page using Inertia
63
63
  */
64
- async render(component, pageProps) {
64
+ async render(component, pageProps, viewProps) {
65
65
  const pageObject = await this.#buildPageObject(component, pageProps);
66
66
  const isInertiaRequest = !!this.ctx.request.header("x-inertia");
67
67
  if (!isInertiaRequest) {
68
- return this.ctx.view.render(this.config.rootView, { page: pageObject });
68
+ return this.ctx.view.render(this.config.rootView, { ...viewProps, page: pageObject });
69
69
  }
70
70
  return pageObject;
71
71
  }
@@ -0,0 +1,85 @@
1
+ // src/plugins/edge/plugin.ts
2
+ import { encode } from "html-entities";
3
+
4
+ // src/debug.ts
5
+ import { debuglog } from "node:util";
6
+ var debug_default = debuglog("adonisjs:inertia");
7
+
8
+ // src/plugins/edge/tags.ts
9
+ import { EdgeError } from "edge-error";
10
+
11
+ // src/plugins/edge/utils.ts
12
+ function isSubsetOf(expression, expressions, errorCallback) {
13
+ if (!expressions.includes(expression.type)) {
14
+ errorCallback();
15
+ }
16
+ }
17
+
18
+ // src/plugins/edge/tags.ts
19
+ var inertiaTag = {
20
+ block: false,
21
+ tagName: "inertia",
22
+ seekable: true,
23
+ compile(parser, buffer, { filename, loc, properties }) {
24
+ if (properties.jsArg.trim() === "") {
25
+ buffer.writeExpression(`out += state.inertia(state.page)`, filename, loc.start.line);
26
+ return;
27
+ }
28
+ properties.jsArg = `(${properties.jsArg})`;
29
+ const parsed = parser.utils.transformAst(
30
+ parser.utils.generateAST(properties.jsArg, loc, filename),
31
+ filename,
32
+ parser
33
+ );
34
+ isSubsetOf(parsed, ["ObjectExpression"], () => {
35
+ const { line, col } = parser.utils.getExpressionLoc(parsed);
36
+ throw new EdgeError(
37
+ `"${properties.jsArg}" is not a valid argument for @inertia`,
38
+ "E_UNALLOWED_EXPRESSION",
39
+ { line, col, filename }
40
+ );
41
+ });
42
+ const attributes = parser.utils.stringify(parsed);
43
+ buffer.writeExpression(
44
+ `out += state.inertia(state.page, ${attributes})`,
45
+ filename,
46
+ loc.start.line
47
+ );
48
+ }
49
+ };
50
+ var inertiaHeadTag = {
51
+ block: false,
52
+ tagName: "inertiaHead",
53
+ seekable: false,
54
+ compile(_, buffer, { filename, loc }) {
55
+ buffer.writeExpression(`out += state.inertiaHead(state.page)`, filename, loc.start.line);
56
+ }
57
+ };
58
+
59
+ // src/plugins/edge/plugin.ts
60
+ var edgePluginInertia = () => {
61
+ return (edge) => {
62
+ debug_default("sharing globals and inertia tags with edge");
63
+ edge.global(
64
+ "inertia",
65
+ (page = {}, attributes = {}) => {
66
+ if (page.ssrBody)
67
+ return page.ssrBody;
68
+ const className = attributes?.class ? ` class="${attributes.class}"` : "";
69
+ const id = attributes?.id ? ` id="${attributes.id}"` : ' id="app"';
70
+ const tag = attributes?.as || "div";
71
+ const dataPage = encode(JSON.stringify(page));
72
+ return `<${tag}${id}${className} data-page="${dataPage}"></${tag}>`;
73
+ }
74
+ );
75
+ edge.global("inertiaHead", (page) => {
76
+ const { ssrHead = [] } = page || {};
77
+ return ssrHead.join("\n");
78
+ });
79
+ edge.registerTag(inertiaHeadTag);
80
+ edge.registerTag(inertiaTag);
81
+ };
82
+ };
83
+ export {
84
+ edgePluginInertia
85
+ };
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  InertiaMiddleware
3
- } from "../chunk-TO74VVAR.js";
3
+ } from "../chunk-GDULL3NT.js";
4
4
 
5
5
  // providers/inertia_provider.ts
6
6
  import { configProvider } from "@adonisjs/core";
@@ -15,7 +15,7 @@ var InertiaProvider = class {
15
15
  async registerEdgePlugin() {
16
16
  try {
17
17
  const edgeExports = await import("edge.js");
18
- const { edgePluginInertia } = await import("../src/plugins/edge.js");
18
+ const { edgePluginInertia } = await import("../plugin-QOPSYJCV.js");
19
19
  edgeExports.default.use(edgePluginInertia());
20
20
  } catch {
21
21
  }
@@ -17,7 +17,7 @@ declare class Inertia {
17
17
  /**
18
18
  * Render a page using Inertia
19
19
  */
20
- render<T extends PageProps>(component: string, pageProps?: T): Promise<string | {
20
+ render<TPageProps extends Record<string, any> = PageProps, TViewProps extends Record<string, any> = PageProps>(component: string, pageProps?: TPageProps, viewProps?: TViewProps): Promise<string | {
21
21
  component: string;
22
22
  version: string | number;
23
23
  props: any;
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  InertiaMiddleware
3
- } from "../chunk-TO74VVAR.js";
3
+ } from "../chunk-GDULL3NT.js";
4
4
  export {
5
5
  InertiaMiddleware as default
6
6
  };
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-1",
4
+ "version": "1.0.0-3",
5
5
  "engines": {
6
6
  "node": ">=18.16.0"
7
7
  },
@@ -44,6 +44,7 @@
44
44
  "@adonisjs/tsconfig": "^1.1.8",
45
45
  "@japa/api-client": "^2.0.1",
46
46
  "@japa/assert": "2.0.1",
47
+ "@japa/expect-type": "^2.0.0",
47
48
  "@japa/file-system": "^2.0.1",
48
49
  "@japa/plugin-adonisjs": "^2.0.1",
49
50
  "@japa/runner": "3.0.5",
@@ -54,6 +55,7 @@
54
55
  "c8": "^8.0.1",
55
56
  "copyfiles": "^2.4.1",
56
57
  "del-cli": "^5.1.0",
58
+ "edge-parser": "^9.0.0",
57
59
  "edge.js": "^6.0.0",
58
60
  "eslint": "^8.53.0",
59
61
  "get-port": "^7.0.0",
@@ -63,11 +65,12 @@
63
65
  "tinybench": "^2.5.1",
64
66
  "ts-node": "^10.9.1",
65
67
  "tsup": "^7.3.0",
66
- "typescript": "^5.2.2"
68
+ "typescript": "~5.2.2"
67
69
  },
68
70
  "dependencies": {
69
71
  "@poppinss/utils": "^6.5.1",
70
72
  "crc-32": "^1.2.2",
73
+ "edge-error": "^4.0.0",
71
74
  "html-entities": "^2.4.0",
72
75
  "qs": "^6.11.2"
73
76
  },
@@ -1,8 +0,0 @@
1
- import { PluginFn } from 'edge.js/types';
2
-
3
- /**
4
- * Register the Inertia tags and globals within Edge
5
- */
6
- declare const edgePluginInertia: () => PluginFn<undefined>;
7
-
8
- export { edgePluginInertia };
@@ -1,41 +0,0 @@
1
- // src/plugins/edge.ts
2
- import { encode } from "html-entities";
3
-
4
- // src/debug.ts
5
- import { debuglog } from "node:util";
6
- var debug_default = debuglog("adonisjs:inertia");
7
-
8
- // src/plugins/edge.ts
9
- var edgePluginInertia = () => {
10
- return (edge) => {
11
- debug_default("sharing globals and inertia tags with edge");
12
- edge.global("inertia", (page = {}) => {
13
- if (page.ssrBody)
14
- return page.ssrBody;
15
- return `<div id="app" data-page="${encode(JSON.stringify(page))}"></div>`;
16
- });
17
- edge.global("inertiaHead", (page) => {
18
- const { ssrHead = [] } = page || {};
19
- return ssrHead.join("\n");
20
- });
21
- edge.registerTag({
22
- block: false,
23
- tagName: "inertia",
24
- seekable: false,
25
- compile(_, buffer, { filename, loc }) {
26
- buffer.writeExpression(`out += state.inertia(state.page)`, filename, loc.start.line);
27
- }
28
- });
29
- edge.registerTag({
30
- block: false,
31
- tagName: "inertiaHead",
32
- seekable: false,
33
- compile(_, buffer, { filename, loc }) {
34
- buffer.writeExpression(`out += state.inertiaHead(state.page)`, filename, loc.start.line);
35
- }
36
- });
37
- };
38
- };
39
- export {
40
- edgePluginInertia
41
- };