import {z} from 'zod' export type GetApiV14ListArgs = {page?: number | undefined, limit?: number | undefined, selectFields?: (Array<'name' | 'category' | 'slug' | 'moviesCount' | 'cover' | 'updatedAt' | 'createdAt'>) | undefined, notNullFields?: (Array<'name' | 'category' | 'slug' | 'moviesCount' | 'cover.url' | 'cover.previewUrl' | 'updatedAt' | 'createdAt'>) | undefined, sortField?: (Array<'name' | 'category' | 'slug' | 'moviesCount' | 'cover.url' | 'cover.previewUrl' | 'updatedAt' | 'createdAt'>) | undefined, sortType?: Array | undefined, slug?: (Array | null) | undefined, category?: (Array<'Онлайн-кинотеатр' | 'Премии' | 'Сборы' | 'Сериалы' | 'Фильмы'> | null) | undefined, moviesCount?: (Array | null) | undefined, updatedAt?: (Array | null) | undefined, createdAt?: (Array | null) | undefined}; export type GetApiV14ListOptions = {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 list = z.object({category: z.string(), slug: z.string(), moviesCount: z.number(), cover: shortImage, name: z.string(), updatedAt: z.string(), createdAt: z.string()}); export const listDocsResponseDtoV14 = z.object({docs: z.array(list), 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/list */ export const getApiV14List = async (args: GetApiV14ListArgs, options?: GetApiV14ListOptions) => { 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.slug ?? []) query.append("slug", String(item)) for (const item of args.category ?? []) query.append("category", String(item)) for (const item of args.moviesCount ?? []) query.append("moviesCount", 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/list${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(listDocsResponseDtoV14, await res.json()) };