@analogjs/router 3.0.0-alpha.91 → 3.0.0-alpha.93

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.
Files changed (47) hide show
  1. package/fesm2022/analogjs-router-i18n.mjs +4 -0
  2. package/fesm2022/analogjs-router-i18n.mjs.map +1 -1
  3. package/fesm2022/analogjs-router-server-actions.mjs +416 -2
  4. package/fesm2022/analogjs-router-server-actions.mjs.map +1 -0
  5. package/fesm2022/analogjs-router-server.mjs +588 -319
  6. package/fesm2022/analogjs-router-server.mjs.map +1 -1
  7. package/fesm2022/analogjs-router-tanstack-query-server.mjs +4 -4
  8. package/fesm2022/analogjs-router-tanstack-query-server.mjs.map +1 -1
  9. package/fesm2022/analogjs-router-tanstack-query.mjs +70 -2
  10. package/fesm2022/analogjs-router-tanstack-query.mjs.map +1 -1
  11. package/fesm2022/analogjs-router-tokens.mjs.map +1 -1
  12. package/fesm2022/analogjs-router.mjs +147 -80
  13. package/fesm2022/analogjs-router.mjs.map +1 -1
  14. package/fesm2022/debug.page.mjs +3 -3
  15. package/fesm2022/route-files.mjs +1 -1
  16. package/package.json +11 -7
  17. package/types/server/actions/src/actions.d.ts +1 -1
  18. package/types/server/actions/src/define-action.d.ts +8 -15
  19. package/types/server/actions/src/define-api-route.d.ts +3 -4
  20. package/types/server/actions/src/define-page-load.d.ts +9 -10
  21. package/types/server/actions/src/define-server-route.d.ts +2 -2
  22. package/types/server/actions/src/index.d.ts +3 -2
  23. package/types/server/actions/src/parse-request-data.d.ts +3 -4
  24. package/types/server/actions/src/standard-schema.d.ts +39 -0
  25. package/types/server/actions/src/validate.d.ts +1 -1
  26. package/types/server/src/defer-reconcile-runtime.d.ts +1 -1
  27. package/types/server/src/server-fn/dispatch.d.ts +1 -1
  28. package/types/server/src/server-fn/event-handler.d.ts +1 -1
  29. package/types/server/src/server-fn/node-context.d.ts +3 -2
  30. package/types/server/src/ssr-navigation.d.ts +6 -0
  31. package/types/server/src/utils/ssr-stream-lifecycle.d.ts +14 -0
  32. package/types/server/src/utils/stream-request.d.ts +2 -1
  33. package/types/server/src/utils/stream-shell.d.ts +3 -0
  34. package/types/src/index.d.ts +2 -4
  35. package/types/src/lib/form-action.directive.d.ts +11 -9
  36. package/types/src/lib/route-types.d.ts +1 -1
  37. package/types/src/lib/server-fn/inject-server-fn.d.ts +1 -1
  38. package/types/tanstack-query/server/src/define-page-load-queries.d.ts +5 -7
  39. package/types/tanstack-query/server/src/index.d.ts +1 -1
  40. package/types/tanstack-query/server/src/provide-server-analog-query.d.ts +2 -0
  41. package/types/tanstack-query/src/index.d.ts +3 -2
  42. package/types/tanstack-query/src/server-query.d.ts +1 -1
  43. package/types/tokens/src/index.d.ts +8 -0
  44. package/fesm2022/provide-analog-query.mjs +0 -72
  45. package/fesm2022/provide-analog-query.mjs.map +0 -1
  46. package/fesm2022/src.mjs +0 -335
  47. package/fesm2022/src.mjs.map +0 -1
@@ -1,14 +1,14 @@
1
1
  import { API_PREFIX, injectAPIPrefix, injectBaseURL, injectInternalServerFetch, injectRequest } from "./analogjs-router-tokens.mjs";
2
- import { a as updateMetaTagsOnRouteChange, i as injectRouteEndpointURL, n as ANALOG_ROUTE_FILES, o as updateJsonLdOnRouteChange, r as createRoutes$1, t as ANALOG_EXTRA_ROUTE_FILE_SOURCES } from "./route-files.mjs";
2
+ import { a as ANALOG_META_KEY, i as injectRouteEndpointURL, n as ANALOG_ROUTE_FILES, o as updateMetaTagsOnRouteChange, r as createRoutes$1, s as updateJsonLdOnRouteChange, t as ANALOG_EXTRA_ROUTE_FILE_SOURCES } from "./route-files.mjs";
3
3
  import { n as createRoutes, r as routes, t as injectDebugRoutes } from "./routes.mjs";
