I'm making multiple calls to the server and creating pages and posts in yaml format from the API in my gulpfile. The functions seem similar enough that I could just make one function for all of them.
Where I'm getting hung up is when creating the object for each item since jsonObj
can't be defined outside of the XMLHttpRequest
Here's my code:
function collectLoop(slug, tempDir, destDir, args) {
const newObject = args;
fs.writeFile(`${tempDir}${slug}.json`,
JSON.stringify(newObject), function (err) {
if (err) throw err;
});
gulp.src(`${tempDir}${slug}.json`)
.pipe(jsonToYaml({ safe: true}))
.pipe(gulp.dest(destDir));
};
/*********************************************************************/
/******************************* PAGES *******************************/
/*********************************************************************/
function pageCollection(url, tempDir, destDir) {
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
if (xhr.readyState === 4 && xhr.status === 200) {
const jsonObj = JSON.parse(xhr.responseText);
checkDir(tempDir);
checkDir(destDir);
for (let i = 0; i < jsonObj.length; i += 1) {
let hero_heading = false;
let hero_subheading = false;
if(jsonObj[i].acf.hero != undefined) {
hero_heading = jsonObj[i].acf.hero.heading;
hero_subheading = jsonObj[i].acf.hero.subheading;
}
collectLoop(jsonObj[i].slug, tempDir, destDir, {
"obj_id" : jsonObj[i].id,
"title" : jsonObj[i].title.rendered,
"slug" : jsonObj[i].slug,
"modified" : jsonObj[i].modified,
"featured_image" : {
"large" : jsonObj[i]._embedded['wp:featuredmedia'][0].source_url,
"small" : jsonObj[i]._embedded['wp:featuredmedia'][0].media_details.sizes.thumbnail.source_url,
},
"settings" : {
"contact_box" : jsonObj[i].acf.contact_box,
"footer_map" : jsonObj[i].acf.map,
"hero_banner" : jsonObj[i].acf.hero_banner,
},
"hero" : {
"heading" : hero_heading,
"subheading" : hero_subheading,
},
"contact_form" : jsonObj[i].acf.contact_form,
"excerpt" : jsonObj[i].acf.excerpt,
"content" : jsonObj[i].content.rendered,
});
}
}
};
xhr.open('GET', url);
xhr.send();
}
/*********************************************************************/
/******************************* POSTS *******************************/
/*********************************************************************/
function postCollection(url, tempDir, destDir) {
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
if (xhr.readyState === 4 && xhr.status === 200) {
const jsonObj = JSON.parse(xhr.responseText);
checkDir(tempDir);
checkDir(destDir);
for (let i = 0; i < jsonObj.length; i += 1) {
collectLoop(jsonObj[i].slug, tempDir, destDir, {
"obj_id" : jsonObj[i].id,
"title" : jsonObj[i].title.rendered,
"slug" : jsonObj[i].slug,
"date" : jsonObj[i].date,
"modified" : jsonObj[i].modified,
"featured_image" : {
"large" : jsonObj[i]._embedded['wp:featuredmedia'][0].source_url,
"small" : jsonObj[i]._embedded['wp:featuredmedia'][0].media_details.sizes.thumbnail.source_url,
},
"excerpt" : jsonObj[i].excerpt.rendered,
"content" : jsonObj[i].content.rendered,
});
}
}
};
xhr.open('GET', url);
xhr.send();
}