@forgecart/cli 2.202610052143.0 → 2.202610060357.0

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 (125) hide show
  1. package/package.json +1 -1
  2. package/templates/storefront-shadcn/.forgecartignore +2 -0
  3. package/templates/storefront-shadcn/Procfile +1 -0
  4. package/templates/storefront-shadcn/README.md +229 -0
  5. package/templates/storefront-shadcn/SEO-MIGRATION.md +708 -0
  6. package/templates/storefront-shadcn/components.json +21 -0
  7. package/templates/storefront-shadcn/next.config.js +105 -0
  8. package/templates/storefront-shadcn/package.json +39 -0
  9. package/templates/storefront-shadcn/postcss.config.js +5 -0
  10. package/templates/storefront-shadcn/src/app/%5F%5Ffc/identify/route.ts +205 -0
  11. package/templates/storefront-shadcn/src/app/%5F%5Ffc/track/route.ts +189 -0
  12. package/templates/storefront-shadcn/src/app/%5F%5Fforge_beacon/route.ts +87 -0
  13. package/templates/storefront-shadcn/src/app/api/%5F%5Fbackend/methods/route.ts +27 -0
  14. package/templates/storefront-shadcn/src/app/cart/page.tsx +53 -0
  15. package/templates/storefront-shadcn/src/app/checkout/page.tsx +53 -0
  16. package/templates/storefront-shadcn/src/app/error.tsx +23 -0
  17. package/templates/storefront-shadcn/src/app/global-error.tsx +23 -0
  18. package/templates/storefront-shadcn/src/app/globals.css +156 -0
  19. package/templates/storefront-shadcn/src/app/layout.tsx +185 -0
  20. package/templates/storefront-shadcn/src/app/page.tsx +217 -0
  21. package/templates/storefront-shadcn/src/app/pages/[slug]/not-found.tsx +24 -0
  22. package/templates/storefront-shadcn/src/app/pages/[slug]/page.tsx +115 -0
  23. package/templates/storefront-shadcn/src/app/ping/route.ts +21 -0
  24. package/templates/storefront-shadcn/src/app/products/[slug]/not-found.tsx +18 -0
  25. package/templates/storefront-shadcn/src/app/products/[slug]/page.tsx +317 -0
  26. package/templates/storefront-shadcn/src/app/products/page.tsx +107 -0
  27. package/templates/storefront-shadcn/src/app/register/page.tsx +54 -0
  28. package/templates/storefront-shadcn/src/app/reset-password/page.tsx +60 -0
  29. package/templates/storefront-shadcn/src/app/robots.ts +69 -0
  30. package/templates/storefront-shadcn/src/app/sitemap.ts +106 -0
  31. package/templates/storefront-shadcn/src/app/verify/page.tsx +157 -0
  32. package/templates/storefront-shadcn/src/components/CartView.tsx +333 -0
  33. package/templates/storefront-shadcn/src/components/ForgeErrorBeacon.tsx +102 -0
  34. package/templates/storefront-shadcn/src/components/ForgeTracker.tsx +479 -0
  35. package/templates/storefront-shadcn/src/components/ForgecartDesigner.tsx +43 -0
  36. package/templates/storefront-shadcn/src/components/Header.tsx +73 -0
  37. package/templates/storefront-shadcn/src/components/LanguageSwitcher.tsx +96 -0
  38. package/templates/storefront-shadcn/src/components/LocaleLink.tsx +49 -0
  39. package/templates/storefront-shadcn/src/components/ProductCard.tsx +59 -0
  40. package/templates/storefront-shadcn/src/components/ProductPurchase.tsx +235 -0
  41. package/templates/storefront-shadcn/src/components/account/AccountMessage.tsx +63 -0
  42. package/templates/storefront-shadcn/src/components/account/RegisterForm.tsx +257 -0
  43. package/templates/storefront-shadcn/src/components/account/RequestPasswordResetForm.tsx +93 -0
  44. package/templates/storefront-shadcn/src/components/account/ResetPasswordForm.tsx +163 -0
  45. package/templates/storefront-shadcn/src/components/checkout/AddressStep.tsx +271 -0
  46. package/templates/storefront-shadcn/src/components/checkout/CheckoutFlow.tsx +551 -0
  47. package/templates/storefront-shadcn/src/components/checkout/CheckoutGate.tsx +55 -0
  48. package/templates/storefront-shadcn/src/components/checkout/PaymentElementForm.tsx +140 -0
  49. package/templates/storefront-shadcn/src/components/checkout/PaymentFormEmbed.tsx +89 -0
  50. package/templates/storefront-shadcn/src/components/checkout/RatesStep.tsx +115 -0
  51. package/templates/storefront-shadcn/src/components/ui/alert.tsx +75 -0
  52. package/templates/storefront-shadcn/src/components/ui/badge.tsx +40 -0
  53. package/templates/storefront-shadcn/src/components/ui/button.tsx +64 -0
  54. package/templates/storefront-shadcn/src/components/ui/card.tsx +28 -0
  55. package/templates/storefront-shadcn/src/components/ui/input.tsx +26 -0
  56. package/templates/storefront-shadcn/src/components/ui/label.tsx +22 -0
  57. package/templates/storefront-shadcn/src/components/ui/native-select.tsx +27 -0
  58. package/templates/storefront-shadcn/src/components/ui/skeleton.tsx +21 -0
  59. package/templates/storefront-shadcn/src/components/ui/utils.ts +16 -0
  60. package/templates/storefront-shadcn/src/instrumentation.ts +109 -0
  61. package/templates/storefront-shadcn/src/lib/account/account-link.ts +76 -0
  62. package/templates/storefront-shadcn/src/lib/account/register-state.ts +133 -0
  63. package/templates/storefront-shadcn/src/lib/account/reset-password-state.ts +111 -0
  64. package/templates/storefront-shadcn/src/lib/account/verify-state.ts +56 -0
  65. package/templates/storefront-shadcn/src/lib/account-actions.ts +76 -0
  66. package/templates/storefront-shadcn/src/lib/account-session.ts +47 -0
  67. package/templates/storefront-shadcn/src/lib/action-result.ts +30 -0
  68. package/templates/storefront-shadcn/src/lib/asset-alt.ts +34 -0
  69. package/templates/storefront-shadcn/src/lib/backend-actions.ts +20 -0
  70. package/templates/storefront-shadcn/src/lib/backend-client.ts +47 -0
  71. package/templates/storefront-shadcn/src/lib/cart-context.tsx +236 -0
  72. package/templates/storefront-shadcn/src/lib/checkout-session.ts +185 -0
  73. package/templates/storefront-shadcn/src/lib/content/page-metadata.ts +113 -0
  74. package/templates/storefront-shadcn/src/lib/content/render-fields.tsx +256 -0
  75. package/templates/storefront-shadcn/src/lib/content/resolve-page.ts +143 -0
  76. package/templates/storefront-shadcn/src/lib/error-messages.ts +24 -0
  77. package/templates/storefront-shadcn/src/lib/experiments.ts +333 -0
  78. package/templates/storefront-shadcn/src/lib/forgecart.ts +464 -0
  79. package/templates/storefront-shadcn/src/lib/format.ts +89 -0
  80. package/templates/storefront-shadcn/src/lib/identify-forward.ts +152 -0
  81. package/templates/storefront-shadcn/src/lib/locale/channel-locales-loader.ts +169 -0
  82. package/templates/storefront-shadcn/src/lib/locale/channel-locales-map.ts +46 -0
  83. package/templates/storefront-shadcn/src/lib/locale/channel-locales.ts +191 -0
  84. package/templates/storefront-shadcn/src/lib/locale/grammar.ts +194 -0
  85. package/templates/storefront-shadcn/src/lib/locale/localized-path.ts +55 -0
  86. package/templates/storefront-shadcn/src/lib/locale/middleware-plan.ts +107 -0
  87. package/templates/storefront-shadcn/src/lib/locale/request-binding.ts +80 -0
  88. package/templates/storefront-shadcn/src/lib/locale/request-locale.ts +66 -0
  89. package/templates/storefront-shadcn/src/lib/marketing-params.ts +213 -0
  90. package/templates/storefront-shadcn/src/lib/money.ts +50 -0
  91. package/templates/storefront-shadcn/src/lib/seo/alternates.ts +123 -0
  92. package/templates/storefront-shadcn/src/lib/seo/json-ld.ts +266 -0
  93. package/templates/storefront-shadcn/src/lib/seo/metadata.ts +419 -0
  94. package/templates/storefront-shadcn/src/lib/seo/noindex.ts +218 -0
  95. package/templates/storefront-shadcn/src/lib/seo/public-origin.ts +166 -0
  96. package/templates/storefront-shadcn/src/lib/seo/redirect-plan.ts +86 -0
  97. package/templates/storefront-shadcn/src/lib/seo/resolve-path.ts +107 -0
  98. package/templates/storefront-shadcn/src/lib/seo/scaffolded-routes.ts +83 -0
  99. package/templates/storefront-shadcn/src/lib/seo/sidecar.ts +75 -0
  100. package/templates/storefront-shadcn/src/lib/seo/site-verification.ts +98 -0
  101. package/templates/storefront-shadcn/src/lib/seo/sitemap-cache.ts +114 -0
  102. package/templates/storefront-shadcn/src/lib/seo/sitemap-entries.ts +321 -0
  103. package/templates/storefront-shadcn/src/lib/session-actions.ts +61 -0
  104. package/templates/storefront-shadcn/src/lib/session-cookies.ts +98 -0
  105. package/templates/storefront-shadcn/src/lib/shop-config.ts +51 -0
  106. package/templates/storefront-shadcn/src/lib/shop-session.ts +151 -0
  107. package/templates/storefront-shadcn/src/lib/track-forward.ts +200 -0
  108. package/templates/storefront-shadcn/src/lib/uuid.ts +19 -0
  109. package/templates/storefront-shadcn/src/middleware.ts +379 -0
  110. package/templates/storefront-shadcn/src/seo/redirects.ts +44 -0
  111. package/templates/storefront-shadcn/src/server/app.module.ts +18 -0
  112. package/templates/storefront-shadcn/src/server/backend-api.ts +26 -0
  113. package/templates/storefront-shadcn/src/server/backend-method.decorator.ts +23 -0
  114. package/templates/storefront-shadcn/src/server/bootstrap.ts +122 -0
  115. package/templates/storefront-shadcn/src/server/customer-extras/customer-extras.module.ts +13 -0
  116. package/templates/storefront-shadcn/src/server/customer-extras/service/customer-extras.service.ts +58 -0
  117. package/templates/storefront-shadcn/src/server/customer-extras/type/customer-extras.types.ts +11 -0
  118. package/templates/storefront-shadcn/src/server/forge/live-revision.ts +158 -0
  119. package/templates/storefront-shadcn/src/server/forgecart/forgecart-client.factory.ts +69 -0
  120. package/templates/storefront-shadcn/src/server/forgecart/forgecart.module.ts +9 -0
  121. package/templates/storefront-shadcn/src/server/runner.ts +90 -0
  122. package/templates/storefront-shadcn/src/server/types.ts +36 -0
  123. package/templates/storefront-shadcn/tsconfig.json +25 -0
  124. package/templates/storefront-shadcn-sdk-floor.json +1174 -0
  125. package/templates/template-set.json +10 -0
