@eifi1/ui-kit 0.28.1 → 0.30.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 (228) hide show
  1. package/README.md +44 -4
  2. package/dist/account/account-parts.d.ts +21 -0
  3. package/dist/account/account-parts.js +40 -0
  4. package/dist/account/account-parts.js.map +1 -0
  5. package/dist/account/data-export-setting.d.ts +73 -0
  6. package/dist/account/data-export-setting.js +113 -0
  7. package/dist/account/data-export-setting.js.map +1 -0
  8. package/dist/account/delete-account-setting.d.ts +91 -0
  9. package/dist/account/delete-account-setting.js +199 -0
  10. package/dist/account/delete-account-setting.js.map +1 -0
  11. package/dist/account/email-change-setting.d.ts +136 -0
  12. package/dist/account/email-change-setting.js +277 -0
  13. package/dist/account/email-change-setting.js.map +1 -0
  14. package/dist/account/sessions-setting.d.ts +110 -0
  15. package/dist/account/sessions-setting.js +146 -0
  16. package/dist/account/sessions-setting.js.map +1 -0
  17. package/dist/admin/admin-action-confirm.d.ts +91 -0
  18. package/dist/admin/admin-action-confirm.js +276 -0
  19. package/dist/admin/admin-action-confirm.js.map +1 -0
  20. package/dist/admin/admin-action-log.d.ts +105 -0
  21. package/dist/admin/admin-action-log.js +153 -0
  22. package/dist/admin/admin-action-log.js.map +1 -0
  23. package/dist/admin/admin-parts.d.ts +40 -0
  24. package/dist/admin/admin-parts.js +45 -0
  25. package/dist/admin/admin-parts.js.map +1 -0
  26. package/dist/admin/invitations-panel.d.ts +91 -0
  27. package/dist/admin/invitations-panel.js +289 -0
  28. package/dist/admin/invitations-panel.js.map +1 -0
  29. package/dist/admin/reviewer-scope-editor.d.ts +91 -0
  30. package/dist/admin/reviewer-scope-editor.js +134 -0
  31. package/dist/admin/reviewer-scope-editor.js.map +1 -0
  32. package/dist/admin/role-select.d.ts +91 -0
  33. package/dist/admin/role-select.js +128 -0
  34. package/dist/admin/role-select.js.map +1 -0
  35. package/dist/admin/transfer-ownership-dialog.d.ts +91 -0
  36. package/dist/admin/transfer-ownership-dialog.js +83 -0
  37. package/dist/admin/transfer-ownership-dialog.js.map +1 -0
  38. package/dist/admin/user-roster.d.ts +91 -0
  39. package/dist/admin/user-roster.js +285 -0
  40. package/dist/admin/user-roster.js.map +1 -0
  41. package/dist/auth/accept-invitation.d.ts +90 -0
  42. package/dist/auth/accept-invitation.js +117 -0
  43. package/dist/auth/accept-invitation.js.map +1 -0
  44. package/dist/auth/auth-errors.d.ts +139 -0
  45. package/dist/auth/auth-errors.js +92 -0
  46. package/dist/auth/auth-errors.js.map +1 -0
  47. package/dist/auth/complete-name-dialog.d.ts +71 -0
  48. package/dist/auth/complete-name-dialog.js +168 -0
  49. package/dist/auth/complete-name-dialog.js.map +1 -0
  50. package/dist/auth/email-tag.d.ts +60 -0
  51. package/dist/auth/email-tag.js +23 -0
  52. package/dist/auth/email-tag.js.map +1 -0
  53. package/dist/auth/forgot-password-form.d.ts +86 -0
  54. package/dist/auth/forgot-password-form.js +123 -0
  55. package/dist/auth/forgot-password-form.js.map +1 -0
  56. package/dist/auth/form-rules.d.ts +27 -0
  57. package/dist/auth/form-rules.js +23 -0
  58. package/dist/auth/form-rules.js.map +1 -0
  59. package/dist/auth/new-password-fields.d.ts +38 -0
  60. package/dist/auth/new-password-fields.js +54 -0
  61. package/dist/auth/new-password-fields.js.map +1 -0
  62. package/dist/auth/not-found-page.d.ts +57 -0
  63. package/dist/auth/not-found-page.js +40 -0
  64. package/dist/auth/not-found-page.js.map +1 -0
  65. package/dist/auth/register-form.d.ts +91 -0
  66. package/dist/auth/register-form.js +270 -0
  67. package/dist/auth/register-form.js.map +1 -0
  68. package/dist/auth/reset-password-form.d.ts +131 -0
  69. package/dist/auth/reset-password-form.js +167 -0
  70. package/dist/auth/reset-password-form.js.map +1 -0
  71. package/dist/auth/sign-in-form.d.ts +295 -0
  72. package/dist/auth/sign-in-form.js +426 -0
  73. package/dist/auth/sign-in-form.js.map +1 -0
  74. package/dist/auth/status-parts.d.ts +78 -0
  75. package/dist/auth/status-parts.js +83 -0
  76. package/dist/auth/status-parts.js.map +1 -0
  77. package/dist/auth/verify-email.d.ts +126 -0
  78. package/dist/auth/verify-email.js +243 -0
  79. package/dist/auth/verify-email.js.map +1 -0
  80. package/dist/chart.d.ts +25 -5
  81. package/dist/components/account-chips.d.ts +47 -7
  82. package/dist/components/account-chips.js +19 -3
  83. package/dist/components/account-chips.js.map +1 -1
  84. package/dist/components/account-settings-labels.d.ts +12 -1
  85. package/dist/components/account-settings-labels.js +4 -1
  86. package/dist/components/account-settings-labels.js.map +1 -1
  87. package/dist/components/account-settings.d.ts +61 -6
  88. package/dist/components/account-settings.js +115 -15
  89. package/dist/components/account-settings.js.map +1 -1
  90. package/dist/components/amount-input.d.ts +26 -5
  91. package/dist/components/button-group.d.ts +26 -5
  92. package/dist/components/calculator.d.ts +26 -5
  93. package/dist/components/confirm-dialog.d.ts +26 -5
  94. package/dist/components/copy-button.d.ts +23 -2
  95. package/dist/components/danger-confirm.d.ts +26 -5
  96. package/dist/components/facing-pair.d.ts +26 -5
  97. package/dist/components/file-button.d.ts +26 -5
  98. package/dist/components/file-dropzone.d.ts +26 -5
  99. package/dist/components/form-actions.d.ts +26 -5
  100. package/dist/components/iban-input.d.ts +26 -5
  101. package/dist/components/language-select.d.ts +26 -12
  102. package/dist/components/legal-page.d.ts +26 -5
  103. package/dist/components/legal.d.ts +26 -5
  104. package/dist/components/money-field.d.ts +26 -5
  105. package/dist/components/number-field.d.ts +26 -5
  106. package/dist/components/number-input.d.ts +26 -5
  107. package/dist/components/numpad-sheet.d.ts +26 -5
  108. package/dist/components/phone-input.d.ts +26 -5
  109. package/dist/components/series-chart.d.ts +26 -5
  110. package/dist/components/settings-fields.d.ts +25 -7
  111. package/dist/components/share-card.d.ts +25 -4
  112. package/dist/components/text-link.d.ts +26 -5
  113. package/dist/components/time-input.d.ts +26 -5
  114. package/dist/components/ui.d.ts +23 -2
  115. package/dist/components/user-avatar.d.ts +16 -2
  116. package/dist/components/user-avatar.js +7 -3
  117. package/dist/components/user-avatar.js.map +1 -1
  118. package/dist/{kit-labels-Du0uHBq4.d.ts → delete-account-setting-CGVMckMw.d.ts} +6654 -5302
  119. package/dist/feedback/feedback-attachment.d.ts +26 -5
  120. package/dist/feedback/feedback-context.d.ts +26 -5
  121. package/dist/feedback/feedback-crash.d.ts +26 -5
  122. package/dist/feedback/feedback-dialog.d.ts +26 -5
  123. package/dist/feedback/feedback-inbox.d.ts +26 -5
  124. package/dist/feedback/feedback-labels.d.ts +26 -5
  125. package/dist/feedback/feedback-menu.d.ts +26 -5
  126. package/dist/feedback/feedback-record.d.ts +26 -5
  127. package/dist/feedback/feedback-row-detail.d.ts +26 -5
  128. package/dist/feedback/feedback-status-undo.d.ts +26 -5
  129. package/dist/feedback/feedback-submit.d.ts +26 -5
  130. package/dist/feedback/feedback-swipe.d.ts +26 -5
  131. package/dist/feedback/feedback-table.d.ts +23 -2
  132. package/dist/feedback/feedback-thread.d.ts +26 -5
  133. package/dist/feedback.d.ts +26 -5
  134. package/dist/hooks/use-anchored-panel.d.ts +6 -2
  135. package/dist/hooks/use-anchored-panel.js +2 -1
  136. package/dist/hooks/use-anchored-panel.js.map +1 -1
  137. package/dist/hooks/use-file-drop.d.ts +26 -5
  138. package/dist/i18n/defaults.d.ts +26 -5
  139. package/dist/i18n/defaults.js +40 -0
  140. package/dist/i18n/defaults.js.map +1 -1
  141. package/dist/i18n/german.d.ts +26 -5
  142. package/dist/i18n/german.js +343 -2
  143. package/dist/i18n/german.js.map +1 -1
  144. package/dist/i18n/kit-labels.d.ts +23 -2
  145. package/dist/i18n/kit-labels.js.map +1 -1
  146. package/dist/i18n/languages.d.ts +21 -204
  147. package/dist/i18n/locales/de-CH.d.ts +26 -5
  148. package/dist/i18n/locales/en.d.ts +26 -5
  149. package/dist/i18n/locales/es.d.ts +26 -5
  150. package/dist/i18n/locales/es.js +337 -3
  151. package/dist/i18n/locales/es.js.map +1 -1
  152. package/dist/i18n/locales/fr.d.ts +26 -5
  153. package/dist/i18n/locales/fr.js +344 -2
  154. package/dist/i18n/locales/fr.js.map +1 -1
  155. package/dist/i18n/locales/hu.d.ts +26 -5
  156. package/dist/i18n/locales/hu.js +337 -3
  157. package/dist/i18n/locales/hu.js.map +1 -1
  158. package/dist/i18n/locales/it.d.ts +26 -5
  159. package/dist/i18n/locales/it.js +342 -2
  160. package/dist/i18n/locales/it.js.map +1 -1
  161. package/dist/i18n/locales/zh.d.ts +26 -5
  162. package/dist/i18n/locales/zh.js +333 -2
  163. package/dist/i18n/locales/zh.js.map +1 -1
  164. package/dist/i18n/review.d.ts +26 -5
  165. package/dist/i18n/review.js +45 -1
  166. package/dist/i18n/review.js.map +1 -1
  167. package/dist/index.d.ts +19 -3
  168. package/dist/index.js +89 -0
  169. package/dist/index.js.map +1 -1
  170. package/dist/lib/person-name.d.ts +69 -0
  171. package/dist/lib/person-name.js +65 -0
  172. package/dist/lib/person-name.js.map +1 -0
  173. package/dist/rhf/fields.d.ts +26 -5
  174. package/dist/rhf/form.d.ts +26 -5
  175. package/dist/rhf.d.ts +26 -5
  176. package/dist/shell/app-shell.d.ts +26 -5
  177. package/dist/shell/company-switcher.d.ts +59 -0
  178. package/dist/shell/company-switcher.js +85 -0
  179. package/dist/shell/company-switcher.js.map +1 -0
  180. package/dist/shell/top-bar-brand.d.ts +26 -5
  181. package/dist/shell/topbar-action-menu.d.ts +1 -0
  182. package/dist/shell.d.ts +20 -5
  183. package/dist/wizard/stepper-nav.d.ts +26 -5
  184. package/dist/wizard.d.ts +26 -5
  185. package/package.json +1 -1
  186. package/src/account/account-parts.ts +67 -0
  187. package/src/account/data-export-setting.tsx +218 -0
  188. package/src/account/delete-account-setting.tsx +362 -0
  189. package/src/account/email-change-setting.tsx +457 -0
  190. package/src/account/sessions-setting.tsx +283 -0
  191. package/src/admin/admin-action-confirm.tsx +495 -0
  192. package/src/admin/admin-action-log.tsx +297 -0
  193. package/src/admin/admin-parts.ts +82 -0
  194. package/src/admin/invitations-panel.tsx +516 -0
  195. package/src/admin/reviewer-scope-editor.tsx +234 -0
  196. package/src/admin/role-select.tsx +286 -0
  197. package/src/admin/transfer-ownership-dialog.tsx +172 -0
  198. package/src/admin/user-roster.tsx +573 -0
  199. package/src/auth/accept-invitation.tsx +227 -0
  200. package/src/auth/auth-errors.ts +262 -0
  201. package/src/auth/complete-name-dialog.tsx +253 -0
  202. package/src/auth/email-tag.ts +75 -0
  203. package/src/auth/forgot-password-form.tsx +229 -0
  204. package/src/auth/form-rules.ts +40 -0
  205. package/src/auth/new-password-fields.tsx +85 -0
  206. package/src/auth/not-found-page.tsx +98 -0
  207. package/src/auth/register-form.tsx +477 -0
  208. package/src/auth/reset-password-form.tsx +351 -0
  209. package/src/auth/sign-in-form.tsx +859 -0
  210. package/src/auth/status-parts.tsx +138 -0
  211. package/src/auth/verify-email.tsx +409 -0
  212. package/src/components/account-chips.tsx +64 -7
  213. package/src/components/account-settings-labels.ts +15 -1
  214. package/src/components/account-settings.tsx +199 -23
  215. package/src/components/user-avatar.tsx +22 -4
  216. package/src/hooks/use-anchored-panel.ts +14 -3
  217. package/src/i18n/defaults.ts +40 -0
  218. package/src/i18n/german.ts +379 -0
  219. package/src/i18n/kit-labels.tsx +60 -0
  220. package/src/i18n/locales/es.ts +371 -1
  221. package/src/i18n/locales/fr.ts +379 -0
  222. package/src/i18n/locales/hu.ts +366 -1
  223. package/src/i18n/locales/it.ts +373 -0
  224. package/src/i18n/locales/zh.ts +343 -0
  225. package/src/i18n/review.ts +44 -0
  226. package/src/index.ts +146 -0
  227. package/src/lib/person-name.ts +157 -0
  228. package/src/shell/company-switcher.tsx +140 -0
