"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); const Axios = require("axios"); const auth = require("@nextcloud/auth"); const router = require("@nextcloud/router"); const _interopDefault = (e) => e && e.__esModule ? e : { default: e }; const Axios__default = /* @__PURE__ */ _interopDefault(Axios); const RETRY_KEY = Symbol("csrf-retry"); const onError$2 = (axios) => async (error) => { var _a2; const { config, response, request } = error; const responseURL = request == null ? void 0 : request.responseURL; const status = response == null ? void 0 : response.status; if (status === 412 && ((_a2 = response == null ? void 0 : response.data) == null ? void 0 : _a2.message) === "CSRF check failed" && config[RETRY_KEY] === void 0) { console.warn("Request to ".concat(responseURL, " failed because of a CSRF mismatch. Fetching a new token")); const { data: { token } } = await axios.get(router.generateUrl("/csrftoken")); console.debug("New request token ".concat(token, " fetched")); axios.defaults.headers.requesttoken = token; return axios({ ...config, headers: { ...config.headers, requesttoken: token }, [RETRY_KEY]: true }); } return Promise.reject(error); }; const RETRY_DELAY_KEY = Symbol("retryDelay"); const onError$1 = (axios) => async (error) => { var _a2; const { config, response, request } = error; const responseURL = request == null ? void 0 : request.responseURL; const status = response == null ? void 0 : response.status; const headers = response == null ? void 0 : response.headers; if (status === 503 && headers["x-nextcloud-maintenance-mode"] === "1" && config.retryIfMaintenanceMode && (!config[RETRY_DELAY_KEY] || config[RETRY_DELAY_KEY] <= 32)) { const retryDelay = ((_a2 = config[RETRY_DELAY_KEY]) != null ? _a2 : 1) * 2; console.warn("Request to ".concat(responseURL, " failed because of maintenance mode. Retrying in ").concat(retryDelay, "s")); await new Promise((resolve) => { setTimeout(resolve, retryDelay * 1e3); }); return axios({ ...config, [RETRY_DELAY_KEY]: retryDelay }); } return Promise.reject(error); }; const onError = async (error) => { var _a2; const { config, response, request } = error; const responseURL = request == null ? void 0 : request.responseURL; const status = response == null ? void 0 : response.status; if (status === 401 && ((_a2 = response == null ? void 0 : response.data) == null ? void 0 : _a2.message) === "Current user is not logged in" && config.reloadExpiredSession && (window == null ? void 0 : window.location)) { console.error("Request to ".concat(responseURL, " failed because the user session expired. Reloading the page …")); window.location.reload(); } return Promise.reject(error); }; var _a; const client = Axios__default.default.create({ headers: { requesttoken: (_a = auth.getRequestToken()) != null ? _a : "", "X-Requested-With": "XMLHttpRequest" } }); const cancelableClient = Object.assign(client, { CancelToken: Axios__default.default.CancelToken, isCancel: Axios__default.default.isCancel }); cancelableClient.interceptors.response.use((r) => r, onError$2(cancelableClient)); cancelableClient.interceptors.response.use((r) => r, onError$1(cancelableClient)); cancelableClient.interceptors.response.use((r) => r, onError); auth.onRequestTokenUpdate((token) => { client.defaults.headers.requesttoken = token; }); Object.defineProperty(exports, "isAxiosError", { enumerable: true, get: () => Axios.isAxiosError }); Object.defineProperty(exports, "isCancel", { enumerable: true, get: () => Axios.isCancel }); exports.default = cancelableClient;