@softr/sdk 0.0.59 → 0.0.60
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/auth/isomorphic.js +2 -1
- package/dist/formatting.js +1 -43
- package/dist/locale-settings.js +49 -0
- package/dist/package.js +1 -1
- package/package.json +1 -1
package/dist/auth/isomorphic.js
CHANGED
|
@@ -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,
|
|
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";
|
package/dist/formatting.js
CHANGED
|
@@ -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,
|
|
337
|
+
export { FormattedDate, FormattingScope, defaultFormatting, formatDate, formatDateTime, formatNumber, localeSettingsSchema, resolveFormatting, useFormatting };
|
|
@@ -0,0 +1,49 @@
|
|
|
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 cached = parseLocaleSettings(readRequestCookies()[LOCALE_SETTINGS]);
|
|
19
|
+
if (cached !== null) return cached;
|
|
20
|
+
try {
|
|
21
|
+
const settings = await fetchLocaleSettings();
|
|
22
|
+
writeCookies([serializeCookie(LOCALE_SETTINGS, JSON.stringify(settings), {
|
|
23
|
+
maxAge: LOCALE_SETTINGS_TTL_SECONDS,
|
|
24
|
+
secure: getRequestProtocol() === "https"
|
|
25
|
+
})]);
|
|
26
|
+
return settings;
|
|
27
|
+
} catch (error) {
|
|
28
|
+
logger.warn("Could not read the locale settings:", error);
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/** The settings in `cookie`, or `null` for anything else. The cookie is visitor input. */
|
|
33
|
+
function parseLocaleSettings(cookie) {
|
|
34
|
+
if (cookie === void 0) return null;
|
|
35
|
+
try {
|
|
36
|
+
return localeSettingsSchema.safeParse(JSON.parse(cookie)).data ?? null;
|
|
37
|
+
} catch {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
async function fetchLocaleSettings() {
|
|
42
|
+
const { applicationId } = sessionConfig();
|
|
43
|
+
const response = await fetch(`${await platformApiUrl()}/v1/vibe/applications/${applicationId}/locale-settings`, { headers: { accept: "application/json" } });
|
|
44
|
+
if (!response.ok) throw new Error(`The platform answered ${response.status}`);
|
|
45
|
+
return localeSettingsSchema.parse(await response.json());
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
//#endregion
|
|
49
|
+
export { readLocaleSettings };
|
package/dist/package.js
CHANGED