Swagger setup

This commit is contained in:
Zoe Roux 2024-11-16 17:12:10 +01:00
parent 0edf216618
commit b8b536632d
No known key found for this signature in database
2 changed files with 52 additions and 9 deletions

View File

@ -1,9 +1,10 @@
import { Elysia, t } from "elysia"; import { Elysia, t } from "elysia";
import { Movie } from "../models/movie"; import { Movie, MovieTranslation } from "../models/movie";
import { db } from "../db"; import { db } from "../db";
import { shows, showTranslations } from "../db/schema/shows"; import { shows, showTranslations } from "../db/schema/shows";
import { eq, and, sql, or, inArray } from "drizzle-orm"; import { eq, and, sql, or, inArray } from "drizzle-orm";
import { getColumns } from "../db/schema/utils"; import { getColumns } from "../db/schema/utils";
import { bubble } from "../models/examples";
const translations = db const translations = db
.selectDistinctOn([showTranslations.language]) .selectDistinctOn([showTranslations.language])
@ -22,7 +23,6 @@ const translations = db
const { pk: _, kind, startAir, endAir, ...moviesCol } = getColumns(shows); const { pk: _, kind, startAir, endAir, ...moviesCol } = getColumns(shows);
const { pk, language, ...translationsCol } = getColumns(translations); const { pk, language, ...translationsCol } = getColumns(translations);
const findMovie = db const findMovie = db
.select({ .select({
...moviesCol, ...moviesCol,
@ -47,16 +47,29 @@ const findMovie = db
export const movies = new Elysia({ prefix: "/movies" }) export const movies = new Elysia({ prefix: "/movies" })
.model({ .model({
movie: Movie, movie: Movie,
"movie-translation": MovieTranslation,
error: t.Object({}), error: t.Object({}),
}) })
.guard({ .guard({
params: t.Object({ 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" }, response: { 200: "movie", 404: "error" },
tags: ["Movies"],
}) })
.get("/:id", async ({ params: { id }, error }) => { .get(
const ret = await findMovie.execute({ id }); "/:id",
if (ret.length !== 1) return error(404, {}); async ({ params: { id }, error }) => {
return ret[0]; 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",
},
},
);

View File

@ -5,9 +5,12 @@ import { Elysia } from "elysia";
import { entries } from "./controllers/entries"; import { entries } from "./controllers/entries";
import { movies } from "./controllers/movies"; import { movies } from "./controllers/movies";
import { seasons } from "./controllers/seasons"; import { seasons } from "./controllers/seasons";
import { seed } from "./controllers/seed";
import { series } from "./controllers/series"; import { series } from "./controllers/series";
import { videos } from "./controllers/videos"; import { videos } from "./controllers/videos";
import { db } from "./db"; import { db } from "./db";
import { Image } from "./models/utils";
import { comment } from "./utils";
await migrate(db, { migrationsSchema: "kyoo", migrationsFolder: "./drizzle" }); await migrate(db, { migrationsSchema: "kyoo", migrationsFolder: "./drizzle" });
@ -33,13 +36,40 @@ if (!secret) {
const app = new Elysia() const app = new Elysia()
.use(jwt({ secret })) .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") .get("/", () => "Hello Elysia")
.model({ image: Image })
.use(movies) .use(movies)
.use(series) .use(series)
.use(entries) .use(entries)
.use(seasons) .use(seasons)
.use(videos) .use(videos)
.use(seed)
.listen(3000); .listen(3000);
console.log(`Api running at ${app.server?.hostname}:${app.server?.port}`); console.log(`Api running at ${app.server?.hostname}:${app.server?.port}`);