@zjazj/web 0.0.6 → 0.0.8
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/index.d.ts +8 -2
- package/dist/index.js +72 -7
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -107,8 +107,14 @@ interface Session {
|
|
|
107
107
|
getUser(): CurrentUser | null;
|
|
108
108
|
/** The same object until the state changes, so it can back `useSyncExternalStore`. */
|
|
109
109
|
getStatus(): SessionStatus;
|
|
110
|
-
/**
|
|
111
|
-
|
|
110
|
+
/**
|
|
111
|
+
* The bearer token for API calls, refreshed when needed: a minute before it expires, and
|
|
112
|
+
* when a support session reaches its end. `renew` asks for a new one now, which the
|
|
113
|
+
* client does once when a backend answers `token_expired` or `support_ended`.
|
|
114
|
+
*/
|
|
115
|
+
getAccessToken(options?: {
|
|
116
|
+
renew?: boolean;
|
|
117
|
+
}): Promise<string | null>;
|
|
112
118
|
/** Send the browser to the sign-in page. It comes back to the current location. */
|
|
113
119
|
signIn(): Promise<void>;
|
|
114
120
|
signOut(): Promise<void>;
|
package/dist/index.js
CHANGED
|
@@ -5,6 +5,7 @@ import { registerMessages as registerMessages2 } from "@zjazj/i18n";
|
|
|
5
5
|
var en_US_default = {
|
|
6
6
|
account: "Account",
|
|
7
7
|
signOut: "Sign out",
|
|
8
|
+
profile: "Profile",
|
|
8
9
|
theme: "Theme",
|
|
9
10
|
themeLight: "Light",
|
|
10
11
|
themeDark: "Dark",
|
|
@@ -54,6 +55,7 @@ var en_US_default = {
|
|
|
54
55
|
var zh_CN_default = {
|
|
55
56
|
account: "\u8D26\u53F7",
|
|
56
57
|
signOut: "\u9000\u51FA\u767B\u5F55",
|
|
58
|
+
profile: "\u4E2A\u4EBA\u8D44\u6599",
|
|
57
59
|
theme: "\u4E3B\u9898",
|
|
58
60
|
themeLight: "\u6D45\u8272",
|
|
59
61
|
themeDark: "\u6DF1\u8272",
|
|
@@ -478,10 +480,13 @@ function createPlatformSession(options = {}) {
|
|
|
478
480
|
kind: "platform",
|
|
479
481
|
getUser: () => user,
|
|
480
482
|
getStatus: () => status,
|
|
481
|
-
async getAccessToken() {
|
|
483
|
+
async getAccessToken(options2) {
|
|
482
484
|
await started;
|
|
483
485
|
if (!isReady() || !token) return null;
|
|
484
|
-
|
|
486
|
+
const seconds = now() / 1e3;
|
|
487
|
+
const supportOver = user?.support !== void 0 && seconds >= user.support.until;
|
|
488
|
+
const fresh = token.expiresAt - seconds > EXPIRY_LEEWAY_SECONDS;
|
|
489
|
+
if (fresh && !supportOver && !options2?.renew) return token.value;
|
|
485
490
|
refreshing ??= refresh().finally(() => {
|
|
486
491
|
refreshing = void 0;
|
|
487
492
|
});
|
|
@@ -631,6 +636,7 @@ var ApiError = class _ApiError extends Error {
|
|
|
631
636
|
});
|
|
632
637
|
}
|
|
633
638
|
};
|
|
639
|
+
var RENEWABLE = /* @__PURE__ */ new Set(["token_expired", "support_ended"]);
|
|
634
640
|
async function platformFetch(input, init) {
|
|
635
641
|
if (isTenantChanged()) {
|
|
636
642
|
throw new ApiError({
|
|
@@ -643,13 +649,26 @@ async function platformFetch(input, init) {
|
|
|
643
649
|
const headers = new Headers(
|
|
644
650
|
init?.headers ?? (input instanceof Request ? input.headers : void 0)
|
|
645
651
|
);
|
|
646
|
-
|
|
652
|
+
const ownToken = !headers.has("Authorization");
|
|
653
|
+
const canRepeat = !(typeof ReadableStream === "function" && init?.body instanceof ReadableStream);
|
|
654
|
+
const again = ownToken && canRepeat && input instanceof Request ? input.clone() : input;
|
|
655
|
+
if (ownToken) {
|
|
647
656
|
const token = await session.getAccessToken();
|
|
648
657
|
if (token) headers.set("Authorization", `Bearer ${token}`);
|
|
649
658
|
}
|
|
650
659
|
const response = await fetch(input, { ...init, headers });
|
|
651
660
|
if (response.ok) return response;
|
|
652
|
-
|
|
661
|
+
const error = await ApiError.fromResponse(response);
|
|
662
|
+
if (ownToken && canRepeat && RENEWABLE.has(error.code)) {
|
|
663
|
+
const renewed = await session.getAccessToken({ renew: true });
|
|
664
|
+
if (renewed) {
|
|
665
|
+
headers.set("Authorization", `Bearer ${renewed}`);
|
|
666
|
+
const retried = await fetch(again, { ...init, headers });
|
|
667
|
+
if (retried.ok) return retried;
|
|
668
|
+
throw await ApiError.fromResponse(retried);
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
throw error;
|
|
653
672
|
}
|
|
654
673
|
function createClient(baseUrl = apiBaseUrl()) {
|
|
655
674
|
return hc(baseUrl, { fetch: platformFetch });
|
|
@@ -765,7 +784,16 @@ import {
|
|
|
765
784
|
SidebarTrigger,
|
|
766
785
|
useTheme
|
|
767
786
|
} from "@zjazj/ui";
|
|
768
|
-
import {
|
|
787
|
+
import {
|
|
788
|
+
Building2,
|
|
789
|
+
Check,
|
|
790
|
+
CircleUserRound,
|
|
791
|
+
Languages,
|
|
792
|
+
LayoutGrid,
|
|
793
|
+
LifeBuoy,
|
|
794
|
+
LogOut,
|
|
795
|
+
SunMoon
|
|
796
|
+
} from "lucide-react";
|
|
769
797
|
import { useEffect, useMemo, useState, useSyncExternalStore as useSyncExternalStore2 } from "react";
|
|
770
798
|
|
|
771
799
|
// src/menu.ts
|
|
@@ -996,6 +1024,16 @@ function SupportNotice() {
|
|
|
996
1024
|
}, []);
|
|
997
1025
|
const until = (user.support?.until ?? 0) * 1e3;
|
|
998
1026
|
const minutes = Math.max(0, Math.ceil((until - now) / 6e4));
|
|
1027
|
+
const over = now >= until;
|
|
1028
|
+
useEffect(() => {
|
|
1029
|
+
const wait = until - Date.now() + 1e3;
|
|
1030
|
+
if (wait <= 0) return;
|
|
1031
|
+
const end = window.setTimeout(() => setNow(Date.now()), Math.min(wait, 2147483647));
|
|
1032
|
+
return () => window.clearTimeout(end);
|
|
1033
|
+
}, [until]);
|
|
1034
|
+
useEffect(() => {
|
|
1035
|
+
if (over) void session.getAccessToken();
|
|
1036
|
+
}, [over, session]);
|
|
999
1037
|
const leave = async () => {
|
|
1000
1038
|
setLeaving(true);
|
|
1001
1039
|
try {
|
|
@@ -1013,13 +1051,21 @@ function SupportNotice() {
|
|
|
1013
1051
|
/* @__PURE__ */ jsx2(Button, { variant: "outline", size: "sm", disabled: leaving, onClick: () => void leave(), children: t("supportExit") })
|
|
1014
1052
|
] });
|
|
1015
1053
|
}
|
|
1054
|
+
function profileLink(portalUrl, basePath) {
|
|
1055
|
+
const origin = globalThis.location?.origin ?? "http://localhost";
|
|
1056
|
+
const portal = new URL(portalUrl.replace(/\/*$/, "/"), origin);
|
|
1057
|
+
const href = new URL("profile", portal).href;
|
|
1058
|
+
return { here: portal.origin === origin && portal.pathname === basePath, href };
|
|
1059
|
+
}
|
|
1016
1060
|
function AccountMenu() {
|
|
1017
1061
|
const t = useT("web");
|
|
1018
1062
|
const user = useUser();
|
|
1019
1063
|
const session = useSession();
|
|
1064
|
+
const config = useAppConfig();
|
|
1020
1065
|
const { theme, setTheme } = useTheme();
|
|
1021
1066
|
const [locale, setLocale] = useLocale();
|
|
1022
1067
|
const initials = user.username.trim().slice(0, 2).toUpperCase() || "?";
|
|
1068
|
+
const profile = user.isDev ? void 0 : profileLink(config.portalUrl, config.basePath);
|
|
1023
1069
|
return /* @__PURE__ */ jsxs(DropdownMenu, { children: [
|
|
1024
1070
|
/* @__PURE__ */ jsx2(DropdownMenuTrigger, { asChild: true, children: /* @__PURE__ */ jsx2(Button, { variant: "ghost", size: "icon", className: "rounded-full", "aria-label": t("account"), children: /* @__PURE__ */ jsx2(Avatar, { className: "size-8", children: /* @__PURE__ */ jsx2(AvatarFallback, { children: initials }) }) }) }),
|
|
1025
1071
|
/* @__PURE__ */ jsxs(DropdownMenuContent, { align: "end", className: "w-56", children: [
|
|
@@ -1032,8 +1078,24 @@ function AccountMenu() {
|
|
|
1032
1078
|
] })
|
|
1033
1079
|
] }),
|
|
1034
1080
|
/* @__PURE__ */ jsx2(DropdownMenuSeparator, {}),
|
|
1081
|
+
profile ? /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
1082
|
+
/* @__PURE__ */ jsx2(DropdownMenuItem, { asChild: true, children: profile.here ? /* @__PURE__ */ jsxs(Link, { to: "/profile", children: [
|
|
1083
|
+
/* @__PURE__ */ jsx2(CircleUserRound, {}),
|
|
1084
|
+
t("profile")
|
|
1085
|
+
] }) : (
|
|
1086
|
+
// The portal's own tab, as an app opens in its own (D3).
|
|
1087
|
+
/* @__PURE__ */ jsxs("a", { href: profile.href, target: "platform-portal", children: [
|
|
1088
|
+
/* @__PURE__ */ jsx2(CircleUserRound, {}),
|
|
1089
|
+
t("profile")
|
|
1090
|
+
] })
|
|
1091
|
+
) }),
|
|
1092
|
+
/* @__PURE__ */ jsx2(DropdownMenuSeparator, {})
|
|
1093
|
+
] }) : null,
|
|
1035
1094
|
/* @__PURE__ */ jsxs(DropdownMenuSub, { children: [
|
|
1036
|
-
/* @__PURE__ */
|
|
1095
|
+
/* @__PURE__ */ jsxs(DropdownMenuSubTrigger, { children: [
|
|
1096
|
+
/* @__PURE__ */ jsx2(SunMoon, {}),
|
|
1097
|
+
t("theme")
|
|
1098
|
+
] }),
|
|
1037
1099
|
/* @__PURE__ */ jsx2(DropdownMenuSubContent, { children: /* @__PURE__ */ jsx2(
|
|
1038
1100
|
DropdownMenuRadioGroup,
|
|
1039
1101
|
{
|
|
@@ -1044,7 +1106,10 @@ function AccountMenu() {
|
|
|
1044
1106
|
) })
|
|
1045
1107
|
] }),
|
|
1046
1108
|
/* @__PURE__ */ jsxs(DropdownMenuSub, { children: [
|
|
1047
|
-
/* @__PURE__ */
|
|
1109
|
+
/* @__PURE__ */ jsxs(DropdownMenuSubTrigger, { children: [
|
|
1110
|
+
/* @__PURE__ */ jsx2(Languages, {}),
|
|
1111
|
+
t("language")
|
|
1112
|
+
] }),
|
|
1048
1113
|
/* @__PURE__ */ jsx2(DropdownMenuSubContent, { children: /* @__PURE__ */ jsx2(
|
|
1049
1114
|
DropdownMenuRadioGroup,
|
|
1050
1115
|
{
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zjazj/web",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"description": "Browser runtime for platform apps: session, typed API client, layout shell, guards and hooks.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "UNLICENSED",
|
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
"@logto/browser": "^3.1.2",
|
|
28
28
|
"hono": "^4.13.17",
|
|
29
29
|
"lucide-react": "^0.545.0",
|
|
30
|
-
"@zjazj/
|
|
31
|
-
"@zjazj/
|
|
30
|
+
"@zjazj/i18n": "^0.0.8",
|
|
31
|
+
"@zjazj/ui": "^0.0.8"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
34
|
"@tanstack/react-query": "^5.90.0",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"jsdom": "^26.1.0",
|
|
46
46
|
"react": "^19.2.0",
|
|
47
47
|
"react-dom": "^19.2.0",
|
|
48
|
-
"@zjazj/server": "^0.0.
|
|
48
|
+
"@zjazj/server": "^0.0.8"
|
|
49
49
|
},
|
|
50
50
|
"publishConfig": {
|
|
51
51
|
"access": "public",
|