diff --git a/src/lib/utils/db.ts b/src/lib/utils/db.ts index 5bc72ae..1783bc4 100644 --- a/src/lib/utils/db.ts +++ b/src/lib/utils/db.ts @@ -27,9 +27,10 @@ export const resetDatabase = () => { return db; }; -export const prepareDatabase = async (jsonFile: string) => { +export const prepareDatabase = async (jsonFile: string | object) => { const db = resetDatabase(); - const data = await parseOpenAPI(JSON.parse(jsonFile)); + const jsonFileData = typeof jsonFile === 'string' ? JSON.parse(jsonFile) : jsonFile; + const data = await parseOpenAPI(jsonFileData); db.exec( `INSERT INTO GlobalData (data) VALUES ('${JSON.stringify(data.global).replace(/'/g, "''")}')` ); diff --git a/src/lib/utils/openapi.ts b/src/lib/utils/openapi.ts index 1733f30..663467d 100644 --- a/src/lib/utils/openapi.ts +++ b/src/lib/utils/openapi.ts @@ -43,10 +43,10 @@ export const parseOpenAPI = async (openapi: OAS31Document) => { const menu: Menu = []; const topics = oas.getExtension('x-introduction-pages') as Topic[] | undefined; // @ts-expect-error - x-logo is not a standard OpenAPI field - const logo = oas.getDefinition().info['x-logo']?.url; - const description = oas.getDefinition().info.description; - const version = oas.getDefinition().info.version; - const servers = oas.api.servers || []; + const logo = oas.getDefinition().info?.['x-logo']?.url; + const description = oas.getDefinition().info?.description; + const version = oas.getDefinition().info?.version; + const servers = oas.api?.servers || []; const pagesOrder: GlobalData['pagesOrder'] = []; if (topics?.length) { diff --git a/src/routes/api/openapi/+server.ts b/src/routes/api/openapi/+server.ts new file mode 100644 index 0000000..3129db7 --- /dev/null +++ b/src/routes/api/openapi/+server.ts @@ -0,0 +1,11 @@ +import type { RequestHandler } from './$types'; +import { json } from '@sveltejs/kit'; +import { prepareDatabase } from '$lib/utils/db'; + +export const PUT: RequestHandler = async ({ request }) => { + const data = await request.json(); + + await prepareDatabase(data); + + return json({}); +};