@softr/sdk 1.7.6 → 1.8.0-alpha.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # @softr/sdk
2
2
 
3
+ ## 1.8.0
4
+
5
+ ### Minor Changes
6
+
7
+ - `usePwaInstall` returns `install()` while the browser offers its own install dialog (Chrome and Edge), and turns unavailable once the app is installed.
8
+
9
+ ## 1.7.7
10
+
11
+ ### Patch Changes
12
+
13
+ - `docs/user-groups.md` names the custom user groups past the workspace's plan limit, in a section of their own. A page or an action gated on one of them admits nobody.
14
+ - `DefaultErrorOverlay` shows why a guard denied the page in development, not `[object Object]`.
15
+ - `guardRoute` and `assertRouteAccess` print why they denied the page in development when they throw an access error.
16
+
3
17
  ## 1.7.6
4
18
 
5
19
  ### Patch Changes
@@ -19,12 +19,21 @@ function renderUserGroupsDoc(groups) {
19
19
  "This app has no user groups.",
20
20
  ""
21
21
  ].join("\n");
22
+ const pastLimit = groups.filter((group) => group.exceedsPlanLimit === true);
22
23
  return [
23
24
  ...heading,
24
25
  "User groups available in this app:",
25
26
  "",
26
27
  ...groups.map(({ id, name, type }) => `- ${name} (${id}), ${type.toLowerCase()}`),
27
- ""
28
+ "",
29
+ ...pastLimit.length === 0 ? [] : [
30
+ "## Not available on the workspace's plan",
31
+ "",
32
+ "The plan allows fewer custom user groups than this app has. The groups below are kept with their members, but nobody counts as a member of them until the plan is upgraded, so a page or an action limited to one of them admits no user:",
33
+ "",
34
+ ...pastLimit.map(({ id, name }) => `- ${name} (${id})`),
35
+ ""
36
+ ]
28
37
  ].join("\n");
29
38
  }
30
39
 
@@ -1,3 +1,4 @@
1
+ import { ACCESS_DENIED_MESSAGES, isAccessError } from "./route/route.js";
1
2
  import { useEffect, useRef, useState, useSyncExternalStore } from "react";
2
3
  import { useRouter } from "@tanstack/react-router";
3
4
  import { jsx, jsxs } from "react/jsx-runtime";
@@ -66,7 +67,7 @@ function DevView({ error, reset }) {
66
67
  ]);
67
68
  const shown = !running && !retryPending;
68
69
  useEffect(() => {
69
- if (shown) reportError(error);
70
+ if (shown) reportError(error instanceof Error ? error : new Error(messageOf(error)));
70
71
  }, [shown, error]);
71
72
  if (!shown) return /* @__PURE__ */ jsx(UpdatingView, {});
72
73
  return /* @__PURE__ */ jsx("div", {
@@ -92,7 +93,7 @@ function DevView({ error, reset }) {
92
93
  ] }),
93
94
  /* @__PURE__ */ jsx("pre", {
94
95
  style: s.message,
95
- children: error instanceof Error ? error.message : String(error)
96
+ children: messageOf(error)
96
97
  }),
97
98
  error instanceof Error && error.stack && /* @__PURE__ */ jsx(StackTrace, { stack: error.stack }),
98
99
  /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(RetryButton, { onClick: reset }) })
@@ -100,6 +101,15 @@ function DevView({ error, reset }) {
100
101
  })
101
102
  });
102
103
  }
104
+ function messageOf(error) {
105
+ if (error instanceof Error) return error.message;
106
+ if (isAccessError(error)) return ACCESS_DENIED_MESSAGES[error.reason];
107
+ try {
108
+ return JSON.stringify(error) ?? String(error);
109
+ } catch {
110
+ return String(error);
111
+ }
112
+ }
103
113
  function UpdatingView() {
104
114
  return /* @__PURE__ */ jsx("div", {
105
115
  style: s.overlay,
package/dist/package.js CHANGED
@@ -1,5 +1,5 @@
1
1
  //#region package.json
2
- var version = "1.7.6";
2
+ var version = "1.8.0-alpha.0";
3
3
 
4
4
  //#endregion
5
5
  export { version };
package/dist/pwa.d.ts CHANGED
@@ -26,6 +26,7 @@ import "@tanstack/react-start";
26
26
  */
27
27
  declare function SoftrPwaHead(): react5.FunctionComponentElement<react5.FragmentProps>;
28
28
  type PwaPlatform = "ios" | "android" | "desktop";
29
+ type InstallChoice = "accepted" | "dismissed";
29
30
  type PwaInstall = {
30
31
  available: false;
31
32
  } | {
@@ -36,6 +37,12 @@ type PwaInstall = {
36
37
  name: string;
37
38
  /** The icon the home screen will show. */
38
39
  iconUrl: string;
40
+ /**
41
+ * Opens the browser's own install dialog. Only there while the browser offers one: never in
42
+ * iOS, Safari, Firefox or the Studio preview, and not again once the visitor has closed it.
43
+ * Call it from a click, and show the steps for `platform` while it is missing or rejects.
44
+ */
45
+ install?: () => Promise<InstallChoice>;
39
46
  };
40
47
  /**
41
48
  * Whether to offer installing the app, and on which platform. Render an
@@ -47,7 +54,17 @@ type PwaInstall = {
47
54
  * function InstallButton() {
48
55
  * const install = usePwaInstall()
49
56
  * if (!install.available) return null
50
- * return <button onClick={() => openSteps(install.platform)}>Install {install.name}</button>
57
+ * return (
58
+ * <button
59
+ * onClick={() =>
60
+ * install.install
61
+ * ? install.install().catch(() => openSteps(install.platform))
62
+ * : openSteps(install.platform)
63
+ * }
64
+ * >
65
+ * Install {install.name}
66
+ * </button>
67
+ * )
51
68
  * }
52
69
  */
53
70
  declare function usePwaInstall(): PwaInstall;
package/dist/pwa.js CHANGED
@@ -4,7 +4,7 @@ import { fetch } from "./fetch.js";
4
4
  import { sessionConfig } from "./auth/config.js";
5
5
  import { readCached } from "./cache.js";
6
6
  import { z } from "zod";
7
- import { Fragment, createElement, useEffect, useState } from "react";
7
+ import { Fragment, createElement, useEffect, useState, useSyncExternalStore } from "react";
8
8
  import { createMiddleware } from "@tanstack/react-start";
9
9
 
10
10
  //#region src/pwa.ts
@@ -139,6 +139,24 @@ function SoftrPwaHead() {
139
139
  href: image.href
140
140
  })));
141
141
  }
142
+ function isInstallPromptEvent(event) {
143
+ return "prompt" in event && typeof event.prompt === "function" && "userChoice" in event;
144
+ }
145
+ let offeredPrompt = null;
146
+ const offerListeners = /* @__PURE__ */ new Set();
147
+ function setOfferedPrompt(next) {
148
+ offeredPrompt = next;
149
+ for (const listener of offerListeners) listener();
150
+ }
151
+ let installButtonCount = 0;
152
+ if (typeof window !== "undefined") {
153
+ window.addEventListener("beforeinstallprompt", (event) => {
154
+ if (!isInstallPromptEvent(event)) return;
155
+ if (installButtonCount > 0) event.preventDefault();
156
+ setOfferedPrompt(event);
157
+ });
158
+ window.addEventListener("appinstalled", () => setOfferedPrompt(null));
159
+ }
142
160
  const installSchema = pwaSchema.shape.install;
143
161
  /**
144
162
  * Whether to offer installing the app, and on which platform. Render an
@@ -150,21 +168,44 @@ const installSchema = pwaSchema.shape.install;
150
168
  * function InstallButton() {
151
169
  * const install = usePwaInstall()
152
170
  * if (!install.available) return null
153
- * return <button onClick={() => openSteps(install.platform)}>Install {install.name}</button>
171
+ * return (
172
+ * <button
173
+ * onClick={() =>
174
+ * install.install
175
+ * ? install.install().catch(() => openSteps(install.platform))
176
+ * : openSteps(install.platform)
177
+ * }
178
+ * >
179
+ * Install {install.name}
180
+ * </button>
181
+ * )
154
182
  * }
155
183
  */
156
184
  function usePwaInstall() {
157
185
  const [install, setInstall] = useState({ available: false });
186
+ const offered = useSyncExternalStore((listener) => {
187
+ offerListeners.add(listener);
188
+ return () => offerListeners.delete(listener);
189
+ }, () => offeredPrompt, () => null);
190
+ useEffect(() => {
191
+ installButtonCount++;
192
+ return () => {
193
+ installButtonCount--;
194
+ };
195
+ }, []);
158
196
  useEffect(() => {
159
197
  if (isStandalone()) return;
160
198
  let cancelled = false;
161
199
  const standaloneQuery = window.matchMedia(STANDALONE_QUERY);
162
- const hideOnceStandalone = () => {
163
- if (!standaloneQuery.matches) return;
200
+ const hide = () => {
164
201
  cancelled = true;
165
202
  setInstall({ available: false });
166
203
  };
204
+ const hideOnceStandalone = () => {
205
+ if (standaloneQuery.matches) hide();
206
+ };
167
207
  standaloneQuery.addEventListener("change", hideOnceStandalone);
208
+ window.addEventListener("appinstalled", hide);
168
209
  fetchInstallInfo().then((installInfo) => {
169
210
  const parsed = installSchema.safeParse(installInfo);
170
211
  if (!parsed.success || cancelled) return;
@@ -177,9 +218,18 @@ function usePwaInstall() {
177
218
  return () => {
178
219
  cancelled = true;
179
220
  standaloneQuery.removeEventListener("change", hideOnceStandalone);
221
+ window.removeEventListener("appinstalled", hide);
180
222
  };
181
223
  }, []);
182
- return install;
224
+ if (!install.available || offered === null) return install;
225
+ return {
226
+ ...install,
227
+ install: async () => {
228
+ await offered.prompt();
229
+ setOfferedPrompt(null);
230
+ return (await offered.userChoice).outcome;
231
+ }
232
+ };
183
233
  }
184
234
  function detectPlatform() {
185
235
  const userAgent = navigator.userAgent;
@@ -1,4 +1,5 @@
1
1
  import { panic } from "../assertions.js";
2
+ import { logger } from "../logger.js";
2
3
  import { softrConfig } from "../softr-config.js";
3
4
  import { ACCESS_REASONS, checkRule } from "./visibility.js";
4
5
  import { redirect } from "@tanstack/react-router";
@@ -33,6 +34,15 @@ function isForbiddenError(error) {
33
34
  function isAlreadyAuthenticatedError(error) {
34
35
  return isReason(error, "alreadyAuthenticated");
35
36
  }
37
+ /**
38
+ * Why a guard denied the visitor, for the builder who reads it in the preview.
39
+ * Plain words: the builder does not write code.
40
+ */
41
+ const ACCESS_DENIED_MESSAGES = {
42
+ unauthenticated: "This page is only for signed-in users, and nobody is signed in.",
43
+ forbidden: "You are signed in, but your account is not in any user group this page is open to.",
44
+ alreadyAuthenticated: "This page is only for signed-out visitors, and you are signed in."
45
+ };
36
46
  /** What a guard throws for a reason: a redirect, or an {@link AccessError}. */
37
47
  function thrownFor(reason, overrides, location) {
38
48
  const behaviour = overrides?.[reason] ?? softrConfig().auth.guard[reason];
@@ -48,10 +58,12 @@ function thrownFor(reason, overrides, location) {
48
58
  search: { [redirectParam]: location.href }
49
59
  });
50
60
  }
51
- return {
61
+ const error = {
52
62
  type: "access_error",
53
63
  reason
54
64
  };
65
+ logger.error(/* @__PURE__ */ new Error(`Access to ${location.href} denied: ${ACCESS_DENIED_MESSAGES[reason]}`));
66
+ return error;
55
67
  }
56
68
  function ruleOf(page) {
57
69
  return page.visibility ?? panic("the page states no visibility rule to guard it with");
@@ -92,4 +104,4 @@ function assertRouteAccess(page, opts) {
92
104
  }
93
105
 
94
106
  //#endregion
95
- export { assertRouteAccess, guardRoute, isAccessError, isAlreadyAuthenticatedError, isForbiddenError, isUnauthenticatedError };
107
+ export { ACCESS_DENIED_MESSAGES, assertRouteAccess, guardRoute, isAccessError, isAlreadyAuthenticatedError, isForbiddenError, isUnauthenticatedError };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@softr/sdk",
3
- "version": "1.7.6",
3
+ "version": "1.8.0-alpha.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" },
@@ -10,7 +10,7 @@ description: >-
10
10
  metadata:
11
11
  type: core
12
12
  library: softr-sdk
13
- library_version: 1.7.6
13
+ library_version: 1.8.0
14
14
  ---
15
15
 
16
16
  # Softr SDK core
@@ -13,7 +13,7 @@ description: >-
13
13
  metadata:
14
14
  type: sub-skill
15
15
  library: softr-sdk
16
- library_version: 1.7.6
16
+ library_version: 1.8.0
17
17
  ---
18
18
 
19
19
  **Boundary.** `ai` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. `defineAiAction` and every type come from `@softr/sdk` and can be imported anywhere.
@@ -10,7 +10,7 @@ description: >-
10
10
  metadata:
11
11
  type: sub-skill
12
12
  library: softr-sdk
13
- library_version: 1.7.6
13
+ library_version: 1.8.0
14
14
  ---
15
15
 
16
16
  **Boundary.** `auth` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build.
@@ -10,7 +10,7 @@ description: >-
10
10
  metadata:
11
11
  type: sub-skill
12
12
  library: softr-sdk
13
- library_version: 1.7.6
13
+ library_version: 1.8.0
14
14
  ---
15
15
 
16
16
  **Boundary.** `ds` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. A helper that calls `ds` outside a handler goes in `createServerOnlyFn` from `@tanstack/react-start` (the `record-queries` boundary). `defineQuery` and every type come from `@softr/sdk` and can be imported anywhere.
@@ -11,7 +11,7 @@ description: >-
11
11
  metadata:
12
12
  type: sub-skill
13
13
  library: softr-sdk
14
- library_version: 1.7.6
14
+ library_version: 1.8.0
15
15
  ---
16
16
 
17
17
  # The current user
@@ -12,7 +12,7 @@ description: >-
12
12
  metadata:
13
13
  type: sub-skill
14
14
  library: softr-sdk
15
- library_version: 1.7.6
15
+ library_version: 1.8.0
16
16
  ---
17
17
 
18
18
  **Boundary.** `email` and `EmailError` live on `@softr/sdk/server` and run only inside a `createServerFn` handler — importing them into a component fails the build. `defineEmailAction` and every type come from `@softr/sdk` and can be imported anywhere.
@@ -7,7 +7,7 @@ description: >-
7
7
  metadata:
8
8
  type: sub-skill
9
9
  library: softr-sdk
10
- library_version: 1.7.6
10
+ library_version: 1.8.0
11
11
  ---
12
12
 
13
13
  **Boundary.** `upload` lives on `@softr/sdk/server` and runs inside server functions. `useFileUpload` and `FileItem` live on `@softr/sdk` and run in components. `FileAttachment` comes from `@softr/sdk` and can be imported anywhere.
@@ -8,7 +8,7 @@ description: >-
8
8
  metadata:
9
9
  type: sub-skill
10
10
  library: softr-sdk
11
- library_version: 1.7.6
11
+ library_version: 1.8.0
12
12
  ---
13
13
 
14
14
  # Dates, times and numbers
@@ -7,7 +7,7 @@ description: >-
7
7
  metadata:
8
8
  type: sub-skill
9
9
  library: softr-sdk
10
- library_version: 1.7.6
10
+ library_version: 1.8.0
11
11
  ---
12
12
 
13
13
  **Boundary.** `integration` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. `defineIntegration` and every type come from `@softr/sdk` and can be imported anywhere.
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 1.7.6
12
+ library_version: 1.8.0
13
13
  ---
14
14
 
15
15
  **Boundary.** `softrMcpMiddleware` lives on `@softr/sdk` and is registered once, in `src/start.ts`. The actions you hand it are the same `defineQuery` / `defineMutation` values the app's own pages use — declared wherever they already live, not duplicated for MCP.
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 1.7.6
12
+ library_version: 1.8.0
13
13
  ---
14
14
 
15
15
  # Push notifications
@@ -3,13 +3,13 @@ name: pwa
3
3
  description: >-
4
4
  Let visitors install the app on their home screen: SoftrPwaHead for the
5
5
  manifest, icon, iOS name and launch screen tags, usePwaInstall to decide
6
- whether to show an install button and which platform's steps to show, the
7
- PwaInstall and PwaPlatform types, and how to check that the app is
6
+ whether to show an install button, which platform's steps to show and, when
7
+ the browser offers it, install() for its own install dialog, the PwaInstall and PwaPlatform types, and how to check that the app is
8
8
  installable. The manifest and icon are already served; never write them.
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 1.7.6
12
+ library_version: 1.8.0
13
13
  ---
14
14
 
15
15
  # Installing the app
@@ -37,6 +37,7 @@ type PwaInstall =
37
37
  platform: PwaPlatform // which steps install the app on this device
38
38
  name: string // what the home screen will show
39
39
  iconUrl: string // the icon the home screen will show
40
+ install?: () => Promise<'accepted' | 'dismissed'> // the browser's own install dialog, when offered
40
41
  }
41
42
  ```
42
43
 
@@ -71,7 +72,21 @@ function RootDocument({ children }: { children: React.ReactNode }) {
71
72
 
72
73
  It fetches after mount, so the first render, the server render included, always says `available: false`. It stays unavailable when the app is not installable, when the request fails, and when the app already runs installed from the home screen. Inside the Studio preview it says available, so the builder can see the button; installing itself only works on the published app.
73
74
 
74
- There is no `install()` to call. No browser opens its install dialog for this app on request, so the button opens steps for `platform` instead: Share, then Add to Home Screen on iOS; the browser menu's Install app on Android and desktop. `name` and `iconUrl` tell the visitor what they are adding.
75
+ `install` is there only while the browser offers its own install dialog: Chrome and Edge, on desktop and Android, and not in the Studio preview. Call it from a click. It rejects when the browser refuses the prompt, so open the steps then. It resolves with what the visitor chose, and the button disappears by itself once the app is installed. A browser lets one offer prompt once, so after the visitor closes the dialog `install` is gone until the browser offers again.
76
+
77
+ When `install` is missing, the button opens steps for `platform` instead: Share, then Add to Home Screen on iOS; the browser menu's Install app on Android and desktop. Safari and Firefox never offer the dialog. `name` and `iconUrl` tell the visitor what they are adding.
78
+
79
+ ```tsx
80
+ <button
81
+ onClick={() =>
82
+ install.install
83
+ ? install.install().catch(() => openSteps(install.platform))
84
+ : openSteps(install.platform)
85
+ }
86
+ >
87
+ Install {install.name}
88
+ </button>
89
+ ```
75
90
 
76
91
  ## References
77
92
 
@@ -10,7 +10,7 @@ description: >-
10
10
  metadata:
11
11
  type: sub-skill
12
12
  library: softr-sdk
13
- library_version: 1.7.6
13
+ library_version: 1.8.0
14
14
  ---
15
15
 
16
16
  **Boundary.** `ds` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. `defineMutation` and every type come from `@softr/sdk` and can be imported anywhere.
@@ -11,7 +11,7 @@ description: >-
11
11
  metadata:
12
12
  type: sub-skill
13
13
  library: softr-sdk
14
- library_version: 1.7.6
14
+ library_version: 1.8.0
15
15
  ---
16
16
 
17
17
  **Boundary.** `ds` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. A route `loader` also runs in the browser, so it calls a server function, never `ds`. A helper that calls `ds` outside a handler, in this file or in another one, goes in `createServerOnlyFn` from `@tanstack/react-start`, or inside the handler. An exported helper without it keeps the server-only import in the browser bundle, and every page stops loading until it moves. Do not make such a helper a server function when it takes an id: any visitor can call a server function with any id. `defineQuery` and every type come from `@softr/sdk` and can be imported anywhere.
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 1.7.6
12
+ library_version: 1.8.0
13
13
  ---
14
14
 
15
15
  **Boundary.** `Audience` and `defineVisibility` come from `@softr/sdk` and work anywhere.
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 1.7.6
12
+ library_version: 1.8.0
13
13
  ---
14
14
 
15
15
  **Boundary.** `definePage`, `pageHead`, the access functions and the error predicates come from `@softr/sdk` and work anywhere.
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 1.7.6
12
+ library_version: 1.8.0
13
13
  ---
14
14
 
15
15
  **Boundary.** `workflow` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build. `defineWorkflow` and every type come from `@softr/sdk` and can be imported anywhere.