From b8b536632deed96ff2a7f0f3e381b4a5d0716318 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sat, 16 Nov 2024 17:12:10 +0100 Subject: [PATCH] Swagger setup --- api/src/controllers/movies.ts | 29 +++++++++++++++++++++-------- api/src/index.ts | 32 +++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/api/src/controllers/movies.ts b/api/src/controllers/movies.ts index 13242ec6..b0aa6c88 100644 --- a/api/src/controllers/movies.ts +++ b/api/src/controllers/movies.ts @@ -1,9 +1,10 @@ import { Elysia, t } from "elysia"; -import { Movie } from "../models/movie"; +import { Movie, MovieTranslation } from "../models/movie"; import { db } from "../db"; import { shows, showTranslations } from "../db/schema/shows"; import { eq, and, sql, or, inArray } from "drizzle-orm"; import { getColumns } from "../db/schema/utils"; +import { bubble } from "../models/examples"; const translations = db .selectDistinctOn([showTranslations.language]) @@ -22,7 +23,6 @@ const translations = db const { pk: _, kind, startAir, endAir, ...moviesCol } = getColumns(shows); const { pk, language, ...translationsCol } = getColumns(translations); - const findMovie = db .select({ ...moviesCol, @@ -47,16 +47,29 @@ const findMovie = db export const movies = new Elysia({ prefix: "/movies" }) .model({ movie: Movie, + "movie-translation": MovieTranslation, error: t.Object({}), }) .guard({ params: t.Object({ - id: t.String(), + id: t.String({ + description: "The id or slug of the movie to retrieve", + examples: [bubble.slug], + }), }), response: { 200: "movie", 404: "error" }, + tags: ["Movies"], }) - .get("/:id", async ({ params: { id }, error }) => { - const ret = await findMovie.execute({ id }); - if (ret.length !== 1) return error(404, {}); - return ret[0]; - }); + .get( + "/:id", + async ({ params: { id }, error }) => { + const ret = await findMovie.execute({ id }); + if (ret.length !== 1) return error(404, {}); + return ret[0]; + }, + { + detail: { + description: "Get a movie by id or slug", + }, + }, + ); diff --git a/api/src/index.ts b/api/src/index.ts index 465c0167..07bd1d77 100644 --- a/api/src/index.ts +++ b/api/src/index.ts @@ -5,9 +5,12 @@ import { Elysia } from "elysia"; import { entries } from "./controllers/entries"; import { movies } from "./controllers/movies"; import { seasons } from "./controllers/seasons"; +import { seed } from "./controllers/seed"; import { series } from "./controllers/series"; import { videos } from "./controllers/videos"; import { db } from "./db"; +import { Image } from "./models/utils"; +import { comment } from "./utils"; await migrate(db, { migrationsSchema: "kyoo", migrationsFolder: "./drizzle" }); @@ -33,13 +36,40 @@ if (!secret) { const app = new Elysia() .use(jwt({ secret })) - .use(swagger()) + .use( + swagger({ + documentation: { + info: { + title: "Kyoo", + description: comment` + Complete API documentation of Kyoo. + If you need a route not present here, please make an issue over https://github.com/zoriya/kyoo + `, + version: "5.0.0", + contact: { name: "github", url: "https://github.com/zoriya/kyoo" }, + license: { + name: "GPL-3.0 license", + url: "https://github.com/zoriya/Kyoo/blob/master/LICENSE", + }, + }, + servers: [ + { + url: "https://kyoo.zoriya.dev/api", + description: "Kyoo's demo server", + }, + ], + tags: [{ name: "Movies", description: "Routes about movies" }], + }, + }), + ) .get("/", () => "Hello Elysia") + .model({ image: Image }) .use(movies) .use(series) .use(entries) .use(seasons) .use(videos) + .use(seed) .listen(3000); console.log(`Api running at ${app.server?.hostname}:${app.server?.port}`);