feat: Add zod for schema validation

Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
This commit is contained in:
Tim Hårek Andreassen 2024-03-30 12:02:06 +01:00
parent b64d8f2203
commit 33ccbe3e34
No known key found for this signature in database
GPG Key ID: E59C7734F0E10EB5
2 changed files with 23 additions and 17 deletions

View File

@ -12,7 +12,8 @@
"@preact/signals": "https://esm.sh/*@preact/signals@1.2.2",
"@preact/signals-core": "https://esm.sh/*@preact/signals-core@1.5.1",
"twind": "https://esm.sh/twind@0.16.17",
"twind/": "https://esm.sh/twind@0.16.17/"
"twind/": "https://esm.sh/twind@0.16.17/",
"zod": "https://deno.land/x/zod@v3.22.4/mod.ts"
},
"compilerOptions": {
"jsx": "react-jsx",

View File

@ -1,6 +1,7 @@
import { get, OK } from "https://deno.land/x/kall@v0.1.0/mod.ts";
import { components as searchComponents } from "./nrk-search.ts";
import { components as catalogComponents } from "./nrk-catalog.ts";
import { z } from "zod";
type ArrayElement<A> = A extends readonly (infer T)[] ? T : never;
export type Serie = catalogComponents["schemas"]["SeriesViewModel"];
@ -11,20 +12,22 @@ export type SearchResult = ArrayElement<SearchResultList> & {
description?: string;
};
/* Incomplete manifest types */
interface Manifest {
id: string;
playable: {
endSequenceStartTime: null;
duration: string;
assets: {
url: string;
format: string;
mimeType: string;
encrypted: boolean;
}[];
};
}
const manifestSchema = z.object({
id: z.string(),
playable: z.object({
endSequenceStartTime: z.null(),
duration: z.string(),
assets: z.array(z.object({
url: z.string(),
format: z.string(),
mimeType: z.string(),
encrypted: z.boolean(),
})),
}),
});
/** Incomplete manifest types. */
type Manifest = z.infer<typeof manifestSchema>;
const nrkAPI = `https://psapi.nrk.no`;
@ -32,10 +35,12 @@ async function withDownloadLink(
episode: _OriginalEpisode,
): Promise<OriginalEpisode> {
// getting stream link
const [playbackStatus, playbackResponse] = await get<Manifest>(
const [playbackStatus, playbackResponseRaw] = await get<Manifest>(
`${nrkAPI}/playback/manifest/podcast/${episode.episodeId}`,
);
const playbackResponse = manifestSchema.parse(playbackResponseRaw);
if (playbackStatus === OK && playbackResponse) {
return { ...episode, url: playbackResponse.playable.assets[0].url };
} else {