4
4
  import * as i1 from "@angular/router";
5
5
  import { ActivatedRoute, ROUTES, Router, RouterLink, provideRouter } from "@angular/router";
6
6
  import * as i0 from "@angular/core";
7
- import { DOCUMENT, Directive, Injectable, InjectionToken, Injector, PLATFORM_ID, TransferState, assertInInjectionContext, inject, input, makeEnvironmentProviders, makeStateKey, output, provideAppInitializer, resource, signal } from "@angular/core";
7
+ import { DOCUMENT, Directive, ElementRef, Injectable, InjectionToken, Injector, PLATFORM_ID, Renderer2, TransferState, assertInInjectionContext, booleanAttribute, effect, inject, input, makeEnvironmentProviders, makeStateKey, output, provideAppInitializer, resource, runInInjectionContext, signal } from "@angular/core";
8
8
  import { HttpClient, HttpHeaders, HttpResponse, ɵHTTP_ROOT_INTERCEPTOR_FNS } from "@angular/common/http";
9
9
  import { combineLatest, firstValueFrom, from, map, of } from "rxjs";
10
10
  import { Meta } from "@angular/platform-browser";
11
- import { isPlatformServer } from "@angular/common";
11
+ import { isPlatformBrowser, isPlatformServer } from "@angular/common";
12
12
  import { toSignal } from "@angular/core/rxjs-interop";
13
13
  //#region packages/router/src/lib/define-route.ts
14
14
  /**
@@ -370,6 +370,7 @@ function requestContextInterceptor(req, next) {
370
370
  const apiPrefix = injectAPIPrefix();
371
371
  const baseUrl = injectBaseURL();
372
372
  const transferState = inject(TransferState);
373
+ const isBrowser = isPlatformBrowser(inject(PLATFORM_ID));
373
374
  const nitroGlobal = globalThis;
374
375
  const serverFetch = injectInternalServerFetch() ?? nitroGlobal.$fetch;
375
376
  if (serverFetch && baseUrl && (req.url.startsWith("/") || req.url.startsWith(baseUrl) || req.url.startsWith(`/${apiPrefix}`))) {
@@ -403,7 +404,7 @@ function requestContextInterceptor(req, next) {
403
404
  return transferResponse;
404
405
  }));
405
406
  }
406
- if (req.url.startsWith("/") || req.url.includes("/_analog/")) {
407
+ if (isBrowser && (req.url.startsWith("/") || req.url.includes("/_analog/"))) {
407
408
  const toAbsoluteUrl = (url) => new URL(url, window.location.origin);
408
409
  const requestUrl = toAbsoluteUrl(req.url).href;
409
410
  const { pathname, search } = toAbsoluteUrl(req.urlWithParams);
@@ -426,41 +427,73 @@ function requestContextInterceptor(req, next) {
426
427
  var FormAction = class FormAction {
427
428
  constructor() {
428
429
  this.action = input("", ...[]);
430
+ this.enhanceForm = input(false, { transform: booleanAttribute });
429
431
  this.onSuccess = output();
430
432
  this.onError = output();
431
433
  this.state = output();
434
+ this.currentState = signal("idle", ...[]);
435
+ this.injector = inject(Injector);
432
436
  this.router = inject(Router);
433
437
  this.route = inject(ActivatedRoute);
434
- this.currentState = signal("idle", ...[]);
435
- /** Cached during construction (injection context) so inject() works. */
436
- this._endpointUrl = this.route ? injectRouteEndpointURL(this.route.snapshot) : void 0;
438
+ this.path = this._getPath();
439
+ const form = inject(ElementRef).nativeElement;
440
+ const renderer = inject(Renderer2);
441
+ effect((onCleanup) => {
442
+ if (!this.enhanceForm()) return;
443
+ const previous = {
444
+ "data-state": form.getAttribute("data-state"),
445
+ "aria-busy": form.getAttribute("aria-busy")
446
+ };
447
+ const state = this.currentState();
448
+ renderer.setAttribute(form, "data-state", state);
449
+ if (state === "submitting") renderer.setAttribute(form, "aria-busy", "true");
450
+ else renderer.removeAttribute(form, "aria-busy");
451
+ onCleanup(() => {
452
+ for (const [name, value] of Object.entries(previous)) if (value === null) renderer.removeAttribute(form, name);
453
+ else renderer.setAttribute(form, name, value);
454
+ });
455
+ });
437
456
  }
