I am working on an element that given a csv file from a user, takes rows of commands (adding, renaming, updating, removing different items) and executes them into a database on the basis that the commands are correct and legal.
Currently I try to validate each command before it is executed, but I can't catch every mistake like them having a typo when they add an ingredient, and then trying to reference that ingredient by it's correct name in a later row. Because of this, some commands will attempt to execute and fail. Is there a way to "mock-call" every command to ensure they work before actually executing each command?
Currently all commands are executed in this function, and I return true or false if any of the commands failed to execute (for now) which sets the state to either show a success message or not.
try {
const { categories } = await fetchCategories();
let results = await sheetData.reduce(async (accum: any, ingredient: any) => {
let updateArray = await accum;
// console.log(`>>> ${ingredient.command} ${ingredient.name}`)
updateArray.push(await buildExecute(ingredient, categories));
// console.log(`<<< ${ingredient.command} ${ingredient.name}`)
return updateArray;
}, Promise.resolve([]));
const result = await Promise.all(results);
return !result.some(val => val === false)
} catch (err) {
return false;
}
inside buildExecute
, I simply build up the endpoints to call, the data to include in the body, and the method for the fetch as such
try {
const { result, error } = await (await fetch(endpoint, {
method: method,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})).json();
if (error) {
console.error(error);
return false;
}
return true;
} catch (err) {
console.error(err);
return false;
}
If there's a library that does what I'm looking for, I'm game to try it out, or if there's a built in method, that works too.