diff --git a/api/src/base.ts b/api/src/elysia.ts similarity index 61% rename from api/src/base.ts rename to api/src/elysia.ts index 0de1e4aa..68eb2cd3 100644 --- a/api/src/base.ts +++ b/api/src/elysia.ts @@ -1,4 +1,11 @@ -import Elysia from "elysia"; +import { Elysia } from "elysia"; +import { entries } from "./controllers/entries"; +import { movies } from "./controllers/movies"; +import { seasonsH } from "./controllers/seasons"; +import { seed } from "./controllers/seed"; +import { series } from "./controllers/series"; +import { videosH } from "./controllers/videos"; + import type { KError } from "./models/error"; export const base = new Elysia({ name: "base" }) @@ -30,3 +37,12 @@ export const base = new Elysia({ name: "base" }) return error; }) .as("plugin"); + +export const app = new Elysia() + .use(base) + .use(movies) + .use(series) + .use(entries) + .use(seasonsH) + .use(videosH) + .use(seed); diff --git a/api/src/index.ts b/api/src/index.ts index 348d459b..7f2d13ab 100644 --- a/api/src/index.ts +++ b/api/src/index.ts @@ -1,15 +1,7 @@ import jwt from "@elysiajs/jwt"; import { swagger } from "@elysiajs/swagger"; -import { Elysia } from "elysia"; -import { base } from "./base"; -import { entries } from "./controllers/entries"; -import { movies } from "./controllers/movies"; -import { seasonsH } from "./controllers/seasons"; -import { seed } from "./controllers/seed"; -import { series } from "./controllers/series"; -import { videosH } from "./controllers/videos"; import { migrate } from "./db"; -import { Image } from "./models/utils"; +import { app } from "./elysia"; import { comment } from "./utils"; await migrate(); @@ -31,8 +23,7 @@ if (!secret) { process.exit(1); } -const app = new Elysia() - .use(base) +app .use(jwt({ secret })) .use( swagger({ @@ -70,13 +61,6 @@ const app = new Elysia() }, }), ) - .model({ image: Image }) - .use(movies) - .use(series) - .use(entries) - .use(seasonsH) - .use(videosH) - .use(seed) .listen(3000); console.log(`Api running at ${app.server?.hostname}:${app.server?.port}`); diff --git a/api/tests/helpers/index.ts b/api/tests/helpers/index.ts new file mode 100644 index 00000000..62e4bb29 --- /dev/null +++ b/api/tests/helpers/index.ts @@ -0,0 +1,5 @@ +export * from "./movies-helper"; +export * from "./series-helper"; +export * from "./videos-helper"; + +export * from "~/elysia"; diff --git a/api/tests/helper.ts b/api/tests/helpers/movies-helper.ts similarity index 61% rename from api/tests/helper.ts rename to api/tests/helpers/movies-helper.ts index 80950c61..f2317ea6 100644 --- a/api/tests/helper.ts +++ b/api/tests/helpers/movies-helper.ts @@ -1,19 +1,6 @@ -import Elysia from "elysia"; import { buildUrl } from "tests/utils"; -import { base } from "~/base"; -import { movies } from "~/controllers/movies"; -import { seed } from "~/controllers/seed"; -import { series } from "~/controllers/series"; -import { videosH } from "~/controllers/videos"; +import { app } from "~/elysia"; import type { SeedMovie } from "~/models/movie"; -import type { SeedVideo } from "~/models/video"; - -export const app = new Elysia() - .use(base) - .use(movies) - .use(series) - .use(videosH) - .use(seed); export const getMovie = async ( id: string, @@ -72,17 +59,3 @@ export const createMovie = async (movie: SeedMovie) => { const body = await resp.json(); return [resp, body] as const; }; - -export const createVideo = async (video: SeedVideo | SeedVideo[]) => { - const resp = await app.handle( - new Request(buildUrl("videos"), { - method: "POST", - body: JSON.stringify(Array.isArray(video) ? video : [video]), - headers: { - "Content-Type": "application/json", - }, - }), - ); - const body = await resp.json(); - return [resp, body] as const; -}; diff --git a/api/tests/helpers/series-helper.ts b/api/tests/helpers/series-helper.ts new file mode 100644 index 00000000..51ab6567 --- /dev/null +++ b/api/tests/helpers/series-helper.ts @@ -0,0 +1,17 @@ +import { buildUrl } from "tests/utils"; +import { app } from "~/elysia"; +import type { SeedSerie } from "~/models/serie"; + +export const createSerie = async (serie: SeedSerie) => { + const resp = await app.handle( + new Request(buildUrl("series"), { + method: "POST", + body: JSON.stringify(serie), + headers: { + "Content-Type": "application/json", + }, + }), + ); + const body = await resp.json(); + return [resp, body] as const; +}; diff --git a/api/tests/helpers/videos-helper.ts b/api/tests/helpers/videos-helper.ts new file mode 100644 index 00000000..5dd0bb70 --- /dev/null +++ b/api/tests/helpers/videos-helper.ts @@ -0,0 +1,17 @@ +import { buildUrl } from "tests/utils"; +import { app } from "~/elysia"; +import type { SeedVideo } from "~/models/video"; + +export const createVideo = async (video: SeedVideo | SeedVideo[]) => { + const resp = await app.handle( + new Request(buildUrl("videos"), { + method: "POST", + body: JSON.stringify(Array.isArray(video) ? video : [video]), + headers: { + "Content-Type": "application/json", + }, + }), + ); + const body = await resp.json(); + return [resp, body] as const; +}; diff --git a/api/tests/movies/get-all-movies-with-null.test.ts b/api/tests/movies/get-all-movies-with-null.test.ts index 893c4612..5e03cb56 100644 --- a/api/tests/movies/get-all-movies-with-null.test.ts +++ b/api/tests/movies/get-all-movies-with-null.test.ts @@ -6,7 +6,7 @@ import { shows } from "~/db/schema"; import { bubble } from "~/models/examples"; import { dune1984 } from "~/models/examples/dune-1984"; import { dune } from "~/models/examples/dune-2021"; -import { app, createMovie, getMovies } from "../helper"; +import { app, createMovie, getMovies } from "../helpers"; beforeAll(async () => { await db.delete(shows); @@ -14,7 +14,7 @@ beforeAll(async () => { }); describe("with a null value", () => { - // Those before/after hooks are NOT scopped to the describe due to a bun bug + // Those before/after hooks are NOT scoped to the describe due to a bun bug // instead we just make a new file for those /shrug // see: https://github.com/oven-sh/bun/issues/5738 beforeAll(async () => { diff --git a/api/tests/movies/get-all-movies.test.ts b/api/tests/movies/get-all-movies.test.ts index a10c2f56..c15a6548 100644 --- a/api/tests/movies/get-all-movies.test.ts +++ b/api/tests/movies/get-all-movies.test.ts @@ -8,7 +8,7 @@ import { dune1984 } from "~/models/examples/dune-1984"; import { dune } from "~/models/examples/dune-2021"; import type { Movie } from "~/models/movie"; import { isUuid } from "~/models/utils"; -import { getMovies, app } from "../helper"; +import { getMovies, app } from "../helpers"; beforeAll(async () => { await db.delete(shows); diff --git a/api/tests/movies/get-movie.test.ts b/api/tests/movies/get-movie.test.ts index 675f1f70..a0a3f14a 100644 --- a/api/tests/movies/get-movie.test.ts +++ b/api/tests/movies/get-movie.test.ts @@ -2,7 +2,7 @@ import { beforeAll, describe, expect, it } from "bun:test"; import { expectStatus } from "tests/utils"; import { seedMovie } from "~/controllers/seed/movies"; import { bubble } from "~/models/examples"; -import { getMovie } from "../helper"; +import { getMovie } from "../helpers"; let bubbleId = ""; diff --git a/api/tests/movies/seed-movies.test.ts b/api/tests/movies/seed-movies.test.ts index 82d5086b..34e721de 100644 --- a/api/tests/movies/seed-movies.test.ts +++ b/api/tests/movies/seed-movies.test.ts @@ -5,7 +5,7 @@ import { db } from "~/db"; import { showTranslations, shows, videos } from "~/db/schema"; import { bubble } from "~/models/examples"; import { dune, duneVideo } from "~/models/examples/dune-2021"; -import { createMovie, createVideo } from "../helper"; +import { createMovie, createVideo } from "../helpers"; describe("Movie seeding", () => { it("Can create a movie", async () => {