Added support for fetching content directly from host. Improved pattern interpolation. Refactored content modules.
This commit is contained in:
+15
-13
@@ -3,30 +3,32 @@
|
||||
const config = require('config');
|
||||
const fetch = require('node-fetch');
|
||||
|
||||
function fetchItem(url, attempt, post) {
|
||||
function retry(error) {
|
||||
console.log('\x1b[31m%s\x1b[0m', `Failed to fetch '${url}': ${error.message} (${post.permalink})`);
|
||||
async function fetchItem(url, attempt, post) {
|
||||
async function retry(error) {
|
||||
console.log('\x1b[31m%s\x1b[0m', `Failed to fetch '${url}': ${error.message} (${post ? post.permalink : 'no post'})`);
|
||||
|
||||
if(attempt < config.fetch.retries) {
|
||||
if (attempt < config.fetch.retries) {
|
||||
console.log('Retrying...');
|
||||
|
||||
return fetchItem(url, ++attempt, post);
|
||||
return fetchItem(url, attempt + 1, post);
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
}
|
||||
|
||||
return fetch(url).then(res => {
|
||||
if(!res.ok) {
|
||||
try {
|
||||
const res = await fetch(url);
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error(`Response not OK for '${url}', HTTP code '${res.status}'`);
|
||||
}
|
||||
|
||||
return res;
|
||||
}).then(res => {
|
||||
console.log(`Fetched '${url}' (${post.permalink})`);
|
||||
console.log(`Fetched '${url}' (${post ? post.permalink : 'no post'})`);
|
||||
|
||||
return res.body;
|
||||
}).catch(retry);
|
||||
};
|
||||
} catch (error) {
|
||||
return retry(error);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = fetchItem;
|
||||
|
||||
Reference in New Issue
Block a user