import {z} from 'zod' export type GetApiSearchHintsArgs = {startIndex?: (number | null) | undefined, limit?: (number | null) | undefined, userId?: (string | null) | undefined, searchTerm: string, includeItemTypes?: (Array | null) | undefined, excludeItemTypes?: (Array | null) | undefined, mediaTypes?: (Array | null) | undefined, parentId?: (string | null) | undefined, isMovie?: (boolean | null) | undefined, isSeries?: (boolean | null) | undefined, isNews?: (boolean | null) | undefined, isKids?: (boolean | null) | undefined, isSports?: (boolean | null) | undefined, includePeople?: boolean | undefined, includeMedia?: boolean | undefined, includeGenres?: boolean | undefined, includeStudios?: boolean | undefined, includeArtists?: boolean | undefined}; export type GetApiSearchHintsOptions = {baseUrl?: string | undefined, CustomAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const searchHint = z.object({ItemId: z.string().optional(), Id: z.string().optional(), Name: z.string().nullable().optional(), MatchedTerm: z.string().nullable().optional(), IndexNumber: z.number().int().nullable().optional(), ProductionYear: z.number().int().nullable().optional(), ParentIndexNumber: z.number().int().nullable().optional(), PrimaryImageTag: z.string().nullable().optional(), ThumbImageTag: z.string().nullable().optional(), ThumbImageItemId: z.string().nullable().optional(), BackdropImageTag: z.string().nullable().optional(), BackdropImageItemId: z.string().nullable().optional(), Type: z.string().nullable().optional(), IsFolder: z.boolean().nullable().optional(), RunTimeTicks: z.number().int().nullable().optional(), MediaType: z.string().nullable().optional(), StartDate: z.string().nullable().optional(), EndDate: z.string().nullable().optional(), Series: z.string().nullable().optional(), Status: z.string().nullable().optional(), Album: z.string().nullable().optional(), AlbumId: z.string().optional(), AlbumArtist: z.string().nullable().optional(), Artists: z.array(z.string()).nullable().optional(), SongCount: z.number().int().nullable().optional(), EpisodeCount: z.number().int().nullable().optional(), ChannelId: z.string().optional(), ChannelName: z.string().nullable().optional(), PrimaryImageAspectRatio: z.number().nullable().optional()}); export const searchHintResult = z.object({SearchHints: z.array(searchHint).nullable().optional(), TotalRecordCount: z.number().int().optional()}); 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 }; /** * Gets the search hint result. * * GET /Search/Hints */ export const getApiSearchHints = async (args: GetApiSearchHintsArgs, options?: GetApiSearchHintsOptions) => { const {baseUrl = "http://localhost", fetch: fetchFn = fetch, CustomAuthentication} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.startIndex != null) query.append("startIndex", String(args.startIndex)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.userId != null) query.append("userId", String(args.userId)) if (args.searchTerm != null) query.append("searchTerm", String(args.searchTerm)) for (const item of args.includeItemTypes ?? []) query.append("includeItemTypes", String(item)) for (const item of args.excludeItemTypes ?? []) query.append("excludeItemTypes", String(item)) for (const item of args.mediaTypes ?? []) query.append("mediaTypes", String(item)) if (args.parentId != null) query.append("parentId", String(args.parentId)) if (args.isMovie != null) query.append("isMovie", String(args.isMovie)) if (args.isSeries != null) query.append("isSeries", String(args.isSeries)) if (args.isNews != null) query.append("isNews", String(args.isNews)) if (args.isKids != null) query.append("isKids", String(args.isKids)) if (args.isSports != null) query.append("isSports", String(args.isSports)) if (args.includePeople != null) query.append("includePeople", String(args.includePeople)) if (args.includeMedia != null) query.append("includeMedia", String(args.includeMedia)) if (args.includeGenres != null) query.append("includeGenres", String(args.includeGenres)) if (args.includeStudios != null) query.append("includeStudios", String(args.includeStudios)) if (args.includeArtists != null) query.append("includeArtists", String(args.includeArtists)) if (CustomAuthentication !== undefined) headers.set("X-Emby-Authorization", CustomAuthentication) const url = `${baseUrl.replace(/\/+$/, '')}/Search/Hints${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(searchHintResult, await res.json()) };