@softr/sdk 0.0.59 → 0.0.61

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.
@@ -3,7 +3,8 @@ import { readUserTimezone, writeCookies } from "../cookies.js";
3
3
  import { sessionConfig } from "./config.js";
4
4
  import { clearSession, makeSessionDataCookie, readSessionData, readSessionToken } from "./session.js";
5
5
  import { makePlatform } from "./platform.js";
6
- import { defaultFormatting, readLocaleSettings, resolveFormatting } from "../formatting.js";
6
+ import { defaultFormatting, resolveFormatting } from "../formatting.js";
7
+ import { readLocaleSettings } from "../locale-settings.js";
7
8
  import { bootErrorMiddleware } from "../boot-error.js";
8
9
  import { pwaMiddleware } from "../pwa.js";
9
10
  import { createMiddleware, createServerFn, getGlobalStartContext } from "@tanstack/react-start";
@@ -1,10 +1,5 @@
1
1
  import { logger } from "./logger.js";
2
- import { fetch } from "./fetch.js";
3
- import { readRequestCookies, serializeCookie, writeCookies } from "./cookies.js";
4
- import { sessionConfig } from "./auth/config.js";
5
- import { platformApiUrl } from "./auth/platform.js";
6
2
  import { Fragment, createContext, createElement, useContext, useMemo, useSyncExternalStore } from "react";
7
- import { getRequestProtocol } from "@tanstack/react-start/server";
8
3
  import { z } from "zod";
9
4
  import { rootRouteId, useRouteContext } from "@tanstack/react-router";
10
5
 
@@ -64,43 +59,6 @@ const localeSettingsSchema = z.object({
64
59
  rtl: z.boolean()
65
60
  })
66
61
  });
67
- const LOCALE_SETTINGS = "locale_settings";
68
- const LOCALE_SETTINGS_TTL_SECONDS = 300;
69
- /**
70
- * The app's locale settings, or `null` when they cannot be read. Never
71
- * rejects: a failure formats with the visitor's locale and is not remembered,
72
- * so the next request tries again.
73
- */
74
- async function readLocaleSettings() {
75
- const cached = parseLocaleSettings(readRequestCookies()[LOCALE_SETTINGS]);
76
- if (cached !== null) return cached;
77
- try {
78
- const settings = await fetchLocaleSettings();
79
- writeCookies([serializeCookie(LOCALE_SETTINGS, JSON.stringify(settings), {
80
- maxAge: LOCALE_SETTINGS_TTL_SECONDS,
81
- secure: getRequestProtocol() === "https"
82
- })]);
83
- return settings;
84
- } catch (error) {
85
- logger.warn("Could not read the locale settings:", error);
86
- return null;
87
- }
88
- }
89
- /** The settings in `cookie`, or `null` for anything else. The cookie is visitor input. */
90
- function parseLocaleSettings(cookie) {
91
- if (cookie === void 0) return null;
92
- try {
93
- return localeSettingsSchema.safeParse(JSON.parse(cookie)).data ?? null;
94
- } catch {
95
- return null;
96
- }
97
- }
98
- async function fetchLocaleSettings() {
99
- const { applicationId } = sessionConfig();
100
- const response = await fetch(`${await platformApiUrl()}/v1/vibe/applications/${applicationId}/locale-settings`, { headers: { accept: "application/json" } });
101
- if (!response.ok) throw new Error(`The platform answered ${response.status}`);
102
- return localeSettingsSchema.parse(await response.json());
103
- }
104
62
  /**
105
63
  * Combines the app's settings with what the request says about the visitor.
106
64
  * A `FIXED` setting wins. A `LOCAL` one uses the request: `Accept-Language`
@@ -376,4 +334,4 @@ function validTimeZone(zone) {
376
334
  }
377
335
 
378
336
  //#endregion
379
- export { FormattedDate, FormattingScope, defaultFormatting, formatDate, formatDateTime, formatNumber, readLocaleSettings, resolveFormatting, useFormatting };
337
+ export { FormattedDate, FormattingScope, defaultFormatting, formatDate, formatDateTime, formatNumber, localeSettingsSchema, resolveFormatting, useFormatting };
@@ -0,0 +1,51 @@
1
+ import { logger } from "./logger.js";
2
+ import { fetch } from "./fetch.js";
3
+ import { readRequestCookies, serializeCookie, writeCookies } from "./cookies.js";
4
+ import { sessionConfig } from "./auth/config.js";
5
+ import { platformApiUrl } from "./auth/platform.js";
6
+ import { localeSettingsSchema } from "./formatting.js";
7
+ import { getRequestProtocol } from "@tanstack/react-start/server";
8
+
9
+ //#region src/locale-settings.ts
10
+ const LOCALE_SETTINGS = "locale_settings";
11
+ const LOCALE_SETTINGS_TTL_SECONDS = 300;
12
+ /**
13
+ * The app's locale settings, or `null` when they cannot be read. Never
14
+ * rejects: a failure formats with the visitor's locale and is not remembered,
15
+ * so the next request tries again.
16
+ */
17
+ async function readLocaleSettings() {
18
+ const cache = process.env.NODE_ENV === "production";
19
+ const cached = cache ? parseLocaleSettings(readRequestCookies()[LOCALE_SETTINGS]) : null;
20
+ if (cached !== null) return cached;
21
+ try {
22
+ const settings = await fetchLocaleSettings();
23
+ if (!cache) return settings;
24
+ writeCookies([serializeCookie(LOCALE_SETTINGS, JSON.stringify(settings), {
25
+ maxAge: LOCALE_SETTINGS_TTL_SECONDS,
26
+ secure: getRequestProtocol() === "https"
27
+ })]);
28
+ return settings;
29
+ } catch (error) {
30
+ logger.warn("Could not read the locale settings:", error);
31
+ return null;
32
+ }
33
+ }
34
+ /** The settings in `cookie`, or `null` for anything else. The cookie is visitor input. */
35
+ function parseLocaleSettings(cookie) {
36
+ if (cookie === void 0) return null;
37
+ try {
38
+ return localeSettingsSchema.safeParse(JSON.parse(cookie)).data ?? null;
39
+ } catch {
40
+ return null;
41
+ }
42
+ }
43
+ async function fetchLocaleSettings() {
44
+ const { applicationId } = sessionConfig();
45
+ const response = await fetch(`${await platformApiUrl()}/v1/vibe/applications/${applicationId}/locale-settings`, { headers: { accept: "application/json" } });
46
+ if (!response.ok) throw new Error(`The platform answered ${response.status}`);
47
+ return localeSettingsSchema.parse(await response.json());
48
+ }
49
+
50
+ //#endregion
51
+ export { readLocaleSettings };
package/dist/package.js CHANGED
@@ -1,5 +1,5 @@
1
1
  //#region package.json
