Skip to content

Commit

Permalink
chore: openapi swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Aug 16, 2024
1 parent 52a54cc commit 243cbd2
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 5 deletions.
17 changes: 16 additions & 1 deletion apps/backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
71 changes: 67 additions & 4 deletions apps/backend/src/modules/openapi/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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",
Expand All @@ -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(
"/",
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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",
Expand All @@ -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(
Expand All @@ -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(
"/",
Expand All @@ -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",
},
},
)
})
Expand Down

0 comments on commit 243cbd2

Please sign in to comment.