openxiangda 1.0.123 → 1.0.124

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.
@@ -1705,6 +1705,12 @@ interface RuntimeRequestState<T> {
1705
1705
  }
1706
1706
  interface RuntimeReloadOptions {
1707
1707
  accessToken?: string | null;
1708
+ accessTokenOptions?: RuntimeAccessTokenOptions;
1709
+ }
1710
+ interface RuntimeAccessTokenOptions {
1711
+ scope?: "default" | "public";
1712
+ path?: string | null;
1713
+ clearIfToken?: string;
1708
1714
  }
1709
1715
  interface OpenXiangdaProviderProps {
1710
1716
  appType?: string;
@@ -1727,7 +1733,7 @@ interface OpenXiangdaRuntimeStore extends RuntimeRequestState<RuntimeBootstrap>
1727
1733
  fetchImpl: typeof fetch;
1728
1734
  baseFetchImpl: typeof fetch;
1729
1735
  reload: (options?: RuntimeReloadOptions) => Promise<void>;
1730
- setAccessToken: (accessToken?: string | null) => void;
1736
+ setAccessToken: (accessToken?: string | null, options?: RuntimeAccessTokenOptions) => void;
1731
1737
  }
1732
1738
  declare const OpenXiangdaProvider: React__default.FC<OpenXiangdaProviderProps>;
1733
1739
  declare const useOpenXiangda: () => OpenXiangdaRuntimeStore;
@@ -1740,7 +1746,7 @@ declare const useAppMenus: () => {
1740
1746
  fetchImpl: typeof fetch;
1741
1747
  baseFetchImpl: typeof fetch;
1742
1748
  reload: (options?: RuntimeReloadOptions) => Promise<void>;
1743
- setAccessToken: (accessToken?: string | null) => void;
1749
+ setAccessToken: (accessToken?: string | null, options?: RuntimeAccessTokenOptions) => void;
1744
1750
  loading: boolean;
1745
1751
  error: RuntimeRequestError | null;
1746
1752
  };
@@ -1751,7 +1757,7 @@ declare const usePermission: () => {
1751
1757
  fetchImpl: typeof fetch;
1752
1758
  baseFetchImpl: typeof fetch;
1753
1759
  reload: (options?: RuntimeReloadOptions) => Promise<void>;
1754
- setAccessToken: (accessToken?: string | null) => void;
1760
+ setAccessToken: (accessToken?: string | null, options?: RuntimeAccessTokenOptions) => void;
1755
1761
  loading: boolean;
1756
1762
  error: RuntimeRequestError | null;
1757
1763
  };
@@ -1705,6 +1705,12 @@ interface RuntimeRequestState<T> {
1705
1705
  }
1706
1706
  interface RuntimeReloadOptions {
1707
1707
  accessToken?: string | null;
1708
+ accessTokenOptions?: RuntimeAccessTokenOptions;
1709
+ }
1710
+ interface RuntimeAccessTokenOptions {
1711
+ scope?: "default" | "public";
1712
+ path?: string | null;
1713
+ clearIfToken?: string;
1708
1714
  }
1709
1715
  interface OpenXiangdaProviderProps {
1710
1716
  appType?: string;
@@ -1727,7 +1733,7 @@ interface OpenXiangdaRuntimeStore extends RuntimeRequestState<RuntimeBootstrap>
1727
1733
  fetchImpl: typeof fetch;
1728
1734
  baseFetchImpl: typeof fetch;
1729
1735
  reload: (options?: RuntimeReloadOptions) => Promise<void>;
1730
- setAccessToken: (accessToken?: string | null) => void;
1736
+ setAccessToken: (accessToken?: string | null, options?: RuntimeAccessTokenOptions) => void;
1731
1737
  }
1732
1738
  declare const OpenXiangdaProvider: React__default.FC<OpenXiangdaProviderProps>;
1733
1739
  declare const useOpenXiangda: () => OpenXiangdaRuntimeStore;
