import {z} from 'zod' export type GetApiV14SeasonArgs = {page?: number | undefined, limit?: number | undefined, selectFields?: (Array<'movieId' | 'poster' | 'number' | 'name' | 'enName' | 'duration' | 'description' | 'enDescription' | 'episodesCount' | 'airDate' | 'episodes' | 'updatedAt' | 'createdAt'>) | undefined, notNullFields?: (Array<'movieId' | 'poster.url' | 'poster.previewUrl' | 'number' | 'name' | 'enName' | 'episodesCount' | 'airDate' | 'duration' | 'description' | 'enDescription' | 'episodes.number' | 'episodes.name' | 'episodes.enName' | 'episodes.airDate' | 'episodes.date' | 'episodes.description' | 'episodes.enDescription' | 'updatedAt' | 'createdAt'>) | undefined, sortField?: (Array<'movieId' | 'poster.url' | 'poster.previewUrl' | 'number' | 'name' | 'enName' | 'episodesCount' | 'airDate' | 'duration' | 'description' | 'enDescription' | 'episodes.number' | 'episodes.name' | 'episodes.enName' | 'episodes.airDate' | 'episodes.date' | 'episodes.description' | 'episodes.enDescription' | 'updatedAt' | 'createdAt'>) | undefined, sortType?: Array | undefined, movieId?: (Array | null) | undefined, number?: (Array | null) | undefined, 'episodes.number'?: (Array | null) | undefined, airDate?: (Array | null) | undefined, 'episodes.airDate'?: (Array | null) | undefined, updatedAt?: (Array | null) | undefined, createdAt?: (Array | null) | undefined}; export type GetApiV14SeasonOptions = {baseUrl?: string | undefined, XAPIKEY?: string | undefined, fetch?: typeof fetch | undefined}; export const shortImage = z.object({url: z.string().nullable().optional(), previewUrl: z.string().nullable().optional()}); export const episodeV14 = z.object({number: z.number().optional(), name: z.string().optional(), enName: z.string().optional(), date: z.string().optional(), description: z.string().optional(), still: shortImage.optional(), airDate: z.string().optional(), enDescription: z.string().optional()}); export const seasonV14 = z.object({movieId: z.number().optional(), number: z.number().optional(), episodesCount: z.number().optional(), episodes: z.array(episodeV14).optional(), poster: shortImage.optional(), name: z.string().optional(), enName: z.string().optional(), duration: z.number().optional(), description: z.string().optional(), enDescription: z.string().optional(), airDate: z.string().optional(), updatedAt: z.string(), createdAt: z.string()}); export const seasonDocsResponseDtoV14 = z.object({docs: z.array(seasonV14), total: z.number(), limit: z.number(), page: z.number(), pages: z.number()}); 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 }; /** * Поиск сезонов * * GET /v1.4/season */ export const getApiV14Season = async (args: GetApiV14SeasonArgs, options?: GetApiV14SeasonOptions) => { const {baseUrl = "https://api.kinopoisk.dev", fetch: fetchFn = fetch, XAPIKEY} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) for (const item of args.selectFields ?? []) query.append("selectFields", String(item)) for (const item of args.notNullFields ?? []) query.append("notNullFields", String(item)) for (const item of args.sortField ?? []) query.append("sortField", String(item)) for (const item of args.sortType ?? []) query.append("sortType", String(item)) for (const item of args.movieId ?? []) query.append("movieId", String(item)) for (const item of args.number ?? []) query.append("number", String(item)) for (const item of args['episodes.number'] ?? []) query.append("episodes.number", String(item)) for (const item of args.airDate ?? []) query.append("airDate", String(item)) for (const item of args['episodes.airDate'] ?? []) query.append("episodes.airDate", String(item)) for (const item of args.updatedAt ?? []) query.append("updatedAt", String(item)) for (const item of args.createdAt ?? []) query.append("createdAt", String(item)) if (XAPIKEY !== undefined) headers.set("X-API-KEY", XAPIKEY) const url = `${baseUrl.replace(/\/+$/, '')}/v1.4/season${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(seasonDocsResponseDtoV14, await res.json()) };