@softr/sdk 0.0.100-alpha.2 → 0.0.101-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/dist/cache.js ADDED
@@ -0,0 +1,25 @@
1
+ import { logger } from "./logger.js";
2
+
3
+ //#region src/cache.ts
4
+ /**
5
+ * Reads through a short cache. Never rejects: a failure is logged, reads as
6
+ * `null` and is not remembered, so the next request tries again.
7
+ */
8
+ function readCached(what, ttlMs, current, store, load) {
9
+ const now = Date.now();
10
+ const cached = current();
11
+ if (cached !== void 0 && cached.expires > now) return cached.value;
12
+ const entry = {
13
+ expires: now + ttlMs,
14
+ value: load().catch((error) => {
15
+ logger.warn(`Could not read ${what}:`, error);
16
+ if (current() === entry) store(void 0);
17
+ return null;
18
+ })
19
+ };
20
+ store(entry);
21
+ return entry.value;
22
+ }
23
+
24
+ //#endregion
25
+ export { readCached };
@@ -1,5 +1,5 @@
1
1
  import { z } from "zod";
2
- import * as react0 from "react";
2
+ import * as react2 from "react";
3
3
  import { ReactNode } from "react";
4
4
 
5
5
  //#region src/formatting.d.ts
@@ -94,7 +94,7 @@ declare function FormattingScope({
94
94
  /** A BCP 47 locale, such as `de-DE`. It drops the app's fixed date format inside the scope. */
95
95
  locale?: string;
96
96
  children?: ReactNode;
97
- }): react0.FunctionComponentElement<react0.ProviderProps<Scope>>;
97
+ }): react2.FunctionComponentElement<react2.ProviderProps<Scope>>;
98
98
  /**
99
99
  * A date in the app's format, safe to render on the server. A timestamp whose
100
100
  * zone is not known yet renders `fallback` until the browser supplies it, so
@@ -115,6 +115,6 @@ declare function FormattedDate({
115
115
  time?: boolean;
116
116
  /** What renders while the visitor's zone is unknown. */
117
117
  fallback?: ReactNode;
118
- }): react0.FunctionComponentElement<react0.FragmentProps>;
118
+ }): react2.FunctionComponentElement<react2.FragmentProps>;
119
119
  //#endregion
120
120
  export { DateOptions, DateParts, DateSeparator, DateValue, FixedDateFormat, FormattedDate, Formatting, FormattingScope, WeekDay, formatDate, formatDateTime, formatNumber, useFormatting };
package/dist/package.js CHANGED
@@ -1,5 +1,5 @@
1
1
  //#region package.json
2
- var version = "0.0.100-alpha.2";
2
+ var version = "0.0.101-alpha.0";
3
3
 
4
4
  //#endregion
5
5
  export { version };
package/dist/push.js CHANGED
@@ -1,9 +1,9 @@
1
- import { logger } from "./logger.js";
2
1
  import { softrApiUrl } from "./softr-api.js";
3
2
  import { fetch } from "./fetch.js";
4
3
  import { sessionConfig } from "./auth/config.js";
5
4
  import { readSessionToken } from "./auth/session.js";
6
- import { detectPlatform } from "./pwa.js";
5
+ import { readCached } from "./cache.js";
6
+ import { detectPlatform, isStandalone } from "./pwa.js";
7
7
  import { z } from "zod";
8
8
  import { useCallback, useEffect, useState } from "react";
9
9
  import { createMiddleware } from "@tanstack/react-start";
@@ -35,31 +35,12 @@ const browserSubscriptionSchema = z.object({
35
35
  });
36
36
  let cachedSettings;
37
37
  let cachedWorker;
