import {z} from 'zod' export type GetApi3DiscoverMovieArgs = {certification?: string | undefined, 'certification.gte'?: string | undefined, 'certification.lte'?: string | undefined, certification_country?: string | undefined, include_adult?: boolean | undefined, include_video?: boolean | undefined, language?: string | undefined, page?: number | undefined, primary_release_year?: number | undefined, 'primary_release_date.gte'?: string | undefined, 'primary_release_date.lte'?: string | undefined, region?: string | undefined, 'release_date.gte'?: string | undefined, 'release_date.lte'?: string | undefined, sort_by?: ('original_title.asc' | 'original_title.desc' | 'popularity.asc' | 'popularity.desc' | 'revenue.asc' | 'revenue.desc' | 'primary_release_date.asc' | 'title.asc' | 'title.desc' | 'primary_release_date.desc' | 'vote_average.asc' | 'vote_average.desc' | 'vote_count.asc' | 'vote_count.desc') | 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_cast?: string | undefined, with_companies?: string | undefined, with_crew?: string | undefined, with_genres?: string | undefined, with_keywords?: string | undefined, with_origin_country?: string | undefined, with_original_language?: string | undefined, with_people?: string | undefined, with_release_type?: number | undefined, 'with_runtime.gte'?: number | undefined, 'with_runtime.lte'?: number | 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, year?: number | undefined}; export type GetApi3DiscoverMovieOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const getApi3DiscoverMovieResponse = z.object({page: z.number().int().optional(), results: z.array(z.object({adult: z.boolean().optional(), backdrop_path: z.string().optional(), genre_ids: z.array(z.number().int()).optional(), id: z.number().int().optional(), original_language: z.string().optional(), original_title: z.string().optional(), overview: z.string().optional(), popularity: z.number().optional(), poster_path: z.string().optional(), release_date: z.string().optional(), title: z.string().optional(), video: z.boolean().optional(), vote_average: z.number().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 }; /** * Movie * * GET /3/discover/movie */ export const getApi3DiscoverMovie = async (args: GetApi3DiscoverMovieArgs, options?: GetApi3DiscoverMovieOptions) => { 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.certification != null) query.append("certification", String(args.certification)) if (args['certification.gte'] != null) query.append("certification.gte", String(args['certification.gte'])) if (args['certification.lte'] != null) query.append("certification.lte", String(args['certification.lte'])) if (args.certification_country != null) query.append("certification_country", String(args.certification_country)) if (args.include_adult != null) query.append("include_adult", String(args.include_adult)) if (args.include_video != null) query.append("include_video", String(args.include_video)) if (args.language != null) query.append("language", String(args.language)) if (args.page != null) query.append("page", String(args.page)) if (args.primary_release_year != null) query.append("primary_release_year", String(args.primary_release_year)) if (args['primary_release_date.gte'] != null) query.append("primary_release_date.gte", String(args['primary_release_date.gte'])) if (args['primary_release_date.lte'] != null) query.append("primary_release_date.lte", String(args['primary_release_date.lte'])) if (args.region != null) query.append("region", String(args.region)) if (args['release_date.gte'] != null) query.append("release_date.gte", String(args['release_date.gte'])) if (args['release_date.lte'] != null) query.append("release_date.lte", String(args['release_date.lte'])) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) 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_cast != null) query.append("with_cast", String(args.with_cast)) if (args.with_companies != null) query.append("with_companies", String(args.with_companies)) if (args.with_crew != null) query.append("with_crew", String(args.with_crew)) 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_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_people != null) query.append("with_people", String(args.with_people)) if (args.with_release_type != null) query.append("with_release_type", String(args.with_release_type)) 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_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.year != null) query.append("year", String(args.year)) if (sec0 !== undefined) headers.set("Authorization", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/3/discover/movie${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(getApi3DiscoverMovieResponse, await res.json()) };