2
- var version = "0.0.59";
2
+ var version = "0.0.61";
3
3
 
4
4
  //#endregion
5
5
  export { version };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@softr/sdk",
3
- "version": "0.0.59",
3
+ "version": "0.0.61",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" },
@@ -11,7 +11,7 @@ description: >-
11
11
  metadata:
12
12
  type: core
13
13
  library: softr-sdk
14
- library_version: 0.0.58
14
+ library_version: 0.0.61
15
15
  ---
16
16
 
17
17
  # Softr SDK core
@@ -16,7 +16,7 @@ description: >-
16
16
  metadata:
17
17
  type: sub-skill
18
18
  library: softr-sdk
19
- library_version: 0.0.58
19
+ library_version: 0.0.61
20
20
  ---
21
21
 
22
22
  **Boundary.** `auth` lives on `@softr/sdk/server` and runs only inside a `createServerFn` handler — importing it into a component fails the build.
@@ -12,7 +12,7 @@ description: >-
12
12
  metadata:
13
13
  type: sub-skill
14
14
  library: softr-sdk
15
- library_version: 0.0.58
15
+ library_version: 0.0.61
16
16
  ---
17
17
 
18
18
  **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.58
12
+ library_version: 0.0.61
13
13
  ---
14
14
 
15
15
  # The current user
@@ -10,7 +10,7 @@ description: >-
10
10
  metadata:
11
11
  type: sub-skill
12
12
  library: softr-sdk
13
- library_version: 0.0.58
13
+ library_version: 0.0.61
14
14
  ---
15
15
 
16
16
  **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.
@@ -10,7 +10,7 @@ description: >-
10
10
  metadata:
11
11
  type: sub-skill
12
12
  library: softr-sdk
13
- library_version: 0.0.58
13
+ library_version: 0.0.61
14
14
  ---
15
15
 
16
16
  # Dates, times and numbers
@@ -10,7 +10,7 @@ description: >-
10
10
  metadata:
11
11
  type: sub-skill
12
12
  library: softr-sdk
13
- library_version: 0.0.58
13
+ library_version: 0.0.61
14
14
  ---
15
15
 
16
16
  **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.58
12
+ library_version: 0.0.61
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.58
12
+ library_version: 0.0.61
13
13
  ---
14
14
 
15
15
  # Installing the app
@@ -11,7 +11,7 @@ description: >-
11
11
  metadata:
12
12
  type: sub-skill
13
13
  library: softr-sdk
14
- library_version: 0.0.58
14
+ library_version: 0.0.61
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. `defineMutation` and every type come from `@softr/sdk` and can be imported anywhere.
@@ -13,7 +13,7 @@ description: >-
13
13
  metadata:
14
14
  type: sub-skill
15
15
  library: softr-sdk
16
- library_version: 0.0.58
16
+ library_version: 0.0.61
17
17
  ---
18
18
 
19
19
  **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.58
12
+ library_version: 0.0.61
13
13
  ---
14
14
 
15
15
  **Boundary.** `Audience` and `defineVisibility` come from `@softr/sdk` and work anywhere.
@@ -13,7 +13,7 @@ description: >-
13
13
  metadata:
14
14
  type: sub-skill
15
15
  library: softr-sdk
16
- library_version: 0.0.58
16
+ library_version: 0.0.61
17
17
  ---
18
18
 
19
19
  **Boundary.** `defineVisibility`, 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.58
12
+ library_version: 0.0.61
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.