Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fail the upload script if an error occur (#74) #76

Merged
merged 2 commits into from
May 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions scripts/upload-documents.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import path from 'node:path';
// ```
async function uploadDocuments(apiUrl, dataFolder) {
try {
const uploadUrl = `${apiUrl}/api/documents`;
const files = await fs.readdir(dataFolder);
console.log(`Uploading documents to: ${uploadUrl}`);

/* eslint-disable no-await-in-loop */
for (const file of files) {
Expand All @@ -20,18 +22,23 @@ async function uploadDocuments(apiUrl, dataFolder) {
const formData = new FormData();
formData.append('file', new File(blobParts, file));

const response = await fetch(`${apiUrl}/api/documents`, {
const response = await fetch(uploadUrl, {
method: 'post',
body: formData,
});

const responseData = await response.json();
console.log(responseData);
if (response.ok) {
console.log(`${file}: ${responseData.message}`);
} else {
throw new Error(responseData.error);
}
}
}
/* eslint-enable no-await-in-loop */
} catch (error) {
console.error(`Could not upload documents: ${error.message}`);
process.exitCode = -1;
}
}

Expand Down