38
- /**
39
- * Reads through a one-minute cache. Never rejects: a failure reads as `null`
40
- * and is not remembered, so the next request tries again.
41
- */
42
- function readCached(current, store, load) {
43
- const now = Date.now();
44
- const cached = current();
45
- if (cached !== void 0 && cached.expires > now) return cached.value;
46
- const entry = {
47
- expires: now + TTL_MS,
48
- value: load().catch((error) => {
49
- logger.warn("Could not read push notifications from Softr:", error);
50
- if (current() === entry) store(void 0);
51
- return null;
52
- })
53
- };
54
- store(entry);
55
- return entry.value;
56
- }
57
38
  function platformPushUrl() {
58
39
  const { applicationId } = sessionConfig();
59
40
  return `${softrApiUrl("SOFTR_PLATFORM_API_URL")}/v1/vibe/applications/${applicationId}/push`;
60
41
  }
61
42
  function readSettings() {
62
- return readCached(() => cachedSettings, (entry) => cachedSettings = entry, async () => {
43
+ return readCached("push notifications from Softr", TTL_MS, () => cachedSettings, (entry) => cachedSettings = entry, async () => {
63
44
  const response = await fetch(platformPushUrl(), { headers: { accept: "application/json" } });
64
45
  if (response.status === 404) return null;
65
46
  if (!response.ok) throw new Error(`The platform answered ${response.status}`);
@@ -67,7 +48,7 @@ function readSettings() {
67
48
  });
68
49
  }
69
50
  function readWorker() {
70
- return readCached(() => cachedWorker, (entry) => cachedWorker = entry, async () => {
51
+ return readCached("the push service worker from Softr", TTL_MS, () => cachedWorker, (entry) => cachedWorker = entry, async () => {
71
52
  const response = await fetch(`${softrApiUrl("SOFTR_PLATFORM_API_URL")}/pwa/sw.js`);
72
53
  if (!response.ok) throw new Error(`The platform answered ${response.status}`);
73
54
  return response.text();
@@ -103,7 +84,7 @@ function pushMiddleware() {
103
84
  if (token === null) return new Response(null, { status: 401 });
104
85
  const body = z.object({ subscription: browserSubscriptionSchema }).safeParse(await request.json().catch(() => null));
105
86
  if (!body.success) return new Response(null, { status: 400 });
106
- const response = request.method === "POST" ? await fetch(platformPushUrl(), {
87
+ const response = await (request.method === "POST" ? fetch(platformPushUrl(), {
107
88
  method: "POST",
108
89
  headers: {
109
90
  "content-type": "application/json",
@@ -114,10 +95,11 @@ function pushMiddleware() {
114
95
  userAgent: request.headers.get("user-agent"),
115
96
  subscription: body.data.subscription
116
97
  })
117
- }) : await fetch(`${platformPushUrl()}?${new URLSearchParams({ endpoint: body.data.subscription.endpoint })}`, {
98
+ }) : fetch(`${platformPushUrl()}?${new URLSearchParams({ endpoint: body.data.subscription.endpoint })}`, {
118
99
  method: "DELETE",
119
100
  headers: { authorization: `Bearer ${token}` }
120
- });
101
+ })).catch(() => null);
102
+ if (response === null) return new Response(null, { status: 502 });
121
103
  return new Response(null, { status: response.ok ? 204 : response.status });
122
104
  }
123
105
  return next();
@@ -153,6 +135,9 @@ function usePushNotifications() {
153
135
  useEffect(() => {
154
136
  if (process.env.NODE_ENV !== "production" || !("serviceWorker" in navigator)) return;
155
137
  if (!signedIn) {
138
+ try {
139
+ localStorage.removeItem(DISMISSED_AT_KEY);
140
+ } catch {}
156
141
  setDismissedAt(null);
157
142
  setSettings(null);
158
143
  setSubscribed(false);
@@ -172,17 +157,13 @@ function usePushNotifications() {
172
157
  if (cancelled) return;
173
158
  setRegistration(registered);
174
159
  if (Notification.permission !== "granted") return;
175
- if (await registered.pushManager.getSubscription() !== null) {
176
- if (!cancelled) setSubscribed(true);
177
- return;
178
- }
179
- const subscription = await registered.pushManager.subscribe({
160
+ const subscription = await registered.pushManager.getSubscription() ?? await registered.pushManager.subscribe({
180
161
  userVisibleOnly: true,
181
162
  applicationServerKey: decodeBase64Url(parsed.data.vapidPublicKey)
182
163
  });
183
164
  if (cancelled) return;
184
165
  setSubscribed(await sendSubscription("POST", subscription));
185
- })().catch((error) => logger.warn("Could not set up push notifications:", error));
166
+ })().catch((error) => console.warn("[@softr/sdk] Could not set up push notifications:", error));
186
167
  return () => {
187
168
  cancelled = true;
188
169
  };
@@ -257,7 +238,7 @@ async function sendSubscription(method, subscription) {
257
238
  function isDismissed(dismissedAt, skipDays) {
258
239
  if (dismissedAt === null) return false;
259
240
  if (skipDays === null) return true;
260
- return Date.now() - dismissedAt < skipDays * 24 * 60 * 60 * 1e3;
241
+ return Date.now() - dismissedAt < Math.max(skipDays, 1) * 24 * 60 * 60 * 1e3;
261
242
  }
262
243
  function readDismissedAt() {
263
244
  try {
@@ -267,9 +248,6 @@ function readDismissedAt() {
267
248
  return null;
268
249
  }
269
250
  }
270
- function isStandalone() {
271
- return window.matchMedia("(display-mode: standalone)").matches || "standalone" in navigator && navigator.standalone === true;
272
- }
273
251
  /**
274
252
  * Answers the service worker, which routes a notification click to the
275
253
  * installed app when it is open, and keeps the app icon's badge right.
package/dist/pwa.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import * as react4 from "react";
1
+ import * as react0 from "react";
2
2
  import "@tanstack/react-start";
3
3
 
4
4
  //#region src/pwa.d.ts
@@ -23,7 +23,7 @@ import "@tanstack/react-start";
23
23
  * )
24
24
  * }
25
25
  */
26
- declare function SoftrPwaHead(): react4.FunctionComponentElement<react4.FragmentProps>;
26
+ declare function SoftrPwaHead(): react0.FunctionComponentElement<react0.FragmentProps>;
27
27
  type PwaPlatform = "ios" | "android" | "desktop";
28
28
  type PwaInstall = {
29
29
  available: false;
package/dist/pwa.js CHANGED
@@ -2,6 +2,7 @@ import { logger } from "./logger.js";
2
2
  import { softrApiUrl } from "./softr-api.js";
3
3
  import { fetch } from "./fetch.js";
4
4
  import { sessionConfig } from "./auth/config.js";
5
+ import { readCached } from "./cache.js";
5
6
  import { z } from "zod";
6
7
  import { Fragment, createElement, useEffect, useState } from "react";
7
8
  import { createMiddleware } from "@tanstack/react-start";
@@ -20,24 +21,9 @@ const pwaSchema = z.object({
20
21
  })
21
22
  });
22
23
  let cached;
23
- /**
24
- * The app's PWA, or `null` when it is not installable right now. Never
25
- * rejects: a failure reads as not installable and is not remembered, so the
26
- * next request tries again.
27
- */
24
+ /** The app's PWA, or `null` when it is not installable right now. */
28
25
  function readPwa() {
29
- const now = Date.now();
30
- if (cached !== void 0 && cached.expires > now) return cached.pwa;
31
- const entry = {
32
- expires: now + TTL_MS,
33
- pwa: fetchPwa().catch((error) => {
34
- logger.warn("Could not read the PWA settings:", error);
35
- if (cached === entry) cached = void 0;
36
- return null;
37
- })
38
- };
39
- cached = entry;
40
- return entry.pwa;
26
+ return readCached("the PWA settings", TTL_MS, () => cached, (entry) => cached = entry, fetchPwa);
41
27
  }
42
28
  async function fetchPwa() {
43
29
  const { applicationId } = sessionConfig();
@@ -123,7 +109,7 @@ const installSchema = pwaSchema.shape.install;
123
109
  function usePwaInstall() {
124
110
  const [install, setInstall] = useState({ available: false });
125
111
  useEffect(() => {
126
- if (window.matchMedia("(display-mode: standalone)").matches || "standalone" in navigator && navigator.standalone === true) return;
112
+ if (isStandalone()) return;
127
113
  let cancelled = false;
128
114
  globalThis.fetch(INSTALL_PATH, { headers: { accept: "application/json" } }).then(async (response) => {
129
115
  if (!response.ok) return;
@@ -147,6 +133,9 @@ function detectPlatform() {
147
133
  if (/Android/.test(userAgent)) return "android";
148
134
  return "desktop";
149
135
  }
136
+ function isStandalone() {
137
+ return window.matchMedia("(display-mode: standalone)").matches || "standalone" in navigator && navigator.standalone === true;
138
+ }
150
139
 
151
140
  //#endregion
152
- export { SoftrPwaHead, detectPlatform, pwaMiddleware, usePwaInstall };
141
+ export { SoftrPwaHead, detectPlatform, isStandalone, pwaMiddleware, usePwaInstall };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@softr/sdk",
3
- "version": "0.0.100-alpha.2",
3
+ "version": "0.0.101-alpha.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" },
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: core
11
11
  library: softr-sdk
12
- library_version: 0.0.100-alpha.2
12
+ library_version: 0.0.101-alpha.0
13
13
  ---
14
14
 
15
15
  # Softr SDK core
@@ -13,7 +13,7 @@ description: >-
13
13
  metadata:
14
14
  type: sub-skill
15
15
  library: softr-sdk
16
- library_version: 0.0.100-alpha.2
16
+ library_version: 0.0.101-alpha.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: 0.0.100-alpha.2
12
+ library_version: 0.0.101-alpha.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: 0.0.100-alpha.2
12
+ library_version: 0.0.101-alpha.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.
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 0.0.100-alpha.2
12
+ library_version: 0.0.101-alpha.0
13
13
  ---
14
14
 
15
15
  # The current user
@@ -7,7 +7,7 @@ description: >-
7
7
  metadata:
8
8
  type: sub-skill
9
9
  library: softr-sdk
10
- library_version: 0.0.100-alpha.2
10
+ library_version: 0.0.101-alpha.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: 0.0.100-alpha.2
11
+ library_version: 0.0.101-alpha.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: 0.0.100-alpha.2
10
+ library_version: 0.0.101-alpha.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: 0.0.100-alpha.2
12
+ library_version: 0.0.101-alpha.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: 0.0.100-alpha.2
12
+ library_version: 0.0.101-alpha.0
13
13
  ---
14
14
 
15
15
  # Push notifications
@@ -58,7 +58,7 @@ Push works only on the published app. Inside the Studio preview the hook always
58
58
 
59
59
  ### The prompt
60
60
 
61
- Render it once, in `RootDocument` in `src/routes/__root.tsx`, so it shows on every page and the browser stays registered for the visitor. Use the prompt component the template ships when it has one. It already handles each status.
61
+ Render it once, in `RootDocument` in `src/routes/__root.tsx`, so it shows on every page and the browser stays registered for the visitor. Use the prompt component the template ships when it has one. It shows only on `ask`, and shows nothing on `install-first`: on iPhone and iPad, the app's install button does that job (load the Softr SDK `pwa` guidance).
62
62
 
63
63
  Call `usePushNotifications()` in that one component only. Each call keeps state of its own: a second call, in a header or on a settings page, does not see a subscribe made through the first, so the prompt would stay on screen after the visitor said yes.
64
64
 
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 0.0.100-alpha.2
12
+ library_version: 0.0.101-alpha.0
13
13
  ---
14
14
 
15
15
  # Installing the app
@@ -9,7 +9,7 @@ description: >-
9
9
  metadata:
10
10
  type: sub-skill
11
11
  library: softr-sdk
12
- library_version: 0.0.100-alpha.2
12
+ library_version: 0.0.101-alpha.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: 0.0.100-alpha.2
13
+ library_version: 0.0.101-alpha.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: 0.0.100-alpha.2
12
+ library_version: 0.0.101-alpha.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: 0.0.100-alpha.2
12
+ library_version: 0.0.101-alpha.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: 0.0.100-alpha.2
12
+ library_version: 0.0.101-alpha.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.