@@ -0,0 +1,109 @@
1
+ /**
2
+ * DEV-ONLY server-error beacon via Next's `onRequestError` instrumentation hook.
3
+ *
4
+ * This is the PRIMARY server-side crash-detection surface for the in-pod dev-server:
5
+ * when a Server Component, a server action, or a route handler THROWS while rendering
6
+ * a request under `next dev`, Next invokes `onRequestError` with the structured error
7
+ * + request + routing context. The hook POSTs that detail SERVER-side, straight to the
8
+ * in-pod workspace-manager loopback receiver at
9
+ * `http://127.0.0.1:<FORGE_BEACON_PORT>/__forge_beacon`, which folds it into the
10
+ * supervisor's runtime state so the shop's recovery brain can originate a fix for a
11
+ * crash the `/ping` liveness probe cannot see (the dev-server process stays up and
12
+ * `/ping` stays 200, but `/` 500s per request).
13
+ *
14
+ * This complements the browser beacon (`ForgeErrorBeacon`): that one catches
15
+ * CLIENT-component / hydration throws that surface only in the browser; this one
16
+ * catches SERVER render throws that surface only in the dev-server. Both POST the
17
+ * same `/__forge_beacon` shape — the only difference is `source` (`'browser'` vs
18
+ * `'server'`).
19
+ *
20
+ * Gated entirely on `NODE_ENV === 'development'`: a deployed `next start` storefront
21
+ * registers no beacon (the literal check is inlined by the bundler, so the body is
22
+ * dead-code-eliminated from a production build) and the loopback receiver does not
23
+ * exist outside the pod. Next compiles `instrumentation.ts` for BOTH the Node and Edge
24
+ * runtimes; the loopback POST is a Node-only concern, so the hook also gates on the
25
+ * Node runtime — the Edge bundle then carries an inert no-op.
26
+ *
27
+ * The hook NEVER throws out of itself: a failed delivery is swallowed (the beacon is a
28
+ * best-effort backstop, never a hard dependency of the request), so a flapping receiver
29
+ * can never mask or replace the underlying request error Next is already reporting.
30
+ *
31
+ * The error's own `code` rides along verbatim — a fact, never a verdict: a request
32
+ * that races a recompile evaluates Turbopack's stub for a module that no longer parses
33
+ * and throws `code: 'MODULE_UNPARSABLE'`, and the receiver reads that code to leave a
34
+ * BUILD failure to the build detectors instead of folding it as a render crash.
35
+ */
36
+
37
+ const BEACON_PORT = process.env.FORGE_BEACON_PORT ?? '3002';
38
+
39
+ /**
40
+ * Server-start hook: warm-boot the embedded NestJS backend (`src/server/`)
41
+ * so the first `sdk.backend` invocation doesn't pay the context creation.
42
+ *
43
+ * Warm-up ONLY — correctness never depends on it: the dispatch path's lazy
44
+ * `getBackend()` is what re-boots the context after dev-mode (HMR) edits,
45
+ * and a boot failure here is deliberately swallowed so a drifted api-map
46
+ * can never crash the server start — the first invocation (and the dev
47
+ * methods route) reports the same error as a typed envelope instead. The
48
+ * dynamic import keeps the Edge bundle inert (Nest is Node-only), and a
49
+ * no-env boot (image pre-warm) succeeds because clients are only built at
50
+ * invocation time.
51
+ */
52
+ export async function register(): Promise<void> {
53
+ // POSITIVE if-block, never an early return: webpack's parser dead-code-
54
+ // eliminates a statically-false `if` BODY (NEXT_RUNTIME is DefinePlugin-
55
+ // substituted per bundle), but it does NOT track reachability past a
56
+ // `return` — with the guard inverted, the EDGE instrumentation bundle
57
+ // (compiled because middleware.ts exists) chases this import into
58
+ // @nestjs/*'s node-builtin requires and `next build --webpack` dies with
59
+ // five Module-not-found errors ('stream', 'os', 'perf_hooks'; every
60
+ // publish-button CI run since this import landed). Turbopack prunes the
61
+ // dead branch either way — only the webpack publish build sees the
62
+ // difference.
63
+ if (process.env.NEXT_RUNTIME === 'nodejs') {
64
+ try {
65
+ const { getBackend } = await import('./server/bootstrap');
66
+ await getBackend();
67
+ } catch {
68
+ // Surfaced by the first sdk.backend invocation and /api/__backend/methods.
69
+ }
70
+ }
71
+ }
72
+
73
+ interface OnRequestErrorRequest {
74
+ path: string;
75
+ method: string;
76
+ headers: { [key: string]: string | string[] | undefined };
77
+ }
78
+
79
+ interface OnRequestErrorContext {
80
+ routerKind: string;
81
+ routePath: string;
82
+ routeType: string;
83
+ }
84
+
85
+ export async function onRequestError(
86
+ error: unknown,
87
+ request: OnRequestErrorRequest,
88
+ context: OnRequestErrorContext,
89
+ ): Promise<void> {
90
+ if (process.env.NODE_ENV !== 'development' || process.env.NEXT_RUNTIME !== 'nodejs') {
91
+ return;
92
+ }
93
+
94
+ const err = error as { message?: string; digest?: string; code?: unknown } | undefined;
95
+ const body = JSON.stringify({
96
+ message: typeof err?.message === 'string' ? err.message : String(error),
97
+ digest: typeof err?.digest === 'string' ? err.digest : undefined,
98
+ code: typeof err?.code === 'string' ? err.code : undefined,
99
+ route: request.path,
100
+ routeType: context.routeType,
101
+ source: 'server',
102
+ });
103
+
104
+ await fetch(`http://127.0.0.1:${BEACON_PORT}/__forge_beacon`, {
105
+ method: 'POST',
106
+ headers: { 'content-type': 'application/json' },
107
+ body,
108
+ }).catch(() => undefined);
109
+ }
@@ -0,0 +1,76 @@
1
+ import type { RouteSearchParams } from '../seo/noindex';
2
+
3
+ /**
4
+ * The anatomy of a ONE-SHOT account link (#1472).
5
+ *
6
+ * The platform mails a shopper two kinds of link and both have the same shape:
7
+ * this storefront's own origin, a fixed route, and a single-use token in the
8
+ * query — `<origin>/verify?token=…` and `<origin>/reset-password?token=…`. The
9
+ * `?token=` spelling is the CONTRACT between the mail templates and these
10
+ * pages (a merchant may have customized the mail body, so the parameter name
11
+ * cannot be changed on one side alone); the platform supplies the origin, this
12
+ * module reads what arrives.
13
+ *
14
+ * PURE BY CONTRACT: no Next imports, no `process.env`, no fetch, no I/O — the
15
+ * same constraint `lib/locale/grammar.ts` and `lib/seo/noindex.ts` carry, for
16
+ * the same reason. `tool/storefront-template-spec` imports this file directly
17
+ * by relative path from OUTSIDE the scaffold, so anything reachable from here
18
+ * must resolve without the template's Next toolchain. A rule that decides what
19
+ * a shopper sees after clicking a one-shot credential has to be a table of
20
+ * vectors rather than something only a running server can exercise. The
21
+ * `RouteSearchParams` import is type-only and names another pure module, so it
22
+ * erases and drags nothing in.
23
+ */
24
+
25
+ /**
26
+ * The query parameter every account link carries.
27
+ *
28
+ * Named once so the two pages and their vectors cannot disagree about the
29
+ * spelling the platform's mail templates emit.
30
+ */
31
+ export const ACCOUNT_TOKEN_PARAM = 'token';
32
+
33
+ /** What the shop API answered when a token was actually presented to it. */
34
+ export type AccountCallOutcome = { kind: 'ok' } | { kind: 'error'; code: string };
35
+
36
+ /**
37
+ * Everything a link render has to account for: the API's answer, or the fact
38
+ * that there was no usable token to ask about in the first place.
39
+ *
40
+ * `no-token` is not an error the API returned — it is a property of the URL —
41
+ * and keeping it in the same union is what lets ONE total function decide the
42
+ * whole page, instead of a page-level branch plus a function that covers only
43
+ * the half of the cases that reached the network.
44
+ */
45
+ export type AccountLinkOutcome = AccountCallOutcome | { kind: 'no-token' };
46
+
47
+ /**
48
+ * The single usable token in this URL, or null.
49
+ *
50
+ * Three ways to get null, each deliberate:
51
+ *
52
+ * - ABSENT — the page was opened without a link (a typed URL, or a mail
53
+ * client that dropped the query).
54
+ * - BLANK — `?token=` or `?token=%20`. Surrounding whitespace is trimmed
55
+ * because a mail client wrapping a long URL can deliver it padded and the
56
+ * platform's tokens never contain whitespace; a value that is nothing but
57
+ * padding is not a token.
58
+ * - REPEATED — `?token=a&token=b` arrives as an array, and is REFUSED rather
59
+ * than resolved to the first value. This is a one-shot credential: picking
60
+ * one of two candidates would SPEND a real token on a guess about which one
61
+ * the shopper meant, and the one that loses the guess is unrecoverable.
62
+ * Nothing legitimate produces a repeat, so the honest answer is that this
63
+ * URL carries no token the page may act on.
64
+ */
65
+ export function readLinkToken(searchParams: RouteSearchParams): string | null {
66
+ const raw = searchParams[ACCOUNT_TOKEN_PARAM];
67
+ if (typeof raw === 'string') return trimmedOrNull(raw);
68
+ if (!Array.isArray(raw)) return null;
69
+ if (raw.length !== 1) return null;
70
+ return trimmedOrNull(raw[0]);
71
+ }
72
+
73
+ function trimmedOrNull(value: string): string | null {
74
+ const trimmed = value.trim();
75
+ return trimmed === '' ? null : trimmed;
76
+ }
@@ -0,0 +1,133 @@
1
+ import type { AccountCallOutcome } from './account-link';
2
+
3
+ /**
4
+ * What `/register` renders, as a value (#1471).
5
+ *
6
+ * PURE BY CONTRACT, for the reasons `account-link.ts` states. The form owns the
7
+ * COPY for each state; this module owns which state a given outcome IS, so the
8
+ * decision is a table of vectors and the form is a render.
9
+ *
10
+ * Unlike `/verify` and `/reset-password`, this page is not reached with a
11
+ * credential — a shopper types into it, and may type into it twice. That is why
12
+ * the states below are not a success/failure pair: the same submit, repeated,
13
+ * is an ORDINARY thing to happen, and what it should render is a prompt rather
14
+ * than an error.
15
+ */
16
+
17
+ /**
18
+ * The states of the register page.
19
+ *
20
+ * - `form` — the initial render, and every render after a
21
+ * refusal the shopper can act on.
22
+ * - `verify-your-email` — the account exists and awaits verification. This is
23
+ * the ONE state a successful registration lands in:
24
+ * registering never signs anyone in (the account is
25
+ * minted unverified and a mail is dispatched), and a
26
+ * storefront that called `login` here was refused
27
+ * NOT_VERIFIED 287 ms later (prod, 2026-09-06). A
28
+ * repeat submit of the same address from the same
29
+ * session lands here too — that is what the shop
30
+ * API's session-scoped idempotency buys: one account,
31
+ * one prompt, no error on a double-clicked button.
32
+ * - `already-registered` — the address belongs to an account this session did
33
+ * not create. It carries the resend affordance, since
34
+ * a shopper who never confirmed their address cannot
35
+ * sign in and has nowhere else to go.
36
+ * - `signed-in-already` — the account is verified; nothing is pending.
37
+ * - `failed` — the shop API was unreachable, or answered a code
38
+ * this template does not model.
39
+ */
40
+ export type RegisterState =
41
+ | 'form'
42
+ | 'verify-your-email'
43
+ | 'already-registered'
44
+ | 'signed-in-already'
45
+ | 'failed';
46
+
47
+ /**
48
+ * The API's answer to a register submit, as a pure value.
49
+ *
50
+ * Wider than {@link AccountCallOutcome} by exactly one field, because this
51
+ * operation's success is not a bare yes: `verificationPending` is the shop API
52
+ * READING `user.verified` back, and it is the point of the #1471 contract —
53
+ * the two successful answers are two different screens.
54
+ */
55
+ export type RegisterOutcome =
56
+ | { kind: 'ok'; verificationPending: boolean }
57
+ | { kind: 'error'; code: string };
58
+
59
+ /**
60
+ * `ErrorCode` → state for `registerCustomerAccount`.
61
+ *
62
+ * Both refusals collapse into ONE state, and that is a correctness requirement
63
+ * rather than a simplification: `IDENTIFIER_ALREADY_EXISTS` is the login
64
+ * identity's refusal and `EMAIL_ADDRESS_CONFLICT` the customer row's, and which
65
+ * one a shopper receives is decided purely by TIMING — the duplicate-address
66
+ * pre-check runs before the identity claim, so a resubmit arriving after the
67
+ * winner's customer row committed gets the second code where an in-flight twin
68
+ * gets the first. Rendering them differently would show two screens for one
69
+ * situation.
70
+ *
71
+ * `NOT_VERIFIED` is deliberately absent. That is the refusal a LOGIN gets on an
72
+ * unverified account, and this page signs nobody in —
73
+ * `registerCustomerAccount` cannot answer with it, so an arm for it would be a
74
+ * branch no vector can reach.
75
+ *
76
+ * Everything absent lands in `failed`: an unrecognised code is a state this
77
+ * template has not been taught, and guessing a friendlier one would put words
78
+ * in the API's mouth.
79
+ */
80
+ const REGISTER_ERROR_STATES: Readonly<Record<string, RegisterState>> = {
81
+ IDENTIFIER_ALREADY_EXISTS: 'already-registered',
82
+ EMAIL_ADDRESS_CONFLICT: 'already-registered',
83
+ };
84
+
85
+ /**
86
+ * The state a submitted registration lands in. Total over the outcome union.
87
+ *
88
+ * The successful answer splits on `verificationPending` rather than assuming
89
+ * it: a fresh registration carrying a password is always pending, but an
90
+ * idempotent repeat can find the shopper verified in the meantime (they opened
91
+ * the link in another tab between the two submits), and sending them to their
92
+ * inbox for a mail they have already used is a dead end.
93
+ */
94
+ export function registerState(outcome: RegisterOutcome): RegisterState {
95
+ if (outcome.kind === 'ok') {
96
+ return outcome.verificationPending ? 'verify-your-email' : 'signed-in-already';
97
+ }
98
+ return REGISTER_ERROR_STATES[outcome.code] ?? 'failed';
99
+ }
100
+
101
+ /**
102
+ * The states a verification resend lands in (`idle` is pre-submit).
103
+ *
104
+ * TWO outcomes, and the missing third is the design. The shop API throttles
105
+ * this endpoint — it is public and it sends mail — but it throttles SILENTLY:
106
+ * inside the window it answers exactly what it answers outside it, and exactly
107
+ * what it answers for an address that has no account at all. A `throttled`
108
+ * state would therefore be an enumeration tell rather than a courtesy, because
109
+ * the only way this storefront could render one is if the API had told it that
110
+ * THIS address has a pending account waiting on a recently-sent mail — the one
111
+ * fact the neutral answer exists to withhold. A throttled resend renders as
112
+ * `sent`, which is also what is true for the shopper: the mail is in their
113
+ * inbox, from a minute ago.
114
+ *
115
+ * `sent` is neutral for the same reason `passwordResetRequestState`'s is, and
116
+ * `failed` means the same thing it does there: we could not ASK, which says
117
+ * nothing about any account and would otherwise be a sent-mail promise we know
118
+ * to be false.
119
+ */
120
+ export type VerificationResendState = 'idle' | 'sent' | 'failed';
121
+
122
+ /**
123
+ * The state a submitted resend lands in.
124
+ *
125
+ * Its own function rather than an alias of `passwordResetRequestState`: the two
126
+ * surfaces have different pre-submit members (`idle` here, `form` there), and
127
+ * aliasing them would make a change to one silently change the other — that
128
+ * these two answers have the same shape today is a fact about the platform's
129
+ * anti-enumeration posture, not a dependency between the pages.
130
+ */
131
+ export function verificationResendState(outcome: AccountCallOutcome): VerificationResendState {
132
+ return outcome.kind === 'ok' ? 'sent' : 'failed';
133
+ }
@@ -0,0 +1,111 @@
1
+ import type { AccountCallOutcome } from './account-link';
2
+
3
+ /**
4
+ * What `/reset-password` renders, as a value (#1472).
5
+ *
6
+ * One route, two modes — the URL's token decides which. WITH a token the page
7
+ * sets a new password; WITHOUT one it asks for an e-mail address and has a new
8
+ * link sent. Both modes start on their form and move to a terminal state when
9
+ * the API answers, so each mode gets its own union rather than one flat set
10
+ * that would let a request-mode render claim a reset-mode state.
11
+ *
12
+ * PURE BY CONTRACT, for the reasons `account-link.ts` states.
13
+ */
14
+
15
+ /** The states a SUBMITTED new password lands in (`form` is pre-submit). */
16
+ export type ResetPasswordSubmitState = 'done' | 'link-invalid' | 'password-rejected' | 'failed';
17
+
18
+ /**
19
+ * The set-a-new-password mode.
20
+ *
21
+ * - `form` — the initial render, and every render after a
22
+ * refusal the shopper can act on.
23
+ * - `done` — the password was changed.
24
+ * - `link-invalid` — the API refused the token: used, expired, or issued
25
+ * by another store. One state, same reasons as the
26
+ * verify page's.
27
+ * - `password-rejected` — the API refused the PASSWORD. The form stays up:
28
+ * the token is still good and the shopper only has to
29
+ * choose again.
30
+ * - `failed` — unreachable, or a code this template does not model.
31
+ */
32
+ export type ResetPasswordState = 'form' | ResetPasswordSubmitState;
33
+
34
+ /** The states a SUBMITTED reset request lands in (`form` is pre-submit). */
35
+ export type PasswordResetRequestSubmitState = 'sent' | 'failed';
36
+
37
+ /**
38
+ * The ask-for-a-link mode.
39
+ *
40
+ * `sent` is deliberately NEUTRAL and covers both of the API's successful
41
+ * answers: it reports whether an account matched, and rendering that difference
42
+ * would turn this form into an account-enumeration oracle — type an address,
43
+ * read the page, learn whether that person shops here. `failed` is reserved for
44
+ * the case where we could not ASK at all, which tells a visitor nothing about
45
+ * any account and would otherwise be a lie dressed as a sent mail.
46
+ */
47
+ export type PasswordResetRequestState = 'form' | PasswordResetRequestSubmitState;
48
+
49
+ /** Why a new-password form cannot be submitted yet. */
50
+ export type NewPasswordProblem = 'empty' | 'mismatch';
51
+
52
+ /**
53
+ * `ErrorCode` → state for `resetPassword`.
54
+ *
55
+ * `CUSTOMER_NOT_FOUND` is NOT the wrong-store case here, unlike on the verify
56
+ * page: consuming a reset token is scoped to the principal class of the
57
+ * requesting store, so another store's token simply does not resolve and
58
+ * arrives as `PASSWORD_RESET_TOKEN_INVALID`. Reaching `CUSTOMER_NOT_FOUND`
59
+ * means the token DID resolve inside this store and the customer row behind it
60
+ * is missing — a platform-side inconsistency, not something a shopper can act
61
+ * on — so it belongs in `failed` with the rest of the unmodelled codes.
62
+ */
63
+ const RESET_ERROR_STATES: Readonly<Record<string, ResetPasswordSubmitState>> = {
64
+ PASSWORD_RESET_TOKEN_INVALID: 'link-invalid',
65
+ PASSWORD_RESET_TOKEN_EXPIRED: 'link-invalid',
66
+ PASSWORD_VALIDATION_FAILED: 'password-rejected',
67
+ };
68
+
69
+ /** The state a submitted new password lands in. */
70
+ export function resetPasswordState(outcome: AccountCallOutcome): ResetPasswordSubmitState {
71
+ if (outcome.kind === 'ok') return 'done';
72
+ return RESET_ERROR_STATES[outcome.code] ?? 'failed';
73
+ }
74
+
75
+ /**
76
+ * The state a submitted reset REQUEST lands in.
77
+ *
78
+ * Every answer the API gives is `sent`; only a failure to reach it is anything
79
+ * else. See {@link PasswordResetRequestState} for why the successful answers
80
+ * are collapsed.
81
+ */
82
+ export function passwordResetRequestState(
83
+ outcome: AccountCallOutcome,
84
+ ): PasswordResetRequestSubmitState {
85
+ return outcome.kind === 'ok' ? 'sent' : 'failed';
86
+ }
87
+
88
+ /**
89
+ * Whether the two password fields may be submitted, and if not, why.
90
+ *
91
+ * A UX affordance, NOT a security boundary, and the distinction is the whole
92
+ * reason the confirmation never leaves the browser: the shop API is the only
93
+ * authority on whether a password is acceptable (it answers
94
+ * `PASSWORD_VALIDATION_FAILED`, which arrives here as `password-rejected`), and
95
+ * a Server Action is a public endpoint that a caller can reach without ever
96
+ * rendering this form. Checking the confirmation client-side saves a shopper a
97
+ * round-trip and a spent token; it decides nothing the server would not decide
98
+ * again.
99
+ *
100
+ * The password is compared UNTRIMMED. Leading and trailing spaces are legal
101
+ * characters in a password, so trimming here would silently accept two values
102
+ * the API will treat as different.
103
+ */
104
+ export function checkNewPassword(
105
+ password: string,
106
+ confirmation: string,
107
+ ): NewPasswordProblem | null {
108
+ if (password.length === 0) return 'empty';
109
+ if (password !== confirmation) return 'mismatch';
110
+ return null;
111
+ }
@@ -0,0 +1,56 @@
1
+ import type { AccountLinkOutcome } from './account-link';
2
+
3
+ /**
4
+ * What `/verify` renders, as a value (#1472).
5
+ *
6
+ * PURE BY CONTRACT, for the reasons `account-link.ts` states. The page owns the
7
+ * COPY for each state; this module owns which state a given outcome is, so the
8
+ * decision is a table of vectors and the page is a render.
9
+ */
10
+
11
+ /**
12
+ * The states of the e-mail verification page.
13
+ *
14
+ * - `no-token` — the URL carried no token this page may act on.
15
+ * - `verified` — the account is now verified.
16
+ * - `link-invalid` — the API refused the token: already used, expired, or
17
+ * never issued. They are ONE state on purpose — the page's
18
+ * advice is identical ("request a new mail") and telling an
19
+ * unauthenticated visitor whether a token they do not hold
20
+ * once existed is an oracle with no upside.
21
+ * - `wrong-store` — the token was valid, but the account it belongs to is not
22
+ * a customer of THIS store.
23
+ * - `failed` — anything else: the shop API was unreachable, or answered
24
+ * a code this template does not model.
25
+ */
26
+ export type VerifyState = 'no-token' | 'verified' | 'link-invalid' | 'wrong-store' | 'failed';
27
+
28
+ /**
29
+ * `ErrorCode` → state, for the codes `verifyCustomerAccount` can answer with.
30
+ *
31
+ * `CUSTOMER_NOT_FOUND` is the `wrong-store` case and the reason that state
32
+ * exists. Verification resolves the token GLOBALLY (the platform's auth method
33
+ * is keyed by the token alone), and only the customer lookup that follows is
34
+ * scoped to the channel this storefront serves — so a token issued by another
35
+ * store passes the first step and fails the second. That is exactly what a
36
+ * shopper holding an OLD mail hits: before #1472 every store's links pointed at
37
+ * one platform-global host, so a stale link can still arrive at the wrong
38
+ * storefront. "Something went wrong" would be true and useless; naming the
39
+ * cause is what lets the shopper act on it.
40
+ *
41
+ * Everything absent from this table lands in `failed` by design: an
42
+ * unrecognised code is a state this template has not been taught, and guessing
43
+ * at a friendlier one would put words in the API's mouth.
44
+ */
45
+ const VERIFY_ERROR_STATES: Readonly<Record<string, VerifyState>> = {
46
+ VERIFICATION_TOKEN_INVALID: 'link-invalid',
47
+ VERIFICATION_TOKEN_EXPIRED: 'link-invalid',
48
+ CUSTOMER_NOT_FOUND: 'wrong-store',
49
+ };
50
+
51
+ /** The state a verification render lands in. Total over the outcome union. */
52
+ export function verifyState(outcome: AccountLinkOutcome): VerifyState {
53
+ if (outcome.kind === 'no-token') return 'no-token';
54
+ if (outcome.kind === 'ok') return 'verified';
55
+ return VERIFY_ERROR_STATES[outcome.code] ?? 'failed';
56
+ }
@@ -0,0 +1,76 @@
1
+ 'use server';
2
+
3
+ import 'server-only';
4
+
5
+ import { verificationResendState, type VerificationResendState } from './account/register-state';
6
+ import {
7
+ passwordResetRequestState,
8
+ resetPasswordState,
9
+ type PasswordResetRequestSubmitState,
10
+ type ResetPasswordSubmitState,
11
+ } from './account/reset-password-state';
12
+ import { requestCustomerVerification, requestPasswordReset, resetPassword } from './forgecart';
13
+
14
+ /**
15
+ * The Server Actions behind the account forms (#1472, #1471).
16
+ *
17
+ * These are the places in this template where a SHOPPER's input reaches the
18
+ * shop API from the server rather than over their own websocket, and that
19
+ * placement is deliberate: all three operations are unauthenticated by design
20
+ * (whoever holds the token, or the address, may call them), so putting them on
21
+ * the session socket would open a session for someone who has not shopped yet
22
+ * and attach a cart identity to a password reset. They run on the anonymous
23
+ * channel singleton in `lib/forgecart.ts` instead.
24
+ *
25
+ * The register SUBMIT is the one account operation that does NOT belong here,
26
+ * for the mirror image of that reason: it is session-scoped, and the session
27
+ * has to be the shopper's own. It lives in `lib/account-session.ts`, which
28
+ * states that argument in full.
29
+ *
30
+ * Each action returns the page STATE, not the API's envelope: the mapping from
31
+ * `ErrorCode` to state lives once, in `lib/account/reset-password-state.ts`,
32
+ * and a string union crosses the RSC boundary as itself. The client form only
33
+ * has to render what it is given.
34
+ *
35
+ * A Server Action is a public HTTP endpoint — a caller can reach these without
36
+ * ever loading the form — which is exactly why neither of them validates
37
+ * anything the API validates: the token and the password are judged by the
38
+ * shop API, and the form's own confirmation check is a UX affordance that
39
+ * never leaves the browser (see `checkNewPassword`). Abuse budgets (how often
40
+ * an address may be mailed) belong to the platform, which owns the mail — and
41
+ * for the verification resend it enforces one silently, which is why no state
42
+ * here names it (see {@link VerificationResendState}).
43
+ */
44
+
45
+ /** Consume a reset token and set the new password. */
46
+ export async function submitPasswordReset(input: {
47
+ token: string;
48
+ password: string;
49
+ }): Promise<ResetPasswordSubmitState> {
50
+ return resetPasswordState(await resetPassword(input));
51
+ }
52
+
53
+ /**
54
+ * Ask for a reset link.
55
+ *
56
+ * Answers the same neutral state whether or not an account matched — see
57
+ * {@link passwordResetRequestState}.
58
+ */
59
+ export async function submitPasswordResetRequest(
60
+ emailAddress: string,
61
+ ): Promise<PasswordResetRequestSubmitState> {
62
+ return passwordResetRequestState(await requestPasswordReset(emailAddress));
63
+ }
64
+
65
+ /**
66
+ * Ask for the verification mail again (#1471).
67
+ *
68
+ * Answers the same neutral state whether or not the address has anything to
69
+ * verify, and whether or not the platform's throttle swallowed this particular
70
+ * request — see {@link VerificationResendState}.
71
+ */
72
+ export async function submitVerificationRequest(
73
+ emailAddress: string,
74
+ ): Promise<VerificationResendState> {
75
+ return verificationResendState(await requestCustomerVerification(emailAddress));
76
+ }
@@ -0,0 +1,47 @@
1
+ 'use client';
2
+
3
+ import type { RegisterCustomerInput } from '@forgecart/sdk/shop';
4
+
5
+ import type { RegisterOutcome } from './account/register-state';
6
+ import { runSessionOp } from './shop-session';
7
+
8
+ /**
9
+ * Registering, on the shopper's OWN websocket (#1471).
10
+ *
11
+ * Every other unauthenticated account call in this template is a Server Action
12
+ * running on the anonymous channel singleton (`lib/account-actions.ts`). This
13
+ * one is not, and the placement is the design rather than a preference:
14
+ *
15
+ * 1. CORRECTNESS. `lib/forgecart.ts` memoizes ONE client per language for the
16
+ * whole Next process, so every visitor's Server Action shares it — and
17
+ * therefore shares its shop session. The shop API's register idempotency is
18
+ * SESSION-scoped (a session holds one registration claim, which is what
19
+ * turns a double-clicked submit into one account and one prompt instead of
20
+ * a refusal), so running it on a shared session would answer one shopper
21
+ * with another shopper's pending state. The guarantee is only sound on a
22
+ * session that belongs to one person.
23
+ * 2. INTENT. Registering starts the shopper relationship, and the anonymous
24
+ * session this socket holds IS the cart identity that logging in later
25
+ * promotes — so the cart someone filled before registering follows them
26
+ * into their account.
27
+ *
28
+ * That is the opposite placement from `/verify` and `/reset-password`, and
29
+ * deliberately so: those act on a one-shot token out of a mail, where opening a
30
+ * session for a visitor who has not shopped yet would attach a cart identity to
31
+ * a password reset. The verification RESEND on this page is in that second
32
+ * category too — it needs nothing but an address — so it stays a Server Action.
33
+ */
34
+ export async function registerCustomerAccount(
35
+ input: RegisterCustomerInput,
36
+ ): Promise<RegisterOutcome> {
37
+ const result = await runSessionOp(
38
+ async (client) =>
39
+ (await client.customer.registerCustomerAccount({ input })).registerCustomerAccount,
40
+ );
41
+ // `runSessionOp` never throws — a refusal arrives as a typed code, which is
42
+ // the ordinary case here (the address may already be registered) and the form
43
+ // renders it. The envelope is flattened into the pure outcome union so the
44
+ // state table stays the only thing that reads an ErrorCode.
45
+ if (!result.ok) return { kind: 'error', code: result.error.code };
46
+ return { kind: 'ok', verificationPending: result.data.verificationPending };
47
+ }
@@ -0,0 +1,30 @@
1
+ import type { ExtractedError } from '@forgecart/sdk/shop';
2
+
3
+ /**
4
+ * Typed result envelope for every Server Action in this template.
5
+ *
6
+ * Server Actions NEVER throw across the RSC boundary: Next.js production
7
+ * redacts a thrown action error to an opaque digest, so the client would see
8
+ * a generic failure with no code to render. Instead each action catches,
9
+ * translates the failure with the SDK's `extractError` (the shop API's
10
+ * `{ code, variables, classification, message }` extensions — `message`
11
+ * arrives server-localized), and returns it in-band. Callers switch on
12
+ * `ok`; error UIs render `error.message` and key field targeting off
13
+ * `error.code`.
14
+ *
15
+ * The `import type` above erases, so this module is safe for client
16
+ * components — it is the one shape both sides of the action boundary share.
17
+ */
18
+ export type ActionResult<T> = { ok: true; data: T } | { ok: false; error: ExtractedError };
19
+
20
+ /**
21
+ * Fallback envelope error for failures that carry no GraphQL extensions
22
+ * (network refused, DNS, a non-GraphQL throw). `extractError` returns `null`
23
+ * for those; actions substitute this so the client always receives a code it
24
+ * can key on.
25
+ */
26
+ export const UNREACHABLE_ERROR: ExtractedError = {
27
+ code: 'SHOP_API_UNREACHABLE',
28
+ variables: {},
29
+ classification: 'INTERNAL_ERROR',
30
+ };