import {z} from 'zod' export type GetApi3DiscoverTvArgs = {'air_date.gte'?: string | undefined, 'air_date.lte'?: string | undefined, first_air_date_year?: number | undefined, 'first_air_date.gte'?: string | undefined, 'first_air_date.lte'?: string | undefined, include_adult?: boolean | undefined, include_null_first_air_dates?: boolean | undefined, language?: string | undefined, page?: number | undefined, screened_theatrically?: boolean | undefined, sort_by?: ('first_air_date.asc' | 'first_air_date.desc' | 'name.asc' | 'name.desc' | 'original_name.asc' | 'original_name.desc' | 'popularity.asc' | 'popularity.desc' | 'vote_average.asc' | 'vote_average.desc' | 'vote_count.asc' | 'vote_count.desc') | undefined, timezone?: string | undefined, 'vote_average.gte'?: number | undefined, 'vote_average.lte'?: number | undefined, 'vote_count.gte'?: number | undefined, 'vote_count.lte'?: number | undefined, watch_region?: string | undefined, with_companies?: string | undefined, with_genres?: string | undefined, with_keywords?: string | undefined, with_networks?: number | undefined, with_origin_country?: string | undefined, with_original_language?: string | undefined, 'with_runtime.gte'?: number | undefined, 'with_runtime.lte'?: number | undefined, with_status?: string | undefined, with_watch_monetization_types?: string | undefined, with_watch_providers?: string | undefined, without_companies?: string | undefined, without_genres?: string | undefined, without_keywords?: string | undefined, without_watch_providers?: string | undefined, with_type?: string | undefined}; export type GetApi3DiscoverTvOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const getApi3DiscoverTvResponse = z.object({page: z.number().int().optional(), results: z.array(z.object({backdrop_path: z.string().optional(), first_air_date: z.string().optional(), genre_ids: z.array(z.number().int()).optional(), id: z.number().int().optional(), name: z.string().optional(), origin_country: z.array(z.string()).optional(), original_language: z.string().optional(), original_name: z.string().optional(), overview: z.string().optional(), popularity: z.number().optional(), poster_path: z.string().optional(), vote_average: z.number().int().optional(), vote_count: z.number().int().optional()})).optional(), total_pages: z.number().int().optional(), total_results: z.number().int().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * TV * * GET /3/discover/tv */ export const getApi3DiscoverTv = async (args: GetApi3DiscoverTvArgs, options?: GetApi3DiscoverTvOptions) => { const {baseUrl = "https://api.themoviedb.org", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['air_date.gte'] != null) query.append("air_date.gte", String(args['air_date.gte'])) if (args['air_date.lte'] != null) query.append("air_date.lte", String(args['air_date.lte'])) if (args.first_air_date_year != null) query.append("first_air_date_year", String(args.first_air_date_year)) if (args['first_air_date.gte'] != null) query.append("first_air_date.gte", String(args['first_air_date.gte'])) if (args['first_air_date.lte'] != null) query.append("first_air_date.lte", String(args['first_air_date.lte'])) if (args.include_adult != null) query.append("include_adult", String(args.include_adult)) if (args.include_null_first_air_dates != null) query.append("include_null_first_air_dates", String(args.include_null_first_air_dates)) if (args.language != null) query.append("language", String(args.language)) if (args.page != null) query.append("page", String(args.page)) if (args.screened_theatrically != null) query.append("screened_theatrically", String(args.screened_theatrically)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.timezone != null) query.append("timezone", String(args.timezone)) if (args['vote_average.gte'] != null) query.append("vote_average.gte", String(args['vote_average.gte'])) if (args['vote_average.lte'] != null) query.append("vote_average.lte", String(args['vote_average.lte'])) if (args['vote_count.gte'] != null) query.append("vote_count.gte", String(args['vote_count.gte'])) if (args['vote_count.lte'] != null) query.append("vote_count.lte", String(args['vote_count.lte'])) if (args.watch_region != null) query.append("watch_region", String(args.watch_region)) if (args.with_companies != null) query.append("with_companies", String(args.with_companies)) if (args.with_genres != null) query.append("with_genres", String(args.with_genres)) if (args.with_keywords != null) query.append("with_keywords", String(args.with_keywords)) if (args.with_networks != null) query.append("with_networks", String(args.with_networks)) if (args.with_origin_country != null) query.append("with_origin_country", String(args.with_origin_country)) if (args.with_original_language != null) query.append("with_original_language", String(args.with_original_language)) if (args['with_runtime.gte'] != null) query.append("with_runtime.gte", String(args['with_runtime.gte'])) if (args['with_runtime.lte'] != null) query.append("with_runtime.lte", String(args['with_runtime.lte'])) if (args.with_status != null) query.append("with_status", String(args.with_status)) if (args.with_watch_monetization_types != null) query.append("with_watch_monetization_types", String(args.with_watch_monetization_types)) if (args.with_watch_providers != null) query.append("with_watch_providers", String(args.with_watch_providers)) if (args.without_companies != null) query.append("without_companies", String(args.without_companies)) if (args.without_genres != null) query.append("without_genres", String(args.without_genres)) if (args.without_keywords != null) query.append("without_keywords", String(args.without_keywords)) if (args.without_watch_providers != null) query.append("without_watch_providers", String(args.without_watch_providers)) if (args.with_type != null) query.append("with_type", String(args.with_type)) if (sec0 !== undefined) headers.set("Authorization", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/3/discover/tv${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApi3DiscoverTvResponse, await res.json()) };