summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorErik Michelson2021-03-11 16:40:24 +0100
committerErik Michelson2021-03-12 22:27:49 +0100
commit731fb24500d535d0ab8d828b4a0a720c9f09902a (patch)
treeeee2ae26b2230c80cf354c377d1afcd706db23e0 /lib
parent91846ac9a602ccf805882f0706b46f5233eb1c71 (diff)
Replace request library with node-fetch
Signed-off-by: Erik Michelson <github@erik.michelson.eu>
Diffstat (limited to 'lib')
-rw-r--r--lib/response.js105
1 files changed, 58 insertions, 47 deletions
diff --git a/lib/response.js b/lib/response.js
index 10ecd035..4d22d563 100644
--- a/lib/response.js
+++ b/lib/response.js
@@ -3,7 +3,7 @@
// external modules
const fs = require('fs')
const path = require('path')
-const request = require('request')
+const fetch = require('node-fetch')
// core
const config = require('./config')
const logger = require('./logger')
@@ -76,46 +76,57 @@ function githubActionGist (req, res, note) {
state: state
}
const authUrl = 'https://github.com/login/oauth/access_token'
- request({
- url: authUrl,
+ fetch(authUrl, {
method: 'POST',
- json: data
- }, function (error, httpResponse, body) {
- if (!error && httpResponse.statusCode === 200) {
- const accessToken = body.access_token
- if (accessToken) {
- const content = note.content
- const title = models.Note.decodeTitle(note.title)
- const filename = title.replace('/', ' ') + '.md'
- const gist = {
- files: {}
- }
- gist.files[filename] = {
- content: content
- }
- const gistUrl = 'https://api.github.com/gists'
- request({
- url: gistUrl,
- headers: {
- 'User-Agent': 'HedgeDoc',
- Authorization: 'token ' + accessToken
- },
- method: 'POST',
- json: gist
- }, function (error, httpResponse, body) {
- if (!error && httpResponse.statusCode === 201) {
- res.setHeader('referer', '')
- res.redirect(body.html_url)
- } else {
- return errors.errorForbidden(res)
- }
- })
- } else {
- return errors.errorForbidden(res)
+ body: JSON.stringify(data),
+ headers: {
+ 'Content-Type': 'application/json',
+ Accept: 'application/json'
+ }
+ }).then(resp => {
+ if (!resp.ok) {
+ throw new Error('forbidden')
+ }
+ return resp.json()
+ }).then(body => {
+ const accessToken = body.access_token
+ if (!accessToken) {
+ throw new Error('forbidden')
+ }
+ const content = note.content
+ const title = models.Note.decodeTitle(note.title)
+ const filename = title.replace('/', ' ') + '.md'
+ const gist = {
+ files: {}
+ }
+ gist.files[filename] = {
+ content: content
+ }
+ const gistUrl = 'https://api.github.com/gists'
+ return fetch(gistUrl, {
+ method: 'POST',
+ body: JSON.stringify(gist),
+ headers: {
+ 'User-Agent': 'HedgeDoc',
+ Authorization: 'token ' + accessToken,
+ 'Content-Type': 'application/json',
+ Accept: 'application/json'
}
- } else {
+ })
+ }).then(resp => {
+ if (resp.status !== 201) {
+ throw new Error('forbidden')
+ }
+ return resp.json()
+ }).then(body => {
+ res.setHeader('referer', '')
+ res.redirect(body.html_url)
+ }).catch(error => {
+ if (error.message === 'forbidden') {
return errors.errorForbidden(res)
}
+ logger.error('GitHub Gist auth failed: ' + error)
+ return errors.errorInternalError(res)
})
}
}
@@ -146,17 +157,17 @@ function gitlabActionProjects (req, res, note) {
const ret = { baseURL: config.gitlab.baseURL, version: config.gitlab.version }
ret.accesstoken = user.accessToken
ret.profileid = user.profileid
- request(
- config.gitlab.baseURL + '/api/' + config.gitlab.version + '/projects?membership=yes&per_page=100&access_token=' + user.accessToken,
- function (error, httpResponse, body) {
- if (!error && httpResponse.statusCode === 200) {
- ret.projects = JSON.parse(body)
- return res.send(ret)
- } else {
- return res.send(ret)
- }
+ const apiUrl = `${config.gitlab.baseURL}/api/${config.gitlab.version}/projects?membership=yes&per_page=100&access_token=${user.accessToken}`
+ fetch(apiUrl).then(resp => {
+ if (!resp.ok) {
+ res.send(ret)
+ throw new Error('HTTP request returned not okay-ish status')
}
- )
+ return resp.json()
+ }).then(body => {
+ ret.projects = body
+ return res.send(ret)
+ })
}).catch(function (err) {
logger.error('gitlab action projects failed: ' + err)
return errors.errorInternalError(res)