@@ -1740,7 +1746,7 @@ declare const useAppMenus: () => {
1740
1746
  fetchImpl: typeof fetch;
1741
1747
  baseFetchImpl: typeof fetch;
1742
1748
  reload: (options?: RuntimeReloadOptions) => Promise<void>;
1743
- setAccessToken: (accessToken?: string | null) => void;
1749
+ setAccessToken: (accessToken?: string | null, options?: RuntimeAccessTokenOptions) => void;
1744
1750
  loading: boolean;
1745
1751
  error: RuntimeRequestError | null;
1746
1752
  };
@@ -1751,7 +1757,7 @@ declare const usePermission: () => {
1751
1757
  fetchImpl: typeof fetch;
1752
1758
  baseFetchImpl: typeof fetch;
1753
1759
  reload: (options?: RuntimeReloadOptions) => Promise<void>;
1754
- setAccessToken: (accessToken?: string | null) => void;
1760
+ setAccessToken: (accessToken?: string | null, options?: RuntimeAccessTokenOptions) => void;
1755
1761
  loading: boolean;
1756
1762
  error: RuntimeRequestError | null;
1757
1763
  };
@@ -33736,10 +33736,18 @@ var OpenXiangdaProvider = ({
33736
33736
  const [accessToken, setAccessTokenState] = useState61(null);
33737
33737
  const accessTokenRef = useRef82(null);
33738
33738
  const setAccessToken = useCallback22(
33739
- (nextAccessToken) => {
33739
+ (nextAccessToken, options = {}) => {
33740
+ if (!nextAccessToken && options.clearIfToken && accessTokenRef.current?.token !== options.clearIfToken) {
33741
+ return;
33742
+ }
33740
33743
  const normalizedAccessToken = nextAccessToken || null;
33741
- accessTokenRef.current = normalizedAccessToken;
33742
- setAccessTokenState(normalizedAccessToken);
33744
+ const nextState = normalizedAccessToken ? {
33745
+ token: normalizedAccessToken,
33746
+ scope: options.scope || "default",
33747
+ path: options.path || null
33748
+ } : null;
33749
+ accessTokenRef.current = nextState;
33750
+ setAccessTokenState(nextState);
33743
33751
  },
33744
33752
  []
33745
33753
  );
@@ -33765,7 +33773,13 @@ var OpenXiangdaProvider = ({
33765
33773
  return;
33766
33774
  }
33767
33775
  setState((prev) => ({ ...prev, loading: true, error: null }));
33768
- const requestFetch = options.accessToken !== void 0 ? createAuthorizedFetch(resolvedFetch, options.accessToken || null) : createAuthorizedFetch(resolvedFetch, accessTokenRef.current);
33776
+ const requestFetch = options.accessToken !== void 0 ? createAuthorizedFetch(
33777
+ resolvedFetch,
33778
+ createAccessTokenState(
33779
+ options.accessToken || null,
33780
+ options.accessTokenOptions
33781
+ )
33782
+ ) : createAuthorizedFetch(resolvedFetch, accessTokenRef.current);
33769
33783
  try {
33770
33784
  const response = await requestFetch(
33771
33785
  buildServiceUrl2(
@@ -34175,9 +34189,11 @@ var buildServiceUrl2 = (servicePrefix, path) => {
34175
34189
  var createAuthorizedFetch = (baseFetch, accessToken) => {
34176
34190
  if (!accessToken) return baseFetch;
34177
34191
  return ((input, init = {}) => {
34192
+ const token = resolveAccessTokenForCurrentRoute(accessToken);
34193
+ if (!token) return baseFetch(input, init);
34178
34194
  const headers = new Headers(init.headers || {});
34179
34195
  if (!headers.has("authorization")) {
34180
- headers.set("authorization", `Bearer ${accessToken}`);
34196
+ headers.set("authorization", `Bearer ${token}`);
34181
34197
  }
34182
34198
  return baseFetch(input, {
34183
34199
  ...init,
@@ -34185,6 +34201,34 @@ var createAuthorizedFetch = (baseFetch, accessToken) => {
34185
34201
  });
34186
34202
  });
34187
34203
  };
34204
+ var createAccessTokenState = (accessToken, options = {}) => accessToken ? {
34205
+ token: accessToken,
34206
+ scope: options.scope || "default",
34207
+ path: options.path || null
34208
+ } : null;
34209
+ var resolveAccessTokenForCurrentRoute = (accessToken) => {
34210
+ if (accessToken.scope !== "public") return accessToken.token;
34211
+ const scopedPath = normalizeRoutePath(accessToken.path);
34212
+ const currentPath = normalizeRoutePath(getCurrentPathname());
34213
+ if (!scopedPath) {
34214
+ return isPublicRoutePath(currentPath) ? accessToken.token : null;
34215
+ }
34216
+ if (currentPath === scopedPath || isPublicRoutePath(currentPath) && currentPath.startsWith(`${scopedPath}/`)) {
34217
+ return accessToken.token;
34218
+ }
34219
+ return null;
34220
+ };
34221
+ var normalizeRoutePath = (path) => {
34222
+ const text = String(path || "").trim();
34223
+ if (!text) return "";
34224
+ try {
34225
+ const base = typeof window !== "undefined" ? window.location.origin : "http://localhost";
34226
+ return new URL(text, base).pathname.replace(/\/+$/, "") || "/";
34227
+ } catch {
34228
+ return text.split(/[?#]/, 1)[0].replace(/\/+$/, "") || "/";
34229
+ }
34230
+ };
34231
+ var isPublicRoutePath = (path) => /\/public(?:\/|$)/.test(path);
34188
34232
  var readJsonPayload = async (response) => {
34189
34233
  try {
34190
34234
  return await response.json();
@@ -34285,6 +34329,7 @@ var attachCallback = (loginUrl, callback) => {
34285
34329
  }
34286
34330
  };
34287
34331
  var getCurrentHref4 = () => typeof window === "undefined" ? "" : window.location.href;
34332
+ var getCurrentPathname = () => typeof window === "undefined" ? "" : window.location.pathname;
34288
34333
  var getCurrentHostname2 = () => typeof window === "undefined" ? "" : window.location.hostname;
34289
34334
  var getRuntimeEnv = (key) => {
34290
34335
  const env = typeof process !== "undefined" ? process.env : void 0;
@@ -34355,7 +34400,7 @@ var resolveAppTypeFromLocation = () => {
34355
34400
  };
34356
34401
 
34357
34402
  // packages/sdk/src/runtime/react/publicAccess.tsx
34358
- import { useCallback as useCallback23, useEffect as useEffect62, useMemo as useMemo43, useState as useState62 } from "react";
34403
+ import { useCallback as useCallback23, useEffect as useEffect62, useMemo as useMemo43, useRef as useRef83, useState as useState62 } from "react";
34359
34404
 
34360
34405
  // packages/sdk/src/runtime/core/publicAccess.ts
34361
34406
  var PublicAccessClientError = class extends Error {
@@ -34380,7 +34425,7 @@ var createPublicAccessClient = ({
34380
34425
  const request = async (input = {}) => {
34381
34426
  const body = {
34382
34427
  ...input,
34383
- path: input.path || getCurrentPathname(),
34428
+ path: input.path || getCurrentPathname2(),
34384
34429
  domain: input.domain || getCurrentDomain(),
34385
34430
  userAgent: input.userAgent || getCurrentUserAgent(),
34386
34431
  guestIdentifier: input.guestIdentifier || getOrCreatePublicGuestIdentifier(normalizedAppType)
@@ -34448,7 +34493,7 @@ var isSuccessCode6 = (code) => {
34448
34493
  const normalized = Number(code);
34449
34494
  return Number.isFinite(normalized) ? normalized === 0 || normalized >= 200 && normalized < 300 : false;
34450
34495
  };
34451
- var getCurrentPathname = () => typeof window === "undefined" ? void 0 : window.location.pathname;
34496
+ var getCurrentPathname2 = () => typeof window === "undefined" ? void 0 : window.location.pathname;
34452
34497
  var getCurrentDomain = () => typeof window === "undefined" ? void 0 : window.location.host;
34453
34498
  var getCurrentUserAgent = () => typeof navigator === "undefined" ? void 0 : navigator.userAgent;
34454
34499
  var getOrCreatePublicGuestIdentifier = (appType) => {
@@ -34491,6 +34536,8 @@ var usePublicAccess = (options = {}) => {
34491
34536
  const [session, setSession] = useState62(null);
34492
34537
  const [error, setError] = useState62(null);
34493
34538
  const [loading, setLoading] = useState62(Boolean(autoStart));
34539
+ const activeSessionRef = useRef83(null);
34540
+ const mountedRef = useRef83(true);
34494
34541
  const sessionInputKey = JSON.stringify(sessionInput);
34495
34542
  const stableSessionInput = useMemo43(() => sessionInput, [sessionInputKey]);
34496
34543
  const client = useMemo43(
@@ -34506,15 +34553,31 @@ var usePublicAccess = (options = {}) => {
34506
34553
  setLoading(true);
34507
34554
  setError(null);
34508
34555
  try {
34556
+ const resolvedPath = input.path || stableSessionInput.path || readPathFromLocation();
34509
34557
  const data = await client.startSession({
34510
34558
  ...stableSessionInput,
34511
34559
  ...input,
34512
34560
  ticket: input.ticket || stableSessionInput.ticket || readTicketFromLocation(),
34513
- path: input.path || stableSessionInput.path || readPathFromLocation()
34561
+ path: resolvedPath
34514
34562
  });
34563
+ if (!mountedRef.current) return data;
34564
+ activeSessionRef.current = {
34565
+ accessToken: data.accessToken,
34566
+ path: resolvedPath
34567
+ };
34515
34568
  setSession(data);
34516
- setAccessToken(data.accessToken);
34517
- await reloadRuntime({ accessToken: data.accessToken });
34569
+ setAccessToken(data.accessToken, {
34570
+ scope: "public",
34571
+ path: resolvedPath
34572
+ });
34573
+ await reloadRuntime({
34574
+ accessToken: data.accessToken,
34575
+ accessTokenOptions: {
34576
+ scope: "public",
34577
+ path: resolvedPath
34578
+ }
34579
+ });
34580
+ if (!mountedRef.current) return data;
34518
34581
  setLoading(false);
34519
34582
  return data;
34520
34583
  } catch (caught) {
@@ -34522,8 +34585,10 @@ var usePublicAccess = (options = {}) => {
34522
34585
  caught instanceof Error ? caught.message : "\u516C\u5F00\u8BBF\u95EE\u4F1A\u8BDD\u521B\u5EFA\u5931\u8D25",
34523
34586
  { payload: caught }
34524
34587
  );
34525
- setError(nextError);
34526
- setLoading(false);
34588
+ if (mountedRef.current) {
34589
+ setError(nextError);
34590
+ setLoading(false);
34591
+ }
34527
34592
  throw nextError;
34528
34593
  }
34529
34594
  },
@@ -34533,6 +34598,20 @@ var usePublicAccess = (options = {}) => {
34533
34598
  if (!autoStart) return;
34534
34599
  void startSession().catch(() => void 0);
34535
34600
  }, [autoStart, startSession]);
34601
+ useEffect62(
34602
+ () => {
34603
+ mountedRef.current = true;
34604
+ return () => {
34605
+ mountedRef.current = false;
34606
+ const activeSession = activeSessionRef.current;
34607
+ if (!activeSession) return;
34608
+ activeSessionRef.current = null;
34609
+ setAccessToken(null, { clearIfToken: activeSession.accessToken });
34610
+ void reloadRuntime({ accessToken: null });
34611
+ };
34612
+ },
34613
+ [reloadRuntime, setAccessToken]
34614
+ );
34536
34615
  return {
34537
34616
  loading,
34538
34617
  error,