@tricoteuses/senat
Version:
Handle French Sénat's open data
29 lines (28 loc) • 931 B
JavaScript
import fs from "fs-extra";
export function isOptionEmptyOrHasValue(option, value) {
return !option || option.length === 0 || option.includes(value);
}
export function ensureAndClearDir(path) {
if (!fs.existsSync(path)) {
fs.mkdirSync(path, { recursive: true });
}
else {
fs.emptyDirSync(path);
}
}
export async function fetchWithRetry(url, retries = 3, backoff = 300) {
for (let attempt = 0; attempt < retries; attempt++) {
try {
return await fetch(url);
}
catch (error) {
if (attempt === retries - 1) {
throw error;
}
console.warn(`Fetch attempt ${attempt + 1} for ${url} failed. Retrying in ${backoff}ms…`);
await new Promise((resolve) => setTimeout(resolve, backoff));
backoff *= 2;
}
}
throw new Error(`Failed to fetch ${url} after ${retries} attempts`);
}