2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
|
|
|
max-len,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
const request = require('request')
|
2021-07-07 05:38:56 -04:00
|
|
|
const settings = require('@overleaf/settings')
|
2019-05-29 05:21:06 -04:00
|
|
|
const Errors = require('../Errors/Errors')
|
2020-02-25 04:39:53 -05:00
|
|
|
const { promisifyAll } = require('../../util/promises')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
|
|
|
// TODO: check what happens when these settings aren't defined
|
|
|
|
const DEFAULT_V1_PARAMS = {
|
2019-07-15 09:09:04 -04:00
|
|
|
baseUrl: settings.apis.v1.url,
|
2019-05-29 05:21:06 -04:00
|
|
|
auth: {
|
2019-07-15 09:09:04 -04:00
|
|
|
user: settings.apis.v1.user,
|
2021-04-27 03:52:58 -04:00
|
|
|
pass: settings.apis.v1.pass,
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
json: true,
|
2022-02-24 12:04:27 -05:00
|
|
|
timeout: 2 * 1000,
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const v1Request = request.defaults(DEFAULT_V1_PARAMS)
|
|
|
|
|
|
|
|
const DEFAULT_V1_OAUTH_PARAMS = {
|
2019-07-15 09:09:04 -04:00
|
|
|
baseUrl: settings.apis.v1.url,
|
2019-05-29 05:21:06 -04:00
|
|
|
json: true,
|
2021-04-27 03:52:58 -04:00
|
|
|
timeout: 30 * 1000,
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const v1OauthRequest = request.defaults(DEFAULT_V1_OAUTH_PARAMS)
|
|
|
|
|
2020-02-25 04:39:53 -05:00
|
|
|
const V1Api = {
|
2019-05-29 05:21:06 -04:00
|
|
|
request(options, callback) {
|
|
|
|
if (callback == null) {
|
|
|
|
return request(options)
|
|
|
|
}
|
|
|
|
return v1Request(options, (error, response, body) =>
|
|
|
|
V1Api._responseHandler(options, error, response, body, callback)
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
oauthRequest(options, token, callback) {
|
|
|
|
if (options.uri == null) {
|
|
|
|
return callback(new Error('uri required'))
|
|
|
|
}
|
|
|
|
if (options.method == null) {
|
|
|
|
options.method = 'GET'
|
|
|
|
}
|
|
|
|
options.auth = { bearer: token }
|
|
|
|
return v1OauthRequest(options, (error, response, body) =>
|
|
|
|
V1Api._responseHandler(options, error, response, body, callback)
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
_responseHandler(options, error, response, body, callback) {
|
|
|
|
if (error != null) {
|
2019-10-30 11:42:37 -04:00
|
|
|
return callback(
|
|
|
|
new Errors.V1ConnectionError('error from V1 API').withCause(error)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if (response && response.statusCode >= 500) {
|
|
|
|
return callback(
|
|
|
|
new Errors.V1ConnectionError({
|
|
|
|
message: 'error from V1 API',
|
2021-04-27 03:52:58 -04:00
|
|
|
info: { status: response.statusCode, body: body },
|
2019-10-30 11:42:37 -04:00
|
|
|
})
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
if (
|
|
|
|
(response.statusCode >= 200 && response.statusCode < 300) ||
|
|
|
|
Array.from(options.expectedStatusCodes || []).includes(
|
|
|
|
response.statusCode
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return callback(null, response, body)
|
|
|
|
} else if (response.statusCode === 403) {
|
|
|
|
error = new Errors.ForbiddenError('overleaf v1 returned forbidden')
|
|
|
|
error.statusCode = response.statusCode
|
|
|
|
return callback(error)
|
2019-10-07 11:21:04 -04:00
|
|
|
} else if (response.statusCode === 404) {
|
|
|
|
error = new Errors.NotFoundError(
|
2020-12-15 05:23:54 -05:00
|
|
|
`overleaf v1 returned non-success code: ${response.statusCode} ${options.method} ${options.uri}`
|
2019-10-07 11:21:04 -04:00
|
|
|
)
|
|
|
|
error.statusCode = response.statusCode
|
|
|
|
return callback(error)
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
|
|
|
error = new Error(
|
2020-12-15 05:23:54 -05:00
|
|
|
`overleaf v1 returned non-success code: ${response.statusCode} ${options.method} ${options.uri}`
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
error.statusCode = response.statusCode
|
|
|
|
return callback(error)
|
|
|
|
}
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-02-25 04:39:53 -05:00
|
|
|
|
|
|
|
V1Api.promises = promisifyAll(V1Api, {
|
|
|
|
multiResult: {
|
|
|
|
request: ['response', 'body'],
|
2021-04-27 03:52:58 -04:00
|
|
|
oauthRequest: ['response', 'body'],
|
|
|
|
},
|
2020-02-25 04:39:53 -05:00
|
|
|
})
|
|
|
|
module.exports = V1Api
|