diff --git a/front/packages/models/src/resources/collection.ts b/front/packages/models/src/resources/collection.ts index e761b8dc..aa85e4c9 100644 --- a/front/packages/models/src/resources/collection.ts +++ b/front/packages/models/src/resources/collection.ts @@ -19,10 +19,11 @@ */ import { z } from "zod"; -import { withImages, ResourceP } from "../traits"; +import { ImagesP, ResourceP } from "../traits"; -export const CollectionP = withImages( - ResourceP("collection").extend({ +export const CollectionP = ResourceP("collection") + .merge(ImagesP) + .extend({ /** * The title of this collection. */ @@ -31,11 +32,11 @@ export const CollectionP = withImages( * The summary of this show. */ overview: z.string().nullable(), - }), -).transform((x) => ({ - ...x, - href: `/collection/${x.slug}`, -})); + }) + .transform((x) => ({ + ...x, + href: `/collection/${x.slug}`, + })); /** * A class representing collections of show or movies. diff --git a/front/packages/models/src/resources/episode.base.ts b/front/packages/models/src/resources/episode.base.ts index db2e86d2..4908c773 100644 --- a/front/packages/models/src/resources/episode.base.ts +++ b/front/packages/models/src/resources/episode.base.ts @@ -20,11 +20,12 @@ import { z } from "zod"; import { zdate } from "../utils"; -import { withImages, imageFn } from "../traits"; +import { ImagesP, imageFn } from "../traits"; import { ResourceP } from "../traits/resource"; -export const BaseEpisodeP = withImages( - ResourceP("episode").extend({ +export const BaseEpisodeP = ResourceP("episode") + .merge(ImagesP) + .extend({ /** * The season in witch this episode is in. */ @@ -71,8 +72,7 @@ export const BaseEpisodeP = withImages( * The id of the show containing this episode */ showId: z.string(), - }), -) + }) .transform((x) => ({ ...x, runtime: x.runtime === 0 ? null : x.runtime, diff --git a/front/packages/models/src/resources/movie.ts b/front/packages/models/src/resources/movie.ts index df425d6d..7554df1a 100644 --- a/front/packages/models/src/resources/movie.ts +++ b/front/packages/models/src/resources/movie.ts @@ -20,7 +20,7 @@ import { z } from "zod"; import { zdate } from "../utils"; -import { withImages, ResourceP, imageFn } from "../traits"; +import { ImagesP, ResourceP, imageFn } from "../traits"; import { Genre } from "./genre"; import { StudioP } from "./studio"; import { Status } from "./show"; @@ -28,8 +28,9 @@ import { CollectionP } from "./collection"; import { MetadataP } from "./metadata"; import { WatchStatusP } from "./watch-status"; -export const MovieP = withImages( - ResourceP("movie").extend({ +export const MovieP = ResourceP("movie") + .merge(ImagesP) + .extend({ /** * The title of this movie. */ @@ -104,8 +105,7 @@ export const MovieP = withImages( * Metadata of what an user as started/planned to watch. */ watchStatus: WatchStatusP.optional().nullable(), - }), -) + }) .transform((x) => ({ ...x, runtime: x.runtime === 0 ? null : x.runtime, diff --git a/front/packages/models/src/resources/person.ts b/front/packages/models/src/resources/person.ts index 00994383..f069efa7 100644 --- a/front/packages/models/src/resources/person.ts +++ b/front/packages/models/src/resources/person.ts @@ -19,28 +19,25 @@ */ import { z } from "zod"; -import { withImages } from "../traits"; -import { ResourceP } from "../traits/resource"; +import { ImagesP, ResourceP } from "../traits"; -export const PersonP = withImages( - ResourceP("people").extend({ - /** - * The name of this person. - */ - name: z.string(), - /** - * The type of work the person has done for the show. That can be something like "Actor", - * "Writer", "Music", "Voice Actor"... - */ - type: z.string().optional(), +export const PersonP = ResourceP("people").merge(ImagesP).extend({ + /** + * The name of this person. + */ + name: z.string(), + /** + * The type of work the person has done for the show. That can be something like "Actor", + * "Writer", "Music", "Voice Actor"... + */ + type: z.string().optional(), - /** - * The role the People played. This is mostly used to inform witch character was played for actor - * and voice actors. - */ - role: z.string().optional(), - }), -); + /** + * The role the People played. This is mostly used to inform witch character was played for actor + * and voice actors. + */ + role: z.string().optional(), +}); /** * A studio that make shows. diff --git a/front/packages/models/src/resources/season.ts b/front/packages/models/src/resources/season.ts index 839e1519..9c5c1fa2 100644 --- a/front/packages/models/src/resources/season.ts +++ b/front/packages/models/src/resources/season.ts @@ -20,37 +20,34 @@ import { z } from "zod"; import { zdate } from "../utils"; -import { withImages } from "../traits"; -import { ResourceP } from "../traits/resource"; +import { ImagesP, ResourceP } from "../traits"; -export const SeasonP = withImages( - ResourceP("season").extend({ - /** - * The name of this season. - */ - name: z.string(), - /** - * The number of this season. This can be set to 0 to indicate specials. - */ - seasonNumber: z.number(), - /** - * A quick overview of this season. - */ - overview: z.string().nullable(), - /** - * The starting air date of this season. - */ - startDate: zdate().nullable(), - /** - * The ending date of this season. - */ - endDate: zdate().nullable(), - /** - * The number of episodes available on kyoo of this season. - */ - episodesCount: z.number(), - }), -); +export const SeasonP = ResourceP("season").merge(ImagesP).extend({ + /** + * The name of this season. + */ + name: z.string(), + /** + * The number of this season. This can be set to 0 to indicate specials. + */ + seasonNumber: z.number(), + /** + * A quick overview of this season. + */ + overview: z.string().nullable(), + /** + * The starting air date of this season. + */ + startDate: zdate().nullable(), + /** + * The ending date of this season. + */ + endDate: zdate().nullable(), + /** + * The number of episodes available on kyoo of this season. + */ + episodesCount: z.number(), +}); /** * A season of a Show. diff --git a/front/packages/models/src/resources/show.ts b/front/packages/models/src/resources/show.ts index 9275751a..8b8ee32c 100644 --- a/front/packages/models/src/resources/show.ts +++ b/front/packages/models/src/resources/show.ts @@ -20,7 +20,7 @@ import { z } from "zod"; import { zdate } from "../utils"; -import { withImages, ResourceP } from "../traits"; +import { ImagesP, ResourceP } from "../traits"; import { Genre } from "./genre"; import { StudioP } from "./studio"; import { BaseEpisodeP } from "./episode.base"; @@ -37,8 +37,9 @@ export enum Status { Planned = "Planned", } -export const ShowP = withImages( - ResourceP("show").extend({ +export const ShowP = ResourceP("show") + .merge(ImagesP) + .extend({ /** * The title of this show. */ @@ -103,8 +104,7 @@ export const ShowP = withImages( * The number of episodes in this show. */ episodesCount: z.number().int().gte(0).optional(), - }), -) + }) .transform((x) => { if (!x.thumbnail && x.poster) { x.thumbnail = { ...x.poster }; diff --git a/front/packages/models/src/traits/images.ts b/front/packages/models/src/traits/images.ts index 9142e70d..57627532 100644 --- a/front/packages/models/src/traits/images.ts +++ b/front/packages/models/src/traits/images.ts @@ -19,7 +19,7 @@ */ import { Platform } from "react-native"; -import { ZodObject, ZodRawShape, z } from "zod"; +import { z } from "zod"; import { lastUsedUrl } from ".."; export const imageFn = (url: string) => @@ -28,9 +28,12 @@ export const imageFn = (url: string) => export const Img = z.object({ source: z.string(), blurhash: z.string(), + low: z.string().transform(imageFn), + medium: z.string().transform(imageFn), + high: z.string().transform(imageFn), }); -const ImagesP = z.object({ +export const ImagesP = z.object({ /** * An url to the poster of this resource. If this resource does not have an image, the link will * be null. If the kyoo's instance is not capable of handling this kind of image for the specific @@ -53,28 +56,7 @@ const ImagesP = z.object({ logo: Img.nullable(), }); -const addQualities = (x: object | null | undefined, href: string) => { - if (x === null) return null; - return { - ...x, - low: imageFn(`${href}?quality=low`), - medium: imageFn(`${href}?quality=medium`), - high: imageFn(`${href}?quality=high`), - }; -}; - -export const withImages = (parser: ZodObject) => { - return parser.merge(ImagesP).transform((x) => { - return { - ...x, - poster: addQualities(x.poster, `/${x.kind}/${x.slug}/poster`), - thumbnail: addQualities(x.thumbnail, `/${x.kind}/${x.slug}/thumbnail`), - logo: addQualities(x.logo, `/${x.kind}/${x.slug}/logo`), - }; - }); -}; - /** * Base traits for items that has image resources. */ -export type KyooImage = z.infer & { low: string; medium: string; high: string }; +export type KyooImage = z.infer;