@alepha/react 0.10.6 → 0.10.7

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/dist/index.js CHANGED
@@ -106,10 +106,10 @@ const $page = (options) => {
106
106
  };
107
107
  var PageDescriptor = class extends Descriptor {
108
108
  onInit() {
109
- if (this.options.static) this.options.cache ??= {
109
+ if (this.options.static) this.options.cache ??= { store: {
110
110
  provider: "memory",
111
111
  ttl: [1, "week"]
112
- };
112
+ } };
113
113
  }
114
114
  get name() {
115
115
  return this.options.name ?? this.config.propertyKey;
@@ -151,13 +151,13 @@ const ClientOnly = (props) => {
151
151
  if (props.disabled) return props.children;
152
152
  return mounted ? props.children : props.fallback;
153
153
  };
154
+ var ClientOnly_default = ClientOnly;
154
155
 
155
156
  //#endregion
156
157
  //#region src/components/ErrorViewer.tsx
157
158
  const ErrorViewer = ({ error, alepha }) => {
158
159
  const [expanded, setExpanded] = useState(false);
159
- const isProduction = alepha.isProduction();
160
- if (isProduction) return /* @__PURE__ */ jsx(ErrorViewerProduction, {});
160
+ if (alepha.isProduction()) return /* @__PURE__ */ jsx(ErrorViewerProduction, {});
161
161
  const stackLines = error.stack?.split("\n") ?? [];
162
162
  const previewLines = stackLines.slice(0, 5);
163
163
  const hiddenLineCount = stackLines.length - previewLines.length;
@@ -258,6 +258,7 @@ const ErrorViewer = ({ error, alepha }) => {
258
258
  })] })]
259
259
  });
260
260
  };
261
+ var ErrorViewer_default = ErrorViewer;
261
262
  const ErrorViewerProduction = () => {
262
263
  const styles = {
263
264
  container: {
@@ -429,6 +430,7 @@ var ErrorBoundary = class extends React.Component {
429
430
  return this.props.children;
430
431
  }
431
432
  };
433
+ var ErrorBoundary_default = ErrorBoundary;
432
434
 
433
435
  //#endregion
434
436
  //#region src/components/NestedView.tsx
@@ -512,11 +514,11 @@ const NestedView = (props) => {
512
514
  })
513
515
  });
514
516
  if (props.errorBoundary === false) return /* @__PURE__ */ jsx(Fragment, { children: element });
