import {z} from 'zod' export type GetApiGenresGenreIdSongsArgs = {genre_id: string, offset?: number | undefined, limit?: number | undefined, cond?: string | undefined, sort?: string | undefined}; export type GetApiGenresGenreIdSongsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const namedReference = z.object({id: z.string(), name: z.string().nullable(), prefix: z.string().nullable(), basename: z.string().nullable()}).and(z.record(z.string(), z.unknown())); export const genreReference = z.object({id: z.string(), name: z.string()}).and(z.record(z.string(), z.unknown())); export const songObject = z.object({id: z.string(), title: z.string().nullable(), name: z.string().nullable(), artist: namedReference, artists: z.array(namedReference), album: namedReference, albumartist: namedReference.optional(), disk: z.number().int(), disksubtitle: z.string().nullable(), bpm: z.number().nullable(), track: z.number().int(), filename: z.string().nullable(), genre: z.array(genreReference), mood: z.array(z.object({id: z.string(), name: z.string()}).and(z.record(z.string(), z.unknown()))), playlisttrack: z.number().int(), time: z.number().int(), year: z.number().int(), format: z.string().nullable(), stream_format: z.string().nullable(), bitrate: z.number().int().nullable(), stream_bitrate: z.number().int().nullable(), rate: z.number().int(), mode: z.string().nullable(), mime: z.string().nullable(), stream_mime: z.string().nullable(), url: z.string(), size: z.number().int(), mbid: z.string().nullable(), art: z.string().nullable(), has_art: z.boolean(), flag: z.boolean(), rating: z.number().int().nullable(), averagerating: z.number().nullable(), playcount: z.number().int(), last_played: z.string().nullable(), catalog: z.string(), composer: z.string().nullable(), channels: z.number().int().nullable(), comment: z.string().nullable(), license: z.string().nullable(), publisher: z.string().nullable(), language: z.string().nullable(), lyrics: z.string().nullable(), replaygain_album_gain: z.number().nullable(), replaygain_album_peak: z.number().nullable(), replaygain_track_gain: z.number().nullable(), replaygain_track_peak: z.number().nullable(), r128_album_gain: z.number().nullable(), r128_track_gain: z.number().nullable(), metadata: z.record(z.string(), z.string()).optional()}).and(z.record(z.string(), z.unknown())); export const songsResponse = z.object({total_count: z.number().int(), md5: z.string(), song: z.array(songObject)}).and(z.record(z.string(), z.unknown())); 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 songs for a genre * * GET /genres/{genre_id}/songs * * Also accepts auth: ApiKeyAuthHeader, ApiKeyAuthQuery */ export const getApiGenresGenreIdSongs = async (args: GetApiGenresGenreIdSongsArgs, options?: GetApiGenresGenreIdSongsOptions) => { const {baseUrl = "https://develop.ampache.dev/rest/8/json", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cond != null) query.append("cond", String(args.cond)) if (args.sort != null) query.append("sort", String(args.sort)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/genres/${encodeURIComponent(String(args.genre_id))}/songs${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(songsResponse, await res.json()) };