From 243cbd2b75c54f05fd56e920c0112dd40512a961 Mon Sep 17 00:00:00 2001 From: nichenqin Date: Fri, 16 Aug 2024 09:39:09 +0800 Subject: [PATCH] chore: openapi swagger --- apps/backend/src/app.ts | 17 ++++- apps/backend/src/modules/openapi/openapi.ts | 71 +++++++++++++++++++-- 2 files changed, 83 insertions(+), 5 deletions(-) diff --git a/apps/backend/src/app.ts b/apps/backend/src/app.ts index d7e1b2dfb..45d4ce2b8 100644 --- a/apps/backend/src/app.ts +++ b/apps/backend/src/app.ts @@ -22,6 +22,7 @@ import { WebhookEventsHandler } from "@undb/webhook" import { Elysia } from "elysia" import { all } from "radash" import { v4 } from "uuid" +import * as pkg from "../../../package.json" import { Auth, OpenAPI, Realtime, SpaceModule, TableModule, Web } from "./modules" import { FileService } from "./modules/file/file" import { OpenTelemetryModule } from "./modules/opentelemetry/opentelemetry.module" @@ -103,7 +104,21 @@ export const app = new Elysia() }) .use(cors()) .use(html()) - .use(swagger()) + .use( + swagger({ + path: "/openapi", + excludeStaticFile: true, + exclude: new RegExp(/^(?!.*\/api\/bases).*/), + documentation: { + info: { + title: "Undb OpenAPI Documentation", + version: pkg.version, + }, + + tags: [{ name: "Record", description: "Record operations" }], + }, + }), + ) .derive(auth.store()) .onError((ctx) => { ctx.logger.error(ctx.error) diff --git a/apps/backend/src/modules/openapi/openapi.ts b/apps/backend/src/modules/openapi/openapi.ts index cc5de05f0..7075674ca 100644 --- a/apps/backend/src/modules/openapi/openapi.ts +++ b/apps/backend/src/modules/openapi/openapi.ts @@ -112,6 +112,11 @@ export class OpenAPI { }, { params: t.Object({ baseName: t.String(), tableName: t.String() }), + detail: { + tags: ["Doc"], + summary: "Get OpenAPI documentation for a table", + description: "Get OpenAPI documentation for a table", + }, }, ) .group("/records", (app) => { @@ -156,7 +161,14 @@ export class OpenAPI { records: result.values, } }, - { params: t.Object({ baseName: t.String(), tableName: t.String() }) }, + { + params: t.Object({ baseName: t.String(), tableName: t.String() }), + detail: { + tags: ["Record"], + summary: "Get records", + description: "Get records", + }, + }, ) .get( "/:recordId", @@ -169,7 +181,14 @@ export class OpenAPI { data: result, } }, - { params: t.Object({ baseName: t.String(), tableName: t.String(), recordId: t.String() }) }, + { + params: t.Object({ baseName: t.String(), tableName: t.String(), recordId: t.String() }), + detail: { + tags: ["Record"], + summary: "Get record by id", + description: "Get record by id", + }, + }, ) .post( "/", @@ -182,6 +201,11 @@ export class OpenAPI { { params: t.Object({ baseName: t.String(), tableName: t.String() }), body: t.Object({ values: t.Record(t.String(), t.Any()) }), + detail: { + tags: ["Record"], + summary: "Create record", + description: "Create record", + }, }, ) .post( @@ -195,6 +219,11 @@ export class OpenAPI { { params: t.Object({ baseName: t.String(), tableName: t.String() }), body: t.Object({ records: t.Array(t.Object({ id: t.Optional(t.String()), values: t.Any() })) }), + detail: { + tags: ["Record"], + summary: "Create records", + description: "Create records", + }, }, ) .patch( @@ -215,6 +244,11 @@ export class OpenAPI { { params: t.Object({ baseName: t.String(), tableName: t.String(), recordId: t.String() }), body: t.Object({ values: t.Record(t.String(), t.Any()) }), + detail: { + tags: ["Record"], + summary: "Update record by id", + description: "Update record by id", + }, }, ) .patch( @@ -239,6 +273,11 @@ export class OpenAPI { filter: t.Any(), values: t.Record(t.String(), t.Any()), }), + detail: { + tags: ["Record"], + summary: "Update records", + description: "Update records", + }, }, ) .post( @@ -249,7 +288,14 @@ export class OpenAPI { this.commandBus.execute(new DuplicateRecordCommand({ baseName, tableName, id: ctx.params.recordId })), ) }, - { params: t.Object({ baseName: t.String(), tableName: t.String(), recordId: t.String() }) }, + { + params: t.Object({ baseName: t.String(), tableName: t.String(), recordId: t.String() }), + detail: { + tags: ["Record"], + summary: "Duplicate record by id", + description: "Duplicate record by id", + }, + }, ) .post( "/records/duplicate", @@ -269,6 +315,11 @@ export class OpenAPI { { params: t.Object({ baseName: t.String(), tableName: t.String() }), body: t.Object({ filter: t.Any() }), + detail: { + tags: ["Record"], + summary: "Duplicate records", + description: "Duplicate records", + }, }, ) .delete( @@ -279,7 +330,14 @@ export class OpenAPI { this.commandBus.execute(new DeleteRecordCommand({ baseName, tableName, id: ctx.params.recordId })), ) }, - { params: t.Object({ baseName: t.String(), tableName: t.String(), recordId: t.String() }) }, + { + params: t.Object({ baseName: t.String(), tableName: t.String(), recordId: t.String() }), + detail: { + tags: ["Record"], + summary: "Delete record by id", + description: "Delete record by id", + }, + }, ) .delete( "/", @@ -299,6 +357,11 @@ export class OpenAPI { { params: t.Object({ baseName: t.String(), tableName: t.String() }), body: t.Object({ filter: t.Any() }), + detail: { + tags: ["Record"], + summary: "Delete records", + description: "Delete records", + }, }, ) })