import {z} from 'zod' export type GetApiV14StudioArgs = {page?: number | undefined, limit?: number | undefined, selectFields?: (Array<'id' | 'subType' | 'title' | 'type' | 'movies' | 'updatedAt' | 'createdAt'>) | undefined, notNullFields?: (Array<'id' | 'subType' | 'title' | 'type' | 'movies.id' | 'updatedAt' | 'createdAt'>) | undefined, sortField?: (Array<'id' | 'subType' | 'title' | 'type' | 'movies.id' | 'updatedAt' | 'createdAt'>) | undefined, sortType?: Array | undefined, id?: (Array | null) | undefined, 'movies.id'?: (Array | null) | undefined, type?: (Array | null) | undefined, subType?: (Array | null) | undefined, title?: (Array | null) | undefined, updatedAt?: (Array | null) | undefined, createdAt?: (Array | null) | undefined}; export type GetApiV14StudioOptions = {baseUrl?: string | undefined, XAPIKEY?: string | undefined, fetch?: typeof fetch | undefined}; export const movieFromStudio = z.object({id: z.number().optional()}); export const studio = z.object({id: z.string(), subType: z.string(), title: z.string(), type: z.enum(["Производство", "Спецэффекты", "Прокат", "Студия дубляжа"]).optional(), movies: movieFromStudio.optional(), updatedAt: z.string(), createdAt: z.string()}); export const studioDocsResponseDtoV14 = z.object({docs: z.array(studio), 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/studio */ export const getApiV14Studio = async (args: GetApiV14StudioArgs, options?: GetApiV14StudioOptions) => { 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.id ?? []) query.append("id", String(item)) for (const item of args['movies.id'] ?? []) query.append("movies.id", String(item)) for (const item of args.type ?? []) query.append("type", String(item)) for (const item of args.subType ?? []) query.append("subType", String(item)) for (const item of args.title ?? []) query.append("title", 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/studio${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(studioDocsResponseDtoV14, await res.json()) };