|
3 | 3 | * SPDX-License-Identifier: GPL-3.0-or-later |
4 | 4 | */ |
5 | 5 |
|
6 | | -import type { AxiosInstance, CancelTokenStatic } from 'axios' |
7 | | - |
8 | | -import { getRequestToken, onRequestTokenUpdate } from '@nextcloud/auth' |
9 | | -import Axios from 'axios' |
10 | | -import { onError as onCsrfTokenError } from './interceptors/csrf-token.ts' |
11 | | -import { onError as onMaintenanceModeError } from './interceptors/maintenance-mode.ts' |
12 | | -import { onError as onNotLoggedInError } from './interceptors/not-logged-in.ts' |
13 | | - |
14 | | -interface CancelableAxiosInstance extends AxiosInstance { |
15 | | - CancelToken: CancelTokenStatic |
16 | | - isCancel: typeof Axios.isCancel |
17 | | -} |
18 | | - |
19 | | -declare module 'axios' { |
20 | | - // eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any -- needed as we extend the interface only. |
21 | | - interface AxiosRequestConfig<D = any> { |
22 | | - /** |
23 | | - * Only available if the Axios instance from `@nextcloud/axios` is used. |
24 | | - * If set to `true`, the interceptor will reload the page when a 401 response is received |
25 | | - * and the error message indicates that the user is not logged in. |
26 | | - * |
27 | | - * @default false |
28 | | - */ |
29 | | - reloadExpiredSession?: boolean |
30 | | - } |
31 | | -} |
32 | | - |
33 | | -const client = Axios.create({ |
34 | | - headers: { |
35 | | - requesttoken: getRequestToken() ?? '', |
36 | | - 'X-Requested-With': 'XMLHttpRequest', |
37 | | - }, |
38 | | -}) |
39 | | - |
40 | | -const cancelableClient: CancelableAxiosInstance = Object.assign(client, { |
41 | | - CancelToken: Axios.CancelToken, |
42 | | - isCancel: Axios.isCancel, |
43 | | -}) |
| 6 | +import { cancelableClient } from './client.ts' |
| 7 | +import { onCsrfTokenError } from './interceptors/csrf-token.ts' |
| 8 | +import { onMaintenanceModeError } from './interceptors/maintenance-mode.ts' |
| 9 | +import { onNotLoggedInError } from './interceptors/not-logged-in.ts' |
44 | 10 |
|
45 | 11 | cancelableClient.interceptors.response.use((r) => r, onCsrfTokenError(cancelableClient)) |
46 | 12 | cancelableClient.interceptors.response.use((r) => r, onMaintenanceModeError(cancelableClient)) |
47 | 13 | cancelableClient.interceptors.response.use((r) => r, onNotLoggedInError) |
48 | 14 |
|
49 | | -onRequestTokenUpdate((token) => { |
50 | | - client.defaults.headers.requesttoken = token |
51 | | -}) |
52 | | - |
53 | | -export default cancelableClient |
54 | | - |
55 | | -export { isAxiosError, isCancel } from 'axios' |
56 | | - |
57 | 15 | export type * from 'axios' |
| 16 | +export type * from './custom-config.ts' |
| 17 | +export { isAxiosError, isCancel } from 'axios' |
| 18 | +export default cancelableClient |
0 commit comments