secureApiCall.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. Copyright (C) 2025 QuantumNous
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. For commercial licensing, please contact support@quantumnous.com
  14. */
  15. /**
  16. * 安全 API 调用包装器
  17. * 自动处理需要验证的 403 错误,透明地触发验证流程
  18. */
  19. /**
  20. * 检查错误是否是需要安全验证的错误
  21. * @param {Error} error - 错误对象
  22. * @returns {boolean}
  23. */
  24. export function isVerificationRequiredError(error) {
  25. if (!error.response) return false;
  26. const { status, data } = error.response;
  27. // 检查是否是 403 错误且包含验证相关的错误码
  28. if (status === 403 && data) {
  29. const verificationCodes = [
  30. 'VERIFICATION_REQUIRED',
  31. 'VERIFICATION_EXPIRED',
  32. 'VERIFICATION_INVALID',
  33. ];
  34. return verificationCodes.includes(data.code);
  35. }
  36. return false;
  37. }
  38. /**
  39. * 从错误中提取验证需求信息
  40. * @param {Error} error - 错误对象
  41. * @returns {Object} 验证需求信息
  42. */
  43. export function extractVerificationInfo(error) {
  44. const data = error.response?.data || {};
  45. return {
  46. code: data.code,
  47. message: data.message || '需要安全验证',
  48. required: true,
  49. };
  50. }