Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
if (hasFailed(last)) {
console.error(last.error)
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)
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)</article>