openxiangda 1.0.87 → 1.0.89

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.
@@ -200,7 +200,7 @@ interface PageSdkMeta {
200
200
  supportedBridgeMethods?: string[];
201
201
  }
202
202
  interface PageApiResponse<TResult = unknown, TRaw = TResult> {
203
- code: number;
203
+ code: number | string;
204
204
  success: boolean;
205
205
  message?: string;
206
206
  result: TResult | null;
@@ -1292,6 +1292,9 @@ interface RuntimeRequestState<T> {
1292
1292
  loading: boolean;
1293
1293
  error: RuntimeRequestError | null;
1294
1294
  }
1295
+ interface RuntimeReloadOptions {
1296
+ accessToken?: string | null;
1297
+ }
1295
1298
  interface OpenXiangdaProviderProps {
1296
1299
  appType?: string;
1297
1300
  servicePrefix?: string;
@@ -1302,7 +1305,8 @@ interface OpenXiangdaRuntimeStore extends RuntimeRequestState<RuntimeBootstrap>
1302
1305
  appType: string;
1303
1306
  servicePrefix: string;
1304
1307
  fetchImpl: typeof fetch;
1305
- reload: () => Promise<void>;
1308
+ reload: (options?: RuntimeReloadOptions) => Promise<void>;
1309
+ setAccessToken: (accessToken?: string | null) => void;
1306
1310
  }
1307
1311
  declare const OpenXiangdaProvider: React__default.FC<OpenXiangdaProviderProps>;
1308
1312
  declare const useOpenXiangda: () => OpenXiangdaRuntimeStore;
@@ -1312,7 +1316,8 @@ declare const useAppMenus: () => {
1312
1316
  appType: string;
1313
1317
  servicePrefix: string;
1314
1318
  fetchImpl: typeof fetch;
1315
- reload: () => Promise<void>;
1319
+ reload: (options?: RuntimeReloadOptions) => Promise<void>;
1320
+ setAccessToken: (accessToken?: string | null) => void;
1316
1321
  loading: boolean;
1317
1322
  error: RuntimeRequestError | null;
1318
1323
  };
@@ -1321,7 +1326,8 @@ declare const usePermission: () => {
1321
1326
  appType: string;
1322
1327
  servicePrefix: string;
1323
1328
  fetchImpl: typeof fetch;
1324
- reload: () => Promise<void>;
1329
+ reload: (options?: RuntimeReloadOptions) => Promise<void>;
1330
+ setAccessToken: (accessToken?: string | null) => void;
1325
1331
  loading: boolean;
1326
1332
  error: RuntimeRequestError | null;
1327
1333
  };
@@ -200,7 +200,7 @@ interface PageSdkMeta {
200
200
  supportedBridgeMethods?: string[];
201
201
  }
202
202
  interface PageApiResponse<TResult = unknown, TRaw = TResult> {
203
- code: number;
203
+ code: number | string;
204
204
  success: boolean;
205
205
  message?: string;
206
206
  result: TResult | null;
@@ -1292,6 +1292,9 @@ interface RuntimeRequestState<T> {
1292
1292
  loading: boolean;
1293
1293
  error: RuntimeRequestError | null;
1294
1294
  }
1295
+ interface RuntimeReloadOptions {
1296
+ accessToken?: string | null;
1297
+ }
1295
1298
  interface OpenXiangdaProviderProps {
1296
1299
  appType?: string;
1297
1300
  servicePrefix?: string;
@@ -1302,7 +1305,8 @@ interface OpenXiangdaRuntimeStore extends RuntimeRequestState<RuntimeBootstrap>
1302
1305
  appType: string;
1303
1306
  servicePrefix: string;
1304
1307
  fetchImpl: typeof fetch;
1305
- reload: () => Promise<void>;
1308
+ reload: (options?: RuntimeReloadOptions) => Promise<void>;
1309
+ setAccessToken: (accessToken?: string | null) => void;
1306
1310
  }
1307
1311
  declare const OpenXiangdaProvider: React__default.FC<OpenXiangdaProviderProps>;
1308
1312
  declare const useOpenXiangda: () => OpenXiangdaRuntimeStore;
@@ -1312,7 +1316,8 @@ declare const useAppMenus: () => {
1312
1316
  appType: string;
1313
1317
  servicePrefix: string;
1314
1318
  fetchImpl: typeof fetch;
1315
- reload: () => Promise<void>;
1319
+ reload: (options?: RuntimeReloadOptions) => Promise<void>;
1320
+ setAccessToken: (accessToken?: string | null) => void;
1316
1321
  loading: boolean;
1317
1322
  error: RuntimeRequestError | null;
1318
1323
  };
@@ -1321,7 +1326,8 @@ declare const usePermission: () => {
1321
1326
  appType: string;
1322
1327
  servicePrefix: string;
1323
1328
  fetchImpl: typeof fetch;
1324
- reload: () => Promise<void>;
1329
+ reload: (options?: RuntimeReloadOptions) => Promise<void>;
1330
+ setAccessToken: (accessToken?: string | null) => void;
1325
1331
  loading: boolean;
1326
1332
  error: RuntimeRequestError | null;
1327
1333
  };
@@ -781,18 +781,39 @@ var normalizeDynamicOrder = (value) => {
781
781
  }
782
782
  return `${value.id}:${value.isAsc === "n" ? "-" : "+"}`;
783
783
  };
784
+ var hasOwn = (value, key) => Object.prototype.hasOwnProperty.call(value, key);
785
+ var normalizeEnvelopeCode = (value) => {
786
+ if (value === void 0 || value === null || value === "") {
787
+ return 200;
788
+ }
789
+ const normalized = Number(value);
790
+ return Number.isFinite(normalized) ? normalized : String(value);
791
+ };
792
+ var isSuccessCode = (value) => {
793
+ if (value === void 0 || value === null || value === "") {
794
+ return true;
795
+ }
796
+ const normalized = Number(value);
797
+ return Number.isFinite(normalized) ? normalized === 0 || normalized >= 200 && normalized < 300 : false;
798
+ };
799
+ var getEnvelopeCode = (rawResponse) => {
800
+ if (isRecord(rawResponse) && hasOwn(rawResponse, "code")) {
801
+ return rawResponse.code;
802
+ }
803
+ const nestedData = rawResponse?.data;
804
+ if (isRecord(nestedData) && hasOwn(nestedData, "code")) {
805
+ return nestedData.code;
806
+ }
807
+ return void 0;
808
+ };
784
809
  var normalizeJsonResponse = (rawResponse) => {
785
- const topLevelCode = Number(rawResponse?.code);
786
- const nestedCode = Number(
787
- rawResponse?.data?.code
788
- );
789
- const code = Number.isFinite(topLevelCode) ? topLevelCode : Number.isFinite(nestedCode) ? nestedCode : 200;
810
+ const code = normalizeEnvelopeCode(getEnvelopeCode(rawResponse));
790
811
  const topLevelResult = isRecord(rawResponse) && "result" in rawResponse ? rawResponse.result : void 0;
791
812
  const nestedResult = isRecord(rawResponse?.data) && "result" in (rawResponse.data || {}) ? rawResponse.data?.result : void 0;
792
813
  const topLevelData = isRecord(rawResponse) && "data" in rawResponse ? rawResponse.data : void 0;
793
814
  const result = topLevelResult !== void 0 ? topLevelResult : nestedResult !== void 0 ? nestedResult : topLevelData !== void 0 ? topLevelData : isRecord(rawResponse) ? rawResponse : null;
794
815
  const nestedSuccess = typeof rawResponse?.data?.success === "boolean" ? rawResponse.data?.success : void 0;
795
- const success = typeof rawResponse?.success === "boolean" ? Boolean(rawResponse.success) : typeof nestedSuccess === "boolean" ? Boolean(nestedSuccess) : code >= 200 && code < 300;
816
+ const success = rawResponse?.success === false || nestedSuccess === false ? false : isSuccessCode(code);
796
817
  return {
797
818
  code,
798
819
  success,
@@ -837,6 +858,9 @@ var normalizeBinaryResponse = (rawResponse) => {
837
858
  };
838
859
  };
839
860
  var toSdkError = (input, payload) => {
861
+ if (input instanceof Error && input.response) {
862
+ return input;
863
+ }
840
864
  const normalizedResponse = isRecord(input) ? normalizeJsonResponse(input) : void 0;
841
865
  const nextError = input instanceof Error ? input : new Error(
842
866
  normalizedResponse?.message || `\u8BF7\u6C42\u5931\u8D25: ${String(payload.method).toUpperCase()} ${payload.path}`
@@ -2523,7 +2547,8 @@ var createAuthClient = ({
2523
2547
  });
2524
2548
  const payload = await readPayload(response);
2525
2549
  const code = getRecordValue(payload, "code");
2526
- if (!response.ok || typeof code === "number" && code >= 400) {
2550
+ const success = getRecordValue(payload, "success");
2551
+ if (!response.ok || success === false || !isSuccessCode2(code)) {
2527
2552
  throw new AuthClientError(
2528
2553
  String(getRecordValue(payload, "message") || `Auth request failed: ${response.status}`),
2529
2554
  { status: response.status, code, payload }
@@ -2576,6 +2601,11 @@ var getRecordValue = (value, key) => {
2576
2601
  if (!value || typeof value !== "object") return void 0;
2577
2602
  return value[key];
2578
2603
  };
2604
+ var isSuccessCode2 = (code) => {
2605
+ if (code === void 0 || code === null || code === "") return true;
2606
+ const normalized = Number(code);
2607
+ return Number.isFinite(normalized) ? normalized === 0 || normalized >= 200 && normalized < 300 : false;
2608
+ };
2579
2609
  var resolveLoginUrl = (appType, {
2580
2610
  callbackUrl = getCurrentHref2(),
2581
2611
  callbackParamName = "callback",
@@ -3112,12 +3142,23 @@ var OpenXiangdaProvider = ({
3112
3142
  () => appType || resolveAppTypeFromLocation(),
3113
3143
  [appType]
3114
3144
  );
3145
+ const [accessToken, setAccessTokenState] = useState4(null);
3146
+ const setAccessToken = useCallback4(
3147
+ (nextAccessToken) => {
3148
+ setAccessTokenState(nextAccessToken || null);
3149
+ },
3150
+ []
3151
+ );
3152
+ const authorizedFetch = useMemo5(
3153
+ () => createAuthorizedFetch(resolvedFetch, accessToken),
3154
+ [accessToken, resolvedFetch]
3155
+ );
3115
3156
  const [state, setState] = useState4({
3116
3157
  data: null,
3117
3158
  loading: true,
3118
3159
  error: null
3119
3160
  });
3120
- const reload = useCallback4(async () => {
3161
+ const reload = useCallback4(async (options = {}) => {
3121
3162
  if (!resolvedAppType) {
3122
3163
  setState({
3123
3164
  data: null,
@@ -3130,8 +3171,9 @@ var OpenXiangdaProvider = ({
3130
3171
  return;
3131
3172
  }
3132
3173
  setState((prev) => ({ ...prev, loading: true, error: null }));
3174
+ const requestFetch = options.accessToken !== void 0 ? createAuthorizedFetch(resolvedFetch, options.accessToken || null) : authorizedFetch;
3133
3175
  try {
3134
- const response = await resolvedFetch(
3176
+ const response = await requestFetch(
3135
3177
  buildServiceUrl2(
3136
3178
  servicePrefix,
3137
3179
  `/openxiangda-api/v1/apps/${encodeURIComponent(
@@ -3144,7 +3186,7 @@ var OpenXiangdaProvider = ({
3144
3186
  }
3145
3187
  );
3146
3188
  const payload = await readJsonPayload(response);
3147
- if (!response.ok || payload?.code >= 400) {
3189
+ if (isRuntimeEnvelopeFailure(response, payload)) {
3148
3190
  throw createRuntimeHttpError(
3149
3191
  response,
3150
3192
  payload,
@@ -3163,7 +3205,7 @@ var OpenXiangdaProvider = ({
3163
3205
  error: normalizeRuntimeError(error)
3164
3206
  });
3165
3207
  }
3166
- }, [resolvedAppType, resolvedFetch, servicePrefix]);
3208
+ }, [authorizedFetch, resolvedAppType, resolvedFetch, servicePrefix]);
3167
3209
  useEffect4(() => {
3168
3210
  void reload();
3169
3211
  }, [reload]);
@@ -3172,10 +3214,11 @@ var OpenXiangdaProvider = ({
3172
3214
  ...state,
3173
3215
  appType: resolvedAppType,
3174
3216
  servicePrefix,
3175
- fetchImpl: resolvedFetch,
3176
- reload
3217
+ fetchImpl: authorizedFetch,
3218
+ reload,
3219
+ setAccessToken
3177
3220
  }),
3178
- [reload, resolvedAppType, resolvedFetch, servicePrefix, state]
3221
+ [authorizedFetch, reload, resolvedAppType, servicePrefix, state]
3179
3222
  );
3180
3223
  return /* @__PURE__ */ jsx4(OpenXiangdaRuntimeContext.Provider, { value, children });
3181
3224
  };
@@ -3278,7 +3321,7 @@ var useCanAccessRoute = (input) => {
3278
3321
  payload
3279
3322
  };
3280
3323
  if (!disposed) {
3281
- const shouldTreatAsError = !response.ok || typeof code === "number" && code >= 500;
3324
+ const shouldTreatAsError = !response.ok || isServerErrorCode(code);
3282
3325
  setState({
3283
3326
  data,
3284
3327
  loading: false,
@@ -3410,7 +3453,7 @@ var useRuntimeAuth = () => {
3410
3453
  }
3411
3454
  );
3412
3455
  const payload = await readJsonPayload(response);
3413
- if (!response.ok || payload?.code >= 400) {
3456
+ if (isRuntimeEnvelopeFailure(response, payload)) {
3414
3457
  throw createRuntimeHttpError(
3415
3458
  response,
3416
3459
  payload,
@@ -3442,6 +3485,19 @@ var buildServiceUrl2 = (servicePrefix, path) => {
3442
3485
  const suffix = path.startsWith("/") ? path : `/${path}`;
3443
3486
  return `${prefix}${suffix}`;
3444
3487
  };
3488
+ var createAuthorizedFetch = (baseFetch, accessToken) => {
3489
+ if (!accessToken) return baseFetch;
3490
+ return ((input, init = {}) => {
3491
+ const headers = new Headers(init.headers || {});
3492
+ if (!headers.has("authorization")) {
3493
+ headers.set("authorization", `Bearer ${accessToken}`);
3494
+ }
3495
+ return baseFetch(input, {
3496
+ ...init,
3497
+ headers
3498
+ });
3499
+ });
3500
+ };
3445
3501
  var readJsonPayload = async (response) => {
3446
3502
  try {
3447
3503
  return await response.json();
@@ -3487,6 +3543,15 @@ var classifyRuntimeError = (status, code) => {
3487
3543
  const normalizedCode = typeof code === "string" ? Number(code) : code;
3488
3544
  if (status === 401 || normalizedCode === 401) return "unauthenticated";
3489
3545
  if (status === 403 || normalizedCode === 403) return "forbidden";
3546
+ if (typeof code === "string") {
3547
+ const normalizedText = code.toUpperCase();
3548
+ if (normalizedText.includes("DENIED") || normalizedText.includes("FORBIDDEN")) {
3549
+ return "forbidden";
3550
+ }
3551
+ if (normalizedText.includes("UNAUTH") || normalizedText.includes("LOGIN")) {
3552
+ return "unauthenticated";
3553
+ }
3554
+ }
3490
3555
  if (!status && !normalizedCode) return "network";
3491
3556
  return "unknown";
3492
3557
  };
@@ -3542,6 +3607,20 @@ var getRecordValue2 = (value, key) => {
3542
3607
  if (!value || typeof value !== "object") return void 0;
3543
3608
  return value[key];
3544
3609
  };
3610
+ var isSuccessCode3 = (code) => {
3611
+ if (code === void 0 || code === null || code === "") return true;
3612
+ const normalized = Number(code);
3613
+ return Number.isFinite(normalized) ? normalized === 0 || normalized >= 200 && normalized < 300 : false;
3614
+ };
3615
+ var isRuntimeEnvelopeFailure = (response, payload) => {
3616
+ const code = getRecordValue2(payload, "code");
3617
+ const success = getRecordValue2(payload, "success");
3618
+ return !response.ok || success === false || !isSuccessCode3(code);
3619
+ };
3620
+ var isServerErrorCode = (code) => {
3621
+ const normalized = Number(code);
3622
+ return Number.isFinite(normalized) && normalized >= 500;
3623
+ };
3545
3624
  var getRecordString = (value, key) => {
3546
3625
  const result = getRecordValue2(value, key);
3547
3626
  return typeof result === "string" ? result : void 0;
@@ -3605,7 +3684,8 @@ var createPublicAccessClient = ({
3605
3684
  );
3606
3685
  const payload = await readPayload2(response);
3607
3686
  const code = getRecordValue3(payload, "code");
3608
- if (!response.ok || typeof code === "number" && code >= 400) {
3687
+ const success = getRecordValue3(payload, "success");
3688
+ if (!response.ok || success === false || !isSuccessCode4(code)) {
3609
3689
  throw new PublicAccessClientError(
3610
3690
  String(
3611
3691
  getRecordValue3(payload, "message") || `Public access session failed: ${response.status}`
@@ -3643,6 +3723,11 @@ var getRecordValue3 = (value, key) => {
3643
3723
  if (!value || typeof value !== "object") return void 0;
3644
3724
  return value[key];
3645
3725
  };
3726
+ var isSuccessCode4 = (code) => {
3727
+ if (code === void 0 || code === null || code === "") return true;
3728
+ const normalized = Number(code);
3729
+ return Number.isFinite(normalized) ? normalized === 0 || normalized >= 200 && normalized < 300 : false;
3730
+ };
3646
3731
  var getCurrentPathname = () => typeof window === "undefined" ? void 0 : window.location.pathname;
3647
3732
  var getCurrentDomain = () => typeof window === "undefined" ? void 0 : window.location.host;
3648
3733
  var getCurrentUserAgent = () => typeof navigator === "undefined" ? void 0 : navigator.userAgent;
@@ -3673,7 +3758,8 @@ var usePublicAccess = (options = {}) => {
3673
3758
  appType: runtimeAppType,
3674
3759
  servicePrefix: runtimeServicePrefix,
3675
3760
  fetchImpl: runtimeFetchImpl,
3676
- reload: reloadRuntime
3761
+ reload: reloadRuntime,
3762
+ setAccessToken
3677
3763
  } = runtime;
3678
3764
  const {
3679
3765
  appType = runtimeAppType,
@@ -3707,7 +3793,8 @@ var usePublicAccess = (options = {}) => {
3707
3793
  path: input.path || stableSessionInput.path || readPathFromLocation()
3708
3794
  });
3709
3795
  setSession(data);
3710
- await reloadRuntime();
3796
+ setAccessToken(data.accessToken);
3797
+ await reloadRuntime({ accessToken: data.accessToken });
3711
3798
  setLoading(false);
3712
3799
  return data;
3713
3800
  } catch (caught) {
@@ -3720,7 +3807,7 @@ var usePublicAccess = (options = {}) => {
3720
3807
  throw nextError;
3721
3808
  }
3722
3809
  },
3723
- [client, reloadRuntime, stableSessionInput]
3810
+ [client, reloadRuntime, setAccessToken, stableSessionInput]
3724
3811
  );
3725
3812
  useEffect5(() => {
3726
3813
  if (!autoStart) return;