Migrate bent to axios

This commit is contained in:
Yarmo Mackenbach 2022-02-08 19:10:23 +01:00
parent 0565446eae
commit 08e64425a7
No known key found for this signature in database
GPG key ID: 37367F4AF4087AD1
8 changed files with 6128 additions and 5908 deletions

View file

@ -6,7 +6,7 @@
"dependencies": {
"@xmpp/client": "^0.12.0",
"@xmpp/debug": "^0.12.0",
"bent": "^7.3.12",
"axios": "^0.25.0",
"browser-or-node": "^1.3.0",
"cors": "^2.8.5",
"dotenv": "^8.2.0",

View file

@ -13,8 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const bent = require('bent')
const req = bent('GET')
const axios = require('axios')
/**
* @module fetcher/gitlab
@ -47,9 +46,12 @@ module.exports.fn = async (data, opts) => {
const fetchPromise = new Promise((resolve, reject) => {
const urlUser = `https://${data.domain}/api/v4/users?username=${data.username}`
// const resUser = await req(urlUser, null, { Accept: 'application/json' })
const res = req(urlUser, null, { Accept: 'application/json' })
const res = axios.get(urlUser,
{
headers: { Accept: 'application/json' }
})
.then(resUser => {
return resUser.json()
return resUser.data
})
.then(jsonUser => {
return jsonUser.find((user) => user.username === data.username)
@ -62,12 +64,13 @@ module.exports.fn = async (data, opts) => {
})
.then(user => {
const urlProject = `https://${data.domain}/api/v4/users/${user.id}/projects`
return req(urlProject, null, {
Accept: 'application/json'
})
return axios.get(urlProject,
{
headers: { Accept: 'application/json' }
})
})
.then(resProject => {
return resProject.json()
return resProject.data
})
.then(jsonProject => {
return jsonProject.find((proj) => proj.path === 'gitlab_proof')

View file

@ -13,8 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const bent = require('bent')
const req = bent('GET')
const axios = require('axios')
const E = require('../enums')
/**
@ -53,29 +52,33 @@ module.exports.fn = async (data, opts) => {
switch (data.format) {
case E.ProofFormat.JSON:
req(data.url, null, {
Accept: 'application/json',
'User-Agent': `doipjs/${require('../../package.json').version}`
axios.get(data.url, {
headers: {
Accept: 'application/json',
'User-Agent': `doipjs/${require('../../package.json').version}`
},
validateStatus: function (status) {
return status >= 200 && status < 400
}
})
.then(async (res) => {
return await res.json()
.then(res => {
resolve(res.data)
})
.then((res) => {
resolve(res)
})
.catch((e) => {
.catch(e => {
reject(e)
})
break
case E.ProofFormat.TEXT:
req(data.url)
.then(async (res) => {
return await res.text()
axios.get(data.url, {
validateStatus: function (status) {
return status >= 200 && status < 400
},
responseType: 'text'
})
.then(res => {
resolve(res.data)
})
.then((res) => {
resolve(res)
})
.catch((e) => {
.catch(e => {
reject(e)
})
break

View file

@ -13,8 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const bent = require('bent')
const bentReq = bent('GET')
const axios = require('axios')
const validator = require('validator')
/**
@ -57,11 +56,12 @@ module.exports.fn = async (data, opts) => {
}
const url = `https://${opts.claims.matrix.instance}/_matrix/client/r0/rooms/${data.roomId}/event/${data.eventId}?access_token=${opts.claims.matrix.accessToken}`
bentReq(url, null, {
Accept: 'application/json'
})
.then(async (res) => {
return await res.json()
axios.get(url,
{
headers: { Accept: 'application/json' }
})
.then(res => {
return res.data
})
.then((res) => {
resolve(res)

View file

@ -13,8 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const bent = require('bent')
const bentReq = bent('GET')
const axios = require('axios')
const validator = require('validator')
/**
@ -55,16 +54,17 @@ module.exports.fn = async (data, opts) => {
)
}
bentReq(
axios.get(
`https://api.twitter.com/1.1/statuses/show.json?id=${data.tweetId}&tweet_mode=extended`,
null,
{
Accept: 'application/json',
Authorization: `Bearer ${opts.claims.twitter.bearerToken}`
headers: {
Accept: 'application/json',
Authorization: `Bearer ${opts.claims.twitter.bearerToken}`
}
}
)
.then(async (data) => {
return await data.json()
.then(data => {
return data.data
})
.then((data) => {
resolve(data.full_text)

View file

@ -13,8 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const bent = require('bent')
const req = bent('GET')
const axios = require('axios')
const validUrl = require('valid-url')
const openpgp = require('openpgp')
const Claim = require('./claim')
@ -99,13 +98,18 @@ exports.fetchKeybase = async (username, fingerprint) => {
const keyLink = `https://keybase.io/${username}/pgp_keys.asc?fingerprint=${fingerprint}`
let rawKeyContent
try {
rawKeyContent = await req(keyLink)
rawKeyContent = await axios.get(
keyLink,
{
responseType: 'text'
}
)
.then((response) => {
if (response.status === 200) {
return response
}
})
.then((response) => response.text())
.then((response) => response.data)
} catch (e) {
throw new Error(`Error fetching Keybase key: ${e.message}`)
}

View file

@ -15,8 +15,7 @@ limitations under the License.
*/
const router = require('express').Router()
const dns = require('dns')
const bent = require('bent')
const bentReq = bent('GET')
const axios = require('axios')
const validUrl = require('valid-url')
const jsdom = require('jsdom')
const { client, xml } = require('@xmpp/client')
@ -126,11 +125,14 @@ router.param('xmppdata', async (req, res, next, xmppdata) => {
})
router.get('/get/json/:url', (req, res) => {
bentReq(req.params.url, 'json', {
Accept: 'application/json'
})
.then(async (result) => {
return await result.json()
axios.get(req.params.url,
{
headers: {
Accept: 'application/json'
}
})
.then(result => {
return result.data
})
.then(async (result) => {
return res.status(200).json({ url: req.params.url, content: result })
@ -141,9 +143,12 @@ router.get('/get/json/:url', (req, res) => {
})
router.get('/get/text/:url', (req, res) => {
bentReq(req.params.url)
.then(async (result) => {
return await result.text()
axios.get(req.params.url,
{
responseType: 'text'
})
.then(result => {
return result.data
})
.then(async (result) => {
return res.status(200).json({ url: req.params.url, content: result })
@ -238,16 +243,17 @@ router.get('/get/twitter/:tweetid', async (req, res) => {
return res.status(500).json('Twitter not enabled on server')
}
bentReq(
axios.get(
`https://api.twitter.com/1.1/statuses/show.json?id=${req.params.tweetid}`,
null,
{
Accept: 'application/json',
Authorization: `Bearer ${twitterBearerToken}`
headers: {
Accept: 'application/json',
Authorization: `Bearer ${twitterBearerToken}`
}
}
)
.then(async (data) => {
return await data.json()
.then(data => {
return data.data
})
.then((data) => {
return res.status(200).json({ data: data, message: 'Success', error: {} })
@ -268,11 +274,14 @@ router.get('/get/matrix/:matrixroomid/:matrixeventid', async (req, res) => {
const url = `https://${matrixInstance}/_matrix/client/r0/rooms/${req.params.matrixroomid}/event/${req.params.matrixeventid}?access_token=${matrixAccessToken}`
bentReq(url, null, {
Accept: 'application/json'
})
.then(async (data) => {
return await data.json()
axios.get(url,
{
headers: {
Accept: 'application/json'
}
})
.then(data => {
return data.data
})
.then((data) => {
return res.status(200).json({ data: data, message: 'Success', error: {} })

11883
yarn.lock

File diff suppressed because it is too large Load diff