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.
@@ -34562,10 +34562,18 @@ var OpenXiangdaProvider = ({
34562
34562
  const [accessToken, setAccessTokenState] = (0, import_react216.useState)(null);
34563
34563
  const accessTokenRef = (0, import_react216.useRef)(null);
34564
34564
  const setAccessToken = (0, import_react216.useCallback)(
34565
- (nextAccessToken) => {
34565
+ (nextAccessToken, options = {}) => {
34566
+ if (!nextAccessToken && options.clearIfToken && accessTokenRef.current?.token !== options.clearIfToken) {
34567
+ return;
34568
+ }
34566
34569
  const normalizedAccessToken = nextAccessToken || null;
34567
- accessTokenRef.current = normalizedAccessToken;
34568
- setAccessTokenState(normalizedAccessToken);
34570
+ const nextState = normalizedAccessToken ? {
34571
+ token: normalizedAccessToken,
34572
+ scope: options.scope || "default",
34573
+ path: options.path || null
34574
+ } : null;
34575
+ accessTokenRef.current = nextState;
34576
+ setAccessTokenState(nextState);
34569
34577
  },
34570
34578
  []
34571
34579
  );
@@ -34591,7 +34599,13 @@ var OpenXiangdaProvider = ({
34591
34599
  return;
34592
34600
  }
34593
34601
  setState((prev) => ({ ...prev, loading: true, error: null }));
34594
- const requestFetch = options.accessToken !== void 0 ? createAuthorizedFetch(resolvedFetch, options.accessToken || null) : createAuthorizedFetch(resolvedFetch, accessTokenRef.current);
34602
+ const requestFetch = options.accessToken !== void 0 ? createAuthorizedFetch(
34603
+ resolvedFetch,
34604
+ createAccessTokenState(
34605
+ options.accessToken || null,
34606
+ options.accessTokenOptions
34607
+ )
34608
+ ) : createAuthorizedFetch(resolvedFetch, accessTokenRef.current);
34595
34609
  try {
34596
34610
  const response = await requestFetch(
34597
34611
  buildServiceUrl2(
@@ -35001,9 +35015,11 @@ var buildServiceUrl2 = (servicePrefix, path) => {
35001
35015
  var createAuthorizedFetch = (baseFetch, accessToken) => {
35002
35016
  if (!accessToken) return baseFetch;
35003
35017
  return ((input, init = {}) => {
35018
+ const token = resolveAccessTokenForCurrentRoute(accessToken);
35019
+ if (!token) return baseFetch(input, init);
35004
35020
  const headers = new Headers(init.headers || {});
35005
35021
  if (!headers.has("authorization")) {
35006
- headers.set("authorization", `Bearer ${accessToken}`);
35022
+ headers.set("authorization", `Bearer ${token}`);
35007
35023
  }
35008
35024
  return baseFetch(input, {
35009
35025
  ...init,
@@ -35011,6 +35027,34 @@ var createAuthorizedFetch = (baseFetch, accessToken) => {
35011
35027
  });
35012
35028
  });
35013
35029
  };
35030
+ var createAccessTokenState = (accessToken, options = {}) => accessToken ? {
35031
+ token: accessToken,
35032
+ scope: options.scope || "default",
35033
+ path: options.path || null
35034
+ } : null;
35035
+ var resolveAccessTokenForCurrentRoute = (accessToken) => {
35036
+ if (accessToken.scope !== "public") return accessToken.token;
35037
+ const scopedPath = normalizeRoutePath(accessToken.path);
35038
+ const currentPath = normalizeRoutePath(getCurrentPathname());
35039
+ if (!scopedPath) {
35040
+ return isPublicRoutePath(currentPath) ? accessToken.token : null;
35041
+ }
35042
+ if (currentPath === scopedPath || isPublicRoutePath(currentPath) && currentPath.startsWith(`${scopedPath}/`)) {
35043
+ return accessToken.token;
35044
+ }
35045
+ return null;
35046
+ };
35047
+ var normalizeRoutePath = (path) => {
35048
+ const text = String(path || "").trim();
35049
+ if (!text) return "";
35050
+ try {
35051
+ const base = typeof window !== "undefined" ? window.location.origin : "http://localhost";
35052
+ return new URL(text, base).pathname.replace(/\/+$/, "") || "/";
35053
+ } catch {
35054
+ return text.split(/[?#]/, 1)[0].replace(/\/+$/, "") || "/";
35055
+ }
35056
+ };
35057
+ var isPublicRoutePath = (path) => /\/public(?:\/|$)/.test(path);
35014
35058
  var readJsonPayload = async (response) => {
35015
35059
  try {
35016
35060
  return await response.json();
@@ -35111,6 +35155,7 @@ var attachCallback = (loginUrl, callback) => {
35111
35155
  }
35112
35156
  };
35113
35157
  var getCurrentHref4 = () => typeof window === "undefined" ? "" : window.location.href;
35158
+ var getCurrentPathname = () => typeof window === "undefined" ? "" : window.location.pathname;
35114
35159
  var getCurrentHostname2 = () => typeof window === "undefined" ? "" : window.location.hostname;
35115
35160
  var getRuntimeEnv = (key) => {
35116
35161
  const env = typeof process !== "undefined" ? process.env : void 0;
@@ -35208,7 +35253,7 @@ var createPublicAccessClient = ({
35208
35253
  const request = async (input = {}) => {
35209
35254
  const body = {
35210
35255
  ...input,
35211
- path: input.path || getCurrentPathname(),
35256
+ path: input.path || getCurrentPathname2(),
35212
35257
  domain: input.domain || getCurrentDomain(),
35213
35258
  userAgent: input.userAgent || getCurrentUserAgent(),
35214
35259
  guestIdentifier: input.guestIdentifier || getOrCreatePublicGuestIdentifier(normalizedAppType)
@@ -35276,7 +35321,7 @@ var isSuccessCode6 = (code) => {
35276
35321
  const normalized = Number(code);
35277
35322
  return Number.isFinite(normalized) ? normalized === 0 || normalized >= 200 && normalized < 300 : false;
35278
35323
  };
35279
- var getCurrentPathname = () => typeof window === "undefined" ? void 0 : window.location.pathname;
35324
+ var getCurrentPathname2 = () => typeof window === "undefined" ? void 0 : window.location.pathname;
35280
35325
  var getCurrentDomain = () => typeof window === "undefined" ? void 0 : window.location.host;
35281
35326
  var getCurrentUserAgent = () => typeof navigator === "undefined" ? void 0 : navigator.userAgent;
35282
35327
  var getOrCreatePublicGuestIdentifier = (appType) => {
@@ -35319,6 +35364,8 @@ var usePublicAccess = (options = {}) => {
35319
35364
  const [session, setSession] = (0, import_react217.useState)(null);
35320
35365
  const [error, setError] = (0, import_react217.useState)(null);
35321
35366
  const [loading, setLoading] = (0, import_react217.useState)(Boolean(autoStart));
35367
+ const activeSessionRef = (0, import_react217.useRef)(null);
35368
+ const mountedRef = (0, import_react217.useRef)(true);
35322
35369
  const sessionInputKey = JSON.stringify(sessionInput);
35323
35370
  const stableSessionInput = (0, import_react217.useMemo)(() => sessionInput, [sessionInputKey]);
35324
35371
  const client = (0, import_react217.useMemo)(
@@ -35334,15 +35381,31 @@ var usePublicAccess = (options = {}) => {
35334
35381
  setLoading(true);
35335
35382
  setError(null);
35336
35383
  try {
35384
+ const resolvedPath = input.path || stableSessionInput.path || readPathFromLocation();
35337
35385
  const data = await client.startSession({
35338
35386
  ...stableSessionInput,
35339
35387
  ...input,
35340
35388
  ticket: input.ticket || stableSessionInput.ticket || readTicketFromLocation(),
35341
- path: input.path || stableSessionInput.path || readPathFromLocation()
35389
+ path: resolvedPath
35342
35390
  });
35391
+ if (!mountedRef.current) return data;
35392
+ activeSessionRef.current = {
35393
+ accessToken: data.accessToken,
35394
+ path: resolvedPath
35395
+ };
35343
35396
  setSession(data);
35344
- setAccessToken(data.accessToken);
35345
- await reloadRuntime({ accessToken: data.accessToken });
35397
+ setAccessToken(data.accessToken, {
35398
+ scope: "public",
35399
+ path: resolvedPath
35400
+ });
35401
+ await reloadRuntime({
35402
+ accessToken: data.accessToken,
35403
+ accessTokenOptions: {
35404
+ scope: "public",
35405
+ path: resolvedPath
35406
+ }
35407
+ });
35408
+ if (!mountedRef.current) return data;
35346
35409
  setLoading(false);
35347
35410
  return data;
35348
35411
  } catch (caught) {
@@ -35350,8 +35413,10 @@ var usePublicAccess = (options = {}) => {
35350
35413
  caught instanceof Error ? caught.message : "\u516C\u5F00\u8BBF\u95EE\u4F1A\u8BDD\u521B\u5EFA\u5931\u8D25",
35351
35414
  { payload: caught }
35352
35415
  );
35353
- setError(nextError);
35354
- setLoading(false);
35416
+ if (mountedRef.current) {
35417
+ setError(nextError);
35418
+ setLoading(false);
35419
+ }
35355
35420
  throw nextError;
35356
35421
  }
35357
35422
  },
@@ -35361,6 +35426,20 @@ var usePublicAccess = (options = {}) => {
35361
35426
  if (!autoStart) return;
35362
35427
  void startSession().catch(() => void 0);
35363
35428
  }, [autoStart, startSession]);
35429
+ (0, import_react217.useEffect)(
35430
+ () => {
35431
+ mountedRef.current = true;
35432
+ return () => {
35433
+ mountedRef.current = false;
35434
+ const activeSession = activeSessionRef.current;
35435
+ if (!activeSession) return;
35436
+ activeSessionRef.current = null;
35437
+ setAccessToken(null, { clearIfToken: activeSession.accessToken });
35438
+ void reloadRuntime({ accessToken: null });
35439
+ };
35440
+ },
35441
+ [reloadRuntime, setAccessToken]
35442
+ );
35364
35443
  return {
35365
35444
  loading,
35366
35445
  error,