@@ -0,0 +1,227 @@
1
+ import { useState } from "react";
2
+ import type { ComponentPropsWithoutRef, ReactNode } from "react";
3
+ import { CheckCircle2, LogIn, TimerOff, XCircle } from "lucide-react";
4
+ import { cn } from "../lib/cn";
5
+ import { useKitLabels } from "../i18n/kit-labels";
6
+ import { useAnnounce } from "../hooks/use-announce";
7
+ import { LoadingState } from "../components/loading-state";
8
+ import { Button, EmptyState } from "../components/ui";
9
+ import { authErrorCode } from "./auth-errors";
10
+ import { OUTCOME_CLASS, STATUS_HEADING_CLASS, useOncePerKey } from "./status-parts";
11
+ import type { AuthHeadingLevel } from "./status-parts";
12
+
13
+ /* ── Labels ──────────────────────────────────────────────────────────────── */
14
+
15
+ /** Every string the page renders — the `acceptInvitation` namespace. keksdose's words
16
+ * where its `/join` page has them (`budget_share.join*`). */
17
+ export interface AcceptInvitationLabels {
18
+ /** The heading. */
19
+ title: string;
20
+ /** While the invitation is accepted. */
21
+ accepting: string;
22
+ /** Accepted, when the app did not say into what. */
23
+ accepted: string;
24
+ /** Accepted, given what it attached the account to — a company, a team, a budget. */
25
+ joined: (name: string) => string;
26
+ /** Into the app, from every outcome but "sign in first". */
27
+ continue: string;
28
+ /** The token is unknown, spent, or for another address. */
29
+ invalid: string;
30
+ expired: string;
31
+ /** The page was opened without a `token`. */
32
+ noToken: string;
33
+ /** Under `invalid` and `expired`: an invitation is re-sent as a new one (§4.4). */
34
+ askAgain: string;
35
+ /** Nobody is signed in: an existing account accepts after sign-in (§4.4). */
36
+ signInFirst: string;
37
+ /** Under it. */
38
+ signInHint: string;
39
+ signIn: string;
40
+ /** Beside "Sign in", with `registerHref`. */
41
+ register: string;
42
+ }
43
+
44
+ export const DEFAULT_ACCEPT_INVITATION_LABELS: AcceptInvitationLabels = {
45
+ title: "Accept invitation",
46
+ accepting: "Joining…",
47
+ accepted: "Invitation accepted.",
48
+ joined: (name) => `You’ve joined ${name}.`,
49
+ continue: "Continue",
50
+ invalid: "Could not join with this link",
51
+ expired: "This invitation has expired",
52
+ noToken: "This link is missing its invite token.",
53
+ askAgain: "Ask the person who invited you to send a new invitation.",
54
+ signInFirst: "Sign in to accept this invitation",
55
+ signInHint: "Use the account the invitation was sent to.",
56
+ signIn: "Sign in",
57
+ register: "Create account",
58
+ };
59
+
60
+ /* ── Types ───────────────────────────────────────────────────────────────── */
61
+
62
+ /** What `onAccept` may resolve with. */
63
+ export interface AcceptInvitationResult {
64
+ /** What the invitation attached the account to, by name — "Example Property Ltd",
65
+ * "Team North", a budget. Said in the confirmation. */
66
+ name?: string;
67
+ }
68
+
69
+ /** What became of an invitation that was not accepted. `signIn`: the server wants a
70
+ * session first (a `401`), which `signedIn` did not know about. */
71
+ export type AcceptInvitationFailure = "invalid" | "expired" | "signIn";
72
+
73
+ type Outcome = "accepted" | AcceptInvitationFailure;
74
+
75
+ export interface AcceptInvitationProps extends Omit<ComponentPropsWithoutRef<"div">, "children" | "title"> {
76
+ /** The invitation's token — the app's `?invite=` / `?token=`. Empty: the link was cut
77
+ * short, and the page says so without asking anyone. */
78
+ token: string;
79
+ /** Someone is signed in on this device. Until then the page asks for a sign-in and
80
+ * sends nothing: accepting attaches the invitation to the SIGNED-IN account. */
81
+ signedIn: boolean;
82
+ /**
83
+ * Accept it — `POST /auth/invitations/accept {token}`, once per token, as soon as
84
+ * someone is signed in. Resolve (with what it attached the account to, if the app
85
+ * knows); reject with the client's error.
86
+ */
87
+ onAccept: (token: string) => Promise<AcceptInvitationResult | void | undefined>;
88
+ /** Which dead end a rejection is. Default: `invitation_expired` is `expired`,
89
+ * anything else `invalid` (§5.2's codes). Map a `401` to `signIn` here. */
90
+ classifyError?: (error: unknown) => AcceptInvitationFailure;
91
+ /** "Continue" — into the app. Default `/`. */
92
+ continueHref?: string;
93
+ /** "Sign in" — the app's `/login`, with a way back here (`?next=…`). Default
94
+ * `/login`. */
95
+ signInHref?: string;
96
+ /** "Create account" beside "Sign in", for an invitation that may lead to a new
97
+ * account — the app's `/register?invite=…`. Left out, none. */
98
+ registerHref?: string;
99
+ /** The heading. Default `labels.title`; `null` draws none, for a page that puts it in
100
+ * `AuthLayout`'s `title` instead. */
101
+ title?: ReactNode;
102
+ /** The heading's level. Default `h2`, under the page's `h1`. */
103
+ headingAs?: AuthHeadingLevel;
104
+ labels?: Partial<AcceptInvitationLabels>;
105
+ }
106
+
107
+ /**
108
+ * Accept an invitation with an EXISTING account (docs/auth-harmonization.md §4.4): the
109
+ * link leads here, the person signs in if they are not, and the app attaches the
110
+ * invitation — a kastlan user joining a second company, a Kurvenschmiede member a team,
111
+ * a keksdose user a shared budget. A new address never needs this page: its link opens
112
+ * the sign-up form, which spends the invitation itself.
113
+ *
114
+ * keksdose's `JoinSharePage` (features/budgets/join-page.tsx) is the model: it accepts
115
+ * on load, once (the token is single-use, and StrictMode's double effect would send it
116
+ * twice), and shows the outcome where its spinner was. The outcomes: accepted, with the
117
+ * way into the app; not valid; expired (both say to ask for a new invitation, since
118
+ * "resend" mints a new token); or "sign in first", with nothing sent. "Continue" REPLACES
119
+ * this page in the history: the token is spent, so Back must not land here again.
120
+ */
121
+ export function AcceptInvitation({
122
+ token,
123
+ signedIn,
124
+ onAccept,
125
+ classifyError,
126
+ continueHref = "/",
127
+ signInHref = "/login",
128
+ registerHref,
129
+ title,
130
+ headingAs: Heading = "h2",
131
+ labels: labelsProp,
132
+ className,
133
+ ...rest
134
+ }: AcceptInvitationProps) {
135
+ const labels = useKitLabels("acceptInvitation", DEFAULT_ACCEPT_INVITATION_LABELS, labelsProp);
136
+ const [settled, setSettled] = useState<{ token: string; outcome: Outcome; name?: string } | null>(null);
137
+ const { announce, regionProps } = useAnnounce();
138
+
139
+ useOncePerKey(token !== "" && signedIn ? token : null, onAccept, (key, result) => {
140
+ if (result.ok) {
141
+ const name = result.value?.name?.trim() || undefined;
142
+ setSettled({ token: key, outcome: "accepted", name });
143
+ announce(name ? labels.joined(name) : labels.accepted);
144
+ return;
145
+ }
146
+ const failure: AcceptInvitationFailure =
147
+ classifyError?.(result.error) ?? (authErrorCode(result.error) === "invitation_expired" ? "expired" : "invalid");
148
+ setSettled({ token: key, outcome: failure });
149
+ announce(failure === "signIn" ? labels.signInFirst : labels[failure]);
150
+ });
151
+
152
+ const outcome: Outcome | "accepting" =
153
+ token === ""
154
+ ? "invalid"
155
+ : settled?.token === token
156
+ ? settled.outcome
157
+ : signedIn
158
+ ? "accepting"
159
+ : "signIn";
160
+
161
+ const continueButton = (variant: "brand" | "secondary") => (
162
+ <Button href={continueHref} replace variant={variant}>
163
+ {labels.continue}
164
+ </Button>
165
+ );
166
+
167
+ let body: ReactNode;
168
+ if (outcome === "accepting") {
169
+ body = <LoadingState size="sm" label={labels.accepting} />;
170
+ } else if (outcome === "accepted") {
171
+ body = (
172
+ <EmptyState
173
+ tone="success"
174
+ icon={<CheckCircle2 />}
175
+ title={settled?.name ? labels.joined(settled.name) : labels.accepted}
176
+ action={continueButton("brand")}
177
+ className={OUTCOME_CLASS}
178
+ />
179
+ );
180
+ } else if (outcome === "signIn") {
181
+ body = (
182
+ <EmptyState
183
+ icon={<LogIn />}
184
+ title={labels.signInFirst}
185
+ hint={labels.signInHint}
186
+ action={
187
+ <>
188
+ <Button href={signInHref} variant="brand">
189
+ {labels.signIn}
190
+ </Button>
191
+ {registerHref !== undefined && (
192
+ <Button href={registerHref} variant="secondary">
193
+ {labels.register}
194
+ </Button>
195
+ )}
196
+ </>
197
+ }
198
+ className={OUTCOME_CLASS}
199
+ />
200
+ );
201
+ } else {
202
+ body = (
203
+ <EmptyState
204
+ tone="danger"
205
+ icon={outcome === "expired" ? <TimerOff /> : <XCircle />}
206
+ title={token === "" ? labels.noToken : labels[outcome]}
207
+ hint={labels.askAgain}
208
+ action={signedIn ? continueButton("secondary") : undefined}
209
+ className={OUTCOME_CLASS}
210
+ />
211
+ );
212
+ }
213
+
214
+ return (
215
+ <div
216
+ {...rest}
217
+ data-state={outcome}
218
+ aria-busy={outcome === "accepting" || undefined}
219
+ className={cn("space-y-3", className)}
220
+ >
221
+ {title === null ? null : <Heading className={STATUS_HEADING_CLASS}>{title ?? labels.title}</Heading>}
222
+ {body}
223
+ {/* Rendered from the start, so the outcome is read when it arrives. */}
224
+ <div {...regionProps} />
225
+ </div>
226
+ );
227
+ }
@@ -0,0 +1,262 @@
1
+ /**
2
+ * The coded refusals of sign-in and sign-up (docs/auth-harmonization.md §5.2): every
3
+ * refusal the auth routes answer carries a machine-readable `code`, and the pages
4
+ * recognise the CODE, never the server's sentence.
5
+ *
6
+ * Today each app's pages print the server's `detail` string (keksdose's
7
+ * `extractApiErrorMessage`), which is English on every locale and, on a credential
8
+ * check, would be an account-enumeration oracle if it ever said more than one thing.
9
+ * keksdose already keys one refusal on a code (`features/budgets/plan-limit.ts`,
10
+ * `402 {detail, code: "plan_budget_limit"}`); §5.2 makes that the rule for auth. Each
11
+ * app switches its server and its pages together (§9).
12
+ *
13
+ * - `invalid_credentials` — unknown address, wrong password AND deactivated account:
14
+ * one answer, in the same time (§2.10). `401`.
15
+ * - `registration_closed` — the sign-up gate refused the address. `403`.
16
+ * - `email_taken` — an account with this address exists. `409`.
17
+ * - `invitation_invalid`, `invitation_expired` — the `invite` token of a sign-up or an
18
+ * accept (§4.4).
19
+ * - `token_invalid` — a one-time token (reset, verification, a sign-in challenge) is
20
+ * unknown, spent or expired.
21
+ * - `account_inactive` — **no server answers it.** §2.10 decided that a deactivated
22
+ * account gets `invalid_credentials`, because a separate answer after a correct
23
+ * password would confirm a leaked one. It stays in the union only for a future server
24
+ * step that is past the password (an admin view, a signed-in refresh) and may say so.
25
+ */
26
+ export type AuthErrorCode =
27
+ | "invalid_credentials"
28
+ | "registration_closed"
29
+ | "email_taken"
30
+ | "invitation_invalid"
31
+ | "invitation_expired"
32
+ | "token_invalid"
33
+ /** 0.29.1: a link that WAS valid and ran out (verification, reset) — keksdose's step-3
34
+ * refusals tell it from `token_invalid`, so a page can say "expired, ask for a new one". */
35
+ | "token_expired"
36
+ | "account_inactive";
37
+
38
+ /**
39
+ * 0.30.0: the coded refusals of user administration and the account's own settings
40
+ * (docs/user-admin-harmonization.md §4, §6) — server-kit 0.4.0's `AccountErrorCode`,
41
+ * value for value. The same family of answer as the sign-in codes (`{detail, code}`,
42
+ * plus `companies` on kastlan's `last_admin`), read by the same {@link authErrorCode}.
43
+ *
44
+ * - `last_admin` — the action would leave no active admin: deactivating, demoting or
45
+ * deleting the last one (in kastlan, of a company; the answer names the `companies`).
46
+ * - `self_action` — an admin acting on their own account where that is never allowed.
47
+ * - `other_companies` — kastlan: the account belongs to other companies too, so a
48
+ * company admin may not deactivate it; "Remove from company" instead (§9.6).
49
+ * - `household_has_members` — keksdose's guard on a deletion request (§6.4).
50
+ * - `confirmation_required`, `confirmation_mismatch` — the action's confirmation level
51
+ * asked for a typed address (or a tick), and the request carried none, or another
52
+ * address (§4.2).
53
+ * - `password_incorrect` — the CURRENT password is wrong on a signed-in route (the email
54
+ * change, the deletion request). `400`, not `401`: an app's client reads a 401 as an
55
+ * ended session and signs the user out, the wrong answer to a typo. Every other one
56
+ * is a `409`.
57
+ */
58
+ export type AccountErrorCode =
59
+ | "last_admin"
60
+ | "self_action"
61
+ | "other_companies"
62
+ | "household_has_members"
63
+ | "confirmation_required"
64
+ | "confirmation_mismatch"
65
+ | "password_incorrect";
66
+
67
+ /** Every code the kit reads: the sign-in refusals and the account ones (0.30.0). */
68
+ export type KitErrorCode = AuthErrorCode | AccountErrorCode;
69
+
70
+ const CODES: ReadonlySet<string> = new Set<KitErrorCode>([
71
+ "invalid_credentials",
72
+ "registration_closed",
73
+ "email_taken",
74
+ "invitation_invalid",
75
+ "invitation_expired",
76
+ "token_invalid",
77
+ "token_expired",
78
+ "account_inactive",
79
+ "last_admin",
80
+ "self_action",
81
+ "other_companies",
82
+ "household_has_members",
83
+ "confirmation_required",
84
+ "confirmation_mismatch",
85
+ "password_incorrect",
86
+ ]);
87
+
88
+ type Bag = Record<string, unknown>;
89
+
90
+ const isBag = (value: unknown): value is Bag => typeof value === "object" && value !== null;
91
+
92
+ /** A known code, or undefined. */
93
+ function known(value: unknown): KitErrorCode | undefined {
94
+ return typeof value === "string" && CODES.has(value) ? (value as KitErrorCode) : undefined;
95
+ }
96
+
97
+ /** `{code}` on a parsed body, or FastAPI's `{detail: {code}}` — what an
98
+ * `HTTPException(detail={"code": …})` serialises to. */
99
+ function codeOfBody(body: unknown): KitErrorCode | undefined {
100
+ if (!isBag(body)) return undefined;
101
+ return known(body.code) ?? (isBag(body.detail) ? known(body.detail.code) : undefined);
102
+ }
103
+
104
+ /**
105
+ * The code an error carries — an {@link AuthErrorCode}, or since 0.30.0 an
106
+ * {@link AccountErrorCode} — or `undefined` when it carries none.
107
+ *
108
+ * Reads the parsed response body wherever the apps' HTTP clients put it, without
109
+ * depending on any of them:
110
+ *
111
+ * 1. `err.response.data` — axios (keksdose's, kastlan's and Kurvenschmiede's clients;
112
+ * keksdose's `isPlanBudgetLimitRefusal` reads the same path);
113
+ * 2. `err.data` — ofetch's `FetchError`, and most `fetch` wrappers that parse the body;
114
+ * 3. `err.body` — the generated OpenAPI clients' `ApiError`;
115
+ * 4. `err` itself — a body thrown as it came (`throw await res.json()`).
116
+ *
117
+ * In each, `code` or FastAPI's nested `detail.code`. Last, `err.code` — but only when
118
+ * it is one of the codes above: axios writes its OWN codes there (`ERR_NETWORK`,
119
+ * `ECONNABORTED`, `ERR_BAD_REQUEST`), and so does Node, so an unknown value is never
120
+ * read as a refusal. The HTTP status is not checked: the code is the contract, and the
121
+ * status differs per refusal (401, 403, 409, 410).
122
+ */
123
+ export function authErrorCode(err: unknown): KitErrorCode | undefined {
124
+ if (!isBag(err)) return undefined;
125
+ const response = err.response;
126
+ return (
127
+ (isBag(response) ? codeOfBody(response.data) : undefined) ??
128
+ codeOfBody(err.data) ??
129
+ codeOfBody(err.body) ??
130
+ codeOfBody(err)
131
+ );
132
+ }
133
+
134
+ /**
135
+ * Whether `err` is a coded auth refusal — any of them, or the one named:
136
+ *
137
+ * ```ts
138
+ * try { await api.register(values) }
139
+ * catch (err) { if (isAuthError(err, "email_taken")) … }
140
+ * ```
141
+ *
142
+ * The kit's `SignInForm` and `RegisterForm` call it on what their callbacks reject
143
+ * with, so an app's `onSubmit` can simply let its client's error through. See
144
+ * {@link authErrorCode} for the shapes it reads. Since 0.30.0 it knows the account codes
145
+ * too: `isAuthError(err, "last_admin")`.
146
+ */
147
+ export function isAuthError(err: unknown, code?: KitErrorCode): boolean {
148
+ const found = authErrorCode(err);
149
+ return found !== undefined && (code === undefined || found === code);
150
+ }
151
+
152
+ /** An HTTP status as a number, or undefined. */
153
+ function statusOf(value: unknown): number | undefined {
154
+ return typeof value === "number" && Number.isInteger(value) ? value : undefined;
155
+ }
156
+
157
+ /**
158
+ * Whether `err` is a throttled request — HTTP `429 Too Many Requests` (0.30.0, from the
159
+ * apps' 0.29 adoption: every app answered a throttled sign-in, sign-up or reset through
160
+ * its own `describeError`, with three wordings of one sentence).
161
+ *
162
+ * A throttle carries no `code` in the contract — the STATUS is the answer, set by the
163
+ * apps' limiters (server-kit's `AuthLimiters`, keksdose's per-IP limit) — so this reads
164
+ * the status, wherever the apps' HTTP clients put it, without depending on any of them:
165
+ *
166
+ * 1. `err.response.status` — axios (all three apps' clients);
167
+ * 2. `err.status` — ofetch's `FetchError`, the generated OpenAPI clients' `ApiError`, and
168
+ * a `Response` thrown as it came;
169
+ * 3. `err.statusCode` — ofetch's alias, and Node-style errors.
170
+ *
171
+ * There is no `rate_limited` code: keksdose answers its 429s uncoded, with a
172
+ * `Retry-After` header, and the status is the one signal every limiter gives.
173
+ *
174
+ * The kit's `SignInForm`, `RegisterForm`, `ForgotPasswordForm` and `ResetPasswordForm`
175
+ * call it AFTER the app's `describeError` — an app with words of its own keeps them —
176
+ * and before their generic failure, so "Too many attempts. Try again in 30 s." (with
177
+ * {@link retryAfterSeconds}) or "… Wait a moment and try again." needs no app code.
178
+ */
179
+ export function isRateLimited(err: unknown): boolean {
180
+ if (!isBag(err)) return false;
181
+ const response = err.response;
182
+ const status =
183
+ (isBag(response) ? statusOf(response.status) : undefined) ?? statusOf(err.status) ?? statusOf(err.statusCode);
184
+ return status === 429;
185
+ }
186
+
187
+ /** A header's value from a `Headers` (fetch, ofetch's `response`), axios' `AxiosHeaders`
188
+ * or a plain record — whose keys may come in any case. */
189
+ function headerOf(headers: unknown, name: string): unknown {
190
+ if (!isBag(headers)) return undefined;
191
+ if (typeof headers.get === "function") {
192
+ try {
193
+ const value: unknown = (headers.get as (key: string) => unknown).call(headers, name);
194
+ if (value !== undefined && value !== null) return value;
195
+ } catch {
196
+ // A `get` that is not a header lookup; the record below is.
197
+ }
198
+ }
199
+ for (const [key, value] of Object.entries(headers)) {
200
+ if (key.toLowerCase() === name) return value;
201
+ }
202
+ return undefined;
203
+ }
204
+
205
+ /** `Retry-After` as seconds from `now`: delta-seconds ("30"), or an HTTP date
206
+ * ("Wed, 07 Oct 2026 10:00:00 GMT") counted from now, never below 0 (RFC 9110 §10.2.3). */
207
+ function parseRetryAfter(value: unknown, now: number): number | undefined {
208
+ const raw = Array.isArray(value) ? (value as unknown[])[0] : value;
209
+ if (typeof raw === "number") return Number.isFinite(raw) && raw >= 0 ? Math.ceil(raw) : undefined;
210
+ if (typeof raw !== "string") return undefined;
211
+ const text = raw.trim();
212
+ if (/^\d+$/.test(text)) return Number(text);
213
+ // An HTTP date names its day and month ("Wed, 07 Oct 2026 …"). Without a letter it is
214
+ // a malformed number ("-5", "1.5"), which `Date.parse` would happily read as a year.
215
+ if (!/[a-z]/i.test(text)) return undefined;
216
+ const at = Date.parse(text);
217
+ if (Number.isNaN(at)) return undefined;
218
+ return Math.max(0, Math.ceil((at - now) / 1000));
219
+ }
220
+
221
+ /**
222
+ * How long a throttled request asks to wait, in whole seconds — its `Retry-After`
223
+ * header (0.30.0, keksdose: its 429s carry no code, only the header) — or `undefined`
224
+ * when there is none, or none that parses.
225
+ *
226
+ * Read where the apps' clients keep the response headers, without depending on any of
227
+ * them: `err.response.headers` (axios' `AxiosHeaders` or a plain record; ofetch's
228
+ * `FetchError.response`, a fetch `Headers`), then `err.headers` (a `Response` thrown as
229
+ * it came). The value is delta-seconds, or an HTTP date turned into seconds from `now`
230
+ * (default: the present moment) — never below 0.
231
+ *
232
+ * The forms hand it to their `rateLimited` label when {@link isRateLimited} is true:
233
+ * "Too many attempts. Try again in 30 s." — and, without one, "… Wait a moment and try
234
+ * again." A browser only exposes `Retry-After` to a cross-origin page when the server
235
+ * lists it in `Access-Control-Expose-Headers`.
236
+ */
237
+ export function retryAfterSeconds(err: unknown, now: number | Date = Date.now()): number | undefined {
238
+ if (!isBag(err)) return undefined;
239
+ const at = typeof now === "number" ? now : now.getTime();
240
+ const response = err.response;
241
+ const value =
242
+ (isBag(response) ? headerOf(response.headers, "retry-after") : undefined) ?? headerOf(err.headers, "retry-after");
243
+ return value === undefined ? undefined : parseRetryAfter(value, at);
244
+ }
245
+
246
+ /**
247
+ * The English of every `rateLimited` label (0.30.0): "Too many attempts. Try again in
248
+ * 30 s." with a wait under a minute, in whole minutes from one minute up (keksdose's
249
+ * reset throttle answers in hours, and "3600 s" is a number nobody reads), and "Wait a
250
+ * moment and try again." without one — or with a `0`.
251
+ *
252
+ * @internal The namespaces' English; not part of the barrel.
253
+ */
254
+ export function englishRateLimited(seconds?: number): string {
255
+ if (!seconds || seconds <= 0) return "Too many attempts. Wait a moment and try again.";
256
+ return `Too many attempts. Try again in ${englishWait(seconds)}.`;
257
+ }
258
+
259
+ /** "30 s", "2 min" — a wait in English, rounded up. @internal */
260
+ export function englishWait(seconds: number): string {
261
+ return seconds < 60 ? `${Math.ceil(seconds)} s` : `${Math.ceil(seconds / 60)} min`;
262
+ }