Skip to content

Commit f25ee24

Browse files
committed
fix: axios does not preserve symbols in config
Axios (somehow) does not preserve entries in the config, which have an symbol as their key. So this migrates the internally used symbols to string-keys. Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent dd94ef4 commit f25ee24

3 files changed

Lines changed: 16 additions & 15 deletions

File tree

lib/interceptors/csrf-token.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { InterceptorErrorHandler } from './index.ts'
99
import { generateUrl } from '@nextcloud/router'
1010
import { isAxiosError } from 'axios'
1111

12-
const RETRY_KEY = Symbol('csrf-retry')
12+
const RETRY_KEY = '_nextcloudCsrfTokenReloaded'
1313

1414
/**
1515
* Handle CSRF token errors in Axios requests.
@@ -26,7 +26,7 @@ export function onCsrfTokenError(axios: CancelableAxiosInstance): InterceptorErr
2626
const responseURL = request?.responseURL
2727

2828
if (config
29-
&& !config[RETRY_KEY]
29+
&& !(RETRY_KEY in config)
3030
&& response?.status === 412
3131
&& response?.data?.message === 'CSRF check failed') {
3232
console.warn(`Request to ${responseURL} failed because of a CSRF mismatch. Fetching a new token`)
@@ -35,14 +35,9 @@ export function onCsrfTokenError(axios: CancelableAxiosInstance): InterceptorErr
3535
console.debug(`New request token ${token} fetched`)
3636
axios.defaults.headers.requesttoken = token
3737

38-
return axios({
39-
...config,
40-
headers: {
41-
...config.headers,
42-
requesttoken: token,
43-
},
44-
[RETRY_KEY]: true,
45-
})
38+
config.headers.requesttoken = token
39+
config[RETRY_KEY] = true
40+
return axios(config)
4641
}
4742

4843
throw error

lib/interceptors/maintenance-mode.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { InterceptorErrorHandler } from './index.ts'
88

99
import { isAxiosError } from 'axios'
1010

11-
export const RETRY_DELAY_KEY = Symbol('retryDelay')
11+
const RETRY_DELAY_KEY = '_nextcloudMaintenanceModeRetryDelay'
1212

1313
/**
1414
* Handles Nextcloud maintenance mode errors in Axios requests.
@@ -25,9 +25,7 @@ export function onMaintenanceModeError(axios: CancelableAxiosInstance): Intercep
2525
const responseURL = request?.responseURL
2626
const status = response?.status
2727
const headers = response?.headers
28-
let retryDelay = typeof config?.[RETRY_DELAY_KEY] === 'number'
29-
? config?.[RETRY_DELAY_KEY]
30-
: 1
28+
let retryDelay = config?.[RETRY_DELAY_KEY] ?? 1
3129

3230
/**
3331
* Retry requests if they failed due to maintenance mode

lib/axios.d.ts renamed to lib/internal.d.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,16 @@
99
declare module 'axios' {
1010
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any -- needed as we extend the interface only.
1111
interface AxiosRequestConfig<D = any> {
12-
[key: symbol]: unknown
12+
_nextcloudCsrfTokenReloaded?: true
13+
_nextcloudMaintenanceModeRetryDelay?: number
1314
}
1415
}
1516

17+
declare global {
18+
var OC: {
19+
/** NC 32 and before */
20+
reload?: () => void
21+
} | undefined
22+
}
23+
1624
export {}

0 commit comments

Comments
 (0)