Skip to content

Commit

Permalink
feat: new endpoint to update definition
Browse files Browse the repository at this point in the history
  • Loading branch information
Mokto committed May 11, 2024
1 parent 7a5ff3b commit 10b730f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/lib/utils/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, "''")}')`
);
Expand Down
8 changes: 4 additions & 4 deletions src/lib/utils/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions src/routes/api/openapi/+server.ts
Original file line number Diff line number Diff line change
@@ -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({});
};

0 comments on commit 10b730f

Please sign in to comment.