import {z} from 'zod' export type GetApiV14ImageArgs = {page?: number | undefined, limit?: number | undefined, selectFields?: (Array<'movieId' | 'type' | 'language' | 'url' | 'previewUrl' | 'height' | 'width' | 'updatedAt' | 'createdAt'>) | undefined, notNullFields?: (Array<'movieId' | 'type' | 'language' | 'url' | 'previewUrl' | 'height' | 'width' | 'updatedAt' | 'createdAt'>) | undefined, sortField?: (Array<'movieId' | 'type' | 'language' | 'url' | 'previewUrl' | 'height' | 'width' | 'updatedAt' | 'createdAt'>) | undefined, sortType?: Array | undefined, movieId?: Array | undefined, type?: (Array<'backdrops' | 'cover' | 'frame' | 'promo' | 'screenshot' | 'shooting' | 'still' | 'wallpaper'>) | undefined, language?: (Array<'ab' | 'af' | 'am' | 'ar' | 'as' | 'av' | 'ba' | 'be' | 'bg' | 'bn' | 'ca' | 'ce' | 'cn' | 'cs' | 'cu' | 'cv' | 'da' | 'de'>) | undefined, height?: Array | undefined, width?: Array | undefined, updatedAt?: (Array | null) | undefined, createdAt?: (Array | null) | undefined}; export type GetApiV14ImageOptions = {baseUrl?: string | undefined, XAPIKEY?: string | undefined, fetch?: typeof fetch | undefined}; export const image = z.object({movieId: z.number(), type: z.string().optional(), language: z.string().optional(), url: z.string().optional(), previewUrl: z.string().optional(), height: z.number().optional(), width: z.number().optional(), updatedAt: z.string(), createdAt: z.string()}); export const imageDocsResponseDtoV14 = z.object({docs: z.array(image), 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/image */ export const getApiV14Image = async (args: GetApiV14ImageArgs, options?: GetApiV14ImageOptions) => { 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.type ?? []) query.append("type", String(item)) for (const item of args.language ?? []) query.append("language", String(item)) for (const item of args.height ?? []) query.append("height", String(item)) for (const item of args.width ?? []) query.append("width", 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/image${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(imageDocsResponseDtoV14, await res.json()) };