mirror of
https://github.com/oceanprotocol/webtasks
synced 2025-01-07 20:35:30 +01:00
better error feedback when medium response is faulty
This commit is contained in:
parent
6a8232fbfc
commit
6dc4862cbb
@ -7,19 +7,30 @@ const webtask = require('webtask-tools')
|
|||||||
const app = express()
|
const app = express()
|
||||||
|
|
||||||
app.get('/', (req, res) => {
|
app.get('/', (req, res) => {
|
||||||
res.send('Please enter a username as a parameter')
|
res.status(400).send('Please enter a username as a parameter')
|
||||||
})
|
})
|
||||||
|
|
||||||
app.get('/:username', (req, res) => {
|
app.get('/:username', (req, res) => {
|
||||||
const url = `https://medium.com/${req.params.username}/latest?format=json`
|
const url = `https://medium.com/${req.params.username}/latest?format=json`
|
||||||
|
const j = request.jar()
|
||||||
|
const requestWithCookies = request.defaults({ jar: j, json: true })
|
||||||
|
|
||||||
request(url, (error, response) => {
|
requestWithCookies(url, (error, response) => {
|
||||||
const json = JSON.parse(response.body.replace('])}while(1);</x>', ''))
|
const prefix = '])}while(1);</x>'
|
||||||
|
if (!response.body.includes(prefix)) {
|
||||||
|
res.status(500).send({
|
||||||
|
success: false,
|
||||||
|
reason: 'Failed getting posts from Medium.'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
const json = JSON.parse(response.body.replace(prefix, ''))
|
||||||
const { posts } = json.payload
|
const { posts } = json.payload
|
||||||
const parsedPosts = []
|
const parsedPosts = []
|
||||||
let holder = {}
|
let holder = {}
|
||||||
|
|
||||||
if (error) return
|
if (error) {
|
||||||
|
res.status(error.status).send({ success: false })
|
||||||
|
}
|
||||||
|
|
||||||
for (let i = 0; i < posts.length; i++) {
|
for (let i = 0; i < posts.length; i++) {
|
||||||
holder.id = posts[i].id
|
holder.id = posts[i].id
|
||||||
@ -27,12 +38,8 @@ app.get('/:username', (req, res) => {
|
|||||||
holder.readingTime = posts[i].virtuals.readingTime
|
holder.readingTime = posts[i].virtuals.readingTime
|
||||||
holder.title = posts[i].title
|
holder.title = posts[i].title
|
||||||
holder.subtitle = posts[i].virtuals.subtitle
|
holder.subtitle = posts[i].virtuals.subtitle
|
||||||
holder.imageUrl = `https://cdn-images-1.medium.com/max/600/${
|
holder.imageUrl = `https://cdn-images-1.medium.com/max/600/${posts[i].virtuals.previewImage.imageId}`
|
||||||
posts[i].virtuals.previewImage.imageId
|
holder.postUrl = `https://medium.com/${req.params.username}/${posts[i].id}`
|
||||||
}`
|
|
||||||
holder.postUrl = `https://medium.com/${req.params.username}/${
|
|
||||||
posts[i].id
|
|
||||||
}`
|
|
||||||
parsedPosts.push(holder)
|
parsedPosts.push(holder)
|
||||||
holder = {}
|
holder = {}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user