Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
throw new Error('Error when spliting CAPI request (second half)')
}
const firstArray = Object.entries(first)
const lastArray = Object.entries(last)
return fromPairs(firstArray.concat(lastArray))
}
console.log('Making CAPI query', endpoint)
console.log('Debug link:', endpoint.replace(/thrift/g, 'json'))
const resp = await attempt(fetch(endpoint))
if (hasFailed(resp)) throw new Error('Could not connect to CAPI.')
const buffer = await resp.arrayBuffer()
const receiver: BufferedTransport = BufferedTransport.receiver(
Buffer.from(buffer),
)
const input = new CompactProtocol(receiver)
const data = SearchResponseCodec.decode(input)
const results: IContent[] = data.results
const articlePromises = await Promise.all(
results.map(result => attempt(parseArticleResult(result, isFromPrint))),
)
//If we fail to get an article in a collection we just ignore it and move on.
articlePromises.forEach(attempt => {
if (hasFailed(attempt)) {
console.log('failure when parsing', attempt.error)
}
})
const articleEntries = articlePromises.filter(hasSucceeded)
return fromPairs(articleEntries)
}
export const getArticle = async (path: string): Promise<article> => {
const resp = await fetch(url(path))
const buffer = await resp.arrayBuffer()
const receiver: BufferedTransport = BufferedTransport.receiver(
new Buffer(buffer),
)
const input = new CompactProtocol(receiver)
const data = ItemResponseCodec.decode(input)
const title = data && data.content && data.content.webTitle
if (title == null) throw new Error('Title was undefined!')
const image =
data &&
data.content &&
data.content.blocks &&
data.content.blocks.main &&
data.content.blocks.main.elements &&
data.content.blocks.main.elements[0].assets &&
extractImage(data.content.blocks.main.elements[0].assets)
const imageURL =
(image && image.file) ||
'https://media.guim.co.uk/d1c48b0c6ec594b396f786cfd3f6ba6ae0d93516/0_105_2754_1652/2754.jpg'</article>