Set Prettier's "trailingComma" setting to "es5" GitOrigin-RevId: 9f14150511929a855b27467ad17be6ab262fe5d5
28 lines
720 B
JavaScript
28 lines
720 B
JavaScript
const OError = require('@overleaf/o-error')
|
|
const { assertHasStatusCode } = require('./requestHelper')
|
|
const CSRF_REGEX = /<meta name="ol-csrfToken" content="(.+?)">/
|
|
|
|
function _parseCsrf(body) {
|
|
const match = CSRF_REGEX.exec(body)
|
|
if (!match) {
|
|
throw new Error('Cannot find csrfToken in HTML')
|
|
}
|
|
return match[1]
|
|
}
|
|
|
|
function getCsrfTokenForFactory({ request }) {
|
|
return async function getCsrfTokenFor(endpoint) {
|
|
try {
|
|
const response = await request(endpoint)
|
|
assertHasStatusCode(response, 200)
|
|
return _parseCsrf(response.body)
|
|
} catch (err) {
|
|
throw new OError(`error fetching csrf token on ${endpoint}`, {}, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
getCsrfTokenForFactory,
|
|
}
|