515
- if (props.errorBoundary) return /* @__PURE__ */ jsx(ErrorBoundary, {
517
+ if (props.errorBoundary) return /* @__PURE__ */ jsx(ErrorBoundary_default, {
516
518
  fallback: props.errorBoundary,
517
519
  children: element
518
520
  });
519
- return /* @__PURE__ */ jsx(ErrorBoundary, {
521
+ return /* @__PURE__ */ jsx(ErrorBoundary_default, {
520
522
  fallback: (error) => {
521
523
  const result = state.onError(error, state);
522
524
  if (result instanceof Redirection) return "Redirection inside ErrorBoundary is not allowed.";
@@ -527,7 +529,7 @@ const NestedView = (props) => {
527
529
  };
528
530
  var NestedView_default = memo(NestedView);
529
531
  function parseAnimation(animationLike, state, type = "enter") {
530
- if (!animationLike) return void 0;
532
+ if (!animationLike) return;
531
533
  const DEFAULT_DURATION = 300;
532
534
  const animation = typeof animationLike === "function" ? animationLike(state) : animationLike;
533
535
  if (typeof animation === "string") {
@@ -541,20 +543,15 @@ function parseAnimation(animationLike, state, type = "enter") {
541
543
  const anim = animation[type];
542
544
  const duration = typeof anim === "object" ? anim.duration ?? DEFAULT_DURATION : DEFAULT_DURATION;
543
545
  const name = typeof anim === "object" ? anim.name : anim;
544
- if (type === "exit") {
545
- const timing$1 = typeof anim === "object" ? anim.timing ?? "" : "";
546
- return {
547
- duration,
548
- animation: `${duration}ms ${timing$1} ${name}`
549
- };
550
- }
551
- const timing = typeof anim === "object" ? anim.timing ?? "" : "";
546
+ if (type === "exit") return {
547
+ duration,
548
+ animation: `${duration}ms ${typeof anim === "object" ? anim.timing ?? "" : ""} ${name}`
549
+ };
552
550
  return {
553
551
  duration,
554
- animation: `${duration}ms ${timing} ${name}`
552
+ animation: `${duration}ms ${typeof anim === "object" ? anim.timing ?? "" : ""} ${name}`
555
553
  };
556
554
  }
557
- return void 0;
558
555
  }
559
556
 
560
557
  //#endregion
@@ -663,15 +660,13 @@ var ReactPageProvider = class {
663
660
  it.config = { ...config };
664
661
  if (previous?.[i] && !forceRefresh && previous[i].name === route$1.name) {
665
662
  const url = (str) => str ? str.replace(/\/\/+/g, "/") : "/";
666
- const prev = JSON.stringify({
663
+ if (JSON.stringify({
667
664
  part: url(previous[i].part),
668
665
  params: previous[i].config?.params ?? {}
669
- });
670
- const curr = JSON.stringify({
666
+ }) === JSON.stringify({
671
667
  part: url(route$1.path),
672
668
  params: config.params ?? {}
673
- });
674
- if (prev === curr) {
669
+ })) {
675
670
  it.props = previous[i].props;
676
671
  it.error = previous[i].error;
677
672
  it.cache = true;
@@ -774,15 +769,11 @@ var ReactPageProvider = class {
774
769
  }
775
770
  async createElement(page, props) {
776
771
  if (page.lazy && page.component) this.log.warn(`Page ${page.name} has both lazy and component options, lazy will be used`);
777
- if (page.lazy) {
778
- const component = await page.lazy();
779
- return createElement(component.default, props);
780
- }
772
+ if (page.lazy) return createElement((await page.lazy()).default, props);
781
773
  if (page.component) return createElement(page.component, props);
782
- return void 0;
783
774
  }
784
775
  renderError(error) {
785
- return createElement(ErrorViewer, {
776
+ return createElement(ErrorViewer_default, {
786
777
  error,
787
778
  alepha: this.alepha
788
779
  });
@@ -808,7 +799,7 @@ var ReactPageProvider = class {
808
799
  }
809
800
  renderView(index, path, view, page) {
810
801
  view ??= this.renderEmptyView();
811
- const element = page.client ? createElement(ClientOnly, typeof page.client === "object" ? page.client : {}, view) : view;
802
+ const element = page.client ? createElement(ClientOnly_default, typeof page.client === "object" ? page.client : {}, view) : view;
812
803
  return createElement(RouterLayerContext.Provider, { value: {
813
804
  index,
814
805
  path
@@ -821,10 +812,7 @@ var ReactPageProvider = class {
821
812
  const pages = this.alepha.descriptors($page);
822
813
  const hasParent = (it) => {
823
814
  if (it.options.parent) return true;
824
- for (const page of pages) {
825
- const children = page.options.children ? Array.isArray(page.options.children) ? page.options.children : page.options.children() : [];
826
- if (children.includes(it)) return true;
827
- }
815
+ for (const page of pages) if ((page.options.children ? Array.isArray(page.options.children) ? page.options.children : page.options.children() : []).includes(it)) return true;
828
816
  };
829
817
  for (const page of pages) {
830
818
  if (page.options.path === "/*") hasNotFoundHandler = true;
@@ -858,7 +846,7 @@ var ReactPageProvider = class {
858
846
  };
859
847
  }
860
848
  add(entry) {
861
- if (this.alepha.isReady()) throw new Error("Router is already initialized");
849
+ if (this.alepha.isReady()) throw new AlephaError("Router is already initialized");
862
850
  entry.name ??= this.nextId();
863
851
  const page = entry;
864
852
  page.match = this.createMatch(page);
@@ -932,7 +920,7 @@ var ReactServerProvider = class {
932
920
  throw new AlephaError("Invalid HTML response");
933
921
  };
934
922
  }
935
- if (this.alepha.isServerless() === "vite") {
923
+ if (this.alepha.isViteDev()) {
936
924
  await this.configureVite(ssrEnabled);
937
925
  return;
938
926
  }
@@ -1008,7 +996,7 @@ var ReactServerProvider = class {
1008
996
  return async (options = {}) => {
1009
997
  const page = this.pageApi.page(name);
1010
998
  const url = new URL(this.pageApi.url(name, options));
1011
- const entry = {
999
+ const state = {
1012
1000
  url,
1013
1001
  params: options.params ?? {},
1014
1002
  query: options.query ?? {},
@@ -1016,7 +1004,6 @@ var ReactServerProvider = class {
1016
1004
  layers: [],
1017
1005
  meta: {}
1018
1006
  };
1019
- const state = entry;
1020
1007
  this.log.trace("Rendering", { url });
1021
1008
  await this.alepha.events.emit("react:server:render:begin", { state });
1022
1009
  const { redirect } = await this.pageApi.createLayers(page, state);
@@ -1053,14 +1040,13 @@ var ReactServerProvider = class {
1053
1040
  const template = await templateLoader();
1054
1041
  if (!template) throw new Error("Template not found");
1055
1042
  this.log.trace("Rendering page", { name: route.name });
1056
- const entry = {
1043
+ const state = {
1057
1044
  url,
1058
1045
  params,
1059
1046
  query,
1060
1047
  onError: () => null,
1061
1048
  layers: []
1062
1049
  };
1063
- const state = entry;
1064
1050
  if (this.alepha.has(ServerLinksProvider)) this.alepha.state.set("api", await this.alepha.inject(ServerLinksProvider).getUserApiLinks({
1065
1051
  user: serverRequest.user,
1066
1052
  authorization: serverRequest.headers.authorization
@@ -1143,8 +1129,7 @@ var ReactServerProvider = class {
1143
1129
  return response.html;
1144
1130
  }
1145
1131
  preprocessTemplate(template) {
1146
- const bodyCloseMatch = template.match(/<\/body>/i);
1147
- const bodyCloseIndex = bodyCloseMatch?.index ?? template.length;
1132
+ const bodyCloseIndex = template.match(/<\/body>/i)?.index ?? template.length;
1148
1133
  const beforeScript = template.substring(0, bodyCloseIndex);
1149
1134
  const afterScript = template.substring(bodyCloseIndex);
1150
1135
  const rootDivMatch = beforeScript.match(this.ROOT_DIV_REGEX);
@@ -1152,11 +1137,9 @@ var ReactServerProvider = class {
1152
1137
  const beforeDiv = beforeScript.substring(0, rootDivMatch.index);
1153
1138
  const afterDivStart = rootDivMatch.index + rootDivMatch[0].length;
1154
1139
  const afterDiv = beforeScript.substring(afterDivStart);
1155
- const beforeApp = `${beforeDiv}<div${rootDivMatch[1]} id="${this.env.REACT_ROOT_ID}"${rootDivMatch[2]}>`;
1156
- const afterApp = `</div>${afterDiv}`;
1157
1140
  return {
1158
- beforeApp,
1159
- afterApp,
1141
+ beforeApp: `${beforeDiv}<div${rootDivMatch[1]} id="${this.env.REACT_ROOT_ID}"${rootDivMatch[2]}>`,
1142
+ afterApp: `</div>${afterDiv}`,
1160
1143
  beforeScript: "",
1161
1144
  afterScript
1162
1145
  };
@@ -1165,11 +1148,9 @@ var ReactServerProvider = class {
1165
1148
  if (bodyMatch) {
1166
1149
  const beforeBody = beforeScript.substring(0, bodyMatch.index + bodyMatch[0].length);
1167
1150
  const afterBody = beforeScript.substring(bodyMatch.index + bodyMatch[0].length);
1168
- const beforeApp = `${beforeBody}<div id="${this.env.REACT_ROOT_ID}">`;
1169
- const afterApp = `</div>${afterBody}`;
1170
1151
  return {
1171
- beforeApp,
1172
- afterApp,
1152
+ beforeApp: `${beforeBody}<div id="${this.env.REACT_ROOT_ID}">`,
1153
+ afterApp: `</div>${afterBody}`,
1173
1154
  beforeScript: "",
1174
1155
  afterScript
1175
1156
  };
@@ -1207,7 +1188,7 @@ var ReactBrowserRouterProvider = class extends RouterProvider {
1207
1188
  });
1208
1189
  async transition(url, previous = [], meta = {}) {
1209
1190
  const { pathname, search } = url;
1210
- const entry = {
1191
+ const state = {
1211
1192
  url,
1212
1193
  query: {},
1213
1194
  params: {},
@@ -1215,7 +1196,6 @@ var ReactBrowserRouterProvider = class extends RouterProvider {
1215
1196
  onError: () => null,
1216
1197
  meta
1217
1198
  };
1218
- const state = entry;
1219
1199
  await this.alepha.events.emit("react:transition:begin", {
1220
1200
  previous: this.alepha.state.get("react.router.state"),
1221
1201
  state
@@ -1432,7 +1412,6 @@ var ReactRouter = class {
1432
1412
  }
1433
1413
  get browser() {
1434
1414
  if (this.alepha.isBrowser()) return this.alepha.inject(ReactBrowserProvider);
1435
- return void 0;
1436
1415
  }
1437
1416
  path(name, config = {}) {
1438
1417
  return this.pageApi.pathname(name, {
@@ -1565,14 +1544,14 @@ const Link = (props) => {
1565
1544
  children: props.children
1566
1545
  });
1567
1546
  };
1547
+ var Link_default = Link;
1568
1548
 
1569
1549
  //#endregion
1570
1550
  //#region src/hooks/useActive.ts
1571
1551
  const useActive = (args) => {
1572
1552
  const router = useRouter();
1573
1553
  const [isPending, setPending] = useState(false);
1574
- const state = useRouterState();
1575
- const current = state.url.pathname;
1554
+ const current = useRouterState().url.pathname;
1576
1555
  const options = typeof args === "string" ? { href: args } : {
1577
1556
  ...args,
1578
1557
  href: args.href
@@ -1656,8 +1635,7 @@ const useSchema = (action) => {
1656
1635
  const [schema, setSchema] = useState(ssrSchemaLoading(alepha, name));
1657
1636
  useEffect(() => {
1658
1637
  if (!schema.loading) return;
1659
- const opts = { cache: true };
1660
- httpClient.fetch(`${LinkProvider.path.apiLinks}/${name}/schema`, {}, opts).then((it) => setSchema(it.data));
1638
+ httpClient.fetch(`${LinkProvider.path.apiLinks}/${name}/schema`, {}, { cache: true }).then((it) => setSchema(it.data));
1661
1639
  }, [name]);
1662
1640
  return schema;
1663
1641
  };
@@ -1706,5 +1684,5 @@ const AlephaReact = $module({
1706
1684
  });
1707
1685
 
1708
1686
  //#endregion
1709
- export { $page, AlephaContext, AlephaReact, ClientOnly, ErrorBoundary, ErrorViewer, Link, NestedView_default as NestedView, NotFoundPage as NotFound, PageDescriptor, ReactBrowserProvider, ReactPageProvider, ReactRouter, ReactServerProvider, Redirection, RouterLayerContext, isPageRoute, ssrSchemaLoading, useActive, useAlepha, useClient, useInject, useQueryParams, useRouter, useRouterEvents, useRouterState, useSchema, useStore };
1687
+ export { $page, AlephaContext, AlephaReact, ClientOnly_default as ClientOnly, ErrorBoundary_default as ErrorBoundary, ErrorViewer_default as ErrorViewer, Link_default as Link, NestedView_default as NestedView, NotFoundPage as NotFound, PageDescriptor, ReactBrowserProvider, ReactPageProvider, ReactRouter, ReactServerProvider, Redirection, RouterLayerContext, isPageRoute, ssrSchemaLoading, useActive, useAlepha, useClient, useInject, useQueryParams, useRouter, useRouterEvents, useRouterState, useSchema, useStore };
1710
1688
  //# sourceMappingURL=index.js.map