438
457
  submitted($event) {
439
458
  $event.preventDefault();
440
- const form = $event.target;
441
459
  this._emitState("submitting");
442
- const body = new FormData(form);
443
- if (form.method.toUpperCase() === "GET") this._handleGet(body, this._getGetPath(form));
444
- else this._handlePost(body, this._getPostPath(form), form.method);
460
+ const body = new FormData($event.target);
461
+ if (this.enhanceForm()) {
462
+ const form = $event.target;
463
+ try {
464
+ const action = this.action().trim() || form.getAttribute("action")?.trim();
465
+ if (form.method.toUpperCase() === "GET") this._handleEnhancedGet(body, action || this.router.url);
466
+ else {
467
+ const path = action ? new URL(action, window.location.href).toString() : this._getPath();
468
+ this._handleEnhancedPost(body, path, form.method);
469
+ }
470
+ } catch {
471
+ this._emitState("error");
472
+ }
473
+ return;
474
+ }
475
+ if ($event.target.method.toUpperCase() === "GET") this._handleGet(body, this.router.url);
476
+ else this._handlePost(body, this.path, $event);
445
477
  }
446
478
  _handleGet(body, path) {
447
- const url = new URL(path, window.location.href);
448
- const params = new URLSearchParams(url.search);
449
- body.forEach((value, key) => {
450
- params.append(key, value instanceof File ? value.name : value);
451
- });
452
- url.search = params.toString();
479
+ const params = {};
480
+ body.forEach((formVal, formKey) => params[formKey] = formVal);
453
481
  this._emitState("navigate");
454
- this._navigateTo(url);
482
+ const url = path.split("?")[0];
483
+ this.router.navigate([url], {
484
+ queryParams: params,
485
+ onSameUrlNavigation: "reload"
486
+ });
455
487
  }
