diff --git a/api/src/controllers/entries.ts b/api/src/controllers/entries.ts new file mode 100644 index 00000000..90ca27cf --- /dev/null +++ b/api/src/controllers/entries.ts @@ -0,0 +1,32 @@ +import { Elysia, t } from "elysia"; +import { + type Entry, + Episode, + Extra, + MovieEntry, + Special, + UnknownEntry, +} from "../models/entry"; + +export const entries = new Elysia() + .model({ + episode: Episode, + movie_entry: MovieEntry, + special: Special, + extra: Extra, + unknown_entry: UnknownEntry, + error: t.Object({}), + }) + .model((models) => ({ + ...models, + entry: t.Union([models.episode, models.movie_entry, models.special]), + })) + .get("/entries/:id", () => "hello" as unknown as Entry, { + response: { 200: "entry" }, + }) + .get("/extras/:id", () => "hello" as unknown as Extra, { + response: { 200: "extra" }, + }) + .get("/unknowns/:id", () => "hello" as unknown as UnknownEntry, { + response: { 200: "unknown_entry" }, + }); diff --git a/api/src/index.ts b/api/src/index.ts index 7295c631..4460373d 100644 --- a/api/src/index.ts +++ b/api/src/index.ts @@ -2,6 +2,7 @@ import jwt from "@elysiajs/jwt"; import { swagger } from "@elysiajs/swagger"; import { migrate } from "drizzle-orm/node-postgres/migrator"; import { Elysia } from "elysia"; +import { entries } from "./controllers/entries"; import { movies } from "./controllers/movies"; import { series } from "./controllers/series"; import { videos } from "./controllers/videos"; @@ -35,6 +36,7 @@ const app = new Elysia() .get("/", () => "Hello Elysia") .use(movies) .use(series) + .use(entries) .use(videos) .listen(3000); diff --git a/api/src/models/entry.ts b/api/src/models/entry.ts index 9a5f6e38..c2aff7e3 100644 --- a/api/src/models/entry.ts +++ b/api/src/models/entry.ts @@ -3,7 +3,7 @@ import { Image } from "./utils/image"; import { ExternalId, EpisodeId } from "./utils/external-id"; import { comment } from "../utils"; -export const Entry = t.Object({ +const BaseEntry = t.Object({ id: t.String({ format: "uuid" }), slug: t.String(), serieId: t.String({ format: "uuid" }), @@ -19,8 +19,8 @@ export const Entry = t.Object({ nextRefresh: t.String({ format: "date-time" }), }); -export const Episode = t.Union([ - Entry, +export const Episode = t.Intersect([ + BaseEntry, t.Object({ kind: t.Literal("episode"), seasonId: t.String({ format: "uuid" }), @@ -32,9 +32,9 @@ export const Episode = t.Union([ ]); export type Episode = typeof Episode.static; -export const MovieEntry = t.Union( +export const MovieEntry = t.Intersect( [ - Entry, + BaseEntry, t.Object({ kind: t.Literal("movie"), order: t.Number({ @@ -53,9 +53,9 @@ export const MovieEntry = t.Union( ); export type MovieEntry = typeof MovieEntry.static; -export const Special = t.Union( +export const Special = t.Intersect( [ - Entry, + BaseEntry, t.Object({ kind: t.Literal("special"), order: t.Number({ @@ -75,9 +75,9 @@ export const Special = t.Union( ); export type Special = typeof Special.static; -export const Extra = t.Union( +export const Extra = t.Intersect( [ - Entry, + BaseEntry, t.Object({ kind: t.Literal("extra"), number: t.Number({ minimum: 1 }), @@ -94,9 +94,9 @@ export const Extra = t.Union( ); export type Extra = typeof Extra.static; -export const Video = t.Union( +export const UnknownEntry = t.Intersect( [ - t.Omit(Entry, ["serieId", "airDate"]), + t.Omit(BaseEntry, ["serieId", "airDate", "description"]), t.Object({ kind: t.Literal("unknown"), }), @@ -108,4 +108,7 @@ export const Video = t.Union( `, }, ); -export type Video = typeof Video.static; +export type UnknownEntry = typeof UnknownEntry.static; + +export const Entry = t.Union([Episode, MovieEntry, Special]); +export type Entry = typeof Entry.static;