Take images url from api

This commit is contained in:
Zoe Roux 2024-04-19 13:51:53 +02:00
parent dc5fd8f5a3
commit 69e8340c95
No known key found for this signature in database
7 changed files with 74 additions and 97 deletions

View File

@ -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.

View File

@ -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,

View File

@ -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,

View File

@ -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.

View File

@ -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.

View File

@ -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 };

View File

@ -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 = <T extends ZodRawShape>(parser: ZodObject<T>) => {
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<typeof Img> & { low: string; medium: string; high: string };
export type KyooImage = z.infer<typeof Img>;