456
- _handlePost(body, path, method) {
488
+ _handlePost(body, path, $event) {
457
489
  fetch(path, {
458
- method,
490
+ method: $event.target.method,
459
491
  body
460
492
  }).then((res) => {
461
493
  if (res.ok) if (res.redirected) {
494
+ const redirectUrl = new URL(res.url).pathname;
462
495
  this._emitState("redirect");
463
- this._navigateTo(new URL(res.url, window.location.href));
496
+ this.router.navigate([redirectUrl]);
464
497
  } else if (this._isJSON(res.headers.get("Content-type"))) res.json().then((result) => {
465
498
  this.onSuccess.emit(result);
466
499
  this._emitState("success");
@@ -478,17 +511,43 @@ var FormAction = class FormAction {
478
511
  this._emitState("error");
479
512
  });
480
513
  }
481
- _getExplicitAction(form) {
482
- return this.action().trim() || form.getAttribute("action")?.trim() || void 0;
514
+ _getPath() {
515
+ const snapshot = this.route.snapshot;
516
+ if (snapshot.routeConfig && ANALOG_META_KEY in snapshot.routeConfig) return runInInjectionContext(this.injector, () => injectRouteEndpointURL(snapshot)).pathname;
517
+ return `/api/_analog/pages${window.location.pathname}`;
483
518
  }
484
- _getGetPath(form) {
485
- return this._getExplicitAction(form) ?? this.router.url;
519
+ _handleEnhancedGet(body, path) {
520
+ const url = new URL(path, window.location.href);
521
+ const params = new URLSearchParams(url.search);
522
+ body.forEach((_value, key) => params.delete(key));
523
+ body.forEach((value, key) => {
524
+ params.append(key, value instanceof File ? value.name : value);
525
+ });
526
+ url.search = params.toString();
527
+ this._emitState("navigate");
528
+ this._navigateTo(url);
486
529
  }
487
- _getPostPath(form) {
488
- const explicitAction = this._getExplicitAction(form);
489
- if (explicitAction) return new URL(explicitAction, window.location.href).toString();
490
- if (this._endpointUrl) return this._endpointUrl.pathname;
491
- return `/api/_analog/pages${window.location.pathname}`;
530
+ async _handleEnhancedPost(body, path, method) {
531
+ try {
532
+ const response = await fetch(path, {
533
+ method,
534
+ body
535
+ });
536
+ if (response.ok) if (response.redirected) {
537
+ this._emitState("redirect");
538
+ this._navigateTo(new URL(response.url, window.location.href));
539
+ } else {
540
+ const result = this._isJSON(response.headers.get("Content-type")) ? await response.json() : await response.text();
541
+ this.onSuccess.emit(result);
542
+ this._emitState("success");
543
+ }
544
+ else {
545
+ if (response.headers.get("X-Analog-Errors")) this.onError.emit(await response.json());
546
+ this._emitState("error");
547
+ }
548
+ } catch {
549
+ this._emitState("error");
550
+ }
492
551
  }
493
552
  _emitState(state) {
494
553
  this.currentState.set(state);
@@ -507,7 +566,7 @@ var FormAction = class FormAction {
507
566
  static {
508
567
  this.ɵfac = i0.ɵɵngDeclareFactory({
509
568
  minVersion: "12.0.0",
510
- version: "22.1.5",
569
+ version: "22.2.0",
511
570
  ngImport: i0,
512
571
  type: FormAction,
513
572
  deps: [],
@@ -517,50 +576,50 @@ var FormAction = class FormAction {
517
576
  static {
518
577
  this.ɵdir = i0.ɵɵngDeclareDirective({
519
578
  minVersion: "17.1.0",
520
- version: "22.1.5",
579
+ version: "22.2.0",
521
580
  type: FormAction,
522
581
  isStandalone: true,
523
- selector: "form[action],form[method]",
524
- inputs: { action: {
525
- classPropertyName: "action",
526
- publicName: "action",
527
- isSignal: true,
528
- isRequired: false,
529
- transformFunction: null
530
- } },
582
+ selector: "form[action],form[method],form[enhanceForm]",
583
+ inputs: {
584
+ action: {
585
+ classPropertyName: "action",
586
+ publicName: "action",
587
+ isSignal: true,
588
+ isRequired: false,
589
+ transformFunction: null
590
+ },
591
+ enhanceForm: {
592
+ classPropertyName: "enhanceForm",
593
+ publicName: "enhanceForm",
594
+ isSignal: true,
595
+ isRequired: false,
596
+ transformFunction: null
597
+ }
598
+ },
531
599
  outputs: {
532
600
  onSuccess: "onSuccess",
533
601
  onError: "onError",
534
602
  state: "state"
535
603
  },
536
- host: {
537
- listeners: { "submit": "submitted($event)" },
538
- properties: {
539
- "attr.data-state": "currentState()",
540
- "attr.aria-busy": "currentState() === \"submitting\" ? \"true\" : null"
541
- }
542
- },
604
+ host: { listeners: { "submit": "submitted($event)" } },
543
605
  ngImport: i0
544
606
  });
545
607
  }
546
608
  };
547
609
  i0.ɵɵngDeclareClassMetadata({
548
610
  minVersion: "12.0.0",
549
- version: "22.1.5",
611
+ version: "22.2.0",
550
612
  ngImport: i0,
551
613
  type: FormAction,
552
614
  decorators: [{
553
615
  type: Directive,
554
616
  args: [{
555
- selector: "form[action],form[method]",
556
- host: {
557
- "(submit)": `submitted($event)`,
558
- "[attr.data-state]": "currentState()",
559
- "[attr.aria-busy]": "currentState() === \"submitting\" ? \"true\" : null"
560
- },
617
+ selector: "form[action],form[method],form[enhanceForm]",
618
+ host: { "(submit)": `submitted($event)` },
561
619
  standalone: true
562
620
  }]
563
621
  }],
622
+ ctorParameters: () => [],
564
623
  propDecorators: {
565
624
  action: [{
566
625
  type: i0.Input,
@@ -570,6 +629,14 @@ i0.ɵɵngDeclareClassMetadata({
570
629
  required: false
571
630
  }]
572
631
  }],
632
+ enhanceForm: [{
633
+ type: i0.Input,
634
+ args: [{
635
+ isSignal: true,
636
+ alias: "enhanceForm",
637
+ required: false
638
+ }]
639
+ }],
573
640
  onSuccess: [{
574
641
  type: i0.Output,
575
642
  args: ["onSuccess"]
@@ -585,6 +652,26 @@ i0.ɵɵngDeclareClassMetadata({
585
652
  }
586
653
  });
587
654
  //#endregion
655
+ //#region packages/router/src/lib/validation-errors.ts
656
+ function getPathSegmentKey(segment) {
657
+ return typeof segment === "object" ? segment.key : segment;
658
+ }
659
+ function issuePathToFieldName(path) {
660
+ return path.map((segment) => String(getPathSegmentKey(segment))).join(".");
661
+ }
662
+ function issuesToFieldErrors(issues) {
663
+ return issues.reduce((errors, issue) => {
664
+ if (!issue.path?.length) return errors;
665
+ const fieldName = issuePathToFieldName(issue.path);
666
+ errors[fieldName] ??= [];
667
+ errors[fieldName].push(issue.message);
668
+ return errors;
669
+ }, Object.create(null));
670
+ }
671
+ function issuesToFormErrors(issues) {
672
+ return issues.filter((issue) => !issue.path?.length).map((issue) => issue.message);
673
+ }
674
+ //#endregion
588
675
  //#region packages/router/src/lib/debug/index.ts
589
676
  /**
590
677
  * Provides routes that provide additional
@@ -605,26 +692,6 @@ function withDebugRoutes() {
605
692
  };
606
693
  }
607
694
  //#endregion
608
- //#region packages/router/src/lib/validation-errors.ts
609
- function getPathSegmentKey(segment) {
610
- return typeof segment === "object" ? segment.key : segment;
611
- }
612
- function issuePathToFieldName(path) {
613
- return path.map((segment) => String(getPathSegmentKey(segment))).join(".");
614
- }
615
- function issuesToFieldErrors(issues) {
616
- return issues.reduce((errors, issue) => {
617
- if (!issue.path?.length) return errors;
618
- const fieldName = issuePathToFieldName(issue.path);
619
- errors[fieldName] ??= [];
620
- errors[fieldName].push(issue.message);
621
- return errors;
622
- }, {});
623
- }
624
- function issuesToFormErrors(issues) {
625
- return issues.filter((issue) => !issue.path?.length).map((issue) => issue.message);
626
- }
627
- //#endregion
628
695
  //#region packages/router/src/lib/server-fn/dispatcher.ts
629
696
  var SERVER_FN_DISPATCHER = new InjectionToken("@analogjs/router Server Function Dispatcher");
630
697
  //#endregion
@@ -669,7 +736,7 @@ var ServerFnClient = class ServerFnClient {
669
736
  static {
670
737
  this.ɵfac = i0.ɵɵngDeclareFactory({
671
738
  minVersion: "12.0.0",
672
- version: "22.1.5",
739
+ version: "22.2.0",
673
740
  ngImport: i0,
674
741
  type: ServerFnClient,
675
742
  deps: [],
@@ -679,7 +746,7 @@ var ServerFnClient = class ServerFnClient {
679
746
  static {
680
747
  this.ɵprov = i0.ɵɵngDeclareInjectable({
681
748
  minVersion: "12.0.0",
682
- version: "22.1.5",
749
+ version: "22.2.0",
683
750
  ngImport: i0,
684
751
  type: ServerFnClient,
685
752
  providedIn: "root"
@@ -688,7 +755,7 @@ var ServerFnClient = class ServerFnClient {
688
755
  };
689
756
  i0.ɵɵngDeclareClassMetadata({
690
757
  minVersion: "12.0.0",
691
- version: "22.1.5",
758
+ version: "22.2.0",
692
759
  ngImport: i0,
693
760
  type: ServerFnClient,
694
761
  decorators: [{
@@ -962,7 +1029,7 @@ var LinkTo = class LinkTo {
962
1029
  static {
963
1030
  this.ɵfac = i0.ɵɵngDeclareFactory({
964
1031
  minVersion: "12.0.0",
965
- version: "22.1.5",
1032
+ version: "22.2.0",
966
1033
  ngImport: i0,
967
1034
  type: LinkTo,
968
1035
  deps: [],
@@ -972,7 +1039,7 @@ var LinkTo = class LinkTo {
972
1039
  static {
973
1040
  this.ɵdir = i0.ɵɵngDeclareDirective({
974
1041
  minVersion: "17.1.0",
975
- version: "22.1.5",
1042
+ version: "22.2.0",
976
1043
  type: LinkTo,
977
1044
  isStandalone: true,
978
1045
  selector: "[linkTo]",
@@ -1016,7 +1083,7 @@ var LinkTo = class LinkTo {
1016
1083
  };
1017
1084
  i0.ɵɵngDeclareClassMetadata({
1018
1085
  minVersion: "12.0.0",
1019
- version: "22.1.5",
1086
+ version: "22.2.0",
1020
1087
  ngImport: i0,
1021
1088
  type: LinkTo,
1022
1089
  decorators: [{