import {z} from 'zod' export type GetApiV14PersonSearchArgs = {page?: number | undefined, limit?: number | undefined, query: string}; export type GetApiV14PersonSearchOptions = {baseUrl?: string | undefined, XAPIKEY?: string | undefined, fetch?: typeof fetch | undefined}; export const birthPlace = z.object({value: z.string().optional()}); export const deathPlace = z.object({value: z.string().optional()}); export const profession = z.object({value: z.string().optional()}); export const meiliPersonEntityV14 = z.object({"1": z.object({}), id: z.number(), name: z.string(), enName: z.string(), photo: z.string(), sex: z.string(), birthday: z.string(), death: z.string(), age: z.number(), birthPlace: z.array(birthPlace).optional(), deathPlace: z.array(deathPlace).optional(), profession: z.array(profession).optional(), growth: z.number()}); export const searchPersonResponseDtoV14 = z.object({docs: z.array(meiliPersonEntityV14), 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/person/search */ export const getApiV14PersonSearch = async (args: GetApiV14PersonSearchArgs, options?: GetApiV14PersonSearchOptions) => { 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)) if (args.query != null) query.append("query", String(args.query)) if (XAPIKEY !== undefined) headers.set("X-API-KEY", XAPIKEY) const url = `${baseUrl.replace(/\/+$/, '')}/v1.4/person/search${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(searchPersonResponseDtoV14, await res.json()) };