e70e0d2224
验证:868 项单测、ESLint、Prettier、UI 依赖审计和差异检查通过。
110 lines
3.5 KiB
JavaScript
110 lines
3.5 KiB
JavaScript
/*
|
|
Copyright (C) 2025 QuantumNous
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as
|
|
published by the Free Software Foundation, either version 3 of the
|
|
License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
For commercial licensing, please contact support@quantumnous.com
|
|
*/
|
|
|
|
import { CanceledError, isCancel } from 'axios';
|
|
|
|
const PUBLIC_API_PATH =
|
|
/^\/api\/(?:status|setup|i18n|notice|about|privacy_policy|user_agreement|home_page_content|customer-service|verification|reset_password|oauth|user\/(?:login|register|reset|passkey\/login)|org\/(?:login|register))(?:\/|$)/;
|
|
const READ_METHODS = new Set(['get', 'head', 'options']);
|
|
|
|
function isSessionBound(config) {
|
|
const pathname = config?.url?.split(/[?#]/, 1)[0];
|
|
return (
|
|
config?.sessionBound !== false &&
|
|
pathname?.startsWith('/api/') &&
|
|
!PUBLIC_API_PATH.test(pathname)
|
|
);
|
|
}
|
|
|
|
function combineSignals(signals, config, cleanups) {
|
|
const inputs = [...new Set(signals.filter(Boolean))];
|
|
if (inputs.length < 2) return inputs[0];
|
|
if (typeof AbortSignal.any === 'function') return AbortSignal.any(inputs);
|
|
|
|
const controller = new AbortController();
|
|
const abort = () => controller.abort();
|
|
for (const input of inputs) {
|
|
if (input.aborted) abort();
|
|
else input.addEventListener('abort', abort, { once: true });
|
|
}
|
|
cleanups.set(config, () => {
|
|
for (const input of inputs) input.removeEventListener('abort', abort);
|
|
});
|
|
return controller.signal;
|
|
}
|
|
|
|
export function installRequestLifecycle(
|
|
instance,
|
|
{ onError, onSessionExpired },
|
|
) {
|
|
const session = new AbortController();
|
|
const cleanups = new WeakMap();
|
|
|
|
instance.interceptors.request.use((config) => {
|
|
const sessionBound = isSessionBound(config);
|
|
const read = READ_METHODS.has(config.method);
|
|
const groupSignal = read ? config.requestGroup?.signal : undefined;
|
|
if ((sessionBound && session.signal.aborted) || groupSignal?.aborted) {
|
|
throw new CanceledError(undefined, config);
|
|
}
|
|
config.signal = combineSignals(
|
|
[config.signal, read && sessionBound && session.signal, groupSignal],
|
|
config,
|
|
cleanups,
|
|
);
|
|
return config;
|
|
});
|
|
|
|
const cleanup = (config) => {
|
|
cleanups.get(config)?.();
|
|
cleanups.delete(config);
|
|
};
|
|
|
|
instance.interceptors.response.use(
|
|
(response) => {
|
|
cleanup(response.config);
|
|
return response;
|
|
},
|
|
(error) => {
|
|
const { config, response } = error;
|
|
cleanup(config);
|
|
const sessionBound = isSessionBound(config);
|
|
const read = READ_METHODS.has(config?.method);
|
|
if (isCancel(error)) throw error;
|
|
if (sessionBound && session.signal.aborted) {
|
|
if (read) throw new CanceledError(undefined, config);
|
|
throw error;
|
|
}
|
|
if (response?.status >= 400 && read) config.requestGroup?.abort();
|
|
|
|
if (!config?.skipErrorHandler) {
|
|
if (response?.status === 401 && sessionBound) {
|
|
session.abort();
|
|
onSessionExpired();
|
|
} else {
|
|
onError(error);
|
|
}
|
|
}
|
|
throw error;
|
|
},
|
|
);
|
|
|
|
return () => session.abort();
|
|
}
|