Skip to content

Commit

Permalink
fix: return an error if no element is found when adding the schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Mokto committed May 11, 2024
1 parent 84fb831 commit eb57c85
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/lib/utils/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ export const resetDatabase = () => {
export const prepareDatabase = async (jsonFile: string | object) => {
const jsonFileData = typeof jsonFile === 'string' ? JSON.parse(jsonFile) : jsonFile;
const data = await parseOpenAPI(jsonFileData);
const countOperations = Object.keys(data.operations).length + Object.keys(data.webhooks).length;
if (countOperations === 0) {
return countOperations;
const elementsCount = Object.keys(data.operations).length + Object.keys(data.webhooks).length;
if (elementsCount === 0) {
return elementsCount;
}
const db = resetDatabase();
db.exec(
Expand All @@ -51,7 +51,7 @@ export const prepareDatabase = async (jsonFile: string | object) => {
);
});

return countOperations;
return elementsCount;
};
export const getGlobalData = async () => {
const db = getDb();
Expand Down
5 changes: 4 additions & 1 deletion src/routes/api/openapi/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ export const PUT: RequestHandler = async ({ request, url }) => {
}

const data = await request.json();
await prepareDatabase(data);
const elementsCount = await prepareDatabase(data);
if (elementsCount === 0) {
error(400, 'No operations or webhooks found in the provided JSON.');
}

return json({});
};

0 comments on commit eb57c85

Please sign in to comment.