@softr/sdk 1.2.1-alpha.0 → 1.3.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 +7 -1
- package/dist/auth/idle-timeout.d.ts +17 -0
- package/dist/auth/idle-timeout.js +200 -0
- package/dist/favicon.js +30 -8
- package/dist/formatting.d.ts +3 -3
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/package.js +1 -1
- package/dist/pwa.d.ts +4 -5
- package/dist/pwa.js +5 -41
- package/package.json +34 -10
- package/skills/softr-sdk/SKILL.md +17 -5
- package/skills/softr-sdk/ai/SKILL.md +1 -1
- package/skills/softr-sdk/auth/SKILL.md +1 -1
- package/skills/softr-sdk/charts-and-metrics/SKILL.md +1 -1
- package/skills/softr-sdk/current-user/SKILL.md +1 -1
- package/skills/softr-sdk/file-uploads/SKILL.md +1 -1
- package/skills/softr-sdk/formatting/SKILL.md +1 -1
- package/skills/softr-sdk/integrations/SKILL.md +1 -1
- package/skills/softr-sdk/mcp/SKILL.md +1 -1
- package/skills/softr-sdk/push-notifications/SKILL.md +1 -1
- package/skills/softr-sdk/pwa/SKILL.md +7 -7
- package/skills/softr-sdk/record-mutations/SKILL.md +1 -1
- package/skills/softr-sdk/record-queries/SKILL.md +1 -1
- package/skills/softr-sdk/region-visibility/SKILL.md +1 -1
- package/skills/softr-sdk/routes-and-visibility/SKILL.md +1 -1
- package/skills/softr-sdk/workflows/SKILL.md +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
# @softr/sdk
|
|
2
2
|
|
|
3
|
+
## 1.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- Add `useIdleTimeout()`, which signs the visitor out of this browser after the idle time set in Studio's "End session if idle", and sends them to `/`. Action: call it once in the root route's document component, next to `useUserTimezone()`.
|
|
8
|
+
|
|
3
9
|
## 1.2.1
|
|
4
10
|
|
|
5
11
|
### Patch Changes
|
|
6
12
|
|
|
7
|
-
- `
|
|
13
|
+
- `softrAuthMiddleware` serves the favicon set in Studio, Settings → General → Favicon, at `/favicon.ico`. Without one, it serves the Softr icon. An icon link or `public/favicon.ico` in the app still wins.
|
|
8
14
|
|
|
9
15
|
## 1.2.0
|
|
10
16
|
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
//#region src/auth/idle-timeout.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Signs the visitor out of this browser after the idle time set in Studio's
|
|
4
|
+
* "End session if idle", and sends them to `/`. Activity in any tab of the app
|
|
5
|
+
* keeps the session alive. Does nothing for anonymous visitors.
|
|
6
|
+
*
|
|
7
|
+
* Call it once, in the root route's document component.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* function RootDocument({ children }: { children: React.ReactNode }) {
|
|
11
|
+
* useIdleTimeout()
|
|
12
|
+
* // ...
|
|
13
|
+
* }
|
|
14
|
+
*/
|
|
15
|
+
declare function useIdleTimeout(): void;
|
|
16
|
+
//#endregion
|
|
17
|
+
export { useIdleTimeout };
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { clearSession } from "./session.js";
|
|
2
|
+
import { readSoftrContext } from "./context.js";
|
|
3
|
+
import { settings } from "./methods/settings.js";
|
|
4
|
+
import { useCurrentUser } from "./isomorphic.js";
|
|
5
|
+
import { useEffect } from "react";
|
|
6
|
+
import { createServerFn } from "@tanstack/react-start";
|
|
7
|
+
|
|
8
|
+
//#region src/auth/idle-timeout.ts
|
|
9
|
+
/**
|
|
10
|
+
* The "End session if idle" setting from Studio, in milliseconds. `null` when it
|
|
11
|
+
* does not apply: nobody is signed in, it is set to never, or this is not a
|
|
12
|
+
* production build. Preview runs on a dev server and is the owner's own session.
|
|
13
|
+
*/
|
|
14
|
+
const readIdleTimeout = createServerFn({ method: "GET" }).handler(async () => {
|
|
15
|
+
if (process.env.NODE_ENV !== "production") return null;
|
|
16
|
+
if (readSoftrContext().user === null) return null;
|
|
17
|
+
const minutes = (await settings()).inactivityTimeoutMinutes;
|
|
18
|
+
return minutes === null ? null : minutes * 6e4;
|
|
19
|
+
});
|
|
20
|
+
/**
|
|
21
|
+
* Signs this browser out and nothing else. Unlike `auth.signOut()`, the user's
|
|
22
|
+
* other devices stay signed in.
|
|
23
|
+
*/
|
|
24
|
+
const endIdleSession = createServerFn({ method: "POST" }).handler(() => {
|
|
25
|
+
clearSession();
|
|
26
|
+
});
|
|
27
|
+
/**
|
|
28
|
+
* Signs the visitor out of this browser after the idle time set in Studio's
|
|
29
|
+
* "End session if idle", and sends them to `/`. Activity in any tab of the app
|
|
30
|
+
* keeps the session alive. Does nothing for anonymous visitors.
|
|
31
|
+
*
|
|
32
|
+
* Call it once, in the root route's document component.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* function RootDocument({ children }: { children: React.ReactNode }) {
|
|
36
|
+
* useIdleTimeout()
|
|
37
|
+
* // ...
|
|
38
|
+
* }
|
|
39
|
+
*/
|
|
40
|
+
function useIdleTimeout() {
|
|
41
|
+
const signedIn = useCurrentUser() !== null;
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
if (!signedIn) return;
|
|
44
|
+
let stop = null;
|
|
45
|
+
let unmounted = false;
|
|
46
|
+
readIdleTimeout().then((idleMs) => {
|
|
47
|
+
if (unmounted || idleMs === null) return;
|
|
48
|
+
stop = startIdleClock(idleMs);
|
|
49
|
+
}, () => {});
|
|
50
|
+
return () => {
|
|
51
|
+
unmounted = true;
|
|
52
|
+
stop?.();
|
|
53
|
+
};
|
|
54
|
+
}, [signedIn]);
|
|
55
|
+
}
|
|
56
|
+
const KEY_LAST_ACTIVITY = "softr.idle.v1.lastActivity";
|
|
57
|
+
const KEY_SIGNED_OUT = "softr.idle.v1.signedOutAt";
|
|
58
|
+
/** Shared clock writes at most this often, so a mousemove is not a storage write in every tab. */
|
|
59
|
+
const WRITE_EVERY_MS = 5e3;
|
|
60
|
+
/**
|
|
61
|
+
* Activity events, listened for on the document in the capture phase. `scroll`
|
|
62
|
+
* does not bubble, and capture still sees events a handler stops.
|
|
63
|
+
*/
|
|
64
|
+
const ACTIVITY_EVENTS = [
|
|
65
|
+
"pointerdown",
|
|
66
|
+
"pointermove",
|
|
67
|
+
"mousedown",
|
|
68
|
+
"mousemove",
|
|
69
|
+
"wheel",
|
|
70
|
+
"keydown",
|
|
71
|
+
"touchstart",
|
|
72
|
+
"touchmove",
|
|
73
|
+
"scroll",
|
|
74
|
+
"click",
|
|
75
|
+
"input",
|
|
76
|
+
"focusin",
|
|
77
|
+
"submit"
|
|
78
|
+
];
|
|
79
|
+
/**
|
|
80
|
+
* Starts the idle clock and returns what stops it.
|
|
81
|
+
*
|
|
82
|
+
* Idle time is checked against the wall clock on a tick, not with one long
|
|
83
|
+
* timeout. Browsers delay timers in background tabs, and a laptop that wakes
|
|
84
|
+
* from sleep resumes a pending timeout with its full delay left.
|
|
85
|
+
*/
|
|
86
|
+
function startIdleClock(idleMs) {
|
|
87
|
+
const tickMs = Math.min(15e3, Math.max(2e3, Math.floor(idleMs / 30)));
|
|
88
|
+
const graceCapMs = idleMs;
|
|
89
|
+
let localLast = Date.now();
|
|
90
|
+
let lastWrite = 0;
|
|
91
|
+
let graceUsed = 0;
|
|
92
|
+
let ending = false;
|
|
93
|
+
let finished = false;
|
|
94
|
+
const listeners = new AbortController();
|
|
95
|
+
function lastActivity() {
|
|
96
|
+
const shared = Number(readStorage(KEY_LAST_ACTIVITY));
|
|
97
|
+
return Number.isFinite(shared) && shared > localLast ? shared : localLast;
|
|
98
|
+
}
|
|
99
|
+
function mark(now) {
|
|
100
|
+
if (now > localLast) localLast = now;
|
|
101
|
+
if (now - lastWrite >= WRITE_EVERY_MS) {
|
|
102
|
+
lastWrite = now;
|
|
103
|
+
writeStorage(KEY_LAST_ACTIVITY, String(now));
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
function onActivity() {
|
|
107
|
+
graceUsed = 0;
|
|
108
|
+
mark(Date.now());
|
|
109
|
+
}
|
|
110
|
+
function stop() {
|
|
111
|
+
finished = true;
|
|
112
|
+
clearInterval(ticker);
|
|
113
|
+
listeners.abort();
|
|
114
|
+
}
|
|
115
|
+
function leave() {
|
|
116
|
+
if (finished) return;
|
|
117
|
+
stop();
|
|
118
|
+
window.location.replace("/");
|
|
119
|
+
}
|
|
120
|
+
async function endSession() {
|
|
121
|
+
if (ending) return;
|
|
122
|
+
ending = true;
|
|
123
|
+
try {
|
|
124
|
+
await endIdleSession();
|
|
125
|
+
} catch {
|
|
126
|
+
ending = false;
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
writeStorage(KEY_SIGNED_OUT, String(Date.now()));
|
|
130
|
+
leave();
|
|
131
|
+
}
|
|
132
|
+
function tick() {
|
|
133
|
+
if (finished) return;
|
|
134
|
+
if (readStorage(KEY_SIGNED_OUT)) {
|
|
135
|
+
leave();
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
const now = Date.now();
|
|
139
|
+
if (now - lastActivity() < idleMs) return;
|
|
140
|
+
if (iframeHasFocus() && graceUsed < graceCapMs) {
|
|
141
|
+
graceUsed += tickMs;
|
|
142
|
+
mark(now);
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
endSession();
|
|
146
|
+
}
|
|
147
|
+
removeStorage(KEY_SIGNED_OUT);
|
|
148
|
+
mark(Date.now());
|
|
149
|
+
const ticker = setInterval(tick, tickMs);
|
|
150
|
+
const { signal } = listeners;
|
|
151
|
+
for (const event of ACTIVITY_EVENTS) document.addEventListener(event, onActivity, {
|
|
152
|
+
capture: true,
|
|
153
|
+
passive: true,
|
|
154
|
+
signal
|
|
155
|
+
});
|
|
156
|
+
window.addEventListener("focus", onActivity, {
|
|
157
|
+
capture: true,
|
|
158
|
+
signal
|
|
159
|
+
});
|
|
160
|
+
document.addEventListener("visibilitychange", () => {
|
|
161
|
+
if (document.visibilityState !== "visible") return;
|
|
162
|
+
tick();
|
|
163
|
+
if (!finished && !ending) onActivity();
|
|
164
|
+
}, { signal });
|
|
165
|
+
window.addEventListener("storage", (event) => {
|
|
166
|
+
if (event.key === KEY_SIGNED_OUT && event.newValue) {
|
|
167
|
+
leave();
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
if (event.key === KEY_LAST_ACTIVITY && event.newValue) {
|
|
171
|
+
const shared = Number(event.newValue);
|
|
172
|
+
if (Number.isFinite(shared) && shared > localLast) localLast = shared;
|
|
173
|
+
}
|
|
174
|
+
}, { signal });
|
|
175
|
+
return stop;
|
|
176
|
+
}
|
|
177
|
+
function iframeHasFocus() {
|
|
178
|
+
const active = document.activeElement;
|
|
179
|
+
return active !== null && active.tagName === "IFRAME" && document.hasFocus();
|
|
180
|
+
}
|
|
181
|
+
function readStorage(key) {
|
|
182
|
+
try {
|
|
183
|
+
return window.localStorage.getItem(key);
|
|
184
|
+
} catch {
|
|
185
|
+
return null;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
function writeStorage(key, value) {
|
|
189
|
+
try {
|
|
190
|
+
window.localStorage.setItem(key, value);
|
|
191
|
+
} catch {}
|
|
192
|
+
}
|
|
193
|
+
function removeStorage(key) {
|
|
194
|
+
try {
|
|
195
|
+
window.localStorage.removeItem(key);
|
|
196
|
+
} catch {}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
//#endregion
|
|
200
|
+
export { useIdleTimeout };
|
package/dist/favicon.js
CHANGED
|
@@ -1,13 +1,34 @@
|
|
|
1
1
|
import { logger } from "./logger.js";
|
|
2
|
+
import { softrApiUrl } from "./softr-api.js";
|
|
2
3
|
import { fetch } from "./fetch.js";
|
|
4
|
+
import { sessionConfig } from "./auth/config.js";
|
|
5
|
+
import { readCached } from "./cache.js";
|
|
6
|
+
import { z } from "zod";
|
|
3
7
|
import { createMiddleware } from "@tanstack/react-start";
|
|
4
8
|
|
|
5
9
|
//#region src/favicon.ts
|
|
6
10
|
const FAVICON_PATH = "/favicon.ico";
|
|
7
11
|
const DEFAULT_FAVICON_URL = "https://assets.softr-files.com/assets/images/favicon.ico";
|
|
8
|
-
const
|
|
12
|
+
const TTL_MS = 6e4;
|
|
13
|
+
const CACHE_CONTROL = "public, max-age=60";
|
|
14
|
+
const faviconSchema = z.object({ url: z.string() });
|
|
15
|
+
let cached;
|
|
9
16
|
/**
|
|
10
|
-
*
|
|
17
|
+
* The URL of the favicon set in Studio, or `null` when there is none. A
|
|
18
|
+
* failure also reads as `null`, so the app shows the default icon.
|
|
19
|
+
*/
|
|
20
|
+
function readStudioFaviconUrl() {
|
|
21
|
+
return readCached("the favicon setting", TTL_MS, () => cached, (entry) => cached = entry, async () => {
|
|
22
|
+
const { applicationId } = sessionConfig();
|
|
23
|
+
const response = await fetch(`${softrApiUrl("SOFTR_PLATFORM_API_URL")}/v1/vibe/applications/${applicationId}/favicon`, { headers: { accept: "application/json" } });
|
|
24
|
+
if (response.status === 404) return null;
|
|
25
|
+
if (!response.ok) throw new Error(`The platform answered ${response.status}`);
|
|
26
|
+
return faviconSchema.parse(await response.json()).url;
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Serves the app's favicon at `/favicon.ico`: the one the builder set in
|
|
31
|
+
* Studio, or the default Softr favicon when there is none. Every other request
|
|
11
32
|
* passes through untouched.
|
|
12
33
|
*
|
|
13
34
|
* Proxied, not redirected: some clients do not follow a redirect for an icon.
|
|
@@ -19,15 +40,16 @@ const CACHE_CONTROL = "public, max-age=86400";
|
|
|
19
40
|
function faviconMiddleware() {
|
|
20
41
|
return createMiddleware({ type: "request" }).server(async ({ next, pathname }) => {
|
|
21
42
|
if (pathname !== FAVICON_PATH) return next();
|
|
43
|
+
const iconUrl = await readStudioFaviconUrl() ?? DEFAULT_FAVICON_URL;
|
|
22
44
|
try {
|
|
23
|
-
const iconResponse = await fetch(
|
|
45
|
+
const iconResponse = await fetch(iconUrl);
|
|
24
46
|
if (!iconResponse.ok) return new Response(null, { status: 404 });
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
47
|
+
const headers = new Headers({ "cache-control": CACHE_CONTROL });
|
|
48
|
+
const contentType = iconResponse.headers.get("content-type");
|
|
49
|
+
if (contentType !== null) headers.set("content-type", contentType);
|
|
50
|
+
return new Response(iconResponse.body, { headers });
|
|
29
51
|
} catch (error) {
|
|
30
|
-
logger.warn("Could not fetch the
|
|
52
|
+
logger.warn("Could not fetch the favicon:", error);
|
|
31
53
|
return new Response(null, { status: 404 });
|
|
32
54
|
}
|
|
33
55
|
});
|
package/dist/formatting.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
import * as
|
|
2
|
+
import * as react2 from "react";
|
|
3
3
|
import { ReactNode } from "react";
|
|
4
4
|
|
|
5
5
|
//#region src/formatting.d.ts
|
|
@@ -96,7 +96,7 @@ declare function FormattingScope({
|
|
|
96
96
|
/** A BCP 47 locale, such as `de-DE`. It drops the app's fixed date format inside the scope. */
|
|
97
97
|
locale?: string;
|
|
98
98
|
children?: ReactNode;
|
|
99
|
-
}):
|
|
99
|
+
}): react2.FunctionComponentElement<react2.ProviderProps<Scope>>;
|
|
100
100
|
/**
|
|
101
101
|
* A date in the app's format, safe to render on the server. A timestamp whose
|
|
102
102
|
* zone is not known yet renders `fallback` until the browser supplies it, so
|
|
@@ -118,6 +118,6 @@ declare function FormattedDate({
|
|
|
118
118
|
time?: boolean;
|
|
119
119
|
/** What renders while the visitor's zone is unknown. */
|
|
120
120
|
fallback?: ReactNode;
|
|
121
|
-
}):
|
|
121
|
+
}): react2.FunctionComponentElement<react2.FragmentProps> | null;
|
|
122
122
|
//#endregion
|
|
123
123
|
export { DateOptions, DateParts, DateSeparator, DateValue, FixedDateFormat, FormattedDate, Formatting, FormattingScope, WeekDay, formatDate, formatDateTime, formatNumber, useFormatting };
|
package/dist/index.d.ts
CHANGED
|
@@ -11,6 +11,7 @@ import { Page, PageSettings, definePage, pageHead } from "./route/page.js";
|
|
|
11
11
|
import { assertRouteAccess, guardRoute, isAccessError, isAlreadyAuthenticatedError, isForbiddenError, isUnauthenticatedError } from "./route/route.js";
|
|
12
12
|
import { toReturnPath } from "./auth/returnPath.js";
|
|
13
13
|
import { AuthOptions, AuthSettingsProvider, getAuthSettings, getSoftrContext, initialSoftrContext, softrAuthMiddleware, useAuthSettings, useCurrentUser } from "./auth/isomorphic.js";
|
|
14
|
+
import { useIdleTimeout } from "./auth/idle-timeout.js";
|
|
14
15
|
import { softrMcpMiddleware } from "./mcp.js";
|
|
15
16
|
import { FieldShape, SoftrFieldRegistry, fieldShape, formatDuration, formatField, formatValue, registerSoftrFields } from "./fields/format.js";
|
|
16
17
|
import { useOverrides } from "./core/use-overrides.js";
|
|
@@ -19,4 +20,4 @@ import { PwaInstall, PwaPlatform, SoftrPwaHead, usePwaInstall } from "./pwa.js";
|
|
|
19
20
|
import { PushNotifications, PushPrompt, usePushNotifications } from "./push.js";
|
|
20
21
|
import { DefaultErrorOverlay } from "./default-error-overlay.js";
|
|
21
22
|
import { Audience } from "./route/audience.js";
|
|
22
|
-
export { type ActionPermissions, type AiAnswer, type AiFile, type AiGenerateInit, type AiMessage, type AiUsage, Audience, type AuthOptions, type AuthSettings, AuthSettingsProvider, type DateOptions, type DateParts, type DateSeparator, type DateValue, DefaultErrorOverlay, type FieldShape, type FileAttachment, type FileItem, type FixedDateFormat, FormattedDate, type Formatting, FormattingScope, type Page, type PageSettings, type PushNotifications, type PushPrompt, type PwaInstall, type PwaPlatform, type SoftrContext, type SoftrDatasources, type SoftrFieldRegistry, SoftrPwaHead, type SoftrUserGroupId, type SoftrUserGroups, type SoftrUserProperties, type User, type WeekDay, assertRouteAccess, checkAccess, defineAiAction, defineIntegration, defineMutation, definePage, defineQuery, defineVisibility, defineWorkflow, fieldShape, formatDate, formatDateTime, formatDuration, formatField, formatNumber, formatValue, getAuthSettings, getSoftrContext, guardRoute, initialSoftrContext, isAccessError, isAlreadyAuthenticatedError, isAuthorized, isForbiddenError, isUnauthenticatedError, pageHead, registerSoftrFields, softrAuthMiddleware, softrMcpMiddleware, toReturnPath, useAuthSettings, useCurrentUser, useFileUpload, useFormatting, useOverrides, usePushNotifications, usePwaInstall, useUserTimezone };
|
|
23
|
+
export { type ActionPermissions, type AiAnswer, type AiFile, type AiGenerateInit, type AiMessage, type AiUsage, Audience, type AuthOptions, type AuthSettings, AuthSettingsProvider, type DateOptions, type DateParts, type DateSeparator, type DateValue, DefaultErrorOverlay, type FieldShape, type FileAttachment, type FileItem, type FixedDateFormat, FormattedDate, type Formatting, FormattingScope, type Page, type PageSettings, type PushNotifications, type PushPrompt, type PwaInstall, type PwaPlatform, type SoftrContext, type SoftrDatasources, type SoftrFieldRegistry, SoftrPwaHead, type SoftrUserGroupId, type SoftrUserGroups, type SoftrUserProperties, type User, type WeekDay, assertRouteAccess, checkAccess, defineAiAction, defineIntegration, defineMutation, definePage, defineQuery, defineVisibility, defineWorkflow, fieldShape, formatDate, formatDateTime, formatDuration, formatField, formatNumber, formatValue, getAuthSettings, getSoftrContext, guardRoute, initialSoftrContext, isAccessError, isAlreadyAuthenticatedError, isAuthorized, isForbiddenError, isUnauthenticatedError, pageHead, registerSoftrFields, softrAuthMiddleware, softrMcpMiddleware, toReturnPath, useAuthSettings, useCurrentUser, useFileUpload, useFormatting, useIdleTimeout, useOverrides, usePushNotifications, usePwaInstall, useUserTimezone };
|
package/dist/index.js
CHANGED
|
@@ -8,6 +8,7 @@ import { FormattedDate, FormattingScope, formatDate, formatDateTime, formatNumbe
|
|
|
8
8
|
import { SoftrPwaHead, usePwaInstall } from "./pwa.js";
|
|
9
9
|
import { usePushNotifications } from "./push.js";
|
|
10
10
|
import { AuthSettingsProvider, getAuthSettings, getSoftrContext, initialSoftrContext, softrAuthMiddleware, useAuthSettings, useCurrentUser } from "./auth/isomorphic.js";
|
|
11
|
+
import { useIdleTimeout } from "./auth/idle-timeout.js";
|
|
11
12
|
import { softrMcpMiddleware } from "./mcp.js";
|
|
12
13
|
import { fieldShape, formatDuration, formatField, formatValue, registerSoftrFields } from "./fields/format.js";
|
|
13
14
|
import { useOverrides } from "./core/use-overrides.js";
|
|
@@ -15,4 +16,4 @@ import { useUserTimezone } from "./timezone.js";
|
|
|
15
16
|
import { DefaultErrorOverlay } from "./default-error-overlay.js";
|
|
16
17
|
import { Audience } from "./route/audience.js";
|
|
17
18
|
|
|
18
|
-
export { Audience, AuthSettingsProvider, DefaultErrorOverlay, FormattedDate, FormattingScope, SoftrPwaHead, assertRouteAccess, checkAccess, defineAiAction, defineIntegration, defineMutation, definePage, defineQuery, defineVisibility, defineWorkflow, fieldShape, formatDate, formatDateTime, formatDuration, formatField, formatNumber, formatValue, getAuthSettings, getSoftrContext, guardRoute, initialSoftrContext, isAccessError, isAlreadyAuthenticatedError, isAuthorized, isForbiddenError, isUnauthenticatedError, pageHead, registerSoftrFields, softrAuthMiddleware, softrMcpMiddleware, toReturnPath, useAuthSettings, useCurrentUser, useFileUpload, useFormatting, useOverrides, usePushNotifications, usePwaInstall, useUserTimezone };
|
|
19
|
+
export { Audience, AuthSettingsProvider, DefaultErrorOverlay, FormattedDate, FormattingScope, SoftrPwaHead, assertRouteAccess, checkAccess, defineAiAction, defineIntegration, defineMutation, definePage, defineQuery, defineVisibility, defineWorkflow, fieldShape, formatDate, formatDateTime, formatDuration, formatField, formatNumber, formatValue, getAuthSettings, getSoftrContext, guardRoute, initialSoftrContext, isAccessError, isAlreadyAuthenticatedError, isAuthorized, isForbiddenError, isUnauthenticatedError, pageHead, registerSoftrFields, softrAuthMiddleware, softrMcpMiddleware, toReturnPath, useAuthSettings, useCurrentUser, useFileUpload, useFormatting, useIdleTimeout, useOverrides, usePushNotifications, usePwaInstall, useUserTimezone };
|
package/dist/package.js
CHANGED
package/dist/pwa.d.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as react0 from "react";
|
|
2
2
|
import "@tanstack/react-start";
|
|
3
3
|
|
|
4
4
|
//#region src/pwa.d.ts
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* The
|
|
8
|
-
*
|
|
9
|
-
* Render it once, in `<head>` in the root route's `RootDocument`.
|
|
7
|
+
* The links that make the app installable: its manifest and its home screen
|
|
8
|
+
* icon. Render it once, in `<head>` in the root route's `RootDocument`.
|
|
10
9
|
*
|
|
11
10
|
* @example
|
|
12
11
|
* function RootDocument({ children }: { children: React.ReactNode }) {
|
|
@@ -24,7 +23,7 @@ import "@tanstack/react-start";
|
|
|
24
23
|
* )
|
|
25
24
|
* }
|
|
26
25
|
*/
|
|
27
|
-
declare function SoftrPwaHead():
|
|
26
|
+
declare function SoftrPwaHead(): react0.FunctionComponentElement<react0.FragmentProps>;
|
|
28
27
|
type PwaPlatform = "ios" | "android" | "desktop";
|
|
29
28
|
type PwaInstall = {
|
|
30
29
|
available: false;
|
package/dist/pwa.js
CHANGED
|
@@ -18,13 +18,8 @@ const pwaSchema = z.object({
|
|
|
18
18
|
install: z.object({
|
|
19
19
|
name: z.string(),
|
|
20
20
|
iconUrl: z.string()
|
|
21
|
-
})
|
|
22
|
-
startupImages: z.array(z.object({
|
|
23
|
-
media: z.string(),
|
|
24
|
-
href: z.string()
|
|
25
|
-
})).default([])
|
|
21
|
+
})
|
|
26
22
|
});
|
|
27
|
-
const iosHeadSchema = pwaSchema.pick({ startupImages: true }).extend({ name: z.string() });
|
|
28
23
|
let cached;
|
|
29
24
|
/** The app's PWA, or `null` when it is not installable right now. */
|
|
30
25
|
function readPwa() {
|
|
@@ -54,10 +49,7 @@ function pwaMiddleware() {
|
|
|
54
49
|
"content-type": "application/manifest+json",
|
|
55
50
|
"cache-control": CACHE_CONTROL
|
|
56
51
|
} });
|
|
57
|
-
if (pathname === INSTALL_PATH) return Response.json({
|
|
58
|
-
...pwa.install,
|
|
59
|
-
startupImages: pwa.startupImages
|
|
60
|
-
}, { headers: { "cache-control": CACHE_CONTROL } });
|
|
52
|
+
if (pathname === INSTALL_PATH) return Response.json(pwa.install, { headers: { "cache-control": CACHE_CONTROL } });
|
|
61
53
|
try {
|
|
62
54
|
const iconResponse = await fetch(pwa.install.iconUrl);
|
|
63
55
|
if (!iconResponse.ok) return new Response(null, { status: 404 });
|
|
@@ -72,9 +64,8 @@ function pwaMiddleware() {
|
|
|
72
64
|
});
|
|
73
65
|
}
|
|
74
66
|
/**
|
|
75
|
-
* The
|
|
76
|
-
*
|
|
77
|
-
* Render it once, in `<head>` in the root route's `RootDocument`.
|
|
67
|
+
* The links that make the app installable: its manifest and its home screen
|
|
68
|
+
* icon. Render it once, in `<head>` in the root route's `RootDocument`.
|
|
78
69
|
*
|
|
79
70
|
* @example
|
|
80
71
|
* function RootDocument({ children }: { children: React.ReactNode }) {
|
|
@@ -93,40 +84,13 @@ function pwaMiddleware() {
|
|
|
93
84
|
* }
|
|
94
85
|
*/
|
|
95
86
|
function SoftrPwaHead() {
|
|
96
|
-
const [iosHead, setIosHead] = useState(null);
|
|
97
|
-
useEffect(() => {
|
|
98
|
-
if (detectPlatform() !== "ios") return;
|
|
99
|
-
let cancelled = false;
|
|
100
|
-
globalThis.fetch(INSTALL_PATH, { headers: { accept: "application/json" } }).then(async (response) => {
|
|
101
|
-
if (!response.ok) return;
|
|
102
|
-
const parsed = iosHeadSchema.safeParse(await response.json());
|
|
103
|
-
if (!parsed.success || cancelled) return;
|
|
104
|
-
setIosHead(parsed.data);
|
|
105
|
-
}).catch(() => {});
|
|
106
|
-
return () => {
|
|
107
|
-
cancelled = true;
|
|
108
|
-
};
|
|
109
|
-
}, []);
|
|
110
87
|
return createElement(Fragment, null, createElement("link", {
|
|
111
88
|
rel: "manifest",
|
|
112
89
|
href: MANIFEST_PATH
|
|
113
90
|
}), createElement("link", {
|
|
114
91
|
rel: "apple-touch-icon",
|
|
115
92
|
href: ICON_PATH
|
|
116
|
-
})
|
|
117
|
-
key: "capable",
|
|
118
|
-
name: "apple-mobile-web-app-capable",
|
|
119
|
-
content: "yes"
|
|
120
|
-
}), createElement("meta", {
|
|
121
|
-
key: "title",
|
|
122
|
-
name: "apple-mobile-web-app-title",
|
|
123
|
-
content: iosHead.name
|
|
124
|
-
})], ...(iosHead?.startupImages ?? []).map((image) => createElement("link", {
|
|
125
|
-
key: image.href,
|
|
126
|
-
rel: "apple-touch-startup-image",
|
|
127
|
-
media: image.media,
|
|
128
|
-
href: image.href
|
|
129
|
-
})));
|
|
93
|
+
}));
|
|
130
94
|
}
|
|
131
95
|
const installSchema = pwaSchema.shape.install;
|
|
132
96
|
/**
|
package/package.json
CHANGED
|
@@ -1,16 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@softr/sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0-alpha.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
|
-
".": {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
6
|
+
".": {
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"import": "./dist/index.js"
|
|
9
|
+
},
|
|
10
|
+
"./client": {
|
|
11
|
+
"types": "./dist/client.d.ts",
|
|
12
|
+
"import": "./dist/client.js"
|
|
13
|
+
},
|
|
14
|
+
"./server": {
|
|
15
|
+
"types": "./dist/server.d.ts",
|
|
16
|
+
"import": "./dist/server.js"
|
|
17
|
+
},
|
|
18
|
+
"./vite": {
|
|
19
|
+
"types": "./dist/vite.d.ts",
|
|
20
|
+
"import": "./dist/vite.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"bin": {
|
|
24
|
+
"softr-sdk": "./dist/cli.js"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist",
|
|
28
|
+
"schema",
|
|
29
|
+
"skills",
|
|
30
|
+
"!skills/_artifacts",
|
|
31
|
+
"CHANGELOG.md"
|
|
32
|
+
],
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
14
36
|
"scripts": {
|
|
15
37
|
"build": "tsdown",
|
|
16
38
|
"test": "node --import ./src/test-utils/setup.ts --test",
|
|
@@ -42,7 +64,9 @@
|
|
|
42
64
|
"optional": true
|
|
43
65
|
}
|
|
44
66
|
},
|
|
45
|
-
"keywords": [
|
|
67
|
+
"keywords": [
|
|
68
|
+
"tanstack-intent"
|
|
69
|
+
],
|
|
46
70
|
"intent": {
|
|
47
71
|
"version": 1,
|
|
48
72
|
"repo": "@softr/sdk",
|
|
@@ -3,13 +3,14 @@ name: softr-sdk
|
|
|
3
3
|
description: >-
|
|
4
4
|
Entry point for @softr/sdk: its four entrypoints (@softr/sdk, /server,
|
|
5
5
|
/client, /vite) and their client-server boundary, softrAuthMiddleware and the
|
|
6
|
-
Softr context, embedding the app in another site's iframe,
|
|
7
|
-
covers each task. Load it when unsure which sub-skill
|
|
8
|
-
the wiring,
|
|
6
|
+
Softr context, embedding the app in another site's iframe, the app's favicon,
|
|
7
|
+
and which sub-skill covers each task. Load it when unsure which sub-skill
|
|
8
|
+
applies, when touching the wiring, when the user wants the app embedded
|
|
9
|
+
elsewhere, or when the favicon set in Studio does not show.
|
|
9
10
|
metadata:
|
|
10
11
|
type: core
|
|
11
12
|
library: softr-sdk
|
|
12
|
-
library_version: 1.
|
|
13
|
+
library_version: 1.3.0
|
|
13
14
|
---
|
|
14
15
|
|
|
15
16
|
# Softr SDK core
|
|
@@ -32,7 +33,7 @@ Each sub-skill stands alone for its task and repeats the boundary above, so most
|
|
|
32
33
|
|
|
33
34
|
| Import | Runs | Holds |
|
|
34
35
|
| --- | --- | --- |
|
|
35
|
-
| `@softr/sdk` | Both | `defineQuery`, `defineMutation`, `defineAiAction`, `definePage`, `pageHead`, `defineVisibility`, `guardRoute`, `assertRouteAccess`, `isAuthorized`, `checkAccess`, `Audience`, `useCurrentUser`, `useUserTimezone`, `FormattedDate`, `FormattingScope`, `useFormatting`, `formatDate`, `formatDateTime`, `formatNumber`, `formatValue`, `formatField`, `fieldShape`, `registerSoftrFields`, `useOverrides`, `SoftrPwaHead`, `usePwaInstall`, `usePushNotifications`, `getSoftrContext`, `initialSoftrContext`, `softrAuthMiddleware`, the error predicates, and every type |
|
|
36
|
+
| `@softr/sdk` | Both | `defineQuery`, `defineMutation`, `defineAiAction`, `definePage`, `pageHead`, `defineVisibility`, `guardRoute`, `assertRouteAccess`, `isAuthorized`, `checkAccess`, `Audience`, `useCurrentUser`, `useUserTimezone`, `useIdleTimeout`, `FormattedDate`, `FormattingScope`, `useFormatting`, `formatDate`, `formatDateTime`, `formatNumber`, `formatValue`, `formatField`, `fieldShape`, `registerSoftrFields`, `useOverrides`, `SoftrPwaHead`, `usePwaInstall`, `usePushNotifications`, `getSoftrContext`, `initialSoftrContext`, `softrAuthMiddleware`, the error predicates, and every type |
|
|
36
37
|
| `@softr/sdk/server` | Server only | `ds`, `auth`, `workflow`, `integration`, `ai` |
|
|
37
38
|
| `@softr/sdk/client` | Browser only | `useFileUpload` |
|
|
38
39
|
| `@softr/sdk/vite` | Build | `softr()`, already in `vite.config.ts` |
|
|
@@ -104,6 +105,9 @@ Letting visitors turn on push notifications, or "notify users when …"?
|
|
|
104
105
|
Showing a date, a time or a number on a page?
|
|
105
106
|
load softr-sdk/formatting
|
|
106
107
|
|
|
108
|
+
The favicon set in Studio does not show?
|
|
109
|
+
read "The app's favicon" below
|
|
110
|
+
|
|
107
111
|
Writing a server function, loader, or middleware in general?
|
|
108
112
|
not a Softr concern. Use the TanStack Start skills.
|
|
109
113
|
```
|
|
@@ -166,3 +170,11 @@ type User = {
|
|
|
166
170
|
## Embedding the app in another site
|
|
167
171
|
|
|
168
172
|
The published app refuses to load inside another site's iframe. The Studio preview is not affected. When the user asks to embed the app on their site, list that site's origin in `softr.json`: `"allowEmbedding": ["https://partner.com"]`. Ask for the exact origin if you don't have it. Only listed sites can embed the app. Use `["*"]` only when the user wants any site to embed it. Remove the list to block embedding again. Sign-in then works inside the embed. Session cookies switch from SameSite=Lax to None, so the app loses the browser's CSRF protection. Only set it when the user asks. There are two more limits to tell the user: Safari may not keep the visitor signed in, and links from emails (magic link, password reset) open the app in a new tab, not in the embed.
|
|
173
|
+
|
|
174
|
+
## The app's favicon
|
|
175
|
+
|
|
176
|
+
The app's favicon is a Studio setting: Settings → General → Favicon. The app shows it, and shows the Softr icon while the setting is empty. The preview shows a change with no publish. The published app shows it after the next publish.
|
|
177
|
+
|
|
178
|
+
A `rel: 'icon'` link in `head.links` or a `public/favicon.ico` file wins over the setting. While the code has one, the Studio setting has no effect.
|
|
179
|
+
|
|
180
|
+
When the user asks why the Studio favicon does not show, check the code for an icon. When only the published app lacks it, the app needs a new publish. The published app keeps the code, the SDK and the favicon of its last publish.
|
|
@@ -13,7 +13,7 @@ description: >-
|
|
|
13
13
|
metadata:
|
|
14
14
|
type: sub-skill
|
|
15
15
|
library: softr-sdk
|
|
16
|
-
library_version: 1.
|
|
16
|
+
library_version: 1.3.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.
|
|
@@ -9,7 +9,7 @@ description: >-
|
|
|
9
9
|
metadata:
|
|
10
10
|
type: sub-skill
|
|
11
11
|
library: softr-sdk
|
|
12
|
-
library_version: 1.
|
|
12
|
+
library_version: 1.3.0
|
|
13
13
|
---
|
|
14
14
|
|
|
15
15
|
**Boundary.** `auth` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build.
|
|
@@ -9,7 +9,7 @@ description: >-
|
|
|
9
9
|
metadata:
|
|
10
10
|
type: sub-skill
|
|
11
11
|
library: softr-sdk
|
|
12
|
-
library_version: 1.
|
|
12
|
+
library_version: 1.3.0
|
|
13
13
|
---
|
|
14
14
|
|
|
15
15
|
**Boundary.** `ds` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler or a server-side route hook — importing it into a component fails the build. `defineQuery` 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.
|
|
10
|
+
library_version: 1.3.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.
|
|
@@ -7,7 +7,7 @@ description: >-
|
|
|
7
7
|
metadata:
|
|
8
8
|
type: sub-skill
|
|
9
9
|
library: softr-sdk
|
|
10
|
-
library_version: 1.
|
|
10
|
+
library_version: 1.3.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.
|
|
12
|
+
library_version: 1.3.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.
|
|
@@ -2,21 +2,21 @@
|
|
|
2
2
|
name: pwa
|
|
3
3
|
description: >-
|
|
4
4
|
Let visitors install the app on their home screen: SoftrPwaHead for the
|
|
5
|
-
manifest
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
manifest and icon links, usePwaInstall to decide whether to show an install
|
|
6
|
+
button and which platform's steps to show, the PwaInstall and PwaPlatform
|
|
7
|
+
types, and how to check that the app is installable. The manifest and icon are
|
|
8
|
+
already served; never write them.
|
|
9
9
|
metadata:
|
|
10
10
|
type: sub-skill
|
|
11
11
|
library: softr-sdk
|
|
12
|
-
library_version: 1.
|
|
12
|
+
library_version: 1.3.0
|
|
13
13
|
---
|
|
14
14
|
|
|
15
15
|
# Installing the app
|
|
16
16
|
|
|
17
17
|
The builder turns installing on in Studio, under Settings → Mobile app, and sets the name, colour and icon there. The app already answers with its manifest and icon once they do. What the app adds is two things: the links that point the browser at them, and a button that tells visitors how to install.
|
|
18
18
|
|
|
19
|
-
> **CRITICAL**: Never write a web app manifest, icon files, a service worker, or `manifest` / `apple-touch-icon`
|
|
19
|
+
> **CRITICAL**: Never write a web app manifest, icon files, a service worker, or `manifest` / `apple-touch-icon` links of your own, in `public/` or in `head()`. The app already serves them from Studio's settings, and a file of your own shadows those settings.
|
|
20
20
|
>
|
|
21
21
|
> **CRITICAL**: Render an install button only while `usePwaInstall()` returns `available: true`. Never show it unconditionally, never cache the answer, never replace the check with your own. Softr can switch installing off for an app, and this check is the only way that reaches the button.
|
|
22
22
|
|
|
@@ -48,7 +48,7 @@ The builder switches installing on in Studio, and `usePwaInstall()` reads that s
|
|
|
48
48
|
|
|
49
49
|
### The links: `<SoftrPwaHead />`
|
|
50
50
|
|
|
51
|
-
Render it once, in `<head>` next to `<HeadContent />`, in `RootDocument` in `src/routes/__root.tsx`.
|
|
51
|
+
Render it once, in `<head>` next to `<HeadContent />`, in `RootDocument` in `src/routes/__root.tsx`.
|
|
52
52
|
|
|
53
53
|
```tsx
|
|
54
54
|
function RootDocument({ children }: { children: React.ReactNode }) {
|
|
@@ -9,7 +9,7 @@ description: >-
|
|
|
9
9
|
metadata:
|
|
10
10
|
type: sub-skill
|
|
11
11
|
library: softr-sdk
|
|
12
|
-
library_version: 1.
|
|
12
|
+
library_version: 1.3.0
|
|
13
13
|
---
|
|
14
14
|
|
|
15
15
|
**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.
|
|
@@ -10,7 +10,7 @@ description: >-
|
|
|
10
10
|
metadata:
|
|
11
11
|
type: sub-skill
|
|
12
12
|
library: softr-sdk
|
|
13
|
-
library_version: 1.
|
|
13
|
+
library_version: 1.3.0
|
|
14
14
|
---
|
|
15
15
|
|
|
16
16
|
**Boundary.** `ds` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler or a server-side route hook — importing it into a component fails the build. The same holds for any helper that calls `ds`: declare it inside the handler. A function at module level that calls `ds` keeps the server-only import in the browser bundle, and every page stops loading until it moves. `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.
|
|
12
|
+
library_version: 1.3.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.
|
|
12
|
+
library_version: